Rename case statement conditions to selectors.

The name conditions isn't quite correct for the case statement. This CL
updates the code to use selectors instead of conditions.

Change-Id: I98b8050b11e2328f97e4443469572ab47d7c1555
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/22520
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-02 17:18:56 +00:00 committed by dan sinclair
parent cb48e79fa5
commit 1d69915155
9 changed files with 37 additions and 38 deletions

View File

@ -22,14 +22,14 @@ CaseStatement::CaseStatement() : Statement() {}
CaseStatement::CaseStatement(StatementList body) CaseStatement::CaseStatement(StatementList body)
: Statement(), body_(std::move(body)) {} : Statement(), body_(std::move(body)) {}
CaseStatement::CaseStatement(CaseSelectorList conditions, StatementList body) CaseStatement::CaseStatement(CaseSelectorList selectors, StatementList body)
: Statement(), conditions_(std::move(conditions)), body_(std::move(body)) {} : Statement(), selectors_(std::move(selectors)), body_(std::move(body)) {}
CaseStatement::CaseStatement(const Source& source, CaseStatement::CaseStatement(const Source& source,
CaseSelectorList conditions, CaseSelectorList selectors,
StatementList body) StatementList body)
: Statement(source), : Statement(source),
conditions_(std::move(conditions)), selectors_(std::move(selectors)),
body_(std::move(body)) {} body_(std::move(body)) {}
CaseStatement::CaseStatement(CaseStatement&&) = default; CaseStatement::CaseStatement(CaseStatement&&) = default;
@ -56,12 +56,12 @@ void CaseStatement::to_str(std::ostream& out, size_t indent) const {
} else { } else {
out << "Case "; out << "Case ";
bool first = true; bool first = true;
for (const auto& lit : conditions_) { for (const auto& selector : selectors_) {
if (!first) if (!first)
out << ", "; out << ", ";
first = false; first = false;
out << lit->to_str(); out << selector->to_str();
} }
out << "{" << std::endl; out << "{" << std::endl;
} }

View File

@ -22,7 +22,6 @@
#include "src/ast/expression.h" #include "src/ast/expression.h"
#include "src/ast/literal.h" #include "src/ast/literal.h"
#include "src/ast/statement.h" #include "src/ast/statement.h"
#include "src/ast/statement_condition.h"
namespace tint { namespace tint {
namespace ast { namespace ast {
@ -40,29 +39,29 @@ class CaseStatement : public Statement {
/// @param body the case body /// @param body the case body
explicit CaseStatement(StatementList body); explicit CaseStatement(StatementList body);
/// Constructor /// Constructor
/// @param conditions the case conditions /// @param selectors the case selectors
/// @param body the case body /// @param body the case body
CaseStatement(CaseSelectorList conditions, StatementList body); CaseStatement(CaseSelectorList selectors, StatementList body);
/// Constructor /// Constructor
/// @param source the source information /// @param source the source information
/// @param conditions the case conditions /// @param selectors the case selectors
/// @param body the case body /// @param body the case body
CaseStatement(const Source& source, CaseStatement(const Source& source,
CaseSelectorList conditions, CaseSelectorList selectors,
StatementList body); StatementList body);
/// Move constructor /// Move constructor
CaseStatement(CaseStatement&&); CaseStatement(CaseStatement&&);
~CaseStatement() override; ~CaseStatement() override;
/// Sets the conditions for the case statement /// Sets the selectors for the case statement
/// @param conditions the conditions to set /// @param selectors the selectors to set
void set_conditions(CaseSelectorList conditions) { void set_selectors(CaseSelectorList selectors) {
conditions_ = std::move(conditions); selectors_ = std::move(selectors);
} }
/// @returns the case condition, empty if none set /// @returns the case selectors, empty if none set
const CaseSelectorList& conditions() const { return conditions_; } const CaseSelectorList& selectors() const { return selectors_; }
/// @returns true if this is a default statement /// @returns true if this is a default statement
bool IsDefault() const { return conditions_.empty(); } bool IsDefault() const { return selectors_.empty(); }
/// Sets the case body /// Sets the case body
/// @param body the case body /// @param body the case body
@ -84,7 +83,7 @@ class CaseStatement : public Statement {
private: private:
CaseStatement(const CaseStatement&) = delete; CaseStatement(const CaseStatement&) = delete;
CaseSelectorList conditions_; CaseSelectorList selectors_;
StatementList body_; StatementList body_;
}; };

View File

@ -41,8 +41,8 @@ TEST_F(CaseStatementTest, Creation) {
auto* kill_ptr = stmts[0].get(); auto* kill_ptr = stmts[0].get();
CaseStatement c(std::move(b), std::move(stmts)); CaseStatement c(std::move(b), std::move(stmts));
ASSERT_EQ(c.conditions().size(), 1); ASSERT_EQ(c.selectors().size(), 1);
EXPECT_EQ(c.conditions()[0].get(), bool_ptr); EXPECT_EQ(c.selectors()[0].get(), bool_ptr);
ASSERT_EQ(c.body().size(), 1u); ASSERT_EQ(c.body().size(), 1u);
EXPECT_EQ(c.body()[0].get(), kill_ptr); EXPECT_EQ(c.body()[0].get(), kill_ptr);
} }
@ -61,7 +61,7 @@ TEST_F(CaseStatementTest, Creation_WithSource) {
EXPECT_EQ(src.column, 2u); EXPECT_EQ(src.column, 2u);
} }
TEST_F(CaseStatementTest, IsDefault_WithoutCondition) { TEST_F(CaseStatementTest, IsDefault_WithoutSelectors) {
StatementList stmts; StatementList stmts;
stmts.push_back(std::make_unique<KillStatement>()); stmts.push_back(std::make_unique<KillStatement>());
@ -70,13 +70,13 @@ TEST_F(CaseStatementTest, IsDefault_WithoutCondition) {
EXPECT_TRUE(c.IsDefault()); EXPECT_TRUE(c.IsDefault());
} }
TEST_F(CaseStatementTest, IsDefault_WithCondition) { TEST_F(CaseStatementTest, IsDefault_WithSelectors) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<BoolLiteral>(&bool_type, true)); b.push_back(std::make_unique<BoolLiteral>(&bool_type, true));
CaseStatement c; CaseStatement c;
c.set_conditions(std::move(b)); c.set_selectors(std::move(b));
EXPECT_FALSE(c.IsDefault()); EXPECT_FALSE(c.IsDefault());
} }
@ -115,7 +115,7 @@ TEST_F(CaseStatementTest, IsValid_InvalidBodyStatement) {
EXPECT_FALSE(c.IsValid()); EXPECT_FALSE(c.IsValid());
} }
TEST_F(CaseStatementTest, ToStr_WithCondition) { TEST_F(CaseStatementTest, ToStr_WithSelectors) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
CaseSelectorList b; CaseSelectorList b;
b.push_back(std::make_unique<BoolLiteral>(&bool_type, true)); b.push_back(std::make_unique<BoolLiteral>(&bool_type, true));
@ -132,7 +132,7 @@ TEST_F(CaseStatementTest, ToStr_WithCondition) {
)"); )");
} }
TEST_F(CaseStatementTest, ToStr_WithMultipleConditions) { TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
ast::type::I32Type i32; ast::type::I32Type i32;
CaseSelectorList b; CaseSelectorList b;
@ -150,7 +150,7 @@ TEST_F(CaseStatementTest, ToStr_WithMultipleConditions) {
)"); )");
} }
TEST_F(CaseStatementTest, ToStr_WithoutCondition) { TEST_F(CaseStatementTest, ToStr_WithoutSelectors) {
StatementList stmts; StatementList stmts;
stmts.push_back(std::make_unique<KillStatement>()); stmts.push_back(std::make_unique<KillStatement>());
CaseStatement c(CaseSelectorList{}, std::move(stmts)); CaseStatement c(CaseSelectorList{}, std::move(stmts));

View File

@ -1813,14 +1813,14 @@ std::unique_ptr<ast::CaseStatement> ParserImpl::switch_body() {
auto stmt = std::make_unique<ast::CaseStatement>(); auto stmt = std::make_unique<ast::CaseStatement>();
stmt->set_source(source); stmt->set_source(source);
if (t.IsCase()) { if (t.IsCase()) {
auto cond = case_selectors(); auto selectors = case_selectors();
if (has_error()) if (has_error())
return nullptr; return nullptr;
if (cond.empty()) { if (selectors.empty()) {
set_error(peek(), "unable to parse case conditional"); set_error(peek(), "unable to parse case selectors");
return nullptr; return nullptr;
} }
stmt->set_conditions(std::move(cond)); stmt->set_selectors(std::move(selectors));
} }
t = next(); t = next();

View File

@ -146,7 +146,7 @@ TEST_F(ParserImplTest, Statement_Switch_Invalid) {
auto e = p->statement(); auto e = p->statement();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(e, nullptr); ASSERT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:18: unable to parse case conditional"); EXPECT_EQ(p->error(), "1:18: unable to parse case selectors");
} }
TEST_F(ParserImplTest, Statement_Loop) { TEST_F(ParserImplTest, Statement_Loop) {

View File

@ -38,7 +38,7 @@ TEST_F(ParserImplTest, SwitchBody_Case_InvalidConstLiteral) {
auto e = p->switch_body(); auto e = p->switch_body();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(e, nullptr); ASSERT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:6: unable to parse case conditional"); EXPECT_EQ(p->error(), "1:6: unable to parse case selectors");
} }
TEST_F(ParserImplTest, SwitchBody_Case_MissingConstLiteral) { TEST_F(ParserImplTest, SwitchBody_Case_MissingConstLiteral) {
@ -46,7 +46,7 @@ TEST_F(ParserImplTest, SwitchBody_Case_MissingConstLiteral) {
auto e = p->switch_body(); auto e = p->switch_body();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(e, nullptr); ASSERT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "1:5: unable to parse case conditional"); EXPECT_EQ(p->error(), "1:5: unable to parse case selectors");
} }
TEST_F(ParserImplTest, SwitchBody_Case_MissingColon) { TEST_F(ParserImplTest, SwitchBody_Case_MissingColon) {

View File

@ -102,7 +102,7 @@ TEST_F(ParserImplTest, SwitchStmt_InvalidBody) {
auto e = p->switch_stmt(); auto e = p->switch_stmt();
ASSERT_TRUE(p->has_error()); ASSERT_TRUE(p->has_error());
ASSERT_EQ(e, nullptr); ASSERT_EQ(e, nullptr);
EXPECT_EQ(p->error(), "2:7: unable to parse case conditional"); EXPECT_EQ(p->error(), "2:7: unable to parse case selectors");
} }
} // namespace } // namespace

View File

@ -1365,7 +1365,7 @@ bool Builder::GenerateSwitchStatement(ast::SwitchStatement* stmt) {
auto block_id = block.to_i(); auto block_id = block.to_i();
case_ids.push_back(block_id); case_ids.push_back(block_id);
for (const auto& selector : item->conditions()) { for (const auto& selector : item->selectors()) {
if (!selector->IsInt()) { if (!selector->IsInt()) {
error_ = "expected integer literal for switch case label"; error_ = "expected integer literal for switch case label";
return false; return false;

View File

@ -725,13 +725,13 @@ bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) {
out_ << "case "; out_ << "case ";
bool first = true; bool first = true;
for (const auto& lit : stmt->conditions()) { for (const auto& selector : stmt->selectors()) {
if (!first) { if (!first) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
if (!EmitLiteral(lit.get())) { if (!EmitLiteral(selector.get())) {
return false; return false;
} }
} }