diff --git a/BUILD.gn b/BUILD.gn index 7c1303989a..78b3515d08 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -890,6 +890,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_assign_test.cc", "src/writer/msl/generator_impl_binary_test.cc", "src/writer/msl/generator_impl_break_test.cc", + "src/writer/msl/generator_impl_case_test.cc", "src/writer/msl/generator_impl_cast_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", "src/writer/msl/generator_impl_continue_test.cc", @@ -900,6 +901,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_loop_test.cc", "src/writer/msl/generator_impl_member_accessor_test.cc", "src/writer/msl/generator_impl_return_test.cc", + "src/writer/msl/generator_impl_switch_test.cc", "src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_type_test.cc", "src/writer/msl/generator_impl_unary_op_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 82f26d0c9b..3976a9101c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -498,6 +498,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_assign_test.cc writer/msl/generator_impl_binary_test.cc writer/msl/generator_impl_break_test.cc + writer/msl/generator_impl_case_test.cc writer/msl/generator_impl_cast_test.cc writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_continue_test.cc @@ -508,6 +509,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_loop_test.cc writer/msl/generator_impl_member_accessor_test.cc writer/msl/generator_impl_return_test.cc + writer/msl/generator_impl_switch_test.cc writer/msl/generator_impl_test.cc writer/msl/generator_impl_type_test.cc writer/msl/generator_impl_unary_op_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 466688edfd..2ab8b028b9 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -20,6 +20,7 @@ #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/break_statement.h" +#include "src/ast/case_statement.h" #include "src/ast/cast_expression.h" #include "src/ast/continue_statement.h" #include "src/ast/else_statement.h" @@ -31,6 +32,7 @@ #include "src/ast/member_accessor_expression.h" #include "src/ast/return_statement.h" #include "src/ast/sint_literal.h" +#include "src/ast/switch_statement.h" #include "src/ast/type/alias_type.h" #include "src/ast/type/array_type.h" #include "src/ast/type/bool_type.h" @@ -48,6 +50,17 @@ namespace tint { namespace writer { namespace msl { +namespace { + +bool last_is_break_or_fallthrough(const ast::StatementList& stmts) { + if (stmts.empty()) { + return false; + } + + return stmts.back()->IsBreak() || stmts.back()->IsFallthrough(); +} + +} // namespace GeneratorImpl::GeneratorImpl() = default; @@ -222,6 +235,50 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { return true; } +bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { + make_indent(); + + if (stmt->IsDefault()) { + out_ << "default:"; + } else { + bool first = true; + for (const auto& selector : stmt->selectors()) { + if (!first) { + out_ << std::endl; + make_indent(); + } + first = false; + + out_ << "case "; + if (!EmitLiteral(selector.get())) { + return false; + } + out_ << ":"; + } + } + + out_ << " {" << std::endl; + + increment_indent(); + + for (const auto& s : stmt->body()) { + if (!EmitStatement(s.get())) { + return false; + } + } + + if (!last_is_break_or_fallthrough(stmt->body())) { + make_indent(); + out_ << "break;" << std::endl; + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + + return true; +} + bool GeneratorImpl::EmitCast(ast::CastExpression* expr) { if (!EmitType(expr->type(), "")) { return false; @@ -567,6 +624,11 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsContinue()) { return EmitContinue(stmt->AsContinue()); } + if (stmt->IsFallthrough()) { + make_indent(); + out_ << "/* fallthrough */" << std::endl; + return true; + } if (stmt->IsIf()) { return EmitIf(stmt->AsIf()); } @@ -579,11 +641,38 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } + if (stmt->IsSwitch()) { + return EmitSwitch(stmt->AsSwitch()); + } error_ = "unknown statement type"; return false; } +bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) { + make_indent(); + + out_ << "switch("; + if (!EmitExpression(stmt->condition())) { + return false; + } + out_ << ") {" << std::endl; + + increment_indent(); + + for (const auto& s : stmt->body()) { + if (!EmitCase(s.get())) { + return false; + } + } + + decrement_indent(); + make_indent(); + out_ << "}" << std::endl; + + return true; +} + bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) { if (type->IsAlias()) { auto* alias = type->AsAlias(); diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index d74ff04345..7573091e1f 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -64,6 +64,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted successfully bool EmitBreak(ast::BreakStatement* stmt); + /// Handles a case statement + /// @param stmt the statement + /// @returns true if the statment was emitted successfully + bool EmitCase(ast::CaseStatement* stmt); /// Handles generating a cast expression /// @param expr the cast expression /// @returns true if the cast was emitted @@ -135,6 +139,10 @@ class GeneratorImpl : public TextGenerator { /// @param stmt the statement to emit /// @returns true if the statement was emitted bool EmitStatement(ast::Statement* stmt); + /// Handles generating a switch statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted + bool EmitSwitch(ast::SwitchStatement* stmt); /// Handles generating type /// @param type the type to generate /// @param name the name of the variable, only used for array emission diff --git a/src/writer/msl/generator_impl_case_test.cc b/src/writer/msl/generator_impl_case_test.cc new file mode 100644 index 0000000000..a53f207d83 --- /dev/null +++ b/src/writer/msl/generator_impl_case_test.cc @@ -0,0 +1,135 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gtest/gtest.h" +#include "src/ast/break_statement.h" +#include "src/ast/case_statement.h" +#include "src/ast/fallthrough_statement.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/sint_literal.h" +#include "src/ast/type/i32_type.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Case) { + ast::type::I32Type i32; + + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::CaseSelectorList lit; + lit.push_back(std::make_unique(&i32, 5)); + ast::CaseStatement c(std::move(lit), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitCase(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( case 5: { + break; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Case_BreaksByDefault) { + ast::type::I32Type i32; + + ast::StatementList body; + + ast::CaseSelectorList lit; + lit.push_back(std::make_unique(&i32, 5)); + ast::CaseStatement c(std::move(lit), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitCase(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( case 5: { + break; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Case_WithFallthrough) { + ast::type::I32Type i32; + + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::CaseSelectorList lit; + lit.push_back(std::make_unique(&i32, 5)); + ast::CaseStatement c(std::move(lit), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitCase(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( case 5: { + /* fallthrough */ + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Case_MultipleSelectors) { + ast::type::I32Type i32; + + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::CaseSelectorList lit; + lit.push_back(std::make_unique(&i32, 5)); + lit.push_back(std::make_unique(&i32, 6)); + ast::CaseStatement c(std::move(lit), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitCase(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( case 5: + case 6: { + break; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_Case_Default) { + ast::CaseStatement c; + + ast::StatementList body; + body.push_back(std::make_unique()); + + c.set_body(std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitCase(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( default: { + break; + } +)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint diff --git a/src/writer/msl/generator_impl_switch_test.cc b/src/writer/msl/generator_impl_switch_test.cc new file mode 100644 index 0000000000..56a919de1e --- /dev/null +++ b/src/writer/msl/generator_impl_switch_test.cc @@ -0,0 +1,74 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "gtest/gtest.h" +#include "src/ast/break_statement.h" +#include "src/ast/case_statement.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/sint_literal.h" +#include "src/ast/switch_statement.h" +#include "src/ast/type/i32_type.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Switch) { + auto def = std::make_unique(); + ast::StatementList def_body; + def_body.push_back(std::make_unique()); + def->set_body(std::move(def_body)); + + ast::type::I32Type i32; + ast::CaseSelectorList case_val; + case_val.push_back(std::make_unique(&i32, 5)); + + ast::StatementList case_body; + case_body.push_back(std::make_unique()); + + auto case_stmt = std::make_unique(std::move(case_val), + std::move(case_body)); + + ast::CaseStatementList body; + body.push_back(std::move(case_stmt)); + body.push_back(std::move(def)); + + auto cond = std::make_unique("cond"); + ast::SwitchStatement s(std::move(cond), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&s)) << g.error(); + EXPECT_EQ(g.result(), R"( switch(cond) { + case 5: { + break; + } + default: { + break; + } + } +)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint