diff options
author | ethannicholas <ethannicholas@google.com> | 2016-09-07 13:37:16 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-07 13:37:16 -0700 |
commit | 14fe8cc16d2a98d5a96f818e4d0b7127ecad59e2 (patch) | |
tree | 71530ded37d30a8385a4568cbac2d9913f6b8b3c /src/sksl/ir | |
parent | ef6a9b7f9393d0317c879bc0060f427601b19a81 (diff) |
refactored SkSL VarDeclaration handling
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2312233002
Review-Url: https://codereview.chromium.org/2312233002
Diffstat (limited to 'src/sksl/ir')
-rw-r--r-- | src/sksl/ir/SkSLStatement.h | 2 | ||||
-rw-r--r-- | src/sksl/ir/SkSLVarDeclaration.h | 82 | ||||
-rw-r--r-- | src/sksl/ir/SkSLVarDeclarationStatement.h | 10 |
3 files changed, 54 insertions, 40 deletions
diff --git a/src/sksl/ir/SkSLStatement.h b/src/sksl/ir/SkSLStatement.h index 64b7bdf276..012311fdd3 100644 --- a/src/sksl/ir/SkSLStatement.h +++ b/src/sksl/ir/SkSLStatement.h @@ -27,7 +27,7 @@ struct Statement : public IRNode { kFor_Kind, kIf_Kind, kReturn_Kind, - kVarDeclaration_Kind, + kVarDeclarations_Kind, kWhile_Kind }; diff --git a/src/sksl/ir/SkSLVarDeclaration.h b/src/sksl/ir/SkSLVarDeclaration.h index ca3c7f95b2..e64a874d69 100644 --- a/src/sksl/ir/SkSLVarDeclaration.h +++ b/src/sksl/ir/SkSLVarDeclaration.h @@ -5,8 +5,8 @@ * found in the LICENSE file. */ -#ifndef SKSL_VARDECLARATION -#define SKSL_VARDECLARATION +#ifndef SKSL_VARDECLARATIONS +#define SKSL_VARDECLARATIONS #include "SkSLExpression.h" #include "SkSLStatement.h" @@ -15,51 +15,65 @@ namespace SkSL { /** - * A variable declaration, which may consist of multiple individual variables. For instance - * 'int x, y = 1, z[4][2];' is a single VarDeclaration. This declaration would have a base type of - * 'int', names ['x', 'y', 'z'], sizes of [[], [], [4, 2]], and values of [null, 1, null]. + * A single variable declaration within a var declaration statement. For instance, the statement + * 'int x = 2, y[3];' is a VarDeclarations statement containing two individual VarDeclaration + * instances. */ -struct VarDeclaration : public ProgramElement { - VarDeclaration(Position position, const Type* baseType, std::vector<const Variable*> vars, - std::vector<std::vector<std::unique_ptr<Expression>>> sizes, - std::vector<std::unique_ptr<Expression>> values) +struct VarDeclaration { + VarDeclaration(const Variable* var, + std::vector<std::unique_ptr<Expression>> sizes, + std::unique_ptr<Expression> value) + : fVar(var) + , fSizes(std::move(sizes)) + , fValue(std::move(value)) {} + + std::string description() const { + std::string result = fVar->fName; + for (const auto& size : fSizes) { + if (size) { + result += "[" + size->description() + "]"; + } else { + result += "[]"; + } + } + if (fValue) { + result += " = " + fValue->description(); + } + return result; + } + + const Variable* fVar; + std::vector<std::unique_ptr<Expression>> fSizes; + std::unique_ptr<Expression> fValue; +}; + +/** + * A variable declaration statement, which may consist of one or more individual variables. + */ +struct VarDeclarations : public ProgramElement { + VarDeclarations(Position position, const Type* baseType, + std::vector<VarDeclaration> vars) : INHERITED(position, kVar_Kind) , fBaseType(*baseType) - , fVars(std::move(vars)) - , fSizes(std::move(sizes)) - , fValues(std::move(values)) {} + , fVars(std::move(vars)) {} std::string description() const override { - std::string result = fVars[0]->fModifiers.description(); - const Type* baseType = &fVars[0]->fType; - while (baseType->kind() == Type::kArray_Kind) { - baseType = &baseType->componentType(); + if (!fVars.size()) { + return ""; } - result += baseType->description(); - std::string separator = " "; - for (size_t i = 0; i < fVars.size(); i++) { + std::string result = fVars[0].fVar->fModifiers.description() + fBaseType.description() + + " "; + std::string separator = ""; + for (const auto& var : fVars) { result += separator; separator = ", "; - result += fVars[i]->fName; - for (size_t j = 0; j < fSizes[i].size(); j++) { - if (fSizes[i][j]) { - result += "[" + fSizes[i][j]->description() + "]"; - } else { - result += "[]"; - } - } - if (fValues[i]) { - result += " = " + fValues[i]->description(); - } + result += var.description(); } - result += ";"; return result; } const Type& fBaseType; - const std::vector<const Variable*> fVars; - const std::vector<std::vector<std::unique_ptr<Expression>>> fSizes; - const std::vector<std::unique_ptr<Expression>> fValues; + const std::vector<VarDeclaration> fVars; typedef ProgramElement INHERITED; }; diff --git a/src/sksl/ir/SkSLVarDeclarationStatement.h b/src/sksl/ir/SkSLVarDeclarationStatement.h index e81c0ac3ec..59d37ab915 100644 --- a/src/sksl/ir/SkSLVarDeclarationStatement.h +++ b/src/sksl/ir/SkSLVarDeclarationStatement.h @@ -14,18 +14,18 @@ namespace SkSL { /** - * A variable declaration appearing as a statement within a function. + * One or more variable declarations appearing as a statement within a function. */ -struct VarDeclarationStatement : public Statement { - VarDeclarationStatement(std::unique_ptr<VarDeclaration> decl) - : INHERITED(decl->fPosition, kVarDeclaration_Kind) +struct VarDeclarationsStatement : public Statement { + VarDeclarationsStatement(std::unique_ptr<VarDeclarations> decl) + : INHERITED(decl->fPosition, kVarDeclarations_Kind) , fDeclaration(std::move(decl)) {} std::string description() const override { return fDeclaration->description(); } - const std::shared_ptr<VarDeclaration> fDeclaration; + const std::shared_ptr<VarDeclarations> fDeclaration; typedef Statement INHERITED; }; |