In VarDeclarations, align constructor parameter type with storage

This moves the type juggling to the callers, but means that we don't
have to allocate a second vector in the constructor and copy things
over.

Change-Id: If017a7d6fe4eaa678679b1506f6c0c241d72a381
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/322626
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
This commit is contained in:
Brian Osman 2020-10-05 15:56:44 -04:00 committed by Skia Commit-Bot
parent 05162812fd
commit 347e5dc371
4 changed files with 22 additions and 23 deletions

View File

@ -306,7 +306,7 @@ std::unique_ptr<VarDeclarations> IRGenerator::convertVarDeclarations(const ASTNo
auto declarationsIter = decls.begin();
const Modifiers& modifiers = declarationsIter++->getModifiers();
const ASTNode& rawType = *(declarationsIter++);
std::vector<std::unique_ptr<VarDeclaration>> variables;
std::vector<std::unique_ptr<Statement>> variables;
const Type* baseType = this->convertType(rawType);
if (!baseType) {
return nullptr;

View File

@ -562,9 +562,10 @@ std::unique_ptr<Statement> Inliner::inlineStatement(int offset,
}
case Statement::Kind::kVarDeclarations: {
const VarDeclarations& decls = *statement.as<VarDeclarationsStatement>().fDeclaration;
std::vector<std::unique_ptr<VarDeclaration>> vars;
std::vector<std::unique_ptr<Statement>> vars;
vars.reserve(decls.fVars.size());
for (const auto& var : decls.fVars) {
vars.emplace_back(&stmt(var).release()->as<VarDeclaration>());
vars.push_back(stmt(var));
}
const Type* typePtr = copy_if_needed(&decls.fBaseType, *symbolTableForStatement);
return std::unique_ptr<Statement>(new VarDeclarationsStatement(
@ -645,7 +646,7 @@ Inliner::InlinedCall Inliner::inlineCall(FunctionCall* call,
// Prepare the variable declaration (taking extra care with `out` params to not clobber any
// initial value).
std::vector<std::unique_ptr<VarDeclaration>> variables;
std::vector<std::unique_ptr<Statement>> variables;
if (initialValue && (modifiers.fFlags & Modifiers::kOut_Flag)) {
variables.push_back(std::make_unique<VarDeclaration>(
variableSymbol, /*sizes=*/std::vector<std::unique_ptr<Expression>>{},

View File

@ -315,12 +315,10 @@ std::unique_ptr<ProgramElement> Rehydrator::element() {
case Rehydrator::kVarDeclarations_Command: {
const Type* baseType = this->type();
int count = this->readU8();
std::vector<std::unique_ptr<VarDeclaration>> vars;
std::vector<std::unique_ptr<Statement>> vars;
vars.reserve(count);
for (int i = 0 ; i < count; ++i) {
std::unique_ptr<Statement> s = this->statement();
SkASSERT(s->kind() == Statement::Kind::kVarDeclaration);
vars.emplace_back((VarDeclaration*) s.release());
vars.push_back(this->statement());
}
return std::unique_ptr<ProgramElement>(new VarDeclarations(-1, baseType,
std::move(vars)));
@ -438,12 +436,10 @@ std::unique_ptr<Statement> Rehydrator::statement() {
case Rehydrator::kVarDeclarations_Command: {
const Type* baseType = this->type();
int count = this->readU8();
std::vector<std::unique_ptr<VarDeclaration>> vars;
std::vector<std::unique_ptr<Statement>> vars;
vars.reserve(count);
for (int i = 0 ; i < count; ++i) {
std::unique_ptr<Statement> s = this->statement();
SkASSERT(s->kind() == Statement::Kind::kVarDeclaration);
vars.emplace_back((VarDeclaration*) s.release());
vars.push_back(this->statement());
}
return std::make_unique<VarDeclarationsStatement>(
std::make_unique<VarDeclarations>(-1, baseType, std::move(vars)));

View File

@ -72,23 +72,25 @@ struct VarDeclaration : public Statement {
struct VarDeclarations : public ProgramElement {
static constexpr Kind kProgramElementKind = Kind::kVar;
VarDeclarations(int offset, const Type* baseType,
std::vector<std::unique_ptr<VarDeclaration>> vars)
: INHERITED(offset, kProgramElementKind)
, fBaseType(*baseType) {
for (auto& var : vars) {
fVars.push_back(std::unique_ptr<Statement>(var.release()));
// vars must be a vector of unique_ptr<VarDeclaration>, but to simplify the CFG, we store
// (and thus are constructed with) Statements.
VarDeclarations(int offset, const Type* baseType, std::vector<std::unique_ptr<Statement>> vars)
: INHERITED(offset, kProgramElementKind), fBaseType(*baseType)
, fVars(std::move(vars)) {
#if defined(SK_DEBUG)
for (auto& var : fVars) {
SkASSERT(var->is<VarDeclaration>());
}
#endif
}
std::unique_ptr<ProgramElement> clone() const override {
std::vector<std::unique_ptr<VarDeclaration>> cloned;
std::vector<std::unique_ptr<Statement>> cloned;
cloned.reserve(fVars.size());
for (const auto& v : fVars) {
cloned.push_back(std::unique_ptr<VarDeclaration>(
(VarDeclaration*) v->clone().release()));
cloned.push_back(v->clone());
}
return std::unique_ptr<ProgramElement>(new VarDeclarations(fOffset, &fBaseType,
std::move(cloned)));
return std::make_unique<VarDeclarations>(fOffset, &fBaseType, std::move(cloned));
}
String description() const override {