Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 */ | |
| 7 | |
| 8 #include "SkSLIRGenerator.h" | |
| 9 | |
| 10 #include "limits.h" | |
| 11 | |
| 12 #include "ast/SkSLASTBoolLiteral.h" | |
| 13 #include "ast/SkSLASTFieldSuffix.h" | |
| 14 #include "ast/SkSLASTFloatLiteral.h" | |
| 15 #include "ast/SkSLASTIndexSuffix.h" | |
| 16 #include "ast/SkSLASTIntLiteral.h" | |
| 17 #include "ir/SkSLBinaryExpression.h" | |
| 18 #include "ir/SkSLBoolLiteral.h" | |
| 19 #include "ir/SkSLBreakStatement.h" | |
| 20 #include "ir/SkSLConstructor.h" | |
| 21 #include "ir/SkSLContinueStatement.h" | |
| 22 #include "ir/SkSLDiscardStatement.h" | |
| 23 #include "ir/SkSLDoStatement.h" | |
| 24 #include "ir/SkSLExpressionStatement.h" | |
| 25 #include "ir/SkSLField.h" | |
| 26 #include "ir/SkSLFieldAccess.h" | |
| 27 #include "ir/SkSLFloatLiteral.h" | |
| 28 #include "ir/SkSLForStatement.h" | |
| 29 #include "ir/SkSLFunctionCall.h" | |
| 30 #include "ir/SkSLFunctionDeclaration.h" | |
| 31 #include "ir/SkSLFunctionDefinition.h" | |
| 32 #include "ir/SkSLFunctionReference.h" | |
| 33 #include "ir/SkSLIfStatement.h" | |
| 34 #include "ir/SkSLIndexExpression.h" | |
| 35 #include "ir/SkSLInterfaceBlock.h" | |
| 36 #include "ir/SkSLIntLiteral.h" | |
| 37 #include "ir/SkSLLayout.h" | |
| 38 #include "ir/SkSLPostfixExpression.h" | |
| 39 #include "ir/SkSLPrefixExpression.h" | |
| 40 #include "ir/SkSLReturnStatement.h" | |
| 41 #include "ir/SkSLSwizzle.h" | |
| 42 #include "ir/SkSLTernaryExpression.h" | |
| 43 #include "ir/SkSLUnresolvedFunction.h" | |
| 44 #include "ir/SkSLVariable.h" | |
| 45 #include "ir/SkSLVarDeclaration.h" | |
| 46 #include "ir/SkSLVarDeclarationStatement.h" | |
| 47 #include "ir/SkSLVariableReference.h" | |
| 48 #include "ir/SkSLWhileStatement.h" | |
| 49 | |
| 50 namespace SkSL { | |
| 51 | |
| 52 class AutoSymbolTable { | |
| 53 public: | |
| 54 AutoSymbolTable(IRGenerator* ir) | |
| 55 : fIR(ir) | |
| 56 , fPrevious(fIR->fSymbolTable) { | |
| 57 fIR->pushSymbolTable(); | |
| 58 } | |
| 59 | |
| 60 ~AutoSymbolTable() { | |
| 61 fIR->popSymbolTable(); | |
| 62 ASSERT(fPrevious == fIR->fSymbolTable); | |
| 63 } | |
| 64 | |
| 65 IRGenerator* fIR; | |
| 66 std::shared_ptr<SymbolTable> fPrevious; | |
| 67 }; | |
| 68 | |
| 69 IRGenerator::IRGenerator(std::shared_ptr<SymbolTable> symbolTable, ErrorReporter & errorReporter) | |
| 70 : fSymbolTable(symbolTable) | |
| 71 , fErrors(errorReporter) { | |
| 72 } | |
| 73 | |
| 74 void IRGenerator::pushSymbolTable() { | |
| 75 fSymbolTable.reset(new SymbolTable(fSymbolTable, fErrors)); | |
| 76 } | |
| 77 | |
| 78 void IRGenerator::popSymbolTable() { | |
| 79 fSymbolTable = fSymbolTable->fParent; | |
| 80 } | |
| 81 | |
| 82 std::unique_ptr<Extension> IRGenerator::convertExtension(ASTExtension& extension ) { | |
| 83 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extensi on.fName)); | |
| 84 } | |
| 85 | |
| 86 std::unique_ptr<Statement> IRGenerator::convertStatement(ASTStatement& statement ) { | |
| 87 switch (statement.fKind) { | |
| 88 case ASTStatement::kBlock_Kind: | |
| 89 return this->convertBlock((ASTBlock&) statement); | |
| 90 case ASTStatement::kVarDeclaration_Kind: | |
| 91 return this->convertVarDeclarationStatement((ASTVarDeclarationStatem ent&) statement); | |
| 92 case ASTStatement::kExpression_Kind: | |
| 93 return this->convertExpressionStatement((ASTExpressionStatement&) st atement); | |
| 94 case ASTStatement:: kIf_Kind: | |
| 95 return this->convertIf((ASTIfStatement&) statement); | |
| 96 case ASTStatement::kFor_Kind: | |
| 97 return this->convertFor((ASTForStatement&) statement); | |
| 98 case ASTStatement::kWhile_Kind: | |
| 99 return this->convertWhile((ASTWhileStatement&) statement); | |
| 100 case ASTStatement::kDo_Kind: | |
| 101 return this->convertDo((ASTDoStatement&) statement); | |
| 102 case ASTStatement::kReturn_Kind: | |
| 103 return this->convertReturn((ASTReturnStatement&) statement); | |
| 104 case ASTStatement::kBreak_Kind: | |
| 105 return this->convertBreak((ASTBreakStatement&) statement); | |
| 106 case ASTStatement::kContinue_Kind: | |
| 107 return this->convertContinue((ASTContinueStatement&) statement); | |
| 108 case ASTStatement::kDiscard_Kind: | |
| 109 return this->convertDiscard((ASTDiscardStatement&) statement); | |
| 110 default: | |
| 111 ABORT("unsupported statement type: %d\n", statement.fKind); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 std::unique_ptr<Block> IRGenerator::convertBlock(ASTBlock& block) { | |
| 116 AutoSymbolTable table(this); | |
| 117 std::vector<std::unique_ptr<Statement>> statements; | |
| 118 for (size_t i = 0; i < block.fStatements.size(); i++) { | |
| 119 std::unique_ptr<Statement> statement = this->convertStatement(*block.fSt atements[i]); | |
| 120 if (statement == nullptr) { | |
| 121 return nullptr; | |
| 122 } | |
| 123 statements.push_back(std::move(statement)); | |
| 124 } | |
| 125 return std::unique_ptr<Block>(new Block(block.fPosition, std::move(statement s))); | |
| 126 } | |
| 127 | |
| 128 std::unique_ptr<Statement> IRGenerator::convertVarDeclarationStatement( | |
| 129 ASTVarDeclar ationStatement& s) { | |
| 130 auto decl = this->convertVarDeclaration(*s.fDeclaration, Variable::kLocal_St orage); | |
| 131 if (decl == nullptr) { | |
| 132 return nullptr; | |
| 133 } | |
| 134 return std::unique_ptr<Statement>(new VarDeclarationStatement(std::move(decl ))); | |
| 135 } | |
| 136 | |
| 137 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { | |
| 138 return Modifiers(modifiers); | |
| 139 } | |
| 140 | |
| 141 std::unique_ptr<VarDeclaration> IRGenerator::convertVarDeclaration(ASTVarDeclara tion& decl, | |
| 142 Variable::Sto rage storage) { | |
| 143 std::vector<std::shared_ptr<Variable>> variables; | |
| 144 std::vector<std::vector<std::unique_ptr<Expression>>> sizes; | |
| 145 std::vector<std::unique_ptr<Expression>> values; | |
| 146 std::shared_ptr<Type> baseType = this->convertType(*decl.fType); | |
| 147 if (baseType == nullptr) { | |
| 148 return nullptr; | |
| 149 } | |
| 150 for (size_t i = 0; i < decl.fNames.size(); i++) { | |
| 151 Modifiers modifiers = this->convertModifiers(decl.fModifiers); | |
| 152 std::shared_ptr<Type> type = baseType; | |
| 153 std::vector<std::unique_ptr<Expression>> currentVarSizes; | |
| 154 for (size_t j = 0; j < decl.fSizes[i].size(); j++) { | |
| 155 if (decl.fSizes[i][j] != nullptr) { | |
| 156 ASTExpression& rawSize = *decl.fSizes[i][j]; | |
| 157 std::unique_ptr<Expression> size = this->convertExpression(rawSi ze); | |
| 158 if (size == nullptr) { | |
| 159 return nullptr; | |
| 160 } | |
| 161 std::string name = type->fName; | |
| 162 uint64_t count; | |
| 163 if (size->fKind == Expression::kIntLiteral_Kind) { | |
| 164 count = ((IntLiteral&) *size).fValue; | |
|
dogben
2016/06/26 03:57:53
nit: Maybe check that count is sane?
| |
| 165 name += "[" + to_string(count) + "]"; | |
| 166 } else { | |
| 167 count = -1; | |
| 168 name += "[]"; | |
| 169 } | |
| 170 type = std::shared_ptr<Type>(new Type(name, Type::kArray_Kind, t ype, (int) count)); | |
| 171 currentVarSizes.push_back(std::move(size)); | |
| 172 } else { | |
| 173 currentVarSizes.push_back(nullptr); | |
| 174 } | |
| 175 } | |
| 176 sizes.push_back(std::move(currentVarSizes)); | |
| 177 auto var = std::shared_ptr<Variable>(new Variable(decl.fPosition, modifi ers, | |
| 178 decl.fNames[i], type, storage)); | |
| 179 variables.push_back(var); | |
| 180 std::unique_ptr<Expression> value; | |
| 181 if (decl.fValues[i] != nullptr) { | |
| 182 value = this->convertExpression(*decl.fValues[i]); | |
| 183 if (value == nullptr) { | |
| 184 return nullptr; | |
| 185 } | |
| 186 value = this->coerce(std::move(value), type); | |
| 187 } else { | |
| 188 value = nullptr; | |
| 189 } | |
| 190 values.push_back(std::move(value)); | |
| 191 fSymbolTable->add(var->fName, var); | |
| 192 } | |
| 193 return std::unique_ptr<VarDeclaration>(new VarDeclaration(decl.fPosition, va riables, | |
| 194 std::move(sizes), std::move(values))); | |
| 195 } | |
| 196 | |
| 197 std::unique_ptr<Statement> IRGenerator::convertIf(ASTIfStatement& s) { | |
| 198 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test), kBool_Type); | |
| 199 if (test == nullptr) { | |
| 200 return nullptr; | |
| 201 } | |
| 202 std::unique_ptr<Statement> ifTrue = this->convertStatement(*s.fIfTrue); | |
| 203 if (ifTrue == nullptr) { | |
| 204 return nullptr; | |
| 205 } | |
| 206 std::unique_ptr<Statement> ifFalse; | |
| 207 if (s.fIfFalse != nullptr) { | |
| 208 ifFalse = this->convertStatement(*s.fIfFalse); | |
| 209 if (ifFalse == nullptr) { | |
| 210 return nullptr; | |
| 211 } | |
| 212 } | |
| 213 return std::unique_ptr<Statement>(new IfStatement(s.fPosition, std::move(tes t), | |
| 214 std::move(ifTrue), std::mo ve(ifFalse))); | |
| 215 } | |
| 216 | |
| 217 std::unique_ptr<Statement> IRGenerator::convertFor(ASTForStatement& f) { | |
| 218 AutoSymbolTable table(this); | |
| 219 std::unique_ptr<Statement> initializer = this->convertStatement(*f.fInitiali zer); | |
| 220 if (initializer == nullptr) { | |
| 221 return nullptr; | |
| 222 } | |
| 223 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*f.f Test), kBool_Type); | |
| 224 if (test == nullptr) { | |
| 225 return nullptr; | |
| 226 } | |
| 227 std::unique_ptr<Expression> next = this->convertExpression(*f.fNext); | |
| 228 if (next == nullptr) { | |
| 229 return nullptr; | |
| 230 } | |
| 231 std::unique_ptr<Statement> statement = this->convertStatement(*f.fStatement) ; | |
| 232 if (statement == nullptr) { | |
| 233 return nullptr; | |
| 234 } | |
| 235 return std::unique_ptr<Statement>(new ForStatement(f.fPosition, std::move(in itializer), | |
| 236 std::move(test), std::mov e(next), | |
| 237 std::move(statement))); | |
| 238 } | |
| 239 | |
| 240 std::unique_ptr<Statement> IRGenerator::convertWhile(ASTWhileStatement& w) { | |
| 241 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*w.f Test), kBool_Type); | |
| 242 if (test == nullptr) { | |
| 243 return nullptr; | |
| 244 } | |
| 245 std::unique_ptr<Statement> statement = this->convertStatement(*w.fStatement) ; | |
| 246 if (statement == nullptr) { | |
| 247 return nullptr; | |
| 248 } | |
| 249 return std::unique_ptr<Statement>(new WhileStatement(w.fPosition, std::move( test), | |
| 250 std::move(statement))); | |
| 251 } | |
| 252 | |
| 253 std::unique_ptr<Statement> IRGenerator::convertDo(ASTDoStatement& d) { | |
| 254 std::unique_ptr<Expression> test = this->convertExpression(*d.fTest); | |
| 255 if (test == nullptr) { | |
| 256 return nullptr; | |
| 257 } | |
| 258 if (test->fType != kBool_Type) { | |
| 259 fErrors.error(d.fPosition, "expected 'bool', but found '" + | |
| 260 test->fType->description() + "'"); | |
| 261 return nullptr; | |
| 262 } | |
| 263 std::unique_ptr<Statement> statement = this->convertStatement(*d.fStatement) ; | |
| 264 if (statement == nullptr) { | |
| 265 return nullptr; | |
| 266 } | |
| 267 return std::unique_ptr<Statement>(new DoStatement(d.fPosition, std::move(sta tement), | |
| 268 std::move(test))); | |
| 269 } | |
| 270 | |
| 271 std::unique_ptr<Statement> IRGenerator::convertExpressionStatement(ASTExpression Statement& s) { | |
| 272 std::unique_ptr<Expression> e = this->convertExpression(*s.fExpression); | |
| 273 if (e == nullptr) { | |
| 274 return nullptr; | |
| 275 } | |
| 276 return std::unique_ptr<Statement>(new ExpressionStatement(std::move(e))); | |
| 277 } | |
| 278 | |
| 279 std::unique_ptr<Statement> IRGenerator::convertReturn(ASTReturnStatement& r) { | |
| 280 if (r.fExpression) { | |
| 281 std::unique_ptr<Expression> result = this->convertExpression(*r.fExpress ion); | |
| 282 if (result == nullptr) { | |
| 283 return nullptr; | |
| 284 } | |
| 285 ASSERT(fCurrentFunction); | |
| 286 if (fCurrentFunction->fReturnType == kVoid_Type) { | |
| 287 fErrors.error(result->fPosition, "may not return a value from a void function"); | |
| 288 } else { | |
| 289 result = this->coerce(std::move(result), fCurrentFunction->fReturnTy pe); | |
| 290 if (result == nullptr) { | |
| 291 return nullptr; | |
| 292 } | |
| 293 } | |
| 294 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)) ); | |
| 295 } else { | |
| 296 if (fCurrentFunction->fReturnType != kVoid_Type) { | |
| 297 fErrors.error(r.fPosition, "expected function to return '" + | |
| 298 fCurrentFunction->fReturnType->descriptio n() + "'"); | |
| 299 } | |
| 300 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); | |
| 301 } | |
| 302 } | |
| 303 | |
| 304 std::unique_ptr<Statement> IRGenerator::convertBreak(ASTBreakStatement& b) { | |
| 305 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); | |
| 306 } | |
| 307 | |
| 308 std::unique_ptr<Statement> IRGenerator::convertContinue(ASTContinueStatement& c) { | |
| 309 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); | |
| 310 } | |
| 311 | |
| 312 std::unique_ptr<Statement> IRGenerator::convertDiscard(ASTDiscardStatement& d) { | |
| 313 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); | |
| 314 } | |
| 315 | |
| 316 static std::shared_ptr<Type> expand_generics(std::shared_ptr<Type> type, int i) { | |
| 317 if (type->kind() == Type::kGeneric_Kind) { | |
| 318 return type->coercibleTypes()[i]; | |
| 319 } | |
| 320 return type; | |
| 321 } | |
| 322 | |
| 323 static void expand_generics(std::shared_ptr<FunctionDeclaration> decl, | |
| 324 std::shared_ptr<SymbolTable> symbolTable) { | |
| 325 for (int i = 0; i < 4; i++) { | |
| 326 std::shared_ptr<Type> returnType = expand_generics(decl->fReturnType, i) ; | |
| 327 std::vector<std::shared_ptr<Variable>> parameters; | |
| 328 for (auto p : decl->fParameters) { | |
| 329 parameters.push_back(std::shared_ptr<Variable>(new Variable(decl->fP osition, | |
| 330 Modifier s(p->fModifiers), | |
| 331 p->fName , | |
| 332 expand_g enerics(p->fType, | |
| 333 i), | |
| 334 Variable::kP arameter_Storage))); | |
| 335 } | |
| 336 std::shared_ptr<FunctionDeclaration> expanded(new FunctionDeclaration(de cl->fPosition, | |
| 337 de cl->fName, | |
| 338 pa rameters, | |
| 339 re turnType)); | |
| 340 symbolTable->add(expanded->fName, expanded); | |
| 341 } | |
| 342 } | |
| 343 | |
| 344 std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(ASTFunction& f) { | |
| 345 std::shared_ptr<SymbolTable> old = fSymbolTable; | |
| 346 AutoSymbolTable table(this); | |
| 347 bool isGeneric; | |
| 348 std::shared_ptr<Type> returnType = this->convertType(*f.fReturnType); | |
| 349 if (returnType == nullptr) { | |
| 350 return nullptr; | |
| 351 } | |
| 352 isGeneric = returnType->kind() == Type::kGeneric_Kind; | |
| 353 std::vector<std::shared_ptr<Variable>> parameters; | |
| 354 for (size_t i = 0; i < f.fParameters.size(); i++) { | |
| 355 std::shared_ptr<Type> type = this->convertType(*f.fParameters[i]->fType) ; | |
| 356 if (type == nullptr) { | |
| 357 return nullptr; | |
| 358 } | |
| 359 for (int j = (int) f.fParameters[i]->fSizes.size() - 1; j >= 0; j--) { | |
| 360 int size = f.fParameters[i]->fSizes[j]; | |
| 361 type = std::shared_ptr<Type>(new Type(type->name() + "[" + to_string (size) + "]", | |
| 362 Type::kArray_Kind, type, size) ); | |
| 363 } | |
| 364 std::string name = f.fParameters[i]->fName; | |
| 365 Modifiers modifiers = this->convertModifiers(f.fParameters[i]->fModifier s); | |
| 366 Position pos = f.fParameters[i]->fPosition; | |
| 367 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable(p os, | |
| 368 m odifiers, | |
| 369 n ame, | |
| 370 t ype, | |
| 371 Variable::k Parameter_Storage)); | |
| 372 parameters.push_back(var); | |
| 373 isGeneric |= type->kind() == Type::kGeneric_Kind; | |
| 374 } | |
| 375 | |
| 376 // find existing declaration | |
| 377 std::shared_ptr<FunctionDeclaration> decl; | |
| 378 auto entry = (*old)[f.fName]; | |
| 379 if (entry) { | |
| 380 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | |
| 381 switch (entry->fKind) { | |
| 382 case Symbol::kUnresolvedFunction_Kind: | |
| 383 functions = std::static_pointer_cast<UnresolvedFunction>(entry)- >fFunctions; | |
| 384 break; | |
| 385 case Symbol::kFunctionDeclaration_Kind: | |
| 386 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(entry)); | |
| 387 break; | |
| 388 default: | |
| 389 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); | |
| 390 return nullptr; | |
| 391 } | |
| 392 for (auto other : functions) { | |
| 393 ASSERT(other->fName == f.fName); | |
| 394 if (parameters.size() == other->fParameters.size()) { | |
| 395 bool match = true; | |
| 396 for (size_t i = 0; i < parameters.size(); i++) { | |
| 397 if (parameters[i]->fType != other->fParameters[i]->fType) { | |
| 398 match = false; | |
| 399 break; | |
| 400 } | |
| 401 } | |
| 402 if (match) { | |
| 403 if (returnType != other->fReturnType) { | |
| 404 FunctionDeclaration newDecl = FunctionDeclaration(f.fPos ition, | |
| 405 f.fNam e, | |
| 406 parame ters, | |
| 407 return Type); | |
| 408 fErrors.error(f.fPosition, "functions '" + newDecl.descr iption() + | |
| 409 "' and '" + other->descriptio n() + | |
| 410 "' differ only in return type "); | |
| 411 return nullptr; | |
| 412 } | |
| 413 if (other->fDefined) { | |
| 414 fErrors.error(f.fPosition, "duplicate definition of " + | |
| 415 other->description()); | |
| 416 } | |
| 417 decl = other; | |
| 418 for (size_t i = 0; i < parameters.size(); i++) { | |
| 419 fSymbolTable->add(parameters[i]->fName, decl->fParameter s[i]); | |
| 420 } | |
| 421 break; | |
| 422 } | |
| 423 } | |
| 424 } | |
| 425 } | |
| 426 if (!decl) { | |
| 427 // couldn't find an existing declaration | |
| 428 decl.reset(new FunctionDeclaration(f.fPosition, f.fName, parameters, ret urnType)); | |
| 429 for (auto var : parameters) { | |
| 430 fSymbolTable->add(var->fName, var); | |
| 431 } | |
| 432 } | |
| 433 decl->fDefined = true; | |
| 434 if (isGeneric) { | |
| 435 ASSERT(f.fBody == nullptr); | |
| 436 expand_generics(decl, old); | |
| 437 } else { | |
| 438 old->add(decl->fName, decl); | |
| 439 if (f.fBody != nullptr) { | |
| 440 ASSERT(fCurrentFunction == nullptr); | |
| 441 fCurrentFunction = decl; | |
| 442 std::unique_ptr<Block> body = this->convertBlock(*f.fBody); | |
| 443 fCurrentFunction = nullptr; | |
| 444 if (body == nullptr) { | |
| 445 return nullptr; | |
| 446 } | |
| 447 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f. fPosition, decl, | |
| 448 st d::move(body))); | |
| 449 } | |
| 450 } | |
| 451 return nullptr; | |
| 452 } | |
| 453 | |
| 454 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(ASTInterfaceB lock& intf) { | |
| 455 std::shared_ptr<SymbolTable> old = fSymbolTable; | |
| 456 AutoSymbolTable table(this); | |
| 457 Modifiers mods = this->convertModifiers(intf.fModifiers); | |
| 458 std::vector<Type::Field> fields; | |
| 459 for (size_t i = 0; i < intf.fDeclarations.size(); i++) { | |
| 460 std::unique_ptr<VarDeclaration> decl = this->convertVarDeclaration(*intf .fDeclarations[i], | |
| 461 Variable::kGloba l_Storage); | |
| 462 for (size_t j = 0; j < decl->fVars.size(); j++) { | |
| 463 fields.push_back(Type::Field(decl->fVars[j]->fModifiers, decl->fVars [j]->fName, | |
| 464 decl->fVars[j]->fType)); | |
| 465 if (decl->fValues[j] != nullptr) { | |
| 466 fErrors.error(decl->fPosition, "initializers are not permitted o n interface block " | |
| 467 "fields"); | |
| 468 } | |
| 469 } | |
| 470 } | |
| 471 std::shared_ptr<Type> type = std::shared_ptr<Type>(new Type(intf.fInterfaceN ame, fields)); | |
| 472 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName; | |
| 473 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable(intf. fPosition, mods, | |
| 474 name, type, | |
| 475 Variable::kGloba l_Storage)); | |
| 476 if (intf.fValueName.length()) { | |
| 477 old->add(intf.fValueName, var); | |
| 478 | |
| 479 } else { | |
| 480 for (size_t i = 0; i < fields.size(); i++) { | |
| 481 std::shared_ptr<Field> field = std::shared_ptr<Field>(new Field(intf .fPosition, var, | |
| 482 (int ) i)); | |
| 483 old->add(fields[i].fName, field); | |
| 484 } | |
| 485 } | |
| 486 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, va r)); | |
| 487 } | |
| 488 | |
| 489 std::shared_ptr<Type> IRGenerator::convertType(ASTType& type) { | |
| 490 std::shared_ptr<Symbol> result = (*fSymbolTable)[type.fName]; | |
| 491 if (result != nullptr && result->fKind == Symbol::kType_Kind) { | |
| 492 return std::static_pointer_cast<Type>(result); | |
| 493 } | |
| 494 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); | |
| 495 return nullptr; | |
| 496 } | |
| 497 | |
| 498 std::unique_ptr<Expression> IRGenerator::convertExpression(ASTExpression& expr) { | |
| 499 switch (expr.fKind) { | |
| 500 case ASTExpression::kIdentifier_Kind: | |
| 501 return this->convertIdentifier((ASTIdentifier&) expr); | |
| 502 case ASTExpression::kBool_Kind: | |
| 503 return std::unique_ptr<Expression>(new BoolLiteral(expr.fPosition, | |
| 504 ((ASTBoolLiteral& ) expr).fValue)); | |
| 505 case ASTExpression::kInt_Kind: | |
| 506 return std::unique_ptr<Expression>(new IntLiteral(expr.fPosition, | |
|
dogben
2016/06/26 03:57:53
nit: Maybe check that fValue is in range of uint?
| |
| 507 ((ASTIntLiteral&) expr).fValue)); | |
| 508 case ASTExpression::kFloat_Kind: | |
| 509 return std::unique_ptr<Expression>(new FloatLiteral(expr.fPosition, | |
| 510 ((ASTFloatLitera l&) expr).fValue)); | |
| 511 case ASTExpression::kBinary_Kind: | |
| 512 return this->convertBinaryExpression((ASTBinaryExpression&) expr); | |
| 513 case ASTExpression::kPrefix_Kind: | |
| 514 return this->convertPrefixExpression((ASTPrefixExpression&) expr); | |
| 515 case ASTExpression::kSuffix_Kind: | |
| 516 return this->convertSuffixExpression((ASTSuffixExpression&) expr); | |
| 517 case ASTExpression::kTernary_Kind: | |
| 518 return this->convertTernaryExpression((ASTTernaryExpression&) expr); | |
| 519 default: | |
| 520 ABORT("unsupported expression type: %d\n", expr.fKind); | |
| 521 } | |
| 522 } | |
| 523 | |
| 524 std::unique_ptr<Expression> IRGenerator::convertIdentifier(ASTIdentifier& identi fier) { | |
| 525 std::shared_ptr<Symbol> result = (*fSymbolTable)[identifier.fText]; | |
| 526 if (result == nullptr) { | |
| 527 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier. fText + "'"); | |
| 528 return nullptr; | |
| 529 } | |
| 530 switch (result->fKind) { | |
| 531 case Symbol::kFunctionDeclaration_Kind: { | |
| 532 std::vector<std::shared_ptr<FunctionDeclaration>> f = { | |
| 533 std::static_pointer_cast<FunctionDeclaration>(result) | |
| 534 }; | |
| 535 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, | |
| 536 f)); | |
| 537 } | |
| 538 case Symbol::kUnresolvedFunction_Kind: { | |
| 539 auto f = std::static_pointer_cast<UnresolvedFunction>(result); | |
| 540 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, | |
| 541 f->f Functions)); | |
| 542 } | |
| 543 case Symbol::kVariable_Kind: { | |
| 544 std::shared_ptr<Variable> var = std::static_pointer_cast<Variable>(r esult); | |
| 545 this->markReadFrom(var); | |
| 546 return std::unique_ptr<VariableReference>(new VariableReference(iden tifier.fPosition, | |
| 547 var) ); | |
| 548 } | |
| 549 case Symbol::kField_Kind: { | |
| 550 std::shared_ptr<Field> field = std::static_pointer_cast<Field>(resul t); | |
| 551 VariableReference* base = new VariableReference(identifier.fPosition , field->fOwner); | |
| 552 return std::unique_ptr<Expression>(new FieldAccess(std::unique_ptr<E xpression>(base), | |
| 553 field->fFieldInde x)); | |
| 554 } | |
| 555 case Symbol::kType_Kind: { | |
| 556 auto t = std::static_pointer_cast<Type>(result); | |
| 557 return std::unique_ptr<TypeReference>(new TypeReference(identifier.f Position, t)); | |
| 558 } | |
| 559 default: | |
| 560 ABORT("unsupported symbol type %d\n", result->fKind); | |
| 561 } | |
| 562 | |
| 563 } | |
| 564 | |
| 565 std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr , | |
| 566 std::shared_ptr<Type> type) { | |
| 567 if (expr == nullptr) { | |
| 568 return nullptr; | |
| 569 } | |
| 570 if (*expr->fType == *type) { | |
| 571 return expr; | |
| 572 } | |
| 573 if (!expr->fType->canCoerceTo(type)) { | |
| 574 fErrors.error(expr->fPosition, "expected '" + type->description() + "', but found '" + | |
| 575 expr->fType->description() + "'"); | |
| 576 return nullptr; | |
| 577 } | |
| 578 if (type->kind() == Type::kScalar_Kind) { | |
| 579 std::vector<std::unique_ptr<Expression>> parameters; | |
| 580 parameters.push_back(std::move(expr)); | |
| 581 ASTIdentifier id(Position(), type->description()); | |
| 582 std::unique_ptr<Expression> ctor = this->convertIdentifier(id); | |
| 583 ASSERT(ctor); | |
| 584 return this->call(Position(), std::move(ctor), std::move(parameters)); | |
| 585 } | |
| 586 ABORT("cannot coerce %s to %s", expr->fType->description().c_str(), type->na me().c_str()); | |
| 587 } | |
| 588 | |
| 589 /** | |
| 590 * Determines the operand and result types of a binary expression. Returns true if the expression is | |
| 591 * legal, false otherwise. If false, the values of the out parameters are undefi ned. | |
| 592 */ | |
| 593 static bool determine_binary_type(Token::Kind op, std::shared_ptr<Type> left, | |
| 594 std::shared_ptr<Type> right, | |
| 595 std::shared_ptr<Type>* outLeftType, | |
| 596 std::shared_ptr<Type>* outRightType, | |
| 597 std::shared_ptr<Type>* outResultType, | |
| 598 bool tryFlipped) { | |
| 599 if (op == Token::STAR || op == Token::STAREQ) { | |
| 600 if (left->kind() == Type::kMatrix_Kind && right->kind() == Type::kVector _Kind) { | |
| 601 *outLeftType = left; | |
| 602 *outRightType = right; | |
| 603 *outResultType = right; | |
| 604 return left->rows() == right->columns(); | |
| 605 } | |
| 606 if (left->kind() == Type::kVector_Kind && right->kind() == Type::kMatrix _Kind) { | |
| 607 *outLeftType = left; | |
| 608 *outRightType = right; | |
| 609 *outResultType = left; | |
| 610 return left->columns() == right->columns(); | |
| 611 } | |
| 612 } | |
| 613 bool isLogical; | |
| 614 switch (op) { | |
| 615 case Token::EQEQ: // fall through | |
| 616 case Token::NEQ: // fall through | |
| 617 case Token::LT: // fall through | |
| 618 case Token::GT: // fall through | |
| 619 case Token::LTEQ: // fall through | |
| 620 case Token::GTEQ: | |
| 621 isLogical = true; | |
| 622 break; | |
| 623 default: | |
| 624 isLogical = false; | |
| 625 } | |
| 626 // FIXME: need to disallow illegal operations like vec3 > vec3 | |
| 627 if (left == right) { | |
| 628 *outLeftType = left; | |
| 629 *outRightType = left; | |
| 630 if (isLogical) { | |
| 631 *outResultType = kBool_Type; | |
| 632 } else { | |
| 633 *outResultType = left; | |
| 634 } | |
| 635 return true; | |
| 636 } | |
| 637 if (left->canCoerceTo(right)) { | |
| 638 *outLeftType = right; | |
| 639 *outRightType = right; | |
| 640 if (isLogical) { | |
| 641 *outResultType = kBool_Type; | |
| 642 } else { | |
| 643 *outResultType = right; | |
| 644 } | |
| 645 return true; | |
| 646 } | |
| 647 if ((left->columns() > 1) && (right->kind() == Type::kScalar_Kind)) { | |
| 648 if (determine_binary_type(op, left->componentType(), right, outLeftType, outRightType, | |
| 649 outResultType, false)) { | |
| 650 *outLeftType = (*outLeftType)->toCompound(left->columns(), left->row s()); | |
| 651 if (!isLogical) { | |
| 652 *outResultType = (*outResultType)->toCompound(left->columns(), l eft->rows()); | |
| 653 } | |
| 654 return true; | |
| 655 } | |
| 656 return false; | |
| 657 } | |
| 658 if (tryFlipped) { | |
| 659 return determine_binary_type(op, right, left, outRightType, outLeftType, outResultType, | |
| 660 false); | |
| 661 } | |
| 662 return false; | |
| 663 } | |
| 664 | |
| 665 std::unique_ptr<Expression> IRGenerator::convertBinaryExpression(ASTBinaryExpres sion& expression) { | |
| 666 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft ); | |
| 667 if (left == nullptr) { | |
| 668 return nullptr; | |
| 669 } | |
| 670 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRig ht); | |
| 671 if (right == nullptr) { | |
| 672 return nullptr; | |
| 673 } | |
| 674 std::shared_ptr<Type> leftType; | |
| 675 std::shared_ptr<Type> rightType; | |
| 676 std::shared_ptr<Type> resultType; | |
| 677 if (!determine_binary_type(expression.fOperator, left->fType, right->fType, &leftType, | |
| 678 &rightType, &resultType, true)) { | |
| 679 fErrors.error(expression.fPosition, "type mismatch: '" + | |
| 680 Token::OperatorName(expression.fOper ator) + | |
| 681 "' cannot operate on '" + left->fTyp e->fName + | |
| 682 "', '" + right->fType->fName + "'"); | |
| 683 return nullptr; | |
| 684 } | |
| 685 switch (expression.fOperator) { | |
| 686 case Token::EQ: // fall through | |
| 687 case Token::PLUSEQ: // fall through | |
| 688 case Token::MINUSEQ: // fall through | |
| 689 case Token::STAREQ: // fall through | |
| 690 case Token::SLASHEQ: // fall through | |
| 691 case Token::PERCENTEQ: // fall through | |
| 692 case Token::SHLEQ: // fall through | |
| 693 case Token::SHREQ: // fall through | |
| 694 case Token::BITWISEOREQ: // fall through | |
| 695 case Token::BITWISEXOREQ: // fall through | |
| 696 case Token::BITWISEANDEQ: // fall through | |
| 697 case Token::LOGICALOREQ: // fall through | |
| 698 case Token::LOGICALXOREQ: // fall through | |
| 699 case Token::LOGICALANDEQ: | |
| 700 this->markWrittenTo(*left); | |
| 701 default: | |
| 702 break; | |
| 703 } | |
| 704 return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition , | |
| 705 this->coerce(std::mo ve(left), leftType), | |
| 706 expression.fOperator , | |
| 707 this->coerce(std::mo ve(right), | |
| 708 rightTy pe), | |
| 709 resultType)); | |
| 710 } | |
| 711 | |
| 712 std::unique_ptr<Expression> IRGenerator::convertTernaryExpression(ASTTernaryExpr ession& expression) { | |
| 713 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*exp ression.fTest), | |
| 714 kBool_Type); | |
| 715 if (test == nullptr) { | |
| 716 return nullptr; | |
| 717 } | |
| 718 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIf True); | |
| 719 if (ifTrue == nullptr) { | |
| 720 return nullptr; | |
| 721 } | |
| 722 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fI fFalse); | |
| 723 if (ifFalse == nullptr) { | |
| 724 return nullptr; | |
| 725 } | |
| 726 std::shared_ptr<Type> trueType; | |
| 727 std::shared_ptr<Type> falseType; | |
| 728 std::shared_ptr<Type> resultType; | |
| 729 if (!determine_binary_type(Token::EQEQ, ifTrue->fType, ifFalse->fType, &true Type, | |
| 730 &falseType, &resultType, true)) { | |
| 731 fErrors.error(expression.fPosition, "ternary operator result mismatch: ' " + | |
| 732 ifTrue->fType->fName + "', '" + | |
| 733 ifFalse->fType->fName + "'"); | |
| 734 return nullptr; | |
| 735 } | |
| 736 ASSERT(trueType == falseType); | |
| 737 ifTrue = this->coerce(std::move(ifTrue), trueType); | |
| 738 ifFalse = this->coerce(std::move(ifFalse), falseType); | |
| 739 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio n, | |
| 740 std::move(test), | |
| 741 std::move(ifTrue), | |
| 742 std::move(ifFalse)) ); | |
| 743 } | |
| 744 | |
| 745 std::unique_ptr<Expression> IRGenerator::call(Position position, | |
| 746 std::shared_ptr<FunctionDeclaratio n> function, | |
| 747 std::vector<std::unique_ptr<Expres sion>> parameters) { | |
| 748 if (function->fParameters.size() != parameters.size()) { | |
| 749 std::string msg = "call to '" + function->fName + "' expected " + | |
| 750 to_string(function->fParameters.size()) + | |
| 751 " parameter"; | |
| 752 if (function->fParameters.size() != 1) { | |
| 753 msg += "s"; | |
| 754 } | |
| 755 msg += ", but found " + to_string(parameters.size()); | |
| 756 fErrors.error(position, msg); | |
| 757 return nullptr; | |
| 758 } | |
| 759 for (size_t i = 0; i < parameters.size(); i++) { | |
| 760 parameters[i] = this->coerce(std::move(parameters[i]), function->fParame ters[i]->fType); | |
|
dogben
2016/06/24 17:05:26
The GLSL spec says, "Mismatched types on input par
ethannicholas
2016/06/24 21:23:10
Again, not stressed about following the GLSL spec
| |
| 761 } | |
| 762 return std::unique_ptr<FunctionCall>(new FunctionCall(position, function, | |
| 763 std::move(parameters)) ); | |
| 764 } | |
| 765 | |
| 766 /** | |
| 767 * Determines the cost of coercing the parameters of a function to the required types. Returns true | |
| 768 * if the cost could be computed, false if the call is not valid. Cost has no pa rticular meaning | |
| 769 * other than "lower costs are preferred". | |
| 770 */ | |
| 771 bool IRGenerator::determineCallCost(std::shared_ptr<FunctionDeclaration> functio n, | |
| 772 std::vector<std::unique_ptr<Expression>>& pa rameters, | |
| 773 int* outCost) { | |
| 774 if (function->fParameters.size() != parameters.size()) { | |
| 775 return false; | |
| 776 } | |
| 777 int total = 0; | |
| 778 for (size_t i = 0; i < parameters.size(); i++) { | |
| 779 int cost; | |
| 780 if (parameters[i]->fType->determineCoercionCost(function->fParameters[i] ->fType, &cost)) { | |
| 781 total += cost; | |
| 782 } else { | |
| 783 return false; | |
| 784 } | |
| 785 } | |
| 786 *outCost = total; | |
| 787 return true; | |
| 788 } | |
| 789 | |
| 790 std::unique_ptr<Expression> IRGenerator::call(Position position, | |
| 791 std::unique_ptr<Expression> functi onValue, | |
| 792 std::vector<std::unique_ptr<Expres sion>> parameters) { | |
| 793 if (functionValue->fKind == Expression::kTypeReference_Kind) { | |
| 794 return this->convertConstructor(position, | |
| 795 ((TypeReference&) *functionValue).fValue , | |
| 796 std::move(parameters)); | |
| 797 } | |
| 798 if (functionValue->fKind != Expression::kFunctionReference_Kind) { | |
| 799 fErrors.error(position, "'" + functionValue->description() + "' is not a function"); | |
| 800 return nullptr; | |
| 801 } | |
| 802 FunctionReference* ref = (FunctionReference*) functionValue.get(); | |
| 803 int bestCost = INT_MAX; | |
| 804 std::shared_ptr<FunctionDeclaration> best; | |
| 805 if (ref->fFunctions.size() > 1) { | |
| 806 for (auto f : ref->fFunctions) { | |
| 807 int cost; | |
| 808 if (this->determineCallCost(f, parameters, &cost) && cost < bestCost ) { | |
| 809 bestCost = cost; | |
| 810 best = f; | |
| 811 } | |
| 812 } | |
| 813 if (best != nullptr) { | |
| 814 return this->call(position, best, std::move(parameters)); | |
| 815 } | |
| 816 std::string msg = "no match for " + ref->fFunctions[0]->fName + "("; | |
| 817 std::string separator = ""; | |
| 818 for (size_t i = 0; i < parameters.size(); i++) { | |
| 819 msg += separator; | |
| 820 separator = ", "; | |
| 821 msg += parameters[i]->fType->description(); | |
| 822 } | |
| 823 msg += ")"; | |
| 824 fErrors.error(position, msg); | |
| 825 return nullptr; | |
| 826 } | |
| 827 return this->call(position, ref->fFunctions[0], std::move(parameters)); | |
| 828 } | |
| 829 | |
| 830 std::unique_ptr<Expression> IRGenerator::convertConstructor(Position position, | |
| 831 std::shared_ptr<Type > type, | |
| 832 std::vector<std::unique_ptr<Ex pression>> params) { | |
| 833 if (type == kFloat_Type && params.size() == 1 && | |
| 834 params[0]->fKind == Expression::kIntLiteral_Kind) { | |
| 835 int64_t value = ((IntLiteral&) *params[0]).fValue; | |
| 836 return std::unique_ptr<Expression>(new FloatLiteral(position, (double) v alue)); | |
| 837 } | |
| 838 int min = type->rows() * type->columns(); | |
|
dogben
2016/06/24 17:05:26
I found the following in the GLSL spec: "If a matr
ethannicholas
2016/06/24 21:23:10
I'm definitely unsure that we want to copy GLSL he
| |
| 839 int max = type->columns() > 1 ? INT_MAX : min; | |
| 840 int actual = 0; | |
| 841 for (size_t i = 0; i < params.size(); i++) { | |
| 842 if (params[i]->fType->kind() == Type::kScalar_Kind) { | |
| 843 actual += 1; | |
| 844 if (type->kind() != Type::kScalar_Kind) { | |
|
dogben
2016/06/24 17:05:27
nit: probably can do this for scalars also. There
ethannicholas
2016/06/24 21:23:10
Without this check, we will run into infinite recu
| |
| 845 params[i] = coerce(std::move(params[i]), type->componentType()); | |
| 846 } | |
| 847 } else { | |
| 848 actual += params[i]->fType->rows() * params[i]->fType->columns(); | |
|
dogben
2016/06/24 17:05:26
IMO, somewhere in this function, you should re-arr
ethannicholas
2016/06/24 21:23:10
It's not my intent to leave all of those combinati
| |
| 849 } | |
| 850 } | |
| 851 if ((type->kind() != Type::kVector_Kind || actual != 1) && | |
| 852 (type->kind() != Type::kMatrix_Kind || actual != 1) && | |
| 853 (actual < min || actual > max)) { | |
| 854 fErrors.error(position, "invalid parameters to '" + type->description() + | |
| 855 "' constructor (expected " + to_string(min) + " scalars, " + | |
| 856 "but found " + to_string(actual) + ")"); | |
| 857 return nullptr; | |
| 858 } | |
| 859 if (type->isNumber()) { | |
| 860 ASSERT(params.size() == 1); | |
| 861 if (params[0]->fType == kBool_Type) { | |
| 862 return std::unique_ptr<Expression>(new TernaryExpression(position, s td::move(params[0]), | |
| 863 std::unique_ptr<Expression>(new IntLi teral(position, 1)), | |
|
dogben
2016/06/24 17:05:26
type->isNumber() is true for floats, but this will
| |
| 864 std::unique_ptr<Expression>(new IntLite ral(position, 0)))); | |
| 865 } | |
|
dogben
2016/06/24 17:05:27
Seems that you should check that the argument is a
ethannicholas
2016/06/24 21:23:10
Why? If we're doing a number->number cast, it just
dogben
2016/06/26 03:57:53
Ok, in that case, should we be checking that the a
ethannicholas
2016/06/27 20:35:03
I have tightened this up considerably.
| |
| 866 } | |
| 867 if (params.size() == 1 && params[0]->fType == type) { | |
| 868 // parameter is already the right type, just return it | |
| 869 return std::move(params[0]); | |
| 870 } | |
| 871 return std::unique_ptr<Expression>(new Constructor(position, type, std::move (params))); | |
|
dogben
2016/06/24 17:05:26
Do we need to check the argument types for struct
| |
| 872 } | |
| 873 | |
| 874 std::unique_ptr<Expression> IRGenerator::convertPrefixExpression(ASTPrefixExpres sion& expression) { | |
| 875 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOper and); | |
| 876 if (base == nullptr) { | |
| 877 return nullptr; | |
| 878 } | |
| 879 switch (expression.fOperator) { | |
| 880 case Token::PLUS: | |
| 881 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) { | |
| 882 fErrors.error(expression.fPosition, | |
| 883 "'+' cannot operate on '" + base->fType->descripti on() + "'"); | |
| 884 return nullptr; | |
| 885 } | |
| 886 return base; | |
| 887 case Token::MINUS: | |
| 888 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) { | |
| 889 fErrors.error(expression.fPosition, | |
| 890 "'-' cannot operate on '" + base->fType->descripti on() + "'"); | |
| 891 return nullptr; | |
| 892 } | |
| 893 if (base->fKind == Expression::kIntLiteral_Kind) { | |
| 894 return std::unique_ptr<Expression>(new IntLiteral(base->fPositio n, | |
|
dogben
2016/06/26 03:57:53
nit: Maybe check that -fValue is in range of int?
| |
| 895 -((IntLiteral& ) *base).fValue)); | |
| 896 } | |
| 897 if (base->fKind == Expression::kFloatLiteral_Kind) { | |
| 898 double value = -((FloatLiteral&) *base).fValue; | |
| 899 return std::unique_ptr<Expression>(new FloatLiteral(base->fPosit ion, value)); | |
| 900 } | |
| 901 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS , std::move(base))); | |
| 902 case Token::PLUSPLUS: | |
| 903 if (!base->fType->isNumber()) { | |
| 904 fErrors.error(expression.fPosition, | |
| 905 "'" + Token::OperatorName(expression.fOperator) + | |
| 906 "' cannot operate on '" + base->fType->description () + "'"); | |
| 907 return nullptr; | |
| 908 } | |
| 909 this->markWrittenTo(*base); | |
| 910 break; | |
| 911 case Token::MINUSMINUS: | |
| 912 if (!base->fType->isNumber()) { | |
| 913 fErrors.error(expression.fPosition, | |
| 914 "'" + Token::OperatorName(expression.fOperator) + | |
| 915 "' cannot operate on '" + base->fType->description () + "'"); | |
| 916 return nullptr; | |
| 917 } | |
| 918 this->markWrittenTo(*base); | |
| 919 break; | |
| 920 case Token::NOT: | |
| 921 if (base->fType != kBool_Type) { | |
| 922 fErrors.error(expression.fPosition, | |
| 923 "'" + Token::OperatorName(expression.fOperator) + | |
| 924 "' cannot operate on '" + base->fType->description () + "'"); | |
| 925 return nullptr; | |
| 926 } | |
| 927 break; | |
| 928 default: | |
| 929 ABORT("unsupported prefix operator\n"); | |
| 930 } | |
| 931 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator , | |
| 932 std::move(base))); | |
| 933 } | |
| 934 | |
| 935 std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression > base, | |
| 936 ASTExpression& index) { | |
| 937 if (base->fType->kind() != Type::kArray_Kind && base->fType->kind() != Type: :kMatrix_Kind && | |
| 938 base->fType->kind() != Type::kVector_Kind) { | |
| 939 fErrors.error(base->fPosition, "expected array, but found '" + base->fTy pe->description() + | |
| 940 "'"); | |
| 941 return nullptr; | |
| 942 } | |
| 943 std::unique_ptr<Expression> converted = this->convertExpression(index); | |
| 944 if (converted == nullptr) { | |
| 945 return nullptr; | |
| 946 } | |
| 947 converted = this->coerce(std::move(converted), kInt_Type); | |
| 948 if (converted == nullptr) { | |
| 949 return nullptr; | |
| 950 } | |
| 951 return std::unique_ptr<Expression>(new IndexExpression(std::move(base), std: :move(converted))); | |
| 952 } | |
| 953 | |
| 954 std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression > base, | |
| 955 std::string field) { | |
| 956 auto fields = base->fType->fields(); | |
| 957 for (size_t i = 0; i < fields.size(); i++) { | |
| 958 if (fields[i].fName == field) { | |
| 959 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); | |
| 960 } | |
| 961 } | |
| 962 fErrors.error(base->fPosition, "type '" + base->fType->description() + "' do es not have a " | |
| 963 "field named '" + field + ""); | |
| 964 return nullptr; | |
| 965 } | |
| 966 | |
| 967 std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expressi on> base, | |
| 968 std::string fields) { | |
| 969 if (base->fType->columns() == 0) { | |
| 970 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType->de scription() + "'"); | |
| 971 return nullptr; | |
| 972 } | |
| 973 std::vector<int> swizzleComponents; | |
| 974 for (char c : fields) { | |
| 975 switch (c) { | |
| 976 case 'x': // fall through | |
| 977 case 'r': // fall through | |
| 978 case 's': | |
| 979 swizzleComponents.push_back(0); | |
| 980 break; | |
| 981 case 'y': // fall through | |
| 982 case 'g': // fall through | |
| 983 case 't': | |
| 984 if (base->fType->columns() >= 2) { | |
| 985 swizzleComponents.push_back(1); | |
| 986 break; | |
| 987 } | |
| 988 // fall through | |
| 989 case 'z': // fall through | |
| 990 case 'b': // fall through | |
| 991 case 'p': | |
| 992 if (base->fType->columns() >= 3) { | |
| 993 swizzleComponents.push_back(2); | |
| 994 break; | |
| 995 } | |
| 996 // fall through | |
| 997 case 'w': // fall through | |
| 998 case 'a': // fall through | |
| 999 case 'q': | |
| 1000 if (base->fType->columns() >= 4) { | |
| 1001 swizzleComponents.push_back(3); | |
| 1002 break; | |
| 1003 } | |
| 1004 // fall through | |
| 1005 default: | |
| 1006 fErrors.error(base->fPosition, "invalid swizzle component '" + s td::string(1, c) + | |
| 1007 "'"); | |
| 1008 return nullptr; | |
| 1009 } | |
| 1010 } | |
| 1011 ASSERT(swizzleComponents.size() > 0); | |
| 1012 if (swizzleComponents.size() > 4) { | |
| 1013 fErrors.error(base->fPosition, "too many components in swizzle mask '" + fields + "'"); | |
| 1014 return nullptr; | |
| 1015 } | |
| 1016 return std::unique_ptr<Expression>(new Swizzle(std::move(base), swizzleCompo nents)); | |
|
dogben
2016/06/26 03:57:53
nit: I assume the following is illegal:
vec2 a = v
ethannicholas
2016/06/27 20:35:03
I've added a check in markWrittenTo (it's legal on
| |
| 1017 } | |
| 1018 | |
| 1019 std::unique_ptr<Expression> IRGenerator::convertSuffixExpression(ASTSuffixExpres sion& expression) { | |
| 1020 std::unique_ptr<Expression> base = this->convertExpression(*expression.fBase ); | |
| 1021 if (base == nullptr) { | |
| 1022 return nullptr; | |
| 1023 } | |
| 1024 switch (expression.fSuffix->fKind) { | |
| 1025 case ASTSuffix::kIndex_Kind: | |
| 1026 return this->convertIndex(std::move(base), | |
| 1027 *((ASTIndexSuffix&) *expression.fSuffix).f Expression); | |
| 1028 case ASTSuffix::kCall_Kind: { | |
| 1029 auto rawParameters = &((ASTCallSuffix&) *expression.fSuffix).fParame ters; | |
| 1030 std::vector<std::unique_ptr<Expression>> parameters; | |
| 1031 for (size_t i = 0; i < rawParameters->size(); i++) { | |
| 1032 std::unique_ptr<Expression> converted = this->convertExpression( | |
| 1033 *( *rawParameters)[i]); | |
| 1034 if (converted == nullptr) { | |
| 1035 return nullptr; | |
| 1036 } | |
| 1037 parameters.push_back(std::move(converted)); | |
| 1038 } | |
| 1039 return this->call(expression.fPosition, std::move(base), std::move(p arameters)); | |
| 1040 } | |
| 1041 case ASTSuffix::kField_Kind: { | |
| 1042 std::string field = ((ASTFieldSuffix&) *expression.fSuffix).fField; | |
| 1043 switch (base->fType->kind()) { | |
| 1044 case Type::kVector_Kind: | |
| 1045 return this->convertSwizzle(std::move(base), field); | |
| 1046 case Type::kStruct_Kind: | |
| 1047 return this->convertField(std::move(base), field); | |
| 1048 default: | |
| 1049 fErrors.error(base->fPosition, "cannot swizzle value of type '" + | |
| 1050 base->fType->description() + "'"); | |
| 1051 return nullptr; | |
| 1052 } | |
| 1053 } | |
| 1054 case ASTSuffix::kPostIncrement_Kind: | |
| 1055 if (!base->fType->isNumber()) { | |
| 1056 fErrors.error(expression.fPosition, | |
| 1057 "'++' cannot operate on '" + base->fType->descript ion() + "'"); | |
| 1058 return nullptr; | |
| 1059 } | |
| 1060 this->markWrittenTo(*base); | |
| 1061 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), | |
| 1062 Token::PLUS PLUS)); | |
| 1063 case ASTSuffix::kPostDecrement_Kind: | |
| 1064 if (!base->fType->isNumber()) { | |
| 1065 fErrors.error(expression.fPosition, | |
| 1066 "'--' cannot operate on '" + base->fType->descript ion() + "'"); | |
| 1067 return nullptr; | |
| 1068 } | |
| 1069 this->markWrittenTo(*base); | |
| 1070 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), | |
| 1071 Token::MINU SMINUS)); | |
| 1072 default: | |
| 1073 ABORT("unsupported suffix operator"); | |
| 1074 } | |
| 1075 } | |
| 1076 | |
| 1077 void IRGenerator::markReadFrom(std::shared_ptr<Variable> var) { | |
| 1078 var->fIsReadFrom = true; | |
| 1079 } | |
| 1080 | |
| 1081 void IRGenerator::markWrittenTo(Expression& expr) { | |
| 1082 switch (expr.fKind) { | |
| 1083 case Expression::kVariableReference_Kind: | |
| 1084 ((VariableReference&) expr).fVariable->fIsWrittenTo = true; | |
| 1085 break; | |
| 1086 case Expression::kFieldAccess_Kind: | |
| 1087 this->markWrittenTo(*((FieldAccess&) expr).fBase); | |
| 1088 break; | |
| 1089 case Expression::kSwizzle_Kind: | |
| 1090 this->markWrittenTo(*((Swizzle&) expr).fBase); | |
| 1091 break; | |
| 1092 case Expression::kIndex_Kind: | |
| 1093 this->markWrittenTo(*((IndexExpression&) expr).fBase); | |
| 1094 break; | |
| 1095 default: | |
| 1096 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'"); | |
| 1097 break; | |
| 1098 } | |
| 1099 } | |
| 1100 | |
| 1101 } | |
| OLD | NEW |