Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSLIRGenerator.h" | 8 #include "SkSLIRGenerator.h" |
| 9 | 9 |
| 10 #include "limits.h" | 10 #include "limits.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 fIR->popSymbolTable(); | 61 fIR->popSymbolTable(); |
| 62 ASSERT(fPrevious == fIR->fSymbolTable); | 62 ASSERT(fPrevious == fIR->fSymbolTable); |
| 63 } | 63 } |
| 64 | 64 |
| 65 IRGenerator* fIR; | 65 IRGenerator* fIR; |
| 66 std::shared_ptr<SymbolTable> fPrevious; | 66 std::shared_ptr<SymbolTable> fPrevious; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 IRGenerator::IRGenerator(std::shared_ptr<SymbolTable> symbolTable, | 69 IRGenerator::IRGenerator(std::shared_ptr<SymbolTable> symbolTable, |
| 70 ErrorReporter& errorReporter) | 70 ErrorReporter& errorReporter) |
| 71 : fSymbolTable(std::move(symbolTable)) | 71 : fCurrentFunction(nullptr) |
| 72 , fErrors(errorReporter) { | 72 , fSymbolTable(std::move(symbolTable)) |
| 73 } | 73 , fErrors(errorReporter) {} |
| 74 | 74 |
| 75 void IRGenerator::pushSymbolTable() { | 75 void IRGenerator::pushSymbolTable(bool owner) { |
| 76 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors)); | 76 fSymbolTable.reset(new SymbolTable(std::move(fSymbolTable), fErrors, owner)) ; |
| 77 } | 77 } |
| 78 | 78 |
| 79 void IRGenerator::popSymbolTable() { | 79 void IRGenerator::popSymbolTable() { |
| 80 fSymbolTable = fSymbolTable->fParent; | 80 fSymbolTable = fSymbolTable->fParent; |
| 81 } | 81 } |
| 82 | 82 |
| 83 std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& ext ension) { | 83 std::unique_ptr<Extension> IRGenerator::convertExtension(const ASTExtension& ext ension) { |
| 84 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extensi on.fName)); | 84 return std::unique_ptr<Extension>(new Extension(extension.fPosition, extensi on.fName)); |
| 85 } | 85 } |
| 86 | 86 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 } | 134 } |
| 135 return std::unique_ptr<Statement>(new VarDeclarationStatement(std::move(decl ))); | 135 return std::unique_ptr<Statement>(new VarDeclarationStatement(std::move(decl ))); |
| 136 } | 136 } |
| 137 | 137 |
| 138 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { | 138 Modifiers IRGenerator::convertModifiers(const ASTModifiers& modifiers) { |
| 139 return Modifiers(modifiers); | 139 return Modifiers(modifiers); |
| 140 } | 140 } |
| 141 | 141 |
| 142 std::unique_ptr<VarDeclaration> IRGenerator::convertVarDeclaration(const ASTVarD eclaration& decl, | 142 std::unique_ptr<VarDeclaration> IRGenerator::convertVarDeclaration(const ASTVarD eclaration& decl, |
| 143 Variable::Sto rage storage) { | 143 Variable::Sto rage storage) { |
| 144 std::vector<std::shared_ptr<Variable>> variables; | 144 std::vector<const Variable*> variables; |
| 145 std::vector<std::vector<std::unique_ptr<Expression>>> sizes; | 145 std::vector<std::vector<std::unique_ptr<Expression>>> sizes; |
| 146 std::vector<std::unique_ptr<Expression>> values; | 146 std::vector<std::unique_ptr<Expression>> values; |
| 147 std::shared_ptr<Type> baseType = this->convertType(*decl.fType); | 147 const Type* baseType = this->convertType(*decl.fType); |
| 148 if (!baseType) { | 148 if (!baseType) { |
| 149 return nullptr; | 149 return nullptr; |
| 150 } | 150 } |
| 151 for (size_t i = 0; i < decl.fNames.size(); i++) { | 151 for (size_t i = 0; i < decl.fNames.size(); i++) { |
| 152 Modifiers modifiers = this->convertModifiers(decl.fModifiers); | 152 Modifiers modifiers = this->convertModifiers(decl.fModifiers); |
| 153 std::shared_ptr<Type> type = baseType; | 153 const Type* type = baseType; |
| 154 ASSERT(type->kind() != Type::kArray_Kind); | 154 ASSERT(type->kind() != Type::kArray_Kind); |
| 155 std::vector<std::unique_ptr<Expression>> currentVarSizes; | 155 std::vector<std::unique_ptr<Expression>> currentVarSizes; |
| 156 for (size_t j = 0; j < decl.fSizes[i].size(); j++) { | 156 for (size_t j = 0; j < decl.fSizes[i].size(); j++) { |
| 157 if (decl.fSizes[i][j]) { | 157 if (decl.fSizes[i][j]) { |
| 158 ASTExpression& rawSize = *decl.fSizes[i][j]; | 158 ASTExpression& rawSize = *decl.fSizes[i][j]; |
| 159 auto size = this->coerce(this->convertExpression(rawSize), kInt_ Type); | 159 auto size = this->coerce(this->convertExpression(rawSize), kInt_ Type); |
| 160 if (!size) { | 160 if (!size) { |
| 161 return nullptr; | 161 return nullptr; |
| 162 } | 162 } |
| 163 std::string name = type->fName; | 163 std::string name = type->fName; |
| 164 uint64_t count; | 164 uint64_t count; |
| 165 if (size->fKind == Expression::kIntLiteral_Kind) { | 165 if (size->fKind == Expression::kIntLiteral_Kind) { |
| 166 count = ((IntLiteral&) *size).fValue; | 166 count = ((IntLiteral&) *size).fValue; |
| 167 if (count <= 0) { | 167 if (count <= 0) { |
| 168 fErrors.error(size->fPosition, "array size must be posit ive"); | 168 fErrors.error(size->fPosition, "array size must be posit ive"); |
| 169 } | 169 } |
| 170 name += "[" + to_string(count) + "]"; | 170 name += "[" + to_string(count) + "]"; |
| 171 } else { | 171 } else { |
| 172 count = -1; | 172 count = -1; |
| 173 name += "[]"; | 173 name += "[]"; |
| 174 } | 174 } |
| 175 type = std::shared_ptr<Type>(new Type(name, Type::kArray_Kind, t ype, (int) count)); | 175 type = new Type(name, Type::kArray_Kind, *type, (int) count); |
| 176 fSymbolTable->takeOwnership((Type*) type); | |
| 176 currentVarSizes.push_back(std::move(size)); | 177 currentVarSizes.push_back(std::move(size)); |
| 177 } else { | 178 } else { |
| 178 type = std::shared_ptr<Type>(new Type(type->fName + "[]", Type:: kArray_Kind, type, | 179 type = new Type(type->fName + "[]", Type::kArray_Kind, *type, -1 ); |
| 179 -1)); | 180 fSymbolTable->takeOwnership((Type*) type); |
| 180 currentVarSizes.push_back(nullptr); | 181 currentVarSizes.push_back(nullptr); |
| 181 } | 182 } |
| 182 } | 183 } |
| 183 sizes.push_back(std::move(currentVarSizes)); | 184 sizes.push_back(std::move(currentVarSizes)); |
| 184 auto var = std::make_shared<Variable>(decl.fPosition, modifiers, decl.fN ames[i], type, | 185 Variable* var = new Variable(decl.fPosition, modifiers, decl.fNames[i], *type, |
| 185 storage); | 186 storage); |
| 186 variables.push_back(var); | 187 variables.push_back(var); |
| 187 std::unique_ptr<Expression> value; | 188 std::unique_ptr<Expression> value; |
| 188 if (decl.fValues[i]) { | 189 if (decl.fValues[i]) { |
| 189 value = this->convertExpression(*decl.fValues[i]); | 190 value = this->convertExpression(*decl.fValues[i]); |
| 190 if (!value) { | 191 if (!value) { |
| 191 return nullptr; | 192 return nullptr; |
| 192 } | 193 } |
| 193 value = this->coerce(std::move(value), type); | 194 value = this->coerce(std::move(value), *type); |
| 194 } | 195 } |
| 195 fSymbolTable->add(var->fName, var); | 196 fSymbolTable->add(var->fName, var); |
| 196 values.push_back(std::move(value)); | 197 values.push_back(std::move(value)); |
| 197 } | 198 } |
| 198 return std::unique_ptr<VarDeclaration>(new VarDeclaration(decl.fPosition, st d::move(variables), | 199 return std::unique_ptr<VarDeclaration>(new VarDeclaration(decl.fPosition, st d::move(variables), |
| 199 std::move(sizes), std::move(values))); | 200 std::move(sizes), std::move(values))); |
| 200 } | 201 } |
| 201 | 202 |
| 202 std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { | 203 std::unique_ptr<Statement> IRGenerator::convertIf(const ASTIfStatement& s) { |
| 203 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test), kBool_Type); | 204 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*s.f Test), kBool_Type); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 } else { | 292 } else { |
| 292 result = this->coerce(std::move(result), fCurrentFunction->fReturnTy pe); | 293 result = this->coerce(std::move(result), fCurrentFunction->fReturnTy pe); |
| 293 if (!result) { | 294 if (!result) { |
| 294 return nullptr; | 295 return nullptr; |
| 295 } | 296 } |
| 296 } | 297 } |
| 297 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)) ); | 298 return std::unique_ptr<Statement>(new ReturnStatement(std::move(result)) ); |
| 298 } else { | 299 } else { |
| 299 if (fCurrentFunction->fReturnType != kVoid_Type) { | 300 if (fCurrentFunction->fReturnType != kVoid_Type) { |
| 300 fErrors.error(r.fPosition, "expected function to return '" + | 301 fErrors.error(r.fPosition, "expected function to return '" + |
| 301 fCurrentFunction->fReturnType->descriptio n() + "'"); | 302 fCurrentFunction->fReturnType.description () + "'"); |
| 302 } | 303 } |
| 303 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); | 304 return std::unique_ptr<Statement>(new ReturnStatement(r.fPosition)); |
| 304 } | 305 } |
| 305 } | 306 } |
| 306 | 307 |
| 307 std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { | 308 std::unique_ptr<Statement> IRGenerator::convertBreak(const ASTBreakStatement& b) { |
| 308 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); | 309 return std::unique_ptr<Statement>(new BreakStatement(b.fPosition)); |
| 309 } | 310 } |
| 310 | 311 |
| 311 std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStateme nt& c) { | 312 std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTContinueStateme nt& c) { |
| 312 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); | 313 return std::unique_ptr<Statement>(new ContinueStatement(c.fPosition)); |
| 313 } | 314 } |
| 314 | 315 |
| 315 std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement & d) { | 316 std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTDiscardStatement & d) { |
| 316 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); | 317 return std::unique_ptr<Statement>(new DiscardStatement(d.fPosition)); |
| 317 } | 318 } |
| 318 | 319 |
| 319 static std::shared_ptr<Type> expand_generics(std::shared_ptr<Type> type, int i) { | 320 static const Type& expand_generics(const Type& type, int i) { |
| 320 if (type->kind() == Type::kGeneric_Kind) { | 321 if (type.kind() == Type::kGeneric_Kind) { |
| 321 return type->coercibleTypes()[i]; | 322 return *type.coercibleTypes()[i]; |
| 322 } | 323 } |
| 323 return type; | 324 return type; |
| 324 } | 325 } |
| 325 | 326 |
| 326 static void expand_generics(FunctionDeclaration& decl, | 327 static void expand_generics(const FunctionDeclaration& decl, |
| 327 SymbolTable& symbolTable) { | 328 SymbolTable& symbolTable) { |
| 328 for (int i = 0; i < 4; i++) { | 329 for (int i = 0; i < 4; i++) { |
| 329 std::shared_ptr<Type> returnType = expand_generics(decl.fReturnType, i); | 330 const Type& returnType = expand_generics(decl.fReturnType, i); |
| 330 std::vector<std::shared_ptr<Variable>> arguments; | 331 std::vector<const Variable*> parameters; |
| 331 for (const auto& p : decl.fParameters) { | 332 for (const auto& p : decl.fParameters) { |
| 332 arguments.push_back(std::shared_ptr<Variable>(new Variable( | 333 Variable* var = new Variable(p->fPosition, Modifiers(p->fModifiers), p->fName, |
| 333 p->fPosition , | 334 expand_generics(p->fType, i), |
| 334 Modifiers(p- >fModifiers), | 335 Variable::kParameter_Storage); |
| 335 p->fName, | 336 symbolTable.takeOwnership(var); |
| 336 expand_gener ics(p->fType, i), | 337 parameters.push_back(var); |
| 337 Variable::kP arameter_Storage))); | |
| 338 } | 338 } |
| 339 std::shared_ptr<FunctionDeclaration> expanded(new FunctionDeclaration( | 339 FunctionDeclaration* expanded = new FunctionDeclaration(decl.fPosition, |
| 340 decl .fPosition, | 340 decl.fName, |
| 341 decl .fName, | 341 std::move(parame ters), |
| 342 std: :move(arguments), | 342 std::move(return Type)); |
| 343 std: :move(returnType))); | |
| 344 symbolTable.add(expanded->fName, expanded); | 343 symbolTable.add(expanded->fName, expanded); |
| 345 } | 344 } |
| 346 } | 345 } |
| 347 | 346 |
| 348 std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFuncti on& f) { | 347 std::unique_ptr<FunctionDefinition> IRGenerator::convertFunction(const ASTFuncti on& f) { |
| 349 std::shared_ptr<SymbolTable> old = fSymbolTable; | |
| 350 AutoSymbolTable table(this); | |
| 351 bool isGeneric; | 348 bool isGeneric; |
| 352 std::shared_ptr<Type> returnType = this->convertType(*f.fReturnType); | 349 const Type* returnType = this->convertType(*f.fReturnType); |
| 353 if (!returnType) { | 350 if (!returnType) { |
| 354 return nullptr; | 351 return nullptr; |
| 355 } | 352 } |
| 356 isGeneric = returnType->kind() == Type::kGeneric_Kind; | 353 isGeneric = returnType->kind() == Type::kGeneric_Kind; |
| 357 std::vector<std::shared_ptr<Variable>> parameters; | 354 std::vector<const Variable*> parameters; |
| 358 for (const auto& param : f.fParameters) { | 355 for (const auto& param : f.fParameters) { |
| 359 std::shared_ptr<Type> type = this->convertType(*param->fType); | 356 const Type* type = this->convertType(*param->fType); |
| 360 if (!type) { | 357 if (!type) { |
| 361 return nullptr; | 358 return nullptr; |
| 362 } | 359 } |
| 363 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { | 360 for (int j = (int) param->fSizes.size() - 1; j >= 0; j--) { |
| 364 int size = param->fSizes[j]; | 361 int size = param->fSizes[j]; |
| 365 std::string name = type->name() + "[" + to_string(size) + "]"; | 362 std::string name = type->name() + "[" + to_string(size) + "]"; |
| 366 type = std::shared_ptr<Type>(new Type(std::move(name), Type::kArray_ Kind, | 363 Type* newType = new Type(std::move(name), Type::kArray_Kind, *type, size); |
| 367 std::move(type), size)); | 364 fSymbolTable->takeOwnership(newType); |
| 365 type = newType; | |
| 368 } | 366 } |
| 369 std::string name = param->fName; | 367 std::string name = param->fName; |
| 370 Modifiers modifiers = this->convertModifiers(param->fModifiers); | 368 Modifiers modifiers = this->convertModifiers(param->fModifiers); |
| 371 Position pos = param->fPosition; | 369 Position pos = param->fPosition; |
| 372 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable( | 370 Variable* var = new Variable(pos, modifiers, std::move(name), *type, |
| 373 pos, | 371 Variable::kParameter_Storage); |
| 374 modifiers, | 372 fSymbolTable->takeOwnership(var); |
| 375 std::move(n ame), | 373 parameters.push_back(var); |
| 376 type, | |
| 377 Variable::k Parameter_Storage)); | |
| 378 parameters.push_back(std::move(var)); | |
| 379 isGeneric |= type->kind() == Type::kGeneric_Kind; | 374 isGeneric |= type->kind() == Type::kGeneric_Kind; |
| 380 } | 375 } |
| 381 | 376 |
| 382 // find existing declaration | 377 // find existing declaration |
| 383 std::shared_ptr<FunctionDeclaration> decl; | 378 const FunctionDeclaration* decl = nullptr; |
| 384 auto entry = (*old)[f.fName]; | 379 auto entry = (*fSymbolTable)[f.fName]; |
| 385 if (entry) { | 380 if (entry) { |
| 386 std::vector<std::shared_ptr<FunctionDeclaration>> functions; | 381 std::vector<const FunctionDeclaration*> functions; |
| 387 switch (entry->fKind) { | 382 switch (entry->fKind) { |
| 388 case Symbol::kUnresolvedFunction_Kind: | 383 case Symbol::kUnresolvedFunction_Kind: |
| 389 functions = std::static_pointer_cast<UnresolvedFunction>(entry)- >fFunctions; | 384 functions = ((UnresolvedFunction*) entry)->fFunctions; |
| 390 break; | 385 break; |
| 391 case Symbol::kFunctionDeclaration_Kind: | 386 case Symbol::kFunctionDeclaration_Kind: |
| 392 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(entry)); | 387 functions.push_back((FunctionDeclaration*) entry); |
| 393 break; | 388 break; |
| 394 default: | 389 default: |
| 395 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); | 390 fErrors.error(f.fPosition, "symbol '" + f.fName + "' was already defined"); |
| 396 return nullptr; | 391 return nullptr; |
| 397 } | 392 } |
| 398 for (const auto& other : functions) { | 393 for (const auto& other : functions) { |
| 399 ASSERT(other->fName == f.fName); | 394 ASSERT(other->fName == f.fName); |
| 400 if (parameters.size() == other->fParameters.size()) { | 395 if (parameters.size() == other->fParameters.size()) { |
| 401 bool match = true; | 396 bool match = true; |
| 402 for (size_t i = 0; i < parameters.size(); i++) { | 397 for (size_t i = 0; i < parameters.size(); i++) { |
| 403 if (parameters[i]->fType != other->fParameters[i]->fType) { | 398 if (parameters[i]->fType != other->fParameters[i]->fType) { |
| 404 match = false; | 399 match = false; |
| 405 break; | 400 break; |
| 406 } | 401 } |
| 407 } | 402 } |
| 408 if (match) { | 403 if (match) { |
| 409 if (returnType != other->fReturnType) { | 404 if (*returnType != other->fReturnType) { |
| 410 FunctionDeclaration newDecl = FunctionDeclaration(f.fPos ition, | 405 FunctionDeclaration newDecl(f.fPosition, f.fName, parame ters, *returnType); |
| 411 f.fNam e, | |
| 412 parame ters, | |
| 413 return Type); | |
| 414 fErrors.error(f.fPosition, "functions '" + newDecl.descr iption() + | 406 fErrors.error(f.fPosition, "functions '" + newDecl.descr iption() + |
| 415 "' and '" + other->descriptio n() + | 407 "' and '" + other->descriptio n() + |
| 416 "' differ only in return type "); | 408 "' differ only in return type "); |
| 417 return nullptr; | 409 return nullptr; |
| 418 } | 410 } |
| 419 decl = other; | 411 decl = other; |
| 420 for (size_t i = 0; i < parameters.size(); i++) { | 412 for (size_t i = 0; i < parameters.size(); i++) { |
| 421 if (parameters[i]->fModifiers != other->fParameters[i]-> fModifiers) { | 413 if (parameters[i]->fModifiers != other->fParameters[i]-> fModifiers) { |
| 422 fErrors.error(f.fPosition, "modifiers on parameter " + | 414 fErrors.error(f.fPosition, "modifiers on parameter " + |
| 423 to_string(i + 1) + " diff er between " + | 415 to_string(i + 1) + " diff er between " + |
| 424 "declaration and definiti on"); | 416 "declaration and definiti on"); |
| 425 return nullptr; | 417 return nullptr; |
| 426 } | 418 } |
| 427 fSymbolTable->add(parameters[i]->fName, decl->fParameter s[i]); | |
| 428 } | 419 } |
| 429 if (other->fDefined) { | 420 if (other->fDefined) { |
| 430 fErrors.error(f.fPosition, "duplicate definition of " + | 421 fErrors.error(f.fPosition, "duplicate definition of " + |
| 431 other->description()); | 422 other->description()); |
| 432 } | 423 } |
| 433 break; | 424 break; |
| 434 } | 425 } |
| 435 } | 426 } |
| 436 } | 427 } |
| 437 } | 428 } |
| 438 if (!decl) { | 429 if (!decl) { |
| 439 // couldn't find an existing declaration | 430 // couldn't find an existing declaration |
| 440 decl.reset(new FunctionDeclaration(f.fPosition, f.fName, parameters, ret urnType)); | 431 decl = new FunctionDeclaration(f.fPosition, f.fName, parameters, *return Type); |
| 441 for (auto var : parameters) { | 432 if (isGeneric) { |
| 442 fSymbolTable->add(var->fName, var); | 433 ASSERT(!f.fBody); |
| 434 expand_generics(*decl, *fSymbolTable); | |
| 435 delete decl; | |
|
dogben
2016/07/08 19:56:40
Missing return.
nit: maybe stack-allocate the Fun
| |
| 436 } else { | |
| 437 fSymbolTable->add(decl->fName, (FunctionDeclaration*) decl); | |
| 443 } | 438 } |
| 444 } | 439 } |
| 445 if (isGeneric) { | 440 if (f.fBody) { |
| 446 ASSERT(!f.fBody); | 441 ASSERT(!fCurrentFunction); |
| 447 expand_generics(*decl, *old); | 442 fCurrentFunction = decl; |
| 448 } else { | 443 decl->fDefined = true; |
| 449 old->add(decl->fName, decl); | 444 std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 450 if (f.fBody) { | 445 AutoSymbolTable table(this); |
| 451 ASSERT(!fCurrentFunction); | 446 for (size_t i = 0; i < parameters.size(); i++) { |
| 452 fCurrentFunction = decl; | 447 fSymbolTable->addWithoutOwnership(parameters[i]->fName, decl->fParam eters[i]); |
| 453 decl->fDefined = true; | |
| 454 std::unique_ptr<Block> body = this->convertBlock(*f.fBody); | |
| 455 fCurrentFunction = nullptr; | |
| 456 if (!body) { | |
| 457 return nullptr; | |
| 458 } | |
| 459 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f. fPosition, decl, | |
| 460 st d::move(body))); | |
| 461 } | 448 } |
| 449 std::unique_ptr<Block> body = this->convertBlock(*f.fBody); | |
| 450 fCurrentFunction = nullptr; | |
| 451 if (!body) { | |
| 452 return nullptr; | |
| 453 } | |
| 454 return std::unique_ptr<FunctionDefinition>(new FunctionDefinition(f.fPos ition, *decl, | |
| 455 std::m ove(body))); | |
| 462 } | 456 } |
| 463 return nullptr; | 457 return nullptr; |
| 464 } | 458 } |
| 465 | 459 |
| 466 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInte rfaceBlock& intf) { | 460 std::unique_ptr<InterfaceBlock> IRGenerator::convertInterfaceBlock(const ASTInte rfaceBlock& intf) { |
| 467 std::shared_ptr<SymbolTable> old = fSymbolTable; | 461 std::shared_ptr<SymbolTable> old = fSymbolTable; |
| 468 AutoSymbolTable table(this); | 462 AutoSymbolTable table(this); |
| 469 Modifiers mods = this->convertModifiers(intf.fModifiers); | 463 Modifiers mods = this->convertModifiers(intf.fModifiers); |
| 470 std::vector<Type::Field> fields; | 464 std::vector<Type::Field> fields; |
| 471 for (size_t i = 0; i < intf.fDeclarations.size(); i++) { | 465 for (size_t i = 0; i < intf.fDeclarations.size(); i++) { |
| 472 std::unique_ptr<VarDeclaration> decl = this->convertVarDeclaration( | 466 std::unique_ptr<VarDeclaration> decl = this->convertVarDeclaration( |
| 473 *intf.f Declarations[i], | 467 *intf.f Declarations[i], |
| 474 Variabl e::kGlobal_Storage); | 468 Variabl e::kGlobal_Storage); |
| 475 for (size_t j = 0; j < decl->fVars.size(); j++) { | 469 for (size_t j = 0; j < decl->fVars.size(); j++) { |
| 476 fields.push_back(Type::Field(decl->fVars[j]->fModifiers, decl->fVars [j]->fName, | 470 fields.push_back(Type::Field(decl->fVars[j]->fModifiers, decl->fVars [j]->fName, |
| 477 decl->fVars[j]->fType)); | 471 decl->fVars[j]->fType)); |
| 478 if (decl->fValues[j]) { | 472 if (decl->fValues[j]) { |
| 479 fErrors.error(decl->fPosition, | 473 fErrors.error(decl->fPosition, |
| 480 "initializers are not permitted on interface block fields"); | 474 "initializers are not permitted on interface block fields"); |
| 481 } | 475 } |
| 482 if (decl->fVars[j]->fModifiers.fFlags & (Modifiers::kIn_Flag | | 476 if (decl->fVars[j]->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 483 Modifiers::kOut_Flag | | 477 Modifiers::kOut_Flag | |
| 484 Modifiers::kUniform_Flag | | 478 Modifiers::kUniform_Flag | |
| 485 Modifiers::kConst_Flag)) { | 479 Modifiers::kConst_Flag)) { |
| 486 fErrors.error(decl->fPosition, | 480 fErrors.error(decl->fPosition, |
| 487 "interface block fields may not have storage quali fiers"); | 481 "interface block fields may not have storage quali fiers"); |
| 488 } | 482 } |
| 489 } | 483 } |
| 490 } | 484 } |
| 491 std::shared_ptr<Type> type = std::shared_ptr<Type>(new Type(intf.fInterfaceN ame, fields)); | 485 Type* type = new Type(intf.fInterfaceName, fields); |
| 486 fSymbolTable->takeOwnership(type); | |
| 492 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName; | 487 std::string name = intf.fValueName.length() > 0 ? intf.fValueName : intf.fIn terfaceName; |
| 493 std::shared_ptr<Variable> var = std::shared_ptr<Variable>(new Variable(intf. fPosition, mods, | 488 Variable* var = new Variable(intf.fPosition, mods, name, *type, Variable::kG lobal_Storage); |
| 494 name, type, | |
| 495 Variable::kGloba l_Storage)); | |
| 496 if (intf.fValueName.length()) { | 489 if (intf.fValueName.length()) { |
| 497 old->add(intf.fValueName, var); | 490 old->add(intf.fValueName, var); |
| 498 | |
| 499 } else { | 491 } else { |
| 500 for (size_t i = 0; i < fields.size(); i++) { | 492 for (size_t i = 0; i < fields.size(); i++) { |
| 501 std::shared_ptr<Field> field = std::shared_ptr<Field>(new Field(intf .fPosition, var, | 493 old->add(fields[i].fName, new Field(intf.fPosition, *var, (int) i)); |
| 502 (int ) i)); | |
| 503 old->add(fields[i].fName, field); | |
| 504 } | 494 } |
| 505 } | 495 } |
| 506 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, va r)); | 496 return std::unique_ptr<InterfaceBlock>(new InterfaceBlock(intf.fPosition, *v ar)); |
| 507 } | 497 } |
| 508 | 498 |
| 509 std::shared_ptr<Type> IRGenerator::convertType(const ASTType& type) { | 499 const Type* IRGenerator::convertType(const ASTType& type) { |
| 510 std::shared_ptr<Symbol> result = (*fSymbolTable)[type.fName]; | 500 const Symbol* result = (*fSymbolTable)[type.fName]; |
| 511 if (result && result->fKind == Symbol::kType_Kind) { | 501 if (result && result->fKind == Symbol::kType_Kind) { |
| 512 return std::static_pointer_cast<Type>(result); | 502 return (const Type*) result; |
| 513 } | 503 } |
| 514 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); | 504 fErrors.error(type.fPosition, "unknown type '" + type.fName + "'"); |
| 515 return nullptr; | 505 return nullptr; |
| 516 } | 506 } |
| 517 | 507 |
| 518 std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { | 508 std::unique_ptr<Expression> IRGenerator::convertExpression(const ASTExpression& expr) { |
| 519 switch (expr.fKind) { | 509 switch (expr.fKind) { |
| 520 case ASTExpression::kIdentifier_Kind: | 510 case ASTExpression::kIdentifier_Kind: |
| 521 return this->convertIdentifier((ASTIdentifier&) expr); | 511 return this->convertIdentifier((ASTIdentifier&) expr); |
| 522 case ASTExpression::kBool_Kind: | 512 case ASTExpression::kBool_Kind: |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 535 case ASTExpression::kSuffix_Kind: | 525 case ASTExpression::kSuffix_Kind: |
| 536 return this->convertSuffixExpression((ASTSuffixExpression&) expr); | 526 return this->convertSuffixExpression((ASTSuffixExpression&) expr); |
| 537 case ASTExpression::kTernary_Kind: | 527 case ASTExpression::kTernary_Kind: |
| 538 return this->convertTernaryExpression((ASTTernaryExpression&) expr); | 528 return this->convertTernaryExpression((ASTTernaryExpression&) expr); |
| 539 default: | 529 default: |
| 540 ABORT("unsupported expression type: %d\n", expr.fKind); | 530 ABORT("unsupported expression type: %d\n", expr.fKind); |
| 541 } | 531 } |
| 542 } | 532 } |
| 543 | 533 |
| 544 std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { | 534 std::unique_ptr<Expression> IRGenerator::convertIdentifier(const ASTIdentifier& identifier) { |
| 545 std::shared_ptr<Symbol> result = (*fSymbolTable)[identifier.fText]; | 535 const Symbol* result = (*fSymbolTable)[identifier.fText]; |
| 546 if (!result) { | 536 if (!result) { |
| 547 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier. fText + "'"); | 537 fErrors.error(identifier.fPosition, "unknown identifier '" + identifier. fText + "'"); |
| 548 return nullptr; | 538 return nullptr; |
| 549 } | 539 } |
| 550 switch (result->fKind) { | 540 switch (result->fKind) { |
| 551 case Symbol::kFunctionDeclaration_Kind: { | 541 case Symbol::kFunctionDeclaration_Kind: { |
| 552 std::vector<std::shared_ptr<FunctionDeclaration>> f = { | 542 std::vector<const FunctionDeclaration*> f = { |
| 553 std::static_pointer_cast<FunctionDeclaration>(result) | 543 (const FunctionDeclaration*) result |
| 554 }; | 544 }; |
| 555 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, | 545 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, |
| 556 std: :move(f))); | 546 f)); |
| 557 } | 547 } |
| 558 case Symbol::kUnresolvedFunction_Kind: { | 548 case Symbol::kUnresolvedFunction_Kind: { |
| 559 auto f = std::static_pointer_cast<UnresolvedFunction>(result); | 549 const UnresolvedFunction* f = (const UnresolvedFunction*) result; |
| 560 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, | 550 return std::unique_ptr<FunctionReference>(new FunctionReference(iden tifier.fPosition, |
| 561 f->f Functions)); | 551 f->f Functions)); |
| 562 } | 552 } |
| 563 case Symbol::kVariable_Kind: { | 553 case Symbol::kVariable_Kind: { |
| 564 std::shared_ptr<Variable> var = std::static_pointer_cast<Variable>(r esult); | 554 const Variable* var = (const Variable*) result; |
| 565 this->markReadFrom(var); | 555 this->markReadFrom(*var); |
| 566 return std::unique_ptr<VariableReference>(new VariableReference(iden tifier.fPosition, | 556 return std::unique_ptr<VariableReference>(new VariableReference(iden tifier.fPosition, |
| 567 std: :move(var))); | 557 *var )); |
| 568 } | 558 } |
| 569 case Symbol::kField_Kind: { | 559 case Symbol::kField_Kind: { |
| 570 std::shared_ptr<Field> field = std::static_pointer_cast<Field>(resul t); | 560 const Field* field = (const Field*) result; |
| 571 VariableReference* base = new VariableReference(identifier.fPosition , field->fOwner); | 561 VariableReference* base = new VariableReference(identifier.fPosition , field->fOwner); |
| 572 return std::unique_ptr<Expression>(new FieldAccess(std::unique_ptr<E xpression>(base), | 562 return std::unique_ptr<Expression>(new FieldAccess(std::unique_ptr<E xpression>(base), |
| 573 field->fFieldInde x)); | 563 field->fFieldInde x)); |
| 574 } | 564 } |
| 575 case Symbol::kType_Kind: { | 565 case Symbol::kType_Kind: { |
| 576 auto t = std::static_pointer_cast<Type>(result); | 566 const Type* t = (const Type*) result; |
| 577 return std::unique_ptr<TypeReference>(new TypeReference(identifier.f Position, | 567 return std::unique_ptr<TypeReference>(new TypeReference(identifier.f Position, |
| 578 std::move(t) )); | 568 *t)); |
| 579 } | 569 } |
| 580 default: | 570 default: |
| 581 ABORT("unsupported symbol type %d\n", result->fKind); | 571 ABORT("unsupported symbol type %d\n", result->fKind); |
| 582 } | 572 } |
| 583 | 573 |
| 584 } | 574 } |
| 585 | 575 |
| 586 std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr , | 576 std::unique_ptr<Expression> IRGenerator::coerce(std::unique_ptr<Expression> expr , |
| 587 std::shared_ptr<Type> type) { | 577 const Type& type) { |
| 588 if (!expr) { | 578 if (!expr) { |
| 589 return nullptr; | 579 return nullptr; |
| 590 } | 580 } |
| 591 if (*expr->fType == *type) { | 581 if (expr->fType == type) { |
| 592 return expr; | 582 return expr; |
| 593 } | 583 } |
| 594 this->checkValid(*expr); | 584 this->checkValid(*expr); |
| 595 if (*expr->fType == *kInvalid_Type) { | 585 if (expr->fType == kInvalid_Type) { |
| 596 return nullptr; | 586 return nullptr; |
| 597 } | 587 } |
| 598 if (!expr->fType->canCoerceTo(type)) { | 588 if (!expr->fType.canCoerceTo(type)) { |
| 599 fErrors.error(expr->fPosition, "expected '" + type->description() + "', but found '" + | 589 fErrors.error(expr->fPosition, "expected '" + type.description() + "', b ut found '" + |
| 600 expr->fType->description() + "'"); | 590 expr->fType.description() + "'"); |
| 601 return nullptr; | 591 return nullptr; |
| 602 } | 592 } |
| 603 if (type->kind() == Type::kScalar_Kind) { | 593 if (type.kind() == Type::kScalar_Kind) { |
| 604 std::vector<std::unique_ptr<Expression>> args; | 594 std::vector<std::unique_ptr<Expression>> args; |
| 605 args.push_back(std::move(expr)); | 595 args.push_back(std::move(expr)); |
| 606 ASTIdentifier id(Position(), type->description()); | 596 ASTIdentifier id(Position(), type.description()); |
| 607 std::unique_ptr<Expression> ctor = this->convertIdentifier(id); | 597 std::unique_ptr<Expression> ctor = this->convertIdentifier(id); |
| 608 ASSERT(ctor); | 598 ASSERT(ctor); |
| 609 return this->call(Position(), std::move(ctor), std::move(args)); | 599 return this->call(Position(), std::move(ctor), std::move(args)); |
| 610 } | 600 } |
| 611 ABORT("cannot coerce %s to %s", expr->fType->description().c_str(), | 601 ABORT("cannot coerce %s to %s", expr->fType.description().c_str(), |
| 612 type->description().c_str()); | 602 type.description().c_str()); |
| 613 } | 603 } |
| 614 | 604 |
| 615 /** | 605 /** |
| 616 * Determines the operand and result types of a binary expression. Returns true if the expression is | 606 * Determines the operand and result types of a binary expression. Returns true if the expression is |
| 617 * legal, false otherwise. If false, the values of the out parameters are undefi ned. | 607 * legal, false otherwise. If false, the values of the out parameters are undefi ned. |
| 618 */ | 608 */ |
| 619 static bool determine_binary_type(Token::Kind op, std::shared_ptr<Type> left, | 609 static bool determine_binary_type(Token::Kind op, const Type& left, const Type& right, |
| 620 std::shared_ptr<Type> right, | 610 const Type** outLeftType, |
| 621 std::shared_ptr<Type>* outLeftType, | 611 const Type** outRightType, |
| 622 std::shared_ptr<Type>* outRightType, | 612 const Type** outResultType, |
| 623 std::shared_ptr<Type>* outResultType, | |
| 624 bool tryFlipped) { | 613 bool tryFlipped) { |
| 625 bool isLogical; | 614 bool isLogical; |
| 626 switch (op) { | 615 switch (op) { |
| 627 case Token::EQEQ: // fall through | 616 case Token::EQEQ: // fall through |
| 628 case Token::NEQ: // fall through | 617 case Token::NEQ: // fall through |
| 629 case Token::LT: // fall through | 618 case Token::LT: // fall through |
| 630 case Token::GT: // fall through | 619 case Token::GT: // fall through |
| 631 case Token::LTEQ: // fall through | 620 case Token::LTEQ: // fall through |
| 632 case Token::GTEQ: | 621 case Token::GTEQ: |
| 633 isLogical = true; | 622 isLogical = true; |
| 634 break; | 623 break; |
| 635 case Token::LOGICALOR: // fall through | 624 case Token::LOGICALOR: // fall through |
| 636 case Token::LOGICALAND: // fall through | 625 case Token::LOGICALAND: // fall through |
| 637 case Token::LOGICALXOR: // fall through | 626 case Token::LOGICALXOR: // fall through |
| 638 case Token::LOGICALOREQ: // fall through | 627 case Token::LOGICALOREQ: // fall through |
| 639 case Token::LOGICALANDEQ: // fall through | 628 case Token::LOGICALANDEQ: // fall through |
| 640 case Token::LOGICALXOREQ: | 629 case Token::LOGICALXOREQ: |
| 641 *outLeftType = kBool_Type; | 630 *outLeftType = &kBool_Type; |
| 642 *outRightType = kBool_Type; | 631 *outRightType = &kBool_Type; |
| 643 *outResultType = kBool_Type; | 632 *outResultType = &kBool_Type; |
| 644 return left->canCoerceTo(kBool_Type) && right->canCoerceTo(kBool_Typ e); | 633 return left.canCoerceTo(kBool_Type) && right.canCoerceTo(kBool_Type) ; |
| 645 case Token::STAR: // fall through | 634 case Token::STAR: // fall through |
| 646 case Token::STAREQ: | 635 case Token::STAREQ: |
| 647 // FIXME need to handle non-square matrices | 636 // FIXME need to handle non-square matrices |
| 648 if (left->kind() == Type::kMatrix_Kind && right->kind() == Type::kVe ctor_Kind) { | 637 if (left.kind() == Type::kMatrix_Kind && right.kind() == Type::kVect or_Kind) { |
| 649 *outLeftType = left; | 638 *outLeftType = &left; |
| 650 *outRightType = right; | 639 *outRightType = &right; |
| 651 *outResultType = right; | 640 *outResultType = &right; |
| 652 return left->rows() == right->columns(); | 641 return left.rows() == right.columns(); |
| 653 } | 642 } |
| 654 if (left->kind() == Type::kVector_Kind && right->kind() == Type::kMa trix_Kind) { | 643 if (left.kind() == Type::kVector_Kind && right.kind() == Type::kMatr ix_Kind) { |
| 655 *outLeftType = left; | 644 *outLeftType = &left; |
| 656 *outRightType = right; | 645 *outRightType = &right; |
| 657 *outResultType = left; | 646 *outResultType = &left; |
| 658 return left->columns() == right->columns(); | 647 return left.columns() == right.columns(); |
| 659 } | 648 } |
| 660 // fall through | 649 // fall through |
| 661 default: | 650 default: |
| 662 isLogical = false; | 651 isLogical = false; |
| 663 } | 652 } |
| 664 // FIXME: need to disallow illegal operations like vec3 > vec3. Also do not currently have | 653 // FIXME: need to disallow illegal operations like vec3 > vec3. Also do not currently have |
| 665 // full support for numbers other than float. | 654 // full support for numbers other than float. |
| 666 if (left == right) { | 655 if (left == right) { |
| 667 *outLeftType = left; | 656 *outLeftType = &left; |
| 668 *outRightType = left; | 657 *outRightType = &left; |
| 669 if (isLogical) { | 658 if (isLogical) { |
| 670 *outResultType = kBool_Type; | 659 *outResultType = &kBool_Type; |
| 671 } else { | 660 } else { |
| 672 *outResultType = left; | 661 *outResultType = &left; |
| 673 } | 662 } |
| 674 return true; | 663 return true; |
| 675 } | 664 } |
| 676 // FIXME: incorrect for shift operations | 665 // FIXME: incorrect for shift operations |
| 677 if (left->canCoerceTo(right)) { | 666 if (left.canCoerceTo(right)) { |
| 678 *outLeftType = right; | 667 *outLeftType = &right; |
| 679 *outRightType = right; | 668 *outRightType = &right; |
| 680 if (isLogical) { | 669 if (isLogical) { |
| 681 *outResultType = kBool_Type; | 670 *outResultType = &kBool_Type; |
| 682 } else { | 671 } else { |
| 683 *outResultType = right; | 672 *outResultType = &right; |
| 684 } | 673 } |
| 685 return true; | 674 return true; |
| 686 } | 675 } |
| 687 if ((left->kind() == Type::kVector_Kind || left->kind() == Type::kMatrix_Kin d) && | 676 if ((left.kind() == Type::kVector_Kind || left.kind() == Type::kMatrix_Kind) && |
| 688 (right->kind() == Type::kScalar_Kind)) { | 677 (right.kind() == Type::kScalar_Kind)) { |
| 689 if (determine_binary_type(op, left->componentType(), right, outLeftType, outRightType, | 678 if (determine_binary_type(op, left.componentType(), right, outLeftType, outRightType, |
| 690 outResultType, false)) { | 679 outResultType, false)) { |
| 691 *outLeftType = (*outLeftType)->toCompound(left->columns(), left->row s()); | 680 *outLeftType = &(*outLeftType)->toCompound(left.columns(), left.rows ()); |
| 692 if (!isLogical) { | 681 if (!isLogical) { |
| 693 *outResultType = (*outResultType)->toCompound(left->columns(), l eft->rows()); | 682 *outResultType = &(*outResultType)->toCompound(left.columns(), l eft.rows()); |
| 694 } | 683 } |
| 695 return true; | 684 return true; |
| 696 } | 685 } |
| 697 return false; | 686 return false; |
| 698 } | 687 } |
| 699 if (tryFlipped) { | 688 if (tryFlipped) { |
| 700 return determine_binary_type(op, right, left, outRightType, outLeftType, outResultType, | 689 return determine_binary_type(op, right, left, outRightType, outLeftType, outResultType, |
| 701 false); | 690 false); |
| 702 } | 691 } |
| 703 return false; | 692 return false; |
| 704 } | 693 } |
| 705 | 694 |
| 706 std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( | 695 std::unique_ptr<Expression> IRGenerator::convertBinaryExpression( |
| 707 const ASTBinaryExpre ssion& expression) { | 696 const ASTBinaryExpre ssion& expression) { |
| 708 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft ); | 697 std::unique_ptr<Expression> left = this->convertExpression(*expression.fLeft ); |
| 709 if (!left) { | 698 if (!left) { |
| 710 return nullptr; | 699 return nullptr; |
| 711 } | 700 } |
| 712 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRig ht); | 701 std::unique_ptr<Expression> right = this->convertExpression(*expression.fRig ht); |
| 713 if (!right) { | 702 if (!right) { |
| 714 return nullptr; | 703 return nullptr; |
| 715 } | 704 } |
| 716 std::shared_ptr<Type> leftType; | 705 const Type* leftType; |
| 717 std::shared_ptr<Type> rightType; | 706 const Type* rightType; |
| 718 std::shared_ptr<Type> resultType; | 707 const Type* resultType; |
| 719 if (!determine_binary_type(expression.fOperator, left->fType, right->fType, &leftType, | 708 if (!determine_binary_type(expression.fOperator, left->fType, right->fType, &leftType, |
| 720 &rightType, &resultType, true)) { | 709 &rightType, &resultType, true)) { |
| 721 fErrors.error(expression.fPosition, "type mismatch: '" + | 710 fErrors.error(expression.fPosition, "type mismatch: '" + |
| 722 Token::OperatorName(expression.fOper ator) + | 711 Token::OperatorName(expression.fOper ator) + |
| 723 "' cannot operate on '" + left->fTyp e->fName + | 712 "' cannot operate on '" + left->fTyp e.fName + |
| 724 "', '" + right->fType->fName + "'"); | 713 "', '" + right->fType.fName + "'"); |
| 725 return nullptr; | 714 return nullptr; |
| 726 } | 715 } |
| 727 switch (expression.fOperator) { | 716 switch (expression.fOperator) { |
| 728 case Token::EQ: // fall through | 717 case Token::EQ: // fall through |
| 729 case Token::PLUSEQ: // fall through | 718 case Token::PLUSEQ: // fall through |
| 730 case Token::MINUSEQ: // fall through | 719 case Token::MINUSEQ: // fall through |
| 731 case Token::STAREQ: // fall through | 720 case Token::STAREQ: // fall through |
| 732 case Token::SLASHEQ: // fall through | 721 case Token::SLASHEQ: // fall through |
| 733 case Token::PERCENTEQ: // fall through | 722 case Token::PERCENTEQ: // fall through |
| 734 case Token::SHLEQ: // fall through | 723 case Token::SHLEQ: // fall through |
| 735 case Token::SHREQ: // fall through | 724 case Token::SHREQ: // fall through |
| 736 case Token::BITWISEOREQ: // fall through | 725 case Token::BITWISEOREQ: // fall through |
| 737 case Token::BITWISEXOREQ: // fall through | 726 case Token::BITWISEXOREQ: // fall through |
| 738 case Token::BITWISEANDEQ: // fall through | 727 case Token::BITWISEANDEQ: // fall through |
| 739 case Token::LOGICALOREQ: // fall through | 728 case Token::LOGICALOREQ: // fall through |
| 740 case Token::LOGICALXOREQ: // fall through | 729 case Token::LOGICALXOREQ: // fall through |
| 741 case Token::LOGICALANDEQ: | 730 case Token::LOGICALANDEQ: |
| 742 this->markWrittenTo(*left); | 731 this->markWrittenTo(*left); |
| 743 default: | 732 default: |
| 744 break; | 733 break; |
| 745 } | 734 } |
| 746 return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition , | 735 return std::unique_ptr<Expression>(new BinaryExpression(expression.fPosition , |
| 747 this->coerce(std::mo ve(left), leftType), | 736 this->coerce(std::mo ve(left), |
| 737 *leftTy pe), | |
| 748 expression.fOperator , | 738 expression.fOperator , |
| 749 this->coerce(std::mo ve(right), | 739 this->coerce(std::mo ve(right), |
| 750 rightTy pe), | 740 *rightT ype), |
| 751 resultType)); | 741 *resultType)); |
| 752 } | 742 } |
| 753 | 743 |
| 754 std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( | 744 std::unique_ptr<Expression> IRGenerator::convertTernaryExpression( |
| 755 const ASTTernaryExpre ssion& expression) { | 745 const ASTTernaryExpre ssion& expression) { |
| 756 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*exp ression.fTest), | 746 std::unique_ptr<Expression> test = this->coerce(this->convertExpression(*exp ression.fTest), |
| 757 kBool_Type); | 747 kBool_Type); |
| 758 if (!test) { | 748 if (!test) { |
| 759 return nullptr; | 749 return nullptr; |
| 760 } | 750 } |
| 761 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIf True); | 751 std::unique_ptr<Expression> ifTrue = this->convertExpression(*expression.fIf True); |
| 762 if (!ifTrue) { | 752 if (!ifTrue) { |
| 763 return nullptr; | 753 return nullptr; |
| 764 } | 754 } |
| 765 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fI fFalse); | 755 std::unique_ptr<Expression> ifFalse = this->convertExpression(*expression.fI fFalse); |
| 766 if (!ifFalse) { | 756 if (!ifFalse) { |
| 767 return nullptr; | 757 return nullptr; |
| 768 } | 758 } |
| 769 std::shared_ptr<Type> trueType; | 759 const Type* trueType; |
| 770 std::shared_ptr<Type> falseType; | 760 const Type* falseType; |
| 771 std::shared_ptr<Type> resultType; | 761 const Type* resultType; |
| 772 if (!determine_binary_type(Token::EQEQ, ifTrue->fType, ifFalse->fType, &true Type, | 762 if (!determine_binary_type(Token::EQEQ, ifTrue->fType, ifFalse->fType, &true Type, |
| 773 &falseType, &resultType, true)) { | 763 &falseType, &resultType, true)) { |
| 774 fErrors.error(expression.fPosition, "ternary operator result mismatch: ' " + | 764 fErrors.error(expression.fPosition, "ternary operator result mismatch: ' " + |
| 775 ifTrue->fType->fName + "', '" + | 765 ifTrue->fType.fName + "', '" + |
| 776 ifFalse->fType->fName + "'"); | 766 ifFalse->fType.fName + "'"); |
| 777 return nullptr; | 767 return nullptr; |
| 778 } | 768 } |
| 779 ASSERT(trueType == falseType); | 769 ASSERT(trueType == falseType); |
| 780 ifTrue = this->coerce(std::move(ifTrue), trueType); | 770 ifTrue = this->coerce(std::move(ifTrue), *trueType); |
| 781 ifFalse = this->coerce(std::move(ifFalse), falseType); | 771 ifFalse = this->coerce(std::move(ifFalse), *falseType); |
| 782 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio n, | 772 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio n, |
| 783 std::move(test), | 773 std::move(test), |
| 784 std::move(ifTrue), | 774 std::move(ifTrue), |
| 785 std::move(ifFalse)) ); | 775 std::move(ifFalse)) ); |
| 786 } | 776 } |
| 787 | 777 |
| 788 std::unique_ptr<Expression> IRGenerator::call( | 778 std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 789 Position position, | 779 const FunctionDeclaration& functio n, |
| 790 std::shared_ptr<FunctionDeclaration> fu nction, | 780 std::vector<std::unique_ptr<Expres sion>> arguments) { |
| 791 std::vector<std::unique_ptr<Expression> > arguments) { | 781 if (function.fParameters.size() != arguments.size()) { |
| 792 if (function->fParameters.size() != arguments.size()) { | 782 std::string msg = "call to '" + function.fName + "' expected " + |
| 793 std::string msg = "call to '" + function->fName + "' expected " + | 783 to_string(function.fParameters.size()) + |
| 794 to_string(function->fParameters.size()) + | |
| 795 " argument"; | 784 " argument"; |
| 796 if (function->fParameters.size() != 1) { | 785 if (function.fParameters.size() != 1) { |
| 797 msg += "s"; | 786 msg += "s"; |
| 798 } | 787 } |
| 799 msg += ", but found " + to_string(arguments.size()); | 788 msg += ", but found " + to_string(arguments.size()); |
| 800 fErrors.error(position, msg); | 789 fErrors.error(position, msg); |
| 801 return nullptr; | 790 return nullptr; |
| 802 } | 791 } |
| 803 for (size_t i = 0; i < arguments.size(); i++) { | 792 for (size_t i = 0; i < arguments.size(); i++) { |
| 804 arguments[i] = this->coerce(std::move(arguments[i]), function->fParamete rs[i]->fType); | 793 arguments[i] = this->coerce(std::move(arguments[i]), function.fParameter s[i]->fType); |
| 805 if (arguments[i] && (function->fParameters[i]->fModifiers.fFlags & Modif iers::kOut_Flag)) { | 794 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifi ers::kOut_Flag)) { |
| 806 this->markWrittenTo(*arguments[i]); | 795 this->markWrittenTo(*arguments[i]); |
| 807 } | 796 } |
| 808 } | 797 } |
| 809 return std::unique_ptr<FunctionCall>(new FunctionCall(position, std::move(fu nction), | 798 return std::unique_ptr<FunctionCall>(new FunctionCall(position, function, |
| 810 std::move(arguments))) ; | 799 std::move(arguments))) ; |
| 811 } | 800 } |
| 812 | 801 |
| 813 /** | 802 /** |
| 814 * Determines the cost of coercing the arguments of a function to the required t ypes. Returns true | 803 * Determines the cost of coercing the arguments of a function to the required t ypes. Returns true |
| 815 * if the cost could be computed, false if the call is not valid. Cost has no pa rticular meaning | 804 * if the cost could be computed, false if the call is not valid. Cost has no pa rticular meaning |
| 816 * other than "lower costs are preferred". | 805 * other than "lower costs are preferred". |
| 817 */ | 806 */ |
| 818 bool IRGenerator::determineCallCost(std::shared_ptr<FunctionDeclaration> functio n, | 807 bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
| 819 const std::vector<std::unique_ptr<Expression >>& arguments, | 808 const std::vector<std::unique_ptr<Expression >>& arguments, |
| 820 int* outCost) { | 809 int* outCost) { |
| 821 if (function->fParameters.size() != arguments.size()) { | 810 if (function.fParameters.size() != arguments.size()) { |
| 822 return false; | 811 return false; |
| 823 } | 812 } |
| 824 int total = 0; | 813 int total = 0; |
| 825 for (size_t i = 0; i < arguments.size(); i++) { | 814 for (size_t i = 0; i < arguments.size(); i++) { |
| 826 int cost; | 815 int cost; |
| 827 if (arguments[i]->fType->determineCoercionCost(function->fParameters[i]- >fType, &cost)) { | 816 if (arguments[i]->fType.determineCoercionCost(function.fParameters[i]->f Type, &cost)) { |
| 828 total += cost; | 817 total += cost; |
| 829 } else { | 818 } else { |
| 830 return false; | 819 return false; |
| 831 } | 820 } |
| 832 } | 821 } |
| 833 *outCost = total; | 822 *outCost = total; |
| 834 return true; | 823 return true; |
| 835 } | 824 } |
| 836 | 825 |
| 837 std::unique_ptr<Expression> IRGenerator::call(Position position, | 826 std::unique_ptr<Expression> IRGenerator::call(Position position, |
| 838 std::unique_ptr<Expression> functi onValue, | 827 std::unique_ptr<Expression> functi onValue, |
| 839 std::vector<std::unique_ptr<Expres sion>> arguments) { | 828 std::vector<std::unique_ptr<Expres sion>> arguments) { |
| 840 if (functionValue->fKind == Expression::kTypeReference_Kind) { | 829 if (functionValue->fKind == Expression::kTypeReference_Kind) { |
| 841 return this->convertConstructor(position, | 830 return this->convertConstructor(position, |
| 842 ((TypeReference&) *functionValue).fValue , | 831 ((TypeReference&) *functionValue).fValue , |
| 843 std::move(arguments)); | 832 std::move(arguments)); |
| 844 } | 833 } |
| 845 if (functionValue->fKind != Expression::kFunctionReference_Kind) { | 834 if (functionValue->fKind != Expression::kFunctionReference_Kind) { |
| 846 fErrors.error(position, "'" + functionValue->description() + "' is not a function"); | 835 fErrors.error(position, "'" + functionValue->description() + "' is not a function"); |
| 847 return nullptr; | 836 return nullptr; |
| 848 } | 837 } |
| 849 FunctionReference* ref = (FunctionReference*) functionValue.get(); | 838 FunctionReference* ref = (FunctionReference*) functionValue.get(); |
| 850 int bestCost = INT_MAX; | 839 int bestCost = INT_MAX; |
| 851 std::shared_ptr<FunctionDeclaration> best; | 840 const FunctionDeclaration* best = nullptr; |
| 852 if (ref->fFunctions.size() > 1) { | 841 if (ref->fFunctions.size() > 1) { |
| 853 for (const auto& f : ref->fFunctions) { | 842 for (const auto& f : ref->fFunctions) { |
| 854 int cost; | 843 int cost; |
| 855 if (this->determineCallCost(f, arguments, &cost) && cost < bestCost) { | 844 if (this->determineCallCost(*f, arguments, &cost) && cost < bestCost ) { |
| 856 bestCost = cost; | 845 bestCost = cost; |
| 857 best = f; | 846 best = f; |
| 858 } | 847 } |
| 859 } | 848 } |
| 860 if (best) { | 849 if (best) { |
| 861 return this->call(position, std::move(best), std::move(arguments)); | 850 return this->call(position, *best, std::move(arguments)); |
| 862 } | 851 } |
| 863 std::string msg = "no match for " + ref->fFunctions[0]->fName + "("; | 852 std::string msg = "no match for " + ref->fFunctions[0]->fName + "("; |
| 864 std::string separator = ""; | 853 std::string separator = ""; |
| 865 for (size_t i = 0; i < arguments.size(); i++) { | 854 for (size_t i = 0; i < arguments.size(); i++) { |
| 866 msg += separator; | 855 msg += separator; |
| 867 separator = ", "; | 856 separator = ", "; |
| 868 msg += arguments[i]->fType->description(); | 857 msg += arguments[i]->fType.description(); |
| 869 } | 858 } |
| 870 msg += ")"; | 859 msg += ")"; |
| 871 fErrors.error(position, msg); | 860 fErrors.error(position, msg); |
| 872 return nullptr; | 861 return nullptr; |
| 873 } | 862 } |
| 874 return this->call(position, ref->fFunctions[0], std::move(arguments)); | 863 return this->call(position, *ref->fFunctions[0], std::move(arguments)); |
| 875 } | 864 } |
| 876 | 865 |
| 877 std::unique_ptr<Expression> IRGenerator::convertConstructor( | 866 std::unique_ptr<Expression> IRGenerator::convertConstructor( |
| 878 Position position, | 867 Position position, |
| 879 std::shared_ptr<Type> type, | 868 const Type& type, |
| 880 std::vector<std::unique_ptr< Expression>> args) { | 869 std::vector<std::unique_ptr< Expression>> args) { |
| 881 // FIXME: add support for structs and arrays | 870 // FIXME: add support for structs and arrays |
| 882 Type::Kind kind = type->kind(); | 871 Type::Kind kind = type.kind(); |
| 883 if (!type->isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix _Kind) { | 872 if (!type.isNumber() && kind != Type::kVector_Kind && kind != Type::kMatrix_ Kind) { |
| 884 fErrors.error(position, "cannot construct '" + type->description() + "'" ); | 873 fErrors.error(position, "cannot construct '" + type.description() + "'") ; |
| 885 return nullptr; | 874 return nullptr; |
| 886 } | 875 } |
| 887 if (type == kFloat_Type && args.size() == 1 && | 876 if (type == kFloat_Type && args.size() == 1 && |
| 888 args[0]->fKind == Expression::kIntLiteral_Kind) { | 877 args[0]->fKind == Expression::kIntLiteral_Kind) { |
| 889 int64_t value = ((IntLiteral&) *args[0]).fValue; | 878 int64_t value = ((IntLiteral&) *args[0]).fValue; |
| 890 return std::unique_ptr<Expression>(new FloatLiteral(position, (double) v alue)); | 879 return std::unique_ptr<Expression>(new FloatLiteral(position, (double) v alue)); |
| 891 } | 880 } |
| 892 if (args.size() == 1 && args[0]->fType == type) { | 881 if (args.size() == 1 && args[0]->fType == type) { |
| 893 // argument is already the right type, just return it | 882 // argument is already the right type, just return it |
| 894 return std::move(args[0]); | 883 return std::move(args[0]); |
| 895 } | 884 } |
| 896 if (type->isNumber()) { | 885 if (type.isNumber()) { |
| 897 if (args.size() != 1) { | 886 if (args.size() != 1) { |
| 898 fErrors.error(position, "invalid arguments to '" + type->description () + | 887 fErrors.error(position, "invalid arguments to '" + type.description( ) + |
| 899 "' constructor, (expected exactly 1 argument , but found " + | 888 "' constructor, (expected exactly 1 argument , but found " + |
| 900 to_string(args.size()) + ")"); | 889 to_string(args.size()) + ")"); |
| 901 } | 890 } |
| 902 if (args[0]->fType == kBool_Type) { | 891 if (args[0]->fType == kBool_Type) { |
| 903 std::unique_ptr<IntLiteral> zero(new IntLiteral(position, 0)); | 892 std::unique_ptr<IntLiteral> zero(new IntLiteral(position, 0)); |
| 904 std::unique_ptr<IntLiteral> one(new IntLiteral(position, 1)); | 893 std::unique_ptr<IntLiteral> one(new IntLiteral(position, 1)); |
| 905 return std::unique_ptr<Expression>( | 894 return std::unique_ptr<Expression>( |
| 906 new TernaryExpression(position, std::mo ve(args[0]), | 895 new TernaryExpression(position, std::mo ve(args[0]), |
| 907 this->coerce(std: :move(one), type), | 896 this->coerce(std: :move(one), type), |
| 908 this->coerce(std: :move(zero), | 897 this->coerce(std: :move(zero), |
| 909 type ))); | 898 type ))); |
| 910 } else if (!args[0]->fType->isNumber()) { | 899 } else if (!args[0]->fType.isNumber()) { |
| 911 fErrors.error(position, "invalid argument to '" + type->description( ) + | 900 fErrors.error(position, "invalid argument to '" + type.description() + |
| 912 "' constructor (expected a number or bool, b ut found '" + | 901 "' constructor (expected a number or bool, b ut found '" + |
| 913 args[0]->fType->description() + "')"); | 902 args[0]->fType.description() + "')"); |
| 914 } | 903 } |
| 915 } else { | 904 } else { |
| 916 ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); | 905 ASSERT(kind == Type::kVector_Kind || kind == Type::kMatrix_Kind); |
| 917 int actual = 0; | 906 int actual = 0; |
| 918 for (size_t i = 0; i < args.size(); i++) { | 907 for (size_t i = 0; i < args.size(); i++) { |
| 919 if (args[i]->fType->kind() == Type::kVector_Kind || | 908 if (args[i]->fType.kind() == Type::kVector_Kind || |
| 920 args[i]->fType->kind() == Type::kMatrix_Kind) { | 909 args[i]->fType.kind() == Type::kMatrix_Kind) { |
| 921 int columns = args[i]->fType->columns(); | 910 int columns = args[i]->fType.columns(); |
| 922 int rows = args[i]->fType->rows(); | 911 int rows = args[i]->fType.rows(); |
| 923 args[i] = this->coerce(std::move(args[i]), | 912 args[i] = this->coerce(std::move(args[i]), |
| 924 type->componentType()->toCompound(columns , rows)); | 913 type.componentType().toCompound(columns, rows)); |
| 925 actual += args[i]->fType->rows() * args[i]->fType->columns(); | 914 actual += args[i]->fType.rows() * args[i]->fType.columns(); |
| 926 } else if (args[i]->fType->kind() == Type::kScalar_Kind) { | 915 } else if (args[i]->fType.kind() == Type::kScalar_Kind) { |
| 927 actual += 1; | 916 actual += 1; |
| 928 if (type->kind() != Type::kScalar_Kind) { | 917 if (type.kind() != Type::kScalar_Kind) { |
| 929 args[i] = this->coerce(std::move(args[i]), type->componentTy pe()); | 918 args[i] = this->coerce(std::move(args[i]), type.componentTyp e()); |
| 930 } | 919 } |
| 931 } else { | 920 } else { |
| 932 fErrors.error(position, "'" + args[i]->fType->description() + "' is not a valid " | 921 fErrors.error(position, "'" + args[i]->fType.description() + "' is not a valid " |
| 933 "parameter to '" + type->description() + "' constructor"); | 922 "parameter to '" + type.description() + "' constructor"); |
| 934 return nullptr; | 923 return nullptr; |
| 935 } | 924 } |
| 936 } | 925 } |
| 937 int min = type->rows() * type->columns(); | 926 int min = type.rows() * type.columns(); |
| 938 int max = type->columns() > 1 ? INT_MAX : min; | 927 int max = type.columns() > 1 ? INT_MAX : min; |
| 939 if ((actual < min || actual > max) && | 928 if ((actual < min || actual > max) && |
| 940 !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (act ual == 1))) { | 929 !((kind == Type::kVector_Kind || kind == Type::kMatrix_Kind) && (act ual == 1))) { |
| 941 fErrors.error(position, "invalid arguments to '" + type->description () + | 930 fErrors.error(position, "invalid arguments to '" + type.description( ) + |
| 942 "' constructor (expected " + to_string(min) + " scalar" + | 931 "' constructor (expected " + to_string(min) + " scalar" + |
| 943 (min == 1 ? "" : "s") + ", but found " + to_ string(actual) + | 932 (min == 1 ? "" : "s") + ", but found " + to_ string(actual) + |
| 944 ")"); | 933 ")"); |
| 945 return nullptr; | 934 return nullptr; |
| 946 } | 935 } |
| 947 } | 936 } |
| 948 return std::unique_ptr<Expression>(new Constructor(position, std::move(type) , std::move(args))); | 937 return std::unique_ptr<Expression>(new Constructor(position, std::move(type) , std::move(args))); |
| 949 } | 938 } |
| 950 | 939 |
| 951 std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( | 940 std::unique_ptr<Expression> IRGenerator::convertPrefixExpression( |
| 952 const ASTPrefixExpre ssion& expression) { | 941 const ASTPrefixExpre ssion& expression) { |
| 953 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOper and); | 942 std::unique_ptr<Expression> base = this->convertExpression(*expression.fOper and); |
| 954 if (!base) { | 943 if (!base) { |
| 955 return nullptr; | 944 return nullptr; |
| 956 } | 945 } |
| 957 switch (expression.fOperator) { | 946 switch (expression.fOperator) { |
| 958 case Token::PLUS: | 947 case Token::PLUS: |
| 959 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) { | 948 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_K ind) { |
| 960 fErrors.error(expression.fPosition, | 949 fErrors.error(expression.fPosition, |
| 961 "'+' cannot operate on '" + base->fType->descripti on() + "'"); | 950 "'+' cannot operate on '" + base->fType.descriptio n() + "'"); |
| 962 return nullptr; | 951 return nullptr; |
| 963 } | 952 } |
| 964 return base; | 953 return base; |
| 965 case Token::MINUS: | 954 case Token::MINUS: |
| 966 if (!base->fType->isNumber() && base->fType->kind() != Type::kVector _Kind) { | 955 if (!base->fType.isNumber() && base->fType.kind() != Type::kVector_K ind) { |
| 967 fErrors.error(expression.fPosition, | 956 fErrors.error(expression.fPosition, |
| 968 "'-' cannot operate on '" + base->fType->descripti on() + "'"); | 957 "'-' cannot operate on '" + base->fType.descriptio n() + "'"); |
| 969 return nullptr; | 958 return nullptr; |
| 970 } | 959 } |
| 971 if (base->fKind == Expression::kIntLiteral_Kind) { | 960 if (base->fKind == Expression::kIntLiteral_Kind) { |
| 972 return std::unique_ptr<Expression>(new IntLiteral(base->fPositio n, | 961 return std::unique_ptr<Expression>(new IntLiteral(base->fPositio n, |
| 973 -((IntLiteral& ) *base).fValue)); | 962 -((IntLiteral& ) *base).fValue)); |
| 974 } | 963 } |
| 975 if (base->fKind == Expression::kFloatLiteral_Kind) { | 964 if (base->fKind == Expression::kFloatLiteral_Kind) { |
| 976 double value = -((FloatLiteral&) *base).fValue; | 965 double value = -((FloatLiteral&) *base).fValue; |
| 977 return std::unique_ptr<Expression>(new FloatLiteral(base->fPosit ion, value)); | 966 return std::unique_ptr<Expression>(new FloatLiteral(base->fPosit ion, value)); |
| 978 } | 967 } |
| 979 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS , std::move(base))); | 968 return std::unique_ptr<Expression>(new PrefixExpression(Token::MINUS , std::move(base))); |
| 980 case Token::PLUSPLUS: | 969 case Token::PLUSPLUS: |
| 981 if (!base->fType->isNumber()) { | 970 if (!base->fType.isNumber()) { |
| 982 fErrors.error(expression.fPosition, | 971 fErrors.error(expression.fPosition, |
| 983 "'" + Token::OperatorName(expression.fOperator) + | 972 "'" + Token::OperatorName(expression.fOperator) + |
| 984 "' cannot operate on '" + base->fType->description () + "'"); | 973 "' cannot operate on '" + base->fType.description( ) + "'"); |
| 985 return nullptr; | 974 return nullptr; |
| 986 } | 975 } |
| 987 this->markWrittenTo(*base); | 976 this->markWrittenTo(*base); |
| 988 break; | 977 break; |
| 989 case Token::MINUSMINUS: | 978 case Token::MINUSMINUS: |
| 990 if (!base->fType->isNumber()) { | 979 if (!base->fType.isNumber()) { |
| 991 fErrors.error(expression.fPosition, | 980 fErrors.error(expression.fPosition, |
| 992 "'" + Token::OperatorName(expression.fOperator) + | 981 "'" + Token::OperatorName(expression.fOperator) + |
| 993 "' cannot operate on '" + base->fType->description () + "'"); | 982 "' cannot operate on '" + base->fType.description( ) + "'"); |
| 994 return nullptr; | 983 return nullptr; |
| 995 } | 984 } |
| 996 this->markWrittenTo(*base); | 985 this->markWrittenTo(*base); |
| 997 break; | 986 break; |
| 998 case Token::NOT: | 987 case Token::NOT: |
| 999 if (base->fType != kBool_Type) { | 988 if (base->fType != kBool_Type) { |
| 1000 fErrors.error(expression.fPosition, | 989 fErrors.error(expression.fPosition, |
| 1001 "'" + Token::OperatorName(expression.fOperator) + | 990 "'" + Token::OperatorName(expression.fOperator) + |
| 1002 "' cannot operate on '" + base->fType->description () + "'"); | 991 "' cannot operate on '" + base->fType.description( ) + "'"); |
| 1003 return nullptr; | 992 return nullptr; |
| 1004 } | 993 } |
| 1005 break; | 994 break; |
| 1006 default: | 995 default: |
| 1007 ABORT("unsupported prefix operator\n"); | 996 ABORT("unsupported prefix operator\n"); |
| 1008 } | 997 } |
| 1009 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator , | 998 return std::unique_ptr<Expression>(new PrefixExpression(expression.fOperator , |
| 1010 std::move(base))); | 999 std::move(base))); |
| 1011 } | 1000 } |
| 1012 | 1001 |
| 1013 std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression > base, | 1002 std::unique_ptr<Expression> IRGenerator::convertIndex(std::unique_ptr<Expression > base, |
| 1014 const ASTExpression& index ) { | 1003 const ASTExpression& index ) { |
| 1015 if (base->fType->kind() != Type::kArray_Kind && base->fType->kind() != Type: :kMatrix_Kind) { | 1004 if (base->fType.kind() != Type::kArray_Kind && base->fType.kind() != Type::k Matrix_Kind) { |
| 1016 fErrors.error(base->fPosition, "expected array, but found '" + base->fTy pe->description() + | 1005 fErrors.error(base->fPosition, "expected array, but found '" + base->fTy pe.description() + |
| 1017 "'"); | 1006 "'"); |
| 1018 return nullptr; | 1007 return nullptr; |
| 1019 } | 1008 } |
| 1020 std::unique_ptr<Expression> converted = this->convertExpression(index); | 1009 std::unique_ptr<Expression> converted = this->convertExpression(index); |
| 1021 if (!converted) { | 1010 if (!converted) { |
| 1022 return nullptr; | 1011 return nullptr; |
| 1023 } | 1012 } |
| 1024 converted = this->coerce(std::move(converted), kInt_Type); | 1013 converted = this->coerce(std::move(converted), kInt_Type); |
| 1025 if (!converted) { | 1014 if (!converted) { |
| 1026 return nullptr; | 1015 return nullptr; |
| 1027 } | 1016 } |
| 1028 return std::unique_ptr<Expression>(new IndexExpression(std::move(base), std: :move(converted))); | 1017 return std::unique_ptr<Expression>(new IndexExpression(std::move(base), std: :move(converted))); |
| 1029 } | 1018 } |
| 1030 | 1019 |
| 1031 std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression > base, | 1020 std::unique_ptr<Expression> IRGenerator::convertField(std::unique_ptr<Expression > base, |
| 1032 const std::string& field) { | 1021 const std::string& field) { |
| 1033 auto fields = base->fType->fields(); | 1022 auto fields = base->fType.fields(); |
| 1034 for (size_t i = 0; i < fields.size(); i++) { | 1023 for (size_t i = 0; i < fields.size(); i++) { |
| 1035 if (fields[i].fName == field) { | 1024 if (fields[i].fName == field) { |
| 1036 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); | 1025 return std::unique_ptr<Expression>(new FieldAccess(std::move(base), (int) i)); |
| 1037 } | 1026 } |
| 1038 } | 1027 } |
| 1039 fErrors.error(base->fPosition, "type '" + base->fType->description() + "' do es not have a " | 1028 fErrors.error(base->fPosition, "type '" + base->fType.description() + "' doe s not have a " |
| 1040 "field named '" + field + ""); | 1029 "field named '" + field + ""); |
| 1041 return nullptr; | 1030 return nullptr; |
| 1042 } | 1031 } |
| 1043 | 1032 |
| 1044 std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expressi on> base, | 1033 std::unique_ptr<Expression> IRGenerator::convertSwizzle(std::unique_ptr<Expressi on> base, |
| 1045 const std::string& field s) { | 1034 const std::string& field s) { |
| 1046 if (base->fType->kind() != Type::kVector_Kind) { | 1035 if (base->fType.kind() != Type::kVector_Kind) { |
| 1047 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType->de scription() + "'"); | 1036 fErrors.error(base->fPosition, "cannot swizzle type '" + base->fType.des cription() + "'"); |
| 1048 return nullptr; | 1037 return nullptr; |
| 1049 } | 1038 } |
| 1050 std::vector<int> swizzleComponents; | 1039 std::vector<int> swizzleComponents; |
| 1051 for (char c : fields) { | 1040 for (char c : fields) { |
| 1052 switch (c) { | 1041 switch (c) { |
| 1053 case 'x': // fall through | 1042 case 'x': // fall through |
| 1054 case 'r': // fall through | 1043 case 'r': // fall through |
| 1055 case 's': | 1044 case 's': |
| 1056 swizzleComponents.push_back(0); | 1045 swizzleComponents.push_back(0); |
| 1057 break; | 1046 break; |
| 1058 case 'y': // fall through | 1047 case 'y': // fall through |
| 1059 case 'g': // fall through | 1048 case 'g': // fall through |
| 1060 case 't': | 1049 case 't': |
| 1061 if (base->fType->columns() >= 2) { | 1050 if (base->fType.columns() >= 2) { |
| 1062 swizzleComponents.push_back(1); | 1051 swizzleComponents.push_back(1); |
| 1063 break; | 1052 break; |
| 1064 } | 1053 } |
| 1065 // fall through | 1054 // fall through |
| 1066 case 'z': // fall through | 1055 case 'z': // fall through |
| 1067 case 'b': // fall through | 1056 case 'b': // fall through |
| 1068 case 'p': | 1057 case 'p': |
| 1069 if (base->fType->columns() >= 3) { | 1058 if (base->fType.columns() >= 3) { |
| 1070 swizzleComponents.push_back(2); | 1059 swizzleComponents.push_back(2); |
| 1071 break; | 1060 break; |
| 1072 } | 1061 } |
| 1073 // fall through | 1062 // fall through |
| 1074 case 'w': // fall through | 1063 case 'w': // fall through |
| 1075 case 'a': // fall through | 1064 case 'a': // fall through |
| 1076 case 'q': | 1065 case 'q': |
| 1077 if (base->fType->columns() >= 4) { | 1066 if (base->fType.columns() >= 4) { |
| 1078 swizzleComponents.push_back(3); | 1067 swizzleComponents.push_back(3); |
| 1079 break; | 1068 break; |
| 1080 } | 1069 } |
| 1081 // fall through | 1070 // fall through |
| 1082 default: | 1071 default: |
| 1083 fErrors.error(base->fPosition, "invalid swizzle component '" + s td::string(1, c) + | 1072 fErrors.error(base->fPosition, "invalid swizzle component '" + s td::string(1, c) + |
| 1084 "'"); | 1073 "'"); |
| 1085 return nullptr; | 1074 return nullptr; |
| 1086 } | 1075 } |
| 1087 } | 1076 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 1110 std::unique_ptr<Expression> converted = | 1099 std::unique_ptr<Expression> converted = |
| 1111 this->convertExpression(*(*rawArguments)[i]); | 1100 this->convertExpression(*(*rawArguments)[i]); |
| 1112 if (!converted) { | 1101 if (!converted) { |
| 1113 return nullptr; | 1102 return nullptr; |
| 1114 } | 1103 } |
| 1115 arguments.push_back(std::move(converted)); | 1104 arguments.push_back(std::move(converted)); |
| 1116 } | 1105 } |
| 1117 return this->call(expression.fPosition, std::move(base), std::move(a rguments)); | 1106 return this->call(expression.fPosition, std::move(base), std::move(a rguments)); |
| 1118 } | 1107 } |
| 1119 case ASTSuffix::kField_Kind: { | 1108 case ASTSuffix::kField_Kind: { |
| 1120 switch (base->fType->kind()) { | 1109 switch (base->fType.kind()) { |
| 1121 case Type::kVector_Kind: | 1110 case Type::kVector_Kind: |
| 1122 return this->convertSwizzle(std::move(base), | 1111 return this->convertSwizzle(std::move(base), |
| 1123 ((ASTFieldSuffix&) *expression.f Suffix).fField); | 1112 ((ASTFieldSuffix&) *expression.f Suffix).fField); |
| 1124 case Type::kStruct_Kind: | 1113 case Type::kStruct_Kind: |
| 1125 return this->convertField(std::move(base), | 1114 return this->convertField(std::move(base), |
| 1126 ((ASTFieldSuffix&) *expression.fSu ffix).fField); | 1115 ((ASTFieldSuffix&) *expression.fSu ffix).fField); |
| 1127 default: | 1116 default: |
| 1128 fErrors.error(base->fPosition, "cannot swizzle value of type '" + | 1117 fErrors.error(base->fPosition, "cannot swizzle value of type '" + |
| 1129 base->fType->description() + "'"); | 1118 base->fType.description() + " '"); |
| 1130 return nullptr; | 1119 return nullptr; |
| 1131 } | 1120 } |
| 1132 } | 1121 } |
| 1133 case ASTSuffix::kPostIncrement_Kind: | 1122 case ASTSuffix::kPostIncrement_Kind: |
| 1134 if (!base->fType->isNumber()) { | 1123 if (!base->fType.isNumber()) { |
| 1135 fErrors.error(expression.fPosition, | 1124 fErrors.error(expression.fPosition, |
| 1136 "'++' cannot operate on '" + base->fType->descript ion() + "'"); | 1125 "'++' cannot operate on '" + base->fType.descripti on() + "'"); |
| 1137 return nullptr; | 1126 return nullptr; |
| 1138 } | 1127 } |
| 1139 this->markWrittenTo(*base); | 1128 this->markWrittenTo(*base); |
| 1140 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), | 1129 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), |
| 1141 Token::PLUS PLUS)); | 1130 Token::PLUS PLUS)); |
| 1142 case ASTSuffix::kPostDecrement_Kind: | 1131 case ASTSuffix::kPostDecrement_Kind: |
| 1143 if (!base->fType->isNumber()) { | 1132 if (!base->fType.isNumber()) { |
| 1144 fErrors.error(expression.fPosition, | 1133 fErrors.error(expression.fPosition, |
| 1145 "'--' cannot operate on '" + base->fType->descript ion() + "'"); | 1134 "'--' cannot operate on '" + base->fType.descripti on() + "'"); |
| 1146 return nullptr; | 1135 return nullptr; |
| 1147 } | 1136 } |
| 1148 this->markWrittenTo(*base); | 1137 this->markWrittenTo(*base); |
| 1149 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), | 1138 return std::unique_ptr<Expression>(new PostfixExpression(std::move(b ase), |
| 1150 Token::MINU SMINUS)); | 1139 Token::MINU SMINUS)); |
| 1151 default: | 1140 default: |
| 1152 ABORT("unsupported suffix operator"); | 1141 ABORT("unsupported suffix operator"); |
| 1153 } | 1142 } |
| 1154 } | 1143 } |
| 1155 | 1144 |
| 1156 void IRGenerator::checkValid(const Expression& expr) { | 1145 void IRGenerator::checkValid(const Expression& expr) { |
| 1157 switch (expr.fKind) { | 1146 switch (expr.fKind) { |
| 1158 case Expression::kFunctionReference_Kind: | 1147 case Expression::kFunctionReference_Kind: |
| 1159 fErrors.error(expr.fPosition, "expected '(' to begin function call") ; | 1148 fErrors.error(expr.fPosition, "expected '(' to begin function call") ; |
| 1160 break; | 1149 break; |
| 1161 case Expression::kTypeReference_Kind: | 1150 case Expression::kTypeReference_Kind: |
| 1162 fErrors.error(expr.fPosition, "expected '(' to begin constructor inv ocation"); | 1151 fErrors.error(expr.fPosition, "expected '(' to begin constructor inv ocation"); |
| 1163 break; | 1152 break; |
| 1164 default: | 1153 default: |
| 1165 ASSERT(expr.fType != kInvalid_Type); | 1154 ASSERT(expr.fType != kInvalid_Type); |
| 1166 break; | 1155 break; |
| 1167 } | 1156 } |
| 1168 } | 1157 } |
| 1169 | 1158 |
| 1170 void IRGenerator::markReadFrom(std::shared_ptr<Variable> var) { | 1159 void IRGenerator::markReadFrom(const Variable& var) { |
| 1171 var->fIsReadFrom = true; | 1160 var.fIsReadFrom = true; |
| 1172 } | 1161 } |
| 1173 | 1162 |
| 1174 static bool has_duplicates(const Swizzle& swizzle) { | 1163 static bool has_duplicates(const Swizzle& swizzle) { |
| 1175 int bits = 0; | 1164 int bits = 0; |
| 1176 for (int idx : swizzle.fComponents) { | 1165 for (int idx : swizzle.fComponents) { |
| 1177 ASSERT(idx >= 0 && idx <= 3); | 1166 ASSERT(idx >= 0 && idx <= 3); |
| 1178 int bit = 1 << idx; | 1167 int bit = 1 << idx; |
| 1179 if (bits & bit) { | 1168 if (bits & bit) { |
| 1180 return true; | 1169 return true; |
| 1181 } | 1170 } |
| 1182 bits |= bit; | 1171 bits |= bit; |
| 1183 } | 1172 } |
| 1184 return false; | 1173 return false; |
| 1185 } | 1174 } |
| 1186 | 1175 |
| 1187 void IRGenerator::markWrittenTo(const Expression& expr) { | 1176 void IRGenerator::markWrittenTo(const Expression& expr) { |
| 1188 switch (expr.fKind) { | 1177 switch (expr.fKind) { |
| 1189 case Expression::kVariableReference_Kind: { | 1178 case Expression::kVariableReference_Kind: { |
| 1190 const Variable& var = *((VariableReference&) expr).fVariable; | 1179 const Variable& var = ((VariableReference&) expr).fVariable; |
| 1191 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUn iform_Flag)) { | 1180 if (var.fModifiers.fFlags & (Modifiers::kConst_Flag | Modifiers::kUn iform_Flag)) { |
| 1192 fErrors.error(expr.fPosition, | 1181 fErrors.error(expr.fPosition, |
| 1193 "cannot modify immutable variable '" + var.fName + "'"); | 1182 "cannot modify immutable variable '" + var.fName + "'"); |
| 1194 } | 1183 } |
| 1195 var.fIsWrittenTo = true; | 1184 var.fIsWrittenTo = true; |
| 1196 break; | 1185 break; |
| 1197 } | 1186 } |
| 1198 case Expression::kFieldAccess_Kind: | 1187 case Expression::kFieldAccess_Kind: |
| 1199 this->markWrittenTo(*((FieldAccess&) expr).fBase); | 1188 this->markWrittenTo(*((FieldAccess&) expr).fBase); |
| 1200 break; | 1189 break; |
| 1201 case Expression::kSwizzle_Kind: | 1190 case Expression::kSwizzle_Kind: |
| 1202 if (has_duplicates((Swizzle&) expr)) { | 1191 if (has_duplicates((Swizzle&) expr)) { |
| 1203 fErrors.error(expr.fPosition, | 1192 fErrors.error(expr.fPosition, |
| 1204 "cannot write to the same swizzle field more than once"); | 1193 "cannot write to the same swizzle field more than once"); |
| 1205 } | 1194 } |
| 1206 this->markWrittenTo(*((Swizzle&) expr).fBase); | 1195 this->markWrittenTo(*((Swizzle&) expr).fBase); |
| 1207 break; | 1196 break; |
| 1208 case Expression::kIndex_Kind: | 1197 case Expression::kIndex_Kind: |
| 1209 this->markWrittenTo(*((IndexExpression&) expr).fBase); | 1198 this->markWrittenTo(*((IndexExpression&) expr).fBase); |
| 1210 break; | 1199 break; |
| 1211 default: | 1200 default: |
| 1212 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'"); | 1201 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio n() + "'"); |
| 1213 break; | 1202 break; |
| 1214 } | 1203 } |
| 1215 } | 1204 } |
| 1216 | 1205 |
| 1217 } | 1206 } |
| OLD | NEW |