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 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #include "SkSLIRGenerator.h" |
| 9 | |
| 10 | #include "limits.h" |
| 11 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 12 | #include "SkSLCompiler.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 13 | #include "ast/SkSLASTBoolLiteral.h" |
| 14 | #include "ast/SkSLASTFieldSuffix.h" |
| 15 | #include "ast/SkSLASTFloatLiteral.h" |
| 16 | #include "ast/SkSLASTIndexSuffix.h" |
| 17 | #include "ast/SkSLASTIntLiteral.h" |
| 18 | #include "ir/SkSLBinaryExpression.h" |
| 19 | #include "ir/SkSLBoolLiteral.h" |
| 20 | #include "ir/SkSLBreakStatement.h" |
| 21 | #include "ir/SkSLConstructor.h" |
| 22 | #include "ir/SkSLContinueStatement.h" |
| 23 | #include "ir/SkSLDiscardStatement.h" |
| 24 | #include "ir/SkSLDoStatement.h" |
| 25 | #include "ir/SkSLExpressionStatement.h" |
| 26 | #include "ir/SkSLField.h" |
| 27 | #include "ir/SkSLFieldAccess.h" |
| 28 | #include "ir/SkSLFloatLiteral.h" |
| 29 | #include "ir/SkSLForStatement.h" |
| 30 | #include "ir/SkSLFunctionCall.h" |
| 31 | #include "ir/SkSLFunctionDeclaration.h" |
| 32 | #include "ir/SkSLFunctionDefinition.h" |
| 33 | #include "ir/SkSLFunctionReference.h" |
| 34 | #include "ir/SkSLIfStatement.h" |
| 35 | #include "ir/SkSLIndexExpression.h" |
| 36 | #include "ir/SkSLInterfaceBlock.h" |
| 37 | #include "ir/SkSLIntLiteral.h" |
| 38 | #include "ir/SkSLLayout.h" |
| 39 | #include "ir/SkSLPostfixExpression.h" |
| 40 | #include "ir/SkSLPrefixExpression.h" |
| 41 | #include "ir/SkSLReturnStatement.h" |
| 42 | #include "ir/SkSLSwizzle.h" |
| 43 | #include "ir/SkSLTernaryExpression.h" |
| 44 | #include "ir/SkSLUnresolvedFunction.h" |
| 45 | #include "ir/SkSLVariable.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 46 | #include "ir/SkSLVarDeclarations.h" |
| 47 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 48 | #include "ir/SkSLVariableReference.h" |
| 49 | #include "ir/SkSLWhileStatement.h" |
| 50 | |
| 51 | namespace SkSL { |
| 52 | |
| 53 | class AutoSymbolTable { |
| 54 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 55 | AutoSymbolTable(IRGenerator* ir) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | : fIR(ir) |
| 57 | , fPrevious(fIR->fSymbolTable) { |
| 58 | fIR->pushSymbolTable(); |
| 59 | } |
| 60 | |
| 61 | ~AutoSymbolTable() { |
| 62 | fIR->popSymbolTable(); |
| 63 | ASSERT(fPrevious == fIR->fSymbolTable); |
| 64 | } |
| 65 | |
| 66 | IRGenerator* fIR; |
| 67 | std::shared_ptr<SymbolTable> fPrevious; |
| 68 | }; |
| 69 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 70 | class AutoLoopLevel { |
| 71 | public: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 72 | AutoLoopLevel(IRGenerator* ir) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 73 | : fIR(ir) { |
| 74 | fIR->fLoopLevel++; |
| 75 | } |
| 76 | |
| 77 | ~AutoLoopLevel() { |
| 78 | fIR->fLoopLevel--; |
| 79 | } |
| 80 | |
| 81 | IRGenerator* fIR; |
| 82 | }; |
| 83 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 84 | IRGenerator::IRGenerator(const Context* context, std::shared_ptr<SymbolTable> symbolTable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | ErrorReporter& errorReporter) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 86 | : fContext(*context) |
| 87 | , fCurrentFunction(nullptr) |
| 88 | , fSymbolTable(std::move(symbolTable)) |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 89 | , fLoopLevel(0) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 90 | , fErrors(errorReporter) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 91 | |
| 92 | void IRGenerator::pushSymbolTable() { |
| 93 | fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors)); |
| 94 | } |
| 95 | |
| 96 | void IRGenerator::popSymbolTable() { |
| 97 | fSymbolTable = fSymbolTable->fParent; |
| 98 | } |
| 99 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 100 | static void fill_caps(const GrShaderCaps& caps, std::unordered_map<SkString, CapValue>* capsMap) { |
| 101 | #define CAP(name) capsMap->insert(std::make_pair(SkString(#name), CapValue(caps.name()))); |
| 102 | CAP(fbFetchSupport); |
| 103 | CAP(fbFetchNeedsCustomOutput); |
| 104 | CAP(bindlessTextureSupport); |
| 105 | CAP(dropsTileOnZeroDivide); |
| 106 | CAP(flatInterpolationSupport); |
| 107 | CAP(noperspectiveInterpolationSupport); |
| 108 | CAP(multisampleInterpolationSupport); |
| 109 | CAP(sampleVariablesSupport); |
| 110 | CAP(sampleMaskOverrideCoverageSupport); |
| 111 | CAP(externalTextureSupport); |
| 112 | CAP(texelFetchSupport); |
| 113 | CAP(imageLoadStoreSupport); |
| 114 | CAP(mustEnableAdvBlendEqs); |
| 115 | CAP(mustEnableSpecificAdvBlendEqs); |
| 116 | CAP(mustDeclareFragmentShaderOutput); |
| 117 | CAP(canUseAnyFunctionInShader); |
| 118 | #undef CAP |
| 119 | } |
| 120 | |
| 121 | void IRGenerator::start(const Program::Settings* settings) { |
| 122 | fSettings = settings; |
| 123 | fCapsMap.clear(); |
| 124 | if (settings->fCaps) { |
| 125 | fill_caps(*settings->fCaps, &fCapsMap); |
| 126 | } |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 127 | this->pushSymbolTable(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 128 | fInputs.reset(); |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void IRGenerator::finish() { |
| 132 | this->popSymbolTable(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 133 | fSettings = nullptr; |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 134 | } |
| 135 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 136 | std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& extension) { |
| 137 | return std::unique_ptr<Extension>(new Extension(extension.fPosition, extension.fName)); |
| 138 | } |
| 139 | |
| 140 | std::unique_ptr<Statement> IRGenerator::convertStatement(const ASTStatement& statement) { |
| 141 | switch (statement.fKind) { |
| 142 | case ASTStatement::kBlock_Kind: |
| 143 | return this->convertBlock((ASTBlock&) statement); |
| 144 | case ASTStatement::kVarDeclaration_Kind: |
| 145 | return this->convertVarDeclarationStatement((ASTVarDeclarationStatement&) statement); |
| 146 | case ASTStatement::kExpression_Kind: |
| 147 | return this->convertExpressionStatement((ASTExpressionStatement&) statement); |
| 148 | case ASTStatement::kIf_Kind: |
| 149 | return this->convertIf((ASTIfStatement&) statement); |
| 150 | case ASTStatement::kFor_Kind: |
| 151 | return this->convertFor((ASTForStatement&) statement); |
| 152 | case ASTStatement::kWhile_Kind: |
| 153 | return this->convertWhile((ASTWhileStatement&) statement); |
| 154 | case ASTStatement::kDo_Kind: |
| 155 | return this->convertDo((ASTDoStatement&) statement); |
| 156 | case ASTStatement::kReturn_Kind: |
| 157 | return this->convertReturn((ASTReturnStatement&) statement); |
| 158 | case ASTStatement::kBreak_Kind: |
| 159 | return this->convertBreak((ASTBreakStatement&) statement); |
| 160 | case ASTStatement::kContinue_Kind: |
| 161 | return this->convertContinue((ASTContinueStatement&) statement); |
| 162 | case ASTStatement::kDiscard_Kind: |
| 163 | return this->convertDiscard((ASTDiscardStatement&) statement); |
| 164 | default: |
| 165 | ABORT("unsupported statement type: %d\n", statement.fKind); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | std::unique_ptr<Block> IRGenerator::convertBlock(const ASTBlock& block) { |
| 170 | AutoSymbolTable table(this); |
| 171 | std::vector<std::unique_ptr<Statement>> statements; |
| 172 | for (size_t i = 0; i < block.fStatements.size(); i++) { |
| 173 | std::unique_ptr<Statement> statement = this->convertStatement(*block.fStatements[i]); |
| 174 | if (!statement) { |
| 175 | return nullptr; |
| 176 | } |
| 177 | statements.push_back(std::move(statement)); |
| 178 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 179 | return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statements), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( |
| 183 | const ASTVarDeclarationStatement& s) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 184 | auto decl = this->convertVarDeclarations(*s.fDeclarations, Variable::kLocal_Storage); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 185 | if (!decl) { |
| 186 | return nullptr; |
| 187 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 188 | return std::unique_ptr<Statement>(new VarDeclarationsStatement(std::move(decl))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | } |
| 190 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 191 | std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTVarDeclarations& decl, |
| 192 | Variable::Storage storage) { |
| 193 | std::vector<VarDeclaration> variables; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 194 | const Type* baseType = this->convertType(*decl.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | if (!baseType) { |
| 196 | return nullptr; |
| 197 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 198 | for (const auto& varDecl : decl.fVars) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 199 | const Type* type = baseType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 200 | ASSERT(type->kind() != Type::kArray_Kind); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 201 | std::vector<std::unique_ptr<Expression>> sizes; |
| 202 | for (const auto& rawSize : varDecl.fSizes) { |
| 203 | if (rawSize) { |
| 204 | auto size = this->coerce(this->convertExpression(*rawSize), *fContext.fInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 205 | if (!size) { |
| 206 | return nullptr; |
| 207 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 208 | SkString name = type->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 209 | uint64_t count; |
| 210 | if (size->fKind == Expression::kIntLiteral_Kind) { |
| 211 | count = ((IntLiteral&) *size).fValue; |
| 212 | if (count <= 0) { |
| 213 | fErrors.error(size->fPosition, "array size must be positive"); |
| 214 | } |
| 215 | name += "[" + to_string(count) + "]"; |
| 216 | } else { |
| 217 | count = -1; |
| 218 | name += "[]"; |
| 219 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 220 | type = new Type(name, Type::kArray_Kind, *type, (int) count); |
| 221 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 222 | sizes.push_back(std::move(size)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 223 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 224 | type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1); |
| 225 | fSymbolTable->takeOwnership((Type*) type); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 226 | sizes.push_back(nullptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 229 | auto var = std::unique_ptr<Variable>(new Variable(decl.fPosition, decl.fModifiers, |
| 230 | varDecl.fName, *type, storage)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 231 | std::unique_ptr<Expression> value; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 232 | if (varDecl.fValue) { |
| 233 | value = this->convertExpression(*varDecl.fValue); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | if (!value) { |
| 235 | return nullptr; |
| 236 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 237 | value = this->coerce(std::move(value), *type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 238 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 239 | if (storage == Variable::kGlobal_Storage && varDecl.fName == SkString("sk_FragColor") && |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 240 | (*fSymbolTable)[varDecl.fName]) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 241 | // already defined, ignore |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 242 | } else if (storage == Variable::kGlobal_Storage && (*fSymbolTable)[varDecl.fName] && |
| 243 | (*fSymbolTable)[varDecl.fName]->fKind == Symbol::kVariable_Kind && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 244 | ((Variable*) (*fSymbolTable)[varDecl.fName])->fModifiers.fLayout.fBuiltin >= 0) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 245 | // already defined, just update the modifiers |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 246 | Variable* old = (Variable*) (*fSymbolTable)[varDecl.fName]; |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 247 | old->fModifiers = var->fModifiers; |
| 248 | } else { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 249 | variables.emplace_back(var.get(), std::move(sizes), std::move(value)); |
| 250 | fSymbolTable->add(varDecl.fName, std::move(var)); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 251 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 252 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 253 | return std::unique_ptr<VarDeclarations>(new VarDeclarations(decl.fPosition, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 254 | baseType, |
| 255 | std::move(variables))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 258 | std::unique_ptr<ModifiersDeclaration> IRGenerator::convertModifiersDeclaration( |
| 259 | const ASTModifiersDeclaration& m) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 260 | return std::unique_ptr<ModifiersDeclaration>(new ModifiersDeclaration(m.fModifiers)); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 263 | std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 264 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 265 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | if (!test) { |
| 267 | return nullptr; |
| 268 | } |
| 269 | std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); |
| 270 | if (!ifTrue) { |
| 271 | return nullptr; |
| 272 | } |
| 273 | std::unique_ptr<Statement> ifFalse; |
| 274 | if (s.fIfFalse) { |
| 275 | ifFalse = this->convertStatement(*s.fIfFalse); |
| 276 | if (!ifFalse) { |
| 277 | return nullptr; |
| 278 | } |
| 279 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 280 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 281 | // static boolean value, fold down to a single branch |
| 282 | if (((BoolLiteral&) *test).fValue) { |
| 283 | return ifTrue; |
| 284 | } else if (s.fIfFalse) { |
| 285 | return ifFalse; |
| 286 | } else { |
| 287 | // False & no else clause. Not an error, so don't return null! |
| 288 | std::vector<std::unique_ptr<Statement>> empty; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 289 | return std::unique_ptr<Statement>(new Block(s.fPosition, std::move(empty), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 290 | fSymbolTable)); |
| 291 | } |
| 292 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 293 | return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(test), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 294 | std::move(ifTrue), std::move(ifFalse))); |
| 295 | } |
| 296 | |
| 297 | std::unique_ptr<Statement> IRGenerator::convertFor(const ASTForStatement& f) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 298 | AutoLoopLevel level(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 299 | AutoSymbolTable table(this); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 300 | std::unique_ptr<Statement> initializer; |
| 301 | if (f.fInitializer) { |
| 302 | initializer = this->convertStatement(*f.fInitializer); |
| 303 | if (!initializer) { |
| 304 | return nullptr; |
| 305 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 306 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 307 | std::unique_ptr<Expression> test; |
| 308 | if (f.fTest) { |
| 309 | test = this->coerce(this->convertExpression(*f.fTest), *fContext.fBool_Type); |
| 310 | if (!test) { |
| 311 | return nullptr; |
| 312 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 313 | } |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 314 | std::unique_ptr<Expression> next; |
| 315 | if (f.fNext) { |
| 316 | next = this->convertExpression(*f.fNext); |
| 317 | if (!next) { |
| 318 | return nullptr; |
| 319 | } |
| 320 | this->checkValid(*next); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 321 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 322 | std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement); |
| 323 | if (!statement) { |
| 324 | return nullptr; |
| 325 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 326 | return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(initializer), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 327 | std::move(test), std::move(next), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 328 | std::move(statement), fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | std::unique_ptr<Statement> IRGenerator::convertWhile(const ASTWhileStatement& w) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 332 | AutoLoopLevel level(this); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 333 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 334 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 335 | if (!test) { |
| 336 | return nullptr; |
| 337 | } |
| 338 | std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement); |
| 339 | if (!statement) { |
| 340 | return nullptr; |
| 341 | } |
| 342 | return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move(test), |
| 343 | std::move(statement))); |
| 344 | } |
| 345 | |
| 346 | std::unique_ptr<Statement> IRGenerator::convertDo(const ASTDoStatement& d) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 347 | AutoLoopLevel level(this); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 348 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*d.fTest), |
| 349 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 350 | if (!test) { |
| 351 | return nullptr; |
| 352 | } |
| 353 | std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement); |
| 354 | if (!statement) { |
| 355 | return nullptr; |
| 356 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 357 | return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(statement), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 358 | std::move(test))); |
| 359 | } |
| 360 | |
| 361 | std::unique_ptr<Statement> IRGenerator::convertExpressionStatement( |
| 362 | const ASTExpressionStatement& s) { |
| 363 | std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression); |
| 364 | if (!e) { |
| 365 | return nullptr; |
| 366 | } |
| 367 | this->checkValid(*e); |
| 368 | return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); |
| 369 | } |
| 370 | |
| 371 | std::unique_ptr<Statement> IRGenerator::convertReturn(const ASTReturnStatement& r) { |
| 372 | ASSERT(fCurrentFunction); |
| 373 | if (r.fExpression) { |
| 374 | std::unique_ptr<Expression> result = this->convertExpression(*r.fExpression); |
| 375 | if (!result) { |
| 376 | return nullptr; |
| 377 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 378 | if (fCurrentFunction->fReturnType == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 379 | fErrors.error(result->fPosition, "may not return a value from a void function"); |
| 380 | } else { |
| 381 | result = this->coerce(std::move(result), fCurrentFunction->fReturnType); |
| 382 | if (!result) { |
| 383 | return nullptr; |
| 384 | } |
| 385 | } |
| 386 | return std::unique_ptr<Statement>(new ReturnStatement(std::move(result))); |
| 387 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 388 | if (fCurrentFunction->fReturnType != *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | fErrors.error(r.fPosition, "expected function to return '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 390 | fCurrentFunction->fReturnType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 391 | } |
| 392 | return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 397 | if (fLoopLevel > 0) { |
| 398 | return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); |
| 399 | } else { |
| 400 | fErrors.error(b.fPosition, "break statement must be inside a loop"); |
| 401 | return nullptr; |
| 402 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStatement& c) { |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 406 | if (fLoopLevel > 0) { |
| 407 | return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); |
| 408 | } else { |
| 409 | fErrors.error(c.fPosition, "continue statement must be inside a loop"); |
| 410 | return nullptr; |
| 411 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement& d) { |
| 415 | return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); |
| 416 | } |
| 417 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 418 | std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFunction& f) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 419 | const Type* returnType = this->convertType(*f.fReturnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 420 | if (!returnType) { |
| 421 | return nullptr; |
| 422 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 423 | std::vector<const Variable*> parameters; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 424 | for (const auto& param : f.fParameters) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 425 | const Type* type = this->convertType(*param->fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 426 | if (!type) { |
| 427 | return nullptr; |
| 428 | } |
| 429 | for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { |
| 430 | int size = param->fSizes[j]; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 431 | SkString name = type->name() + "[" + to_string(size) + "]"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 432 | Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size); |
| 433 | fSymbolTable->takeOwnership(newType); |
| 434 | type = newType; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 435 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 436 | SkString name = param->fName; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 437 | Position pos = param->fPosition; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 438 | Variable* var = new Variable(pos, param->fModifiers, std::move(name), *type, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 439 | Variable::kParameter_Storage); |
| 440 | fSymbolTable->takeOwnership(var); |
| 441 | parameters.push_back(var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // find existing declaration |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 445 | const FunctionDeclaration* decl = nullptr; |
| 446 | auto entry = (*fSymbolTable)[f.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 447 | if (entry) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 448 | std::vector<const FunctionDeclaration*> functions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 449 | switch (entry->fKind) { |
| 450 | case Symbol::kUnresolvedFunction_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 451 | functions = ((UnresolvedFunction*) entry)->fFunctions; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 452 | break; |
| 453 | case Symbol::kFunctionDeclaration_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 454 | functions.push_back((FunctionDeclaration*) entry); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 455 | break; |
| 456 | default: |
| 457 | fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); |
| 458 | return nullptr; |
| 459 | } |
| 460 | for (const auto& other : functions) { |
| 461 | ASSERT(other->fName == f.fName); |
| 462 | if (parameters.size() == other->fParameters.size()) { |
| 463 | bool match = true; |
| 464 | for (size_t i = 0; i < parameters.size(); i++) { |
| 465 | if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 466 | match = false; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if (match) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 471 | if (*returnType != other->fReturnType) { |
| 472 | FunctionDeclaration newDecl(f.fPosition, f.fName, parameters, *returnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 473 | fErrors.error(f.fPosition, "functions '" + newDecl.description() + |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 474 | "' and '" + other->description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 475 | "' differ only in return type"); |
| 476 | return nullptr; |
| 477 | } |
| 478 | decl = other; |
| 479 | for (size_t i = 0; i < parameters.size(); i++) { |
| 480 | if (parameters[i]->fModifiers != other->fParameters[i]->fModifiers) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 481 | fErrors.error(f.fPosition, "modifiers on parameter " + |
| 482 | to_string((uint64_t) i + 1) + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 483 | " differ between declaration and " |
| 484 | "definition"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 485 | return nullptr; |
| 486 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 487 | } |
| 488 | if (other->fDefined) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 489 | fErrors.error(f.fPosition, "duplicate definition of " + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 490 | other->description()); |
| 491 | } |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | if (!decl) { |
| 498 | // couldn't find an existing declaration |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 499 | auto newDecl = std::unique_ptr<FunctionDeclaration>(new FunctionDeclaration(f.fPosition, |
| 500 | f.fName, |
| 501 | parameters, |
| 502 | *returnType)); |
| 503 | decl = newDecl.get(); |
| 504 | fSymbolTable->add(decl->fName, std::move(newDecl)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 505 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 506 | if (f.fBody) { |
| 507 | ASSERT(!fCurrentFunction); |
| 508 | fCurrentFunction = decl; |
| 509 | decl->fDefined = true; |
| 510 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 511 | AutoSymbolTable table(this); |
| 512 | for (size_t i = 0; i < parameters.size(); i++) { |
| 513 | fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParameters[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 514 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 515 | std::unique_ptr<Block> body = this->convertBlock(*f.fBody); |
| 516 | fCurrentFunction = nullptr; |
| 517 | if (!body) { |
| 518 | return nullptr; |
| 519 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 520 | return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPosition, *decl, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 521 | std::move(body))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | } |
| 523 | return nullptr; |
| 524 | } |
| 525 | |
| 526 | std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInterfaceBlock& intf) { |
| 527 | std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 528 | AutoSymbolTable table(this); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 529 | std::vector<Type::Field> fields; |
| 530 | for (size_t i = 0; i < intf.fDeclarations.size(); i++) { |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 531 | std::unique_ptr<VarDeclarations> decl = this->convertVarDeclarations( |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 532 | *intf.fDeclarations[i], |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 533 | Variable::kGlobal_Storage); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 534 | if (!decl) { |
| 535 | return nullptr; |
| 536 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 537 | for (const auto& var : decl->fVars) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 538 | fields.push_back(Type::Field(var.fVar->fModifiers, var.fVar->fName, |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 539 | &var.fVar->fType)); |
| 540 | if (var.fValue) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 541 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 542 | "initializers are not permitted on interface block fields"); |
| 543 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 544 | if (var.fVar->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 545 | Modifiers::kOut_Flag | |
| 546 | Modifiers::kUniform_Flag | |
| 547 | Modifiers::kConst_Flag)) { |
| 548 | fErrors.error(decl->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 549 | "interface block fields may not have storage qualifiers"); |
| 550 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 551 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 552 | } |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 553 | Type* type = new Type(intf.fPosition, intf.fInterfaceName, fields); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 554 | old->takeOwnership(type); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 555 | SkString name = intf.fValueName.size() > 0 ? intf.fValueName : intf.fInterfaceName; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 556 | Variable* var = new Variable(intf.fPosition, intf.fModifiers, name, *type, |
| 557 | Variable::kGlobal_Storage); |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 558 | old->takeOwnership(var); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 559 | if (intf.fValueName.size()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 560 | old->addWithoutOwnership(intf.fValueName, var); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 561 | } else { |
| 562 | for (size_t i = 0; i < fields.size(); i++) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 563 | old->add(fields[i].fName, std::unique_ptr<Field>(new Field(intf.fPosition, *var, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 564 | (int) i))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 567 | return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *var, fSymbolTable)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 568 | } |
| 569 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 570 | const Type* IRGenerator::convertType(const ASTType& type) { |
| 571 | const Symbol* result = (*fSymbolTable)[type.fName]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 572 | if (result && result->fKind == Symbol::kType_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 573 | return (const Type*) result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 574 | } |
| 575 | fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
| 579 | std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { |
| 580 | switch (expr.fKind) { |
| 581 | case ASTExpression::kIdentifier_Kind: |
| 582 | return this->convertIdentifier((ASTIdentifier&) expr); |
| 583 | case ASTExpression::kBool_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 584 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | ((ASTBoolLiteral&) expr).fValue)); |
| 586 | case ASTExpression::kInt_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 587 | return std::unique_ptr<Expression>(new IntLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 588 | ((ASTIntLiteral&) expr).fValue)); |
| 589 | case ASTExpression::kFloat_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 590 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 591 | ((ASTFloatLiteral&) expr).fValue)); |
| 592 | case ASTExpression::kBinary_Kind: |
| 593 | return this->convertBinaryExpression((ASTBinaryExpression&) expr); |
| 594 | case ASTExpression::kPrefix_Kind: |
| 595 | return this->convertPrefixExpression((ASTPrefixExpression&) expr); |
| 596 | case ASTExpression::kSuffix_Kind: |
| 597 | return this->convertSuffixExpression((ASTSuffixExpression&) expr); |
| 598 | case ASTExpression::kTernary_Kind: |
| 599 | return this->convertTernaryExpression((ASTTernaryExpression&) expr); |
| 600 | default: |
| 601 | ABORT("unsupported expression type: %d\n", expr.fKind); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 606 | const Symbol* result = (*fSymbolTable)[identifier.fText]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 607 | if (!result) { |
| 608 | fErrors.error(identifier.fPosition, "unknown identifier '" + identifier.fText + "'"); |
| 609 | return nullptr; |
| 610 | } |
| 611 | switch (result->fKind) { |
| 612 | case Symbol::kFunctionDeclaration_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 613 | std::vector<const FunctionDeclaration*> f = { |
| 614 | (const FunctionDeclaration*) result |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 615 | }; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 616 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 617 | identifier.fPosition, |
| 618 | f)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 619 | } |
| 620 | case Symbol::kUnresolvedFunction_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 621 | const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 622 | return std::unique_ptr<FunctionReference>(new FunctionReference(fContext, |
| 623 | identifier.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 624 | f->fFunctions)); |
| 625 | } |
| 626 | case Symbol::kVariable_Kind: { |
Ethan Nicholas | cae3a4c | 2017-02-02 10:43:58 -0500 | [diff] [blame^] | 627 | Variable* var = (Variable*) result; |
| 628 | if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN && |
| 629 | fSettings->fFlipY && |
| 630 | (!fSettings->fCaps || !fSettings->fCaps->fragCoordConventionsExtensionString())) { |
| 631 | fInputs.fRTHeight = true; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 632 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 633 | // default to kRead_RefKind; this will be corrected later if the variable is written to |
| 634 | return std::unique_ptr<VariableReference>(new VariableReference( |
| 635 | identifier.fPosition, |
| 636 | *var, |
| 637 | VariableReference::kRead_RefKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 638 | } |
| 639 | case Symbol::kField_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 640 | const Field* field = (const Field*) result; |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 641 | VariableReference* base = new VariableReference(identifier.fPosition, field->fOwner, |
| 642 | VariableReference::kRead_RefKind); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 643 | return std::unique_ptr<Expression>(new FieldAccess( |
| 644 | std::unique_ptr<Expression>(base), |
| 645 | field->fFieldIndex, |
| 646 | FieldAccess::kAnonymousInterfaceBlock_OwnerKind)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 647 | } |
| 648 | case Symbol::kType_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 649 | const Type* t = (const Type*) result; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 650 | return std::unique_ptr<TypeReference>(new TypeReference(fContext, identifier.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 651 | *t)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 652 | } |
| 653 | default: |
| 654 | ABORT("unsupported symbol type %d\n", result->fKind); |
| 655 | } |
| 656 | |
| 657 | } |
| 658 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 659 | std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 660 | const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 661 | if (!expr) { |
| 662 | return nullptr; |
| 663 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 664 | if (expr->fType == type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 665 | return expr; |
| 666 | } |
| 667 | this->checkValid(*expr); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 668 | if (expr->fType == *fContext.fInvalid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 669 | return nullptr; |
| 670 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 671 | if (!expr->fType.canCoerceTo(type)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 672 | fErrors.error(expr->fPosition, "expected '" + type.description() + "', but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 673 | expr->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 674 | return nullptr; |
| 675 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 676 | if (type.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 677 | std::vector<std::unique_ptr<Expression>> args; |
| 678 | args.push_back(std::move(expr)); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 679 | ASTIdentifier id(Position(), type.description()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 680 | std::unique_ptr<Expression> ctor = this->convertIdentifier(id); |
| 681 | ASSERT(ctor); |
| 682 | return this->call(Position(), std::move(ctor), std::move(args)); |
| 683 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 684 | std::vector<std::unique_ptr<Expression>> args; |
| 685 | args.push_back(std::move(expr)); |
| 686 | return std::unique_ptr<Expression>(new Constructor(Position(), type, std::move(args))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 687 | } |
| 688 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 689 | static bool is_matrix_multiply(const Type& left, const Type& right) { |
| 690 | if (left.kind() == Type::kMatrix_Kind) { |
| 691 | return right.kind() == Type::kMatrix_Kind || right.kind() == Type::kVector_Kind; |
| 692 | } |
| 693 | return left.kind() == Type::kVector_Kind && right.kind() == Type::kMatrix_Kind; |
| 694 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 695 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 696 | /** |
| 697 | * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 698 | * legal, false otherwise. If false, the values of the out parameters are undefined. |
| 699 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 700 | static bool determine_binary_type(const Context& context, |
| 701 | Token::Kind op, |
| 702 | const Type& left, |
| 703 | const Type& right, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 704 | const Type** outLeftType, |
| 705 | const Type** outRightType, |
| 706 | const Type** outResultType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 707 | bool tryFlipped) { |
| 708 | bool isLogical; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 709 | bool validMatrixOrVectorOp; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 710 | switch (op) { |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 711 | case Token::EQ: |
| 712 | *outLeftType = &left; |
| 713 | *outRightType = &left; |
| 714 | *outResultType = &left; |
| 715 | return right.canCoerceTo(left); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 716 | case Token::EQEQ: // fall through |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 717 | case Token::NEQ: |
| 718 | isLogical = true; |
| 719 | validMatrixOrVectorOp = true; |
| 720 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 721 | case Token::LT: // fall through |
| 722 | case Token::GT: // fall through |
| 723 | case Token::LTEQ: // fall through |
| 724 | case Token::GTEQ: |
| 725 | isLogical = true; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 726 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 727 | break; |
| 728 | case Token::LOGICALOR: // fall through |
| 729 | case Token::LOGICALAND: // fall through |
| 730 | case Token::LOGICALXOR: // fall through |
| 731 | case Token::LOGICALOREQ: // fall through |
| 732 | case Token::LOGICALANDEQ: // fall through |
| 733 | case Token::LOGICALXOREQ: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 734 | *outLeftType = context.fBool_Type.get(); |
| 735 | *outRightType = context.fBool_Type.get(); |
| 736 | *outResultType = context.fBool_Type.get(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 737 | return left.canCoerceTo(*context.fBool_Type) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 738 | right.canCoerceTo(*context.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 739 | case Token::STAR: // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 740 | case Token::STAREQ: |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 741 | if (is_matrix_multiply(left, right)) { |
| 742 | // determine final component type |
| 743 | if (determine_binary_type(context, Token::STAR, left.componentType(), |
| 744 | right.componentType(), outLeftType, outRightType, |
| 745 | outResultType, false)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 746 | *outLeftType = &(*outResultType)->toCompound(context, left.columns(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 747 | left.rows());; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 748 | *outRightType = &(*outResultType)->toCompound(context, right.columns(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 749 | right.rows());; |
| 750 | int leftColumns = left.columns(); |
| 751 | int leftRows = left.rows(); |
| 752 | int rightColumns; |
| 753 | int rightRows; |
| 754 | if (right.kind() == Type::kVector_Kind) { |
| 755 | // matrix * vector treats the vector as a column vector, so we need to |
| 756 | // transpose it |
| 757 | rightColumns = right.rows(); |
| 758 | rightRows = right.columns(); |
| 759 | ASSERT(rightColumns == 1); |
| 760 | } else { |
| 761 | rightColumns = right.columns(); |
| 762 | rightRows = right.rows(); |
| 763 | } |
| 764 | if (rightColumns > 1) { |
| 765 | *outResultType = &(*outResultType)->toCompound(context, rightColumns, |
| 766 | leftRows); |
| 767 | } else { |
| 768 | // result was a column vector, transpose it back to a row |
| 769 | *outResultType = &(*outResultType)->toCompound(context, leftRows, |
| 770 | rightColumns); |
| 771 | } |
| 772 | return leftColumns == rightRows; |
| 773 | } else { |
| 774 | return false; |
| 775 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 776 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 777 | isLogical = false; |
| 778 | validMatrixOrVectorOp = true; |
| 779 | break; |
| 780 | case Token::PLUS: // fall through |
| 781 | case Token::PLUSEQ: // fall through |
| 782 | case Token::MINUS: // fall through |
| 783 | case Token::MINUSEQ: // fall through |
| 784 | case Token::SLASH: // fall through |
| 785 | case Token::SLASHEQ: |
| 786 | isLogical = false; |
| 787 | validMatrixOrVectorOp = true; |
| 788 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 789 | default: |
| 790 | isLogical = false; |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 791 | validMatrixOrVectorOp = false; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 792 | } |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 793 | bool isVectorOrMatrix = left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind; |
| 794 | // FIXME: incorrect for shift |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 795 | if (right.canCoerceTo(left) && (left.kind() == Type::kScalar_Kind || |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 796 | (isVectorOrMatrix && validMatrixOrVectorOp))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 797 | *outLeftType = &left; |
| 798 | *outRightType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 799 | if (isLogical) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 800 | *outResultType = context.fBool_Type.get(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 801 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 802 | *outResultType = &left; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 803 | } |
| 804 | return true; |
| 805 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 806 | if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 807 | (right.kind() == Type::kScalar_Kind)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 808 | if (determine_binary_type(context, op, left.componentType(), right, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 809 | outRightType, outResultType, false)) { |
| 810 | *outLeftType = &(*outLeftType)->toCompound(context, left.columns(), left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 811 | if (!isLogical) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 812 | *outResultType = &(*outResultType)->toCompound(context, left.columns(), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 813 | left.rows()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 814 | } |
| 815 | return true; |
| 816 | } |
| 817 | return false; |
| 818 | } |
| 819 | if (tryFlipped) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 820 | return determine_binary_type(context, op, right, left, outRightType, outLeftType, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 821 | outResultType, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 822 | } |
| 823 | return false; |
| 824 | } |
| 825 | |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 826 | std::unique_ptr<Expression> IRGenerator::constantFold(const Expression& left, |
| 827 | Token::Kind op, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 828 | const Expression& right) const { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 829 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 830 | // precision to calculate the results and hope the result makes sense. The plan is to move the |
| 831 | // Skia caps into SkSL, so we have access to all of them including the precisions of the various |
| 832 | // types, which will let us be more intelligent about this. |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 833 | if (left.fKind == Expression::kBoolLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 834 | right.fKind == Expression::kBoolLiteral_Kind) { |
| 835 | bool leftVal = ((BoolLiteral&) left).fValue; |
| 836 | bool rightVal = ((BoolLiteral&) right).fValue; |
| 837 | bool result; |
| 838 | switch (op) { |
| 839 | case Token::LOGICALAND: result = leftVal && rightVal; break; |
| 840 | case Token::LOGICALOR: result = leftVal || rightVal; break; |
| 841 | case Token::LOGICALXOR: result = leftVal ^ rightVal; break; |
| 842 | default: return nullptr; |
| 843 | } |
| 844 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, left.fPosition, result)); |
| 845 | } |
| 846 | #define RESULT(t, op) std::unique_ptr<Expression>(new t ## Literal(fContext, left.fPosition, \ |
| 847 | leftVal op rightVal)) |
| 848 | if (left.fKind == Expression::kIntLiteral_Kind && right.fKind == Expression::kIntLiteral_Kind) { |
| 849 | int64_t leftVal = ((IntLiteral&) left).fValue; |
| 850 | int64_t rightVal = ((IntLiteral&) right).fValue; |
| 851 | switch (op) { |
| 852 | case Token::PLUS: return RESULT(Int, +); |
| 853 | case Token::MINUS: return RESULT(Int, -); |
| 854 | case Token::STAR: return RESULT(Int, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 855 | case Token::SLASH: |
| 856 | if (rightVal) { |
| 857 | return RESULT(Int, /); |
| 858 | } |
| 859 | fErrors.error(right.fPosition, "division by zero"); |
| 860 | return nullptr; |
Ethan Nicholas | 2503ab6 | 2017-01-05 10:44:25 -0500 | [diff] [blame] | 861 | case Token::PERCENT: |
| 862 | if (rightVal) { |
| 863 | return RESULT(Int, %); |
| 864 | } |
| 865 | fErrors.error(right.fPosition, "division by zero"); |
| 866 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 867 | case Token::BITWISEAND: return RESULT(Int, &); |
| 868 | case Token::BITWISEOR: return RESULT(Int, |); |
| 869 | case Token::BITWISEXOR: return RESULT(Int, ^); |
| 870 | case Token::SHL: return RESULT(Int, <<); |
| 871 | case Token::SHR: return RESULT(Int, >>); |
| 872 | case Token::EQEQ: return RESULT(Bool, ==); |
| 873 | case Token::NEQ: return RESULT(Bool, !=); |
| 874 | case Token::GT: return RESULT(Bool, >); |
| 875 | case Token::GTEQ: return RESULT(Bool, >=); |
| 876 | case Token::LT: return RESULT(Bool, <); |
| 877 | case Token::LTEQ: return RESULT(Bool, <=); |
| 878 | default: return nullptr; |
| 879 | } |
| 880 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 881 | if (left.fKind == Expression::kFloatLiteral_Kind && |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 882 | right.fKind == Expression::kFloatLiteral_Kind) { |
| 883 | double leftVal = ((FloatLiteral&) left).fValue; |
| 884 | double rightVal = ((FloatLiteral&) right).fValue; |
| 885 | switch (op) { |
| 886 | case Token::PLUS: return RESULT(Float, +); |
| 887 | case Token::MINUS: return RESULT(Float, -); |
| 888 | case Token::STAR: return RESULT(Float, *); |
Ethan Nicholas | 9a5610e | 2017-01-03 15:16:29 -0500 | [diff] [blame] | 889 | case Token::SLASH: |
| 890 | if (rightVal) { |
| 891 | return RESULT(Float, /); |
| 892 | } |
| 893 | fErrors.error(right.fPosition, "division by zero"); |
| 894 | return nullptr; |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 895 | case Token::EQEQ: return RESULT(Bool, ==); |
| 896 | case Token::NEQ: return RESULT(Bool, !=); |
| 897 | case Token::GT: return RESULT(Bool, >); |
| 898 | case Token::GTEQ: return RESULT(Bool, >=); |
| 899 | case Token::LT: return RESULT(Bool, <); |
| 900 | case Token::LTEQ: return RESULT(Bool, <=); |
| 901 | default: return nullptr; |
| 902 | } |
| 903 | } |
| 904 | #undef RESULT |
| 905 | return nullptr; |
| 906 | } |
| 907 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 908 | std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( |
| 909 | const ASTBinaryExpression& expression) { |
| 910 | std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft); |
| 911 | if (!left) { |
| 912 | return nullptr; |
| 913 | } |
| 914 | std::unique_ptr<Expression> right = this->convertExpression(*expression.fRight); |
| 915 | if (!right) { |
| 916 | return nullptr; |
| 917 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 918 | const Type* leftType; |
| 919 | const Type* rightType; |
| 920 | const Type* resultType; |
| 921 | if (!determine_binary_type(fContext, expression.fOperator, left->fType, right->fType, &leftType, |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 922 | &rightType, &resultType, |
| 923 | !Token::IsAssignment(expression.fOperator))) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 924 | fErrors.error(expression.fPosition, "type mismatch: '" + |
| 925 | Token::OperatorName(expression.fOperator) + |
| 926 | "' cannot operate on '" + left->fType.fName + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 927 | "', '" + right->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 928 | return nullptr; |
| 929 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 930 | if (Token::IsAssignment(expression.fOperator)) { |
| 931 | this->markWrittenTo(*left, expression.fOperator != Token::EQ); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 932 | } |
| 933 | left = this->coerce(std::move(left), *leftType); |
| 934 | right = this->coerce(std::move(right), *rightType); |
| 935 | if (!left || !right) { |
| 936 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 937 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 938 | std::unique_ptr<Expression> result = this->constantFold(*left.get(), expression.fOperator, |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 939 | *right.get()); |
| 940 | if (!result) { |
| 941 | result = std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition, |
| 942 | std::move(left), |
| 943 | expression.fOperator, |
| 944 | std::move(right), |
| 945 | *resultType)); |
| 946 | } |
| 947 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 950 | std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 951 | const ASTTernaryExpression& expression) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 952 | std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*expression.fTest), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 953 | *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 954 | if (!test) { |
| 955 | return nullptr; |
| 956 | } |
| 957 | std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIfTrue); |
| 958 | if (!ifTrue) { |
| 959 | return nullptr; |
| 960 | } |
| 961 | std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fIfFalse); |
| 962 | if (!ifFalse) { |
| 963 | return nullptr; |
| 964 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 965 | const Type* trueType; |
| 966 | const Type* falseType; |
| 967 | const Type* resultType; |
| 968 | if (!determine_binary_type(fContext, Token::EQEQ, ifTrue->fType, ifFalse->fType, &trueType, |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 969 | &falseType, &resultType, true) || trueType != falseType) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 970 | fErrors.error(expression.fPosition, "ternary operator result mismatch: '" + |
| 971 | ifTrue->fType.fName + "', '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 972 | ifFalse->fType.fName + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 973 | return nullptr; |
| 974 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 975 | ifTrue = this->coerce(std::move(ifTrue), *trueType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 976 | if (!ifTrue) { |
| 977 | return nullptr; |
| 978 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 979 | ifFalse = this->coerce(std::move(ifFalse), *falseType); |
Ethan Nicholas | 2be687a | 2017-01-03 16:44:39 -0500 | [diff] [blame] | 980 | if (!ifFalse) { |
| 981 | return nullptr; |
| 982 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 983 | if (test->fKind == Expression::kBoolLiteral_Kind) { |
| 984 | // static boolean test, just return one of the branches |
| 985 | if (((BoolLiteral&) *test).fValue) { |
| 986 | return ifTrue; |
| 987 | } else { |
| 988 | return ifFalse; |
| 989 | } |
| 990 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 991 | return std::unique_ptr<Expression>(new TernaryExpression(expression.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 992 | std::move(test), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 993 | std::move(ifTrue), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 994 | std::move(ifFalse))); |
| 995 | } |
| 996 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 997 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 998 | const FunctionDeclaration& function, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 999 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 1000 | if (function.fParameters.size() != arguments.size()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1001 | SkString msg = "call to '" + function.fName + "' expected " + |
| 1002 | to_string((uint64_t) function.fParameters.size()) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1003 | " argument"; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1004 | if (function.fParameters.size() != 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1005 | msg += "s"; |
| 1006 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1007 | msg += ", but found " + to_string((uint64_t) arguments.size()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1008 | fErrors.error(position, msg); |
| 1009 | return nullptr; |
| 1010 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1011 | std::vector<const Type*> types; |
| 1012 | const Type* returnType; |
| 1013 | if (!function.determineFinalTypes(arguments, &types, &returnType)) { |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1014 | SkString msg = "no match for " + function.fName + "("; |
| 1015 | SkString separator; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1016 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1017 | msg += separator; |
| 1018 | separator = ", "; |
| 1019 | msg += arguments[i]->fType.description(); |
| 1020 | } |
| 1021 | msg += ")"; |
| 1022 | fErrors.error(position, msg); |
| 1023 | return nullptr; |
| 1024 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1025 | for (size_t i = 0; i < arguments.size(); i++) { |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1026 | arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1027 | if (!arguments[i]) { |
| 1028 | return nullptr; |
| 1029 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1030 | if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag)) { |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1031 | this->markWrittenTo(*arguments[i], true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1034 | return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType, function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1035 | std::move(arguments))); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1039 | * Determines the cost of coercing the arguments of a function to the required types. Returns true |
| 1040 | * if the cost could be computed, false if the call is not valid. Cost has no particular meaning |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1041 | * other than "lower costs are preferred". |
| 1042 | */ |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1043 | bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1044 | const std::vector<std::unique_ptr<Expression>>& arguments, |
| 1045 | int* outCost) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1046 | if (function.fParameters.size() != arguments.size()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1047 | return false; |
| 1048 | } |
| 1049 | int total = 0; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1050 | std::vector<const Type*> types; |
| 1051 | const Type* ignored; |
| 1052 | if (!function.determineFinalTypes(arguments, &types, &ignored)) { |
| 1053 | return false; |
| 1054 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1055 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1056 | int cost; |
ethannicholas | 471e894 | 2016-10-28 09:02:46 -0700 | [diff] [blame] | 1057 | if (arguments[i]->fType.determineCoercionCost(*types[i], &cost)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1058 | total += cost; |
| 1059 | } else { |
| 1060 | return false; |
| 1061 | } |
| 1062 | } |
| 1063 | *outCost = total; |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1067 | std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 1068 | std::unique_ptr<Expression> functionValue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1069 | std::vector<std::unique_ptr<Expression>> arguments) { |
| 1070 | if (functionValue->fKind == Expression::kTypeReference_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1071 | return this->convertConstructor(position, |
| 1072 | ((TypeReference&) *functionValue).fValue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1073 | std::move(arguments)); |
| 1074 | } |
| 1075 | if (functionValue->fKind != Expression::kFunctionReference_Kind) { |
| 1076 | fErrors.error(position, "'" + functionValue->description() + "' is not a function"); |
| 1077 | return nullptr; |
| 1078 | } |
| 1079 | FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 1080 | int bestCost = INT_MAX; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1081 | const FunctionDeclaration* best = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1082 | if (ref->fFunctions.size() > 1) { |
| 1083 | for (const auto& f : ref->fFunctions) { |
| 1084 | int cost; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1085 | if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1086 | bestCost = cost; |
| 1087 | best = f; |
| 1088 | } |
| 1089 | } |
| 1090 | if (best) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1091 | return this->call(position, *best, std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1092 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1093 | SkString msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 1094 | SkString separator; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1095 | for (size_t i = 0; i < arguments.size(); i++) { |
| 1096 | msg += separator; |
| 1097 | separator = ", "; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1098 | msg += arguments[i]->fType.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1099 | } |
| 1100 | msg += ")"; |
| 1101 | fErrors.error(position, msg); |
| 1102 | return nullptr; |
| 1103 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1104 | return this->call(position, *ref->fFunctions[0], std::move(arguments)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | std::unique_ptr<Expression> IRGenerator::convertConstructor( |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1108 | Position position, |
| 1109 | const Type& type, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1110 | std::vector<std::unique_ptr<Expression>> args) { |
| 1111 | // FIXME: add support for structs and arrays |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1112 | Type::Kind kind = type.kind(); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1113 | if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_Kind && |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1114 | kind != Type::kArray_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1115 | fErrors.error(position, "cannot construct '" + type.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1116 | return nullptr; |
| 1117 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1118 | if (type == *fContext.fFloat_Type && args.size() == 1 && |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1119 | args[0]->fKind == Expression::kIntLiteral_Kind) { |
| 1120 | int64_t value = ((IntLiteral&) *args[0]).fValue; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1121 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, position, (double) value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1122 | } |
| 1123 | if (args.size() == 1 && args[0]->fType == type) { |
| 1124 | // argument is already the right type, just return it |
| 1125 | return std::move(args[0]); |
| 1126 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1127 | if (type.isNumber()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1128 | if (args.size() != 1) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1129 | fErrors.error(position, "invalid arguments to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1130 | "' constructor, (expected exactly 1 argument, but found " + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1131 | to_string((uint64_t) args.size()) + ")"); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1132 | return nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1133 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1134 | if (args[0]->fType == *fContext.fBool_Type) { |
| 1135 | std::unique_ptr<IntLiteral> zero(new IntLiteral(fContext, position, 0)); |
| 1136 | std::unique_ptr<IntLiteral> one(new IntLiteral(fContext, position, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1137 | return std::unique_ptr<Expression>( |
| 1138 | new TernaryExpression(position, std::move(args[0]), |
| 1139 | this->coerce(std::move(one), type), |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1140 | this->coerce(std::move(zero), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1141 | type))); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1142 | } else if (!args[0]->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1143 | fErrors.error(position, "invalid argument to '" + type.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1144 | "' constructor (expected a number or bool, but found '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1145 | args[0]->fType.description() + "')"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1146 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1147 | if (args[0]->fKind == Expression::kIntLiteral_Kind && (type == *fContext.fInt_Type || |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1148 | type == *fContext.fUInt_Type)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1149 | return std::unique_ptr<Expression>(new IntLiteral(fContext, |
| 1150 | position, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1151 | ((IntLiteral&) *args[0]).fValue, |
| 1152 | &type)); |
| 1153 | } |
| 1154 | } else if (kind == Type::kArray_Kind) { |
| 1155 | const Type& base = type.componentType(); |
| 1156 | for (size_t i = 0; i < args.size(); i++) { |
| 1157 | args[i] = this->coerce(std::move(args[i]), base); |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1158 | if (!args[i]) { |
| 1159 | return nullptr; |
| 1160 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1161 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1162 | } else { |
| 1163 | ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); |
| 1164 | int actual = 0; |
| 1165 | for (size_t i = 0; i < args.size(); i++) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1166 | if (args[i]->fType.kind() == Type::kVector_Kind || |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1167 | args[i]->fType.kind() == Type::kMatrix_Kind) { |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 1168 | if (type.componentType().isNumber() != |
| 1169 | args[i]->fType.componentType().isNumber()) { |
| 1170 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1171 | "parameter to '" + type.description() + |
| 1172 | "' constructor"); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1173 | return nullptr; |
| 1174 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1175 | actual += args[i]->fType.rows() * args[i]->fType.columns(); |
| 1176 | } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1177 | actual += 1; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1178 | if (type.kind() != Type::kScalar_Kind) { |
| 1179 | args[i] = this->coerce(std::move(args[i]), type.componentType()); |
ethannicholas | 7effa7a | 2016-10-14 09:56:33 -0700 | [diff] [blame] | 1180 | if (!args[i]) { |
| 1181 | return nullptr; |
| 1182 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1185 | fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 1186 | "parameter to '" + type.description() + "' constructor"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1187 | return nullptr; |
| 1188 | } |
| 1189 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1190 | int min = type.rows() * type.columns(); |
| 1191 | int max = type.columns() > 1 ? INT_MAX : min; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1192 | if ((actual < min || actual > max) && |
| 1193 | !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (actual == 1))) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1194 | fErrors.error(position, "invalid arguments to '" + type.description() + |
| 1195 | "' constructor (expected " + to_string(min) + " scalar" + |
| 1196 | (min == 1 ? "" : "s") + ", but found " + to_string(actual) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1197 | ")"); |
| 1198 | return nullptr; |
| 1199 | } |
| 1200 | } |
| 1201 | return std::unique_ptr<Expression>(new Constructor(position, std::move(type), std::move(args))); |
| 1202 | } |
| 1203 | |
| 1204 | std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( |
| 1205 | const ASTPrefixExpression& expression) { |
| 1206 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fOperand); |
| 1207 | if (!base) { |
| 1208 | return nullptr; |
| 1209 | } |
| 1210 | switch (expression.fOperator) { |
| 1211 | case Token::PLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1212 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1213 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1214 | "'+' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1215 | return nullptr; |
| 1216 | } |
| 1217 | return base; |
| 1218 | case Token::MINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1219 | if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1220 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1221 | "'-' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1222 | return nullptr; |
| 1223 | } |
| 1224 | if (base->fKind == Expression::kIntLiteral_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1225 | return std::unique_ptr<Expression>(new IntLiteral(fContext, base->fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1226 | -((IntLiteral&) *base).fValue)); |
| 1227 | } |
| 1228 | if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 1229 | double value = -((FloatLiteral&) *base).fValue; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1230 | return std::unique_ptr<Expression>(new FloatLiteral(fContext, base->fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1231 | value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1232 | } |
| 1233 | return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS, std::move(base))); |
| 1234 | case Token::PLUSPLUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1235 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1236 | fErrors.error(expression.fPosition, |
| 1237 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1238 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1239 | return nullptr; |
| 1240 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1241 | this->markWrittenTo(*base, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1242 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1243 | case Token::MINUSMINUS: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1244 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1245 | fErrors.error(expression.fPosition, |
| 1246 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1247 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1248 | return nullptr; |
| 1249 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1250 | this->markWrittenTo(*base, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1251 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1252 | case Token::LOGICALNOT: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1253 | if (base->fType != *fContext.fBool_Type) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1254 | fErrors.error(expression.fPosition, |
| 1255 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1256 | "' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1257 | return nullptr; |
| 1258 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 1259 | if (base->fKind == Expression::kBoolLiteral_Kind) { |
| 1260 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, base->fPosition, |
| 1261 | !((BoolLiteral&) *base).fValue)); |
| 1262 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1263 | break; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1264 | case Token::BITWISENOT: |
| 1265 | if (base->fType != *fContext.fInt_Type) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1266 | fErrors.error(expression.fPosition, |
| 1267 | "'" + Token::OperatorName(expression.fOperator) + |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1268 | "' cannot operate on '" + base->fType.description() + "'"); |
| 1269 | return nullptr; |
| 1270 | } |
| 1271 | break; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1272 | default: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1273 | ABORT("unsupported prefix operator\n"); |
| 1274 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1275 | return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1276 | std::move(base))); |
| 1277 | } |
| 1278 | |
| 1279 | std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression> base, |
| 1280 | const ASTExpression& index) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1281 | if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::kMatrix_Kind && |
| 1282 | base->fType.kind() != Type::kVector_Kind) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1283 | fErrors.error(base->fPosition, "expected array, but found '" + base->fType.description() + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1284 | "'"); |
| 1285 | return nullptr; |
| 1286 | } |
| 1287 | std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 1288 | if (!converted) { |
| 1289 | return nullptr; |
| 1290 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1291 | if (converted->fType != *fContext.fUInt_Type) { |
| 1292 | converted = this->coerce(std::move(converted), *fContext.fInt_Type); |
| 1293 | if (!converted) { |
| 1294 | return nullptr; |
| 1295 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1296 | } |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1297 | return std::unique_ptr<Expression>(new IndexExpression(fContext, std::move(base), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1298 | std::move(converted))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1302 | const SkString& field) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1303 | auto fields = base->fType.fields(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1304 | for (size_t i = 0; i < fields.size(); i++) { |
| 1305 | if (fields[i].fName == field) { |
| 1306 | return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 1307 | } |
| 1308 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1309 | fErrors.error(base->fPosition, "type '" + base->fType.description() + "' does not have a " |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1310 | "field named '" + field + ""); |
| 1311 | return nullptr; |
| 1312 | } |
| 1313 | |
| 1314 | std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expression> base, |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1315 | const SkString& fields) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1316 | if (base->fType.kind() != Type::kVector_Kind) { |
| 1317 | fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1318 | return nullptr; |
| 1319 | } |
| 1320 | std::vector<int> swizzleComponents; |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1321 | for (size_t i = 0; i < fields.size(); i++) { |
| 1322 | switch (fields[i]) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1323 | case 'x': // fall through |
| 1324 | case 'r': // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1325 | case 's': |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1326 | swizzleComponents.push_back(0); |
| 1327 | break; |
| 1328 | case 'y': // fall through |
| 1329 | case 'g': // fall through |
| 1330 | case 't': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1331 | if (base->fType.columns() >= 2) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1332 | swizzleComponents.push_back(1); |
| 1333 | break; |
| 1334 | } |
| 1335 | // fall through |
| 1336 | case 'z': // fall through |
| 1337 | case 'b': // fall through |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1338 | case 'p': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1339 | if (base->fType.columns() >= 3) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1340 | swizzleComponents.push_back(2); |
| 1341 | break; |
| 1342 | } |
| 1343 | // fall through |
| 1344 | case 'w': // fall through |
| 1345 | case 'a': // fall through |
| 1346 | case 'q': |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1347 | if (base->fType.columns() >= 4) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1348 | swizzleComponents.push_back(3); |
| 1349 | break; |
| 1350 | } |
| 1351 | // fall through |
| 1352 | default: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 1353 | fErrors.error(base->fPosition, SkStringPrintf("invalid swizzle component '%c'", |
| 1354 | fields[i])); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1355 | return nullptr; |
| 1356 | } |
| 1357 | } |
| 1358 | ASSERT(swizzleComponents.size() > 0); |
| 1359 | if (swizzleComponents.size() > 4) { |
| 1360 | fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'"); |
| 1361 | return nullptr; |
| 1362 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1363 | return std::unique_ptr<Expression>(new Swizzle(fContext, std::move(base), swizzleComponents)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1366 | std::unique_ptr<Expression> IRGenerator::getCap(Position position, SkString name) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1367 | auto found = fCapsMap.find(name); |
| 1368 | if (found == fCapsMap.end()) { |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1369 | fErrors.error(position, "unknown capability flag '" + name + "'"); |
| 1370 | return nullptr; |
| 1371 | } |
| 1372 | switch (found->second.fKind) { |
| 1373 | case CapValue::kBool_Kind: |
| 1374 | return std::unique_ptr<Expression>(new BoolLiteral(fContext, position, |
| 1375 | (bool) found->second.fValue)); |
| 1376 | case CapValue::kInt_Kind: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1377 | return std::unique_ptr<Expression>(new IntLiteral(fContext, position, |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1378 | found->second.fValue)); |
| 1379 | } |
| 1380 | ASSERT(false); |
| 1381 | return nullptr; |
| 1382 | } |
| 1383 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1384 | std::unique_ptr<Expression> IRGenerator::convertSuffixExpression( |
| 1385 | const ASTSuffixExpression& expression) { |
| 1386 | std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase); |
| 1387 | if (!base) { |
| 1388 | return nullptr; |
| 1389 | } |
| 1390 | switch (expression.fSuffix->fKind) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1391 | case ASTSuffix::kIndex_Kind: { |
| 1392 | const ASTExpression* expr = ((ASTIndexSuffix&) *expression.fSuffix).fExpression.get(); |
| 1393 | if (expr) { |
| 1394 | return this->convertIndex(std::move(base), *expr); |
| 1395 | } else if (base->fKind == Expression::kTypeReference_Kind) { |
| 1396 | const Type& oldType = ((TypeReference&) *base).fValue; |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1397 | Type* newType = new Type(oldType.name() + "[]", Type::kArray_Kind, oldType, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1398 | -1); |
| 1399 | fSymbolTable->takeOwnership(newType); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1400 | return std::unique_ptr<Expression>(new TypeReference(fContext, base->fPosition, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1401 | *newType)); |
| 1402 | } else { |
| 1403 | fErrors.error(expression.fPosition, "'[]' must follow a type name"); |
ethannicholas | a54401d | 2016-10-14 08:37:32 -0700 | [diff] [blame] | 1404 | return nullptr; |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 1405 | } |
| 1406 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1407 | case ASTSuffix::kCall_Kind: { |
| 1408 | auto rawArguments = &((ASTCallSuffix&) *expression.fSuffix).fArguments; |
| 1409 | std::vector<std::unique_ptr<Expression>> arguments; |
| 1410 | for (size_t i = 0; i < rawArguments->size(); i++) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1411 | std::unique_ptr<Expression> converted = |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1412 | this->convertExpression(*(*rawArguments)[i]); |
| 1413 | if (!converted) { |
| 1414 | return nullptr; |
| 1415 | } |
| 1416 | arguments.push_back(std::move(converted)); |
| 1417 | } |
| 1418 | return this->call(expression.fPosition, std::move(base), std::move(arguments)); |
| 1419 | } |
| 1420 | case ASTSuffix::kField_Kind: { |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 1421 | if (base->fType == *fContext.fSkCaps_Type) { |
| 1422 | return this->getCap(expression.fPosition, |
| 1423 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1424 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1425 | switch (base->fType.kind()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1426 | case Type::kVector_Kind: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1427 | return this->convertSwizzle(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1428 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1429 | case Type::kStruct_Kind: |
| 1430 | return this->convertField(std::move(base), |
| 1431 | ((ASTFieldSuffix&) *expression.fSuffix).fField); |
| 1432 | default: |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1433 | fErrors.error(base->fPosition, "cannot swizzle value of type '" + |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1434 | base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1435 | return nullptr; |
| 1436 | } |
| 1437 | } |
| 1438 | case ASTSuffix::kPostIncrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1439 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1440 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1441 | "'++' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1442 | return nullptr; |
| 1443 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1444 | this->markWrittenTo(*base, true); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1445 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1446 | Token::PLUSPLUS)); |
| 1447 | case ASTSuffix::kPostDecrement_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1448 | if (!base->fType.isNumber()) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1449 | fErrors.error(expression.fPosition, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1450 | "'--' cannot operate on '" + base->fType.description() + "'"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1451 | return nullptr; |
| 1452 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1453 | this->markWrittenTo(*base, true); |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1454 | return std::unique_ptr<Expression>(new PostfixExpression(std::move(base), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1455 | Token::MINUSMINUS)); |
| 1456 | default: |
| 1457 | ABORT("unsupported suffix operator"); |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | void IRGenerator::checkValid(const Expression& expr) { |
| 1462 | switch (expr.fKind) { |
| 1463 | case Expression::kFunctionReference_Kind: |
| 1464 | fErrors.error(expr.fPosition, "expected '(' to begin function call"); |
| 1465 | break; |
| 1466 | case Expression::kTypeReference_Kind: |
| 1467 | fErrors.error(expr.fPosition, "expected '(' to begin constructor invocation"); |
| 1468 | break; |
| 1469 | default: |
ethannicholas | ea4567c | 2016-10-17 11:24:37 -0700 | [diff] [blame] | 1470 | if (expr.fType == *fContext.fInvalid_Type) { |
| 1471 | fErrors.error(expr.fPosition, "invalid expression"); |
| 1472 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1476 | static bool has_duplicates(const Swizzle& swizzle) { |
| 1477 | int bits = 0; |
| 1478 | for (int idx : swizzle.fComponents) { |
| 1479 | ASSERT(idx >= 0 && idx <= 3); |
| 1480 | int bit = 1 << idx; |
| 1481 | if (bits & bit) { |
| 1482 | return true; |
| 1483 | } |
| 1484 | bits |= bit; |
| 1485 | } |
| 1486 | return false; |
| 1487 | } |
| 1488 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1489 | void IRGenerator::markWrittenTo(const Expression& expr, bool readWrite) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1490 | switch (expr.fKind) { |
| 1491 | case Expression::kVariableReference_Kind: { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1492 | const Variable& var = ((VariableReference&) expr).fVariable; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1493 | if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUniform_Flag)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1494 | fErrors.error(expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1495 | "cannot modify immutable variable '" + var.fName + "'"); |
| 1496 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1497 | ((VariableReference&) expr).setRefKind(readWrite ? VariableReference::kReadWrite_RefKind |
| 1498 | : VariableReference::kWrite_RefKind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1499 | break; |
| 1500 | } |
| 1501 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1502 | this->markWrittenTo(*((FieldAccess&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1503 | break; |
| 1504 | case Expression::kSwizzle_Kind: |
| 1505 | if (has_duplicates((Swizzle&) expr)) { |
Ethan Nicholas | 11d5397 | 2016-11-28 11:23:23 -0500 | [diff] [blame] | 1506 | fErrors.error(expr.fPosition, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1507 | "cannot write to the same swizzle field more than once"); |
| 1508 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1509 | this->markWrittenTo(*((Swizzle&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1510 | break; |
| 1511 | case Expression::kIndex_Kind: |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 1512 | this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1513 | break; |
| 1514 | default: |
| 1515 | fErrors.error(expr.fPosition, "cannot assign to '" + expr.description() + "'"); |
| 1516 | break; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | } |