From 2ed0d948870714132ca433a0e290567b22e1847d Mon Sep 17 00:00:00 2001 From: Ethan Nicholas Date: Wed, 20 Jan 2021 07:51:23 -0500 Subject: [PATCH] Refactored SkSL convertDo & convertFor No functionality changes, but the new variants of the methods make them more accessible from upcoming DSL code. Change-Id: Iaa9f1fab31cb7db00007b00d7d3b88ff5b9696b6 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/356103 Reviewed-by: Brian Osman Commit-Queue: Ethan Nicholas --- src/sksl/SkSLIRGenerator.cpp | 58 +++++++++++++++++++++++++----------- src/sksl/SkSLIRGenerator.h | 7 +++++ 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/sksl/SkSLIRGenerator.cpp b/src/sksl/SkSLIRGenerator.cpp index af54ac113e..85aee284f2 100644 --- a/src/sksl/SkSLIRGenerator.cpp +++ b/src/sksl/SkSLIRGenerator.cpp @@ -538,6 +538,30 @@ std::unique_ptr IRGenerator::convertIf(int offset, bool isStatic, std::move(ifFalse)); } +std::unique_ptr IRGenerator::convertFor(int offset, + std::unique_ptr initializer, + std::unique_ptr test, + std::unique_ptr next, + std::unique_ptr statement) { + if (test) { + test = this->coerce(std::move(test), *fContext.fTypes.fBool); + if (!test) { + return nullptr; + } + } + + auto forStmt = + std::make_unique(offset, std::move(initializer), std::move(test), + std::move(next), std::move(statement), fSymbolTable); + if (this->strictES2Mode()) { + if (!Analysis::ForLoopIsValidForES2(*forStmt, /*outLoopInfo=*/nullptr, + &this->errorReporter())) { + return nullptr; + } + } + return std::move(forStmt); +} + std::unique_ptr IRGenerator::convertFor(const ASTNode& f) { SkASSERT(f.fKind == ASTNode::Kind::kFor); AutoLoopLevel level(this); @@ -553,7 +577,7 @@ std::unique_ptr IRGenerator::convertFor(const ASTNode& f) { ++iter; std::unique_ptr test; if (*iter) { - test = this->coerce(this->convertExpression(*iter), *fContext.fTypes.fBool); + test = this->convertExpression(*iter); if (!test) { return nullptr; } @@ -572,16 +596,8 @@ std::unique_ptr IRGenerator::convertFor(const ASTNode& f) { return nullptr; } - auto forStmt = - std::make_unique(f.fOffset, std::move(initializer), std::move(test), - std::move(next), std::move(statement), fSymbolTable); - if (this->strictES2Mode()) { - if (!Analysis::ForLoopIsValidForES2(*forStmt, /*outLoopInfo=*/nullptr, - &this->errorReporter())) { - return nullptr; - } - } - return std::move(forStmt); + return this->convertFor(f.fOffset, std::move(initializer), std::move(test), std::move(next), + std::move(statement)); } std::unique_ptr IRGenerator::convertWhile(int offset, std::unique_ptr test, @@ -614,25 +630,33 @@ std::unique_ptr IRGenerator::convertWhile(const ASTNode& w) { return this->convertWhile(w.fOffset, std::move(test), std::move(statement)); } -std::unique_ptr IRGenerator::convertDo(const ASTNode& d) { - SkASSERT(d.fKind == ASTNode::Kind::kDo); +std::unique_ptr IRGenerator::convertDo(std::unique_ptr stmt, + std::unique_ptr test) { if (this->strictES2Mode()) { - this->errorReporter().error(d.fOffset, "do-while loops are not supported"); + this->errorReporter().error(stmt->fOffset, "do-while loops are not supported"); return nullptr; } AutoLoopLevel level(this); + test = this->coerce(std::move(test), *fContext.fTypes.fBool); + if (!test) { + return nullptr; + } + return std::make_unique(stmt->fOffset, std::move(stmt), std::move(test)); +} + +std::unique_ptr IRGenerator::convertDo(const ASTNode& d) { + SkASSERT(d.fKind == ASTNode::Kind::kDo); auto iter = d.begin(); std::unique_ptr statement = this->convertStatement(*(iter++)); if (!statement) { return nullptr; } - std::unique_ptr test = - this->coerce(this->convertExpression(*(iter++)), *fContext.fTypes.fBool); + std::unique_ptr test = this->convertExpression(*(iter++)); if (!test) { return nullptr; } - return std::make_unique(d.fOffset, std::move(statement), std::move(test)); + return this->convertDo(std::move(statement), std::move(test)); } std::unique_ptr IRGenerator::convertSwitch(const ASTNode& s) { diff --git a/src/sksl/SkSLIRGenerator.h b/src/sksl/SkSLIRGenerator.h index 27ecad99d8..a17f98bca4 100644 --- a/src/sksl/SkSLIRGenerator.h +++ b/src/sksl/SkSLIRGenerator.h @@ -188,6 +188,8 @@ private: ExpressionArray params); std::unique_ptr convertContinue(const ASTNode& c); std::unique_ptr convertDiscard(const ASTNode& d); + std::unique_ptr convertDo(std::unique_ptr stmt, + std::unique_ptr test); std::unique_ptr convertDo(const ASTNode& d); std::unique_ptr convertSwitch(const ASTNode& s); std::unique_ptr convertBinaryExpression(const ASTNode& expression); @@ -195,6 +197,11 @@ private: std::unique_ptr convertExpressionStatement(const ASTNode& s); std::unique_ptr convertField(std::unique_ptr base, StringFragment field); + std::unique_ptr convertFor(int offset, + std::unique_ptr initializer, + std::unique_ptr test, + std::unique_ptr next, + std::unique_ptr statement); std::unique_ptr convertFor(const ASTNode& f); std::unique_ptr convertIdentifier(int offset, StringFragment identifier); std::unique_ptr convertIdentifier(const ASTNode& identifier);