From 37f4fb0f759e06cf5a8fa9190e626d151135360a Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Fri, 20 Mar 2020 19:04:45 +0000 Subject: [PATCH] Emit case statement This CL adds case statements to the WGSL generator. Bug: tint:4 Change-Id: Ic7945646d79b7c3e9e1cb1cf57d1633a28f45fc0 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17180 Reviewed-by: David Neto --- src/CMakeLists.txt | 1 + src/writer/wgsl/generator_impl.cc | 42 ++++++++++- src/writer/wgsl/generator_impl.h | 8 +++ src/writer/wgsl/generator_impl_assign_test.cc | 2 +- src/writer/wgsl/generator_impl_break_test.cc | 6 +- src/writer/wgsl/generator_impl_case_test.cc | 71 +++++++++++++++++++ 6 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 src/writer/wgsl/generator_impl_case_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a77e48097a..df4045dd6b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -410,6 +410,7 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_assign_test.cc writer/wgsl/generator_impl_break_test.cc writer/wgsl/generator_impl_call_test.cc + writer/wgsl/generator_impl_case_test.cc writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_entry_point_test.cc writer/wgsl/generator_impl_identifier_test.cc diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index ad9b3da4d5..4f587425a6 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -25,6 +25,7 @@ #include "src/ast/break_statement.h" #include "src/ast/builtin_decoration.h" #include "src/ast/call_expression.h" +#include "src/ast/case_statement.h" #include "src/ast/cast_expression.h" #include "src/ast/const_initializer_expression.h" #include "src/ast/decorated_variable.h" @@ -274,7 +275,10 @@ bool GeneratorImpl::EmitTypeInitializer(ast::TypeInitializerExpression* expr) { bool GeneratorImpl::EmitConstInitializer( ast::ConstInitializerExpression* expr) { - auto lit = expr->literal(); + return EmitLiteral(expr->literal()); +} + +bool GeneratorImpl::EmitLiteral(ast::Literal* lit) { if (lit->IsBool()) { out_ << (lit->AsBool()->IsTrue() ? "true" : "false"); } else if (lit->IsFloat()) { @@ -649,6 +653,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsBreak()) { return EmitBreak(stmt->AsBreak()); } + if (stmt->IsCase()) { + return EmitCase(stmt->AsCase()); + } error_ = "unknown statement type"; return false; @@ -667,7 +674,7 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) { return false; } - out_ << ";"; + out_ << ";" << std::endl; return true; } @@ -692,7 +699,36 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement* stmt) { out_ << ")"; } - out_ << ";"; + out_ << ";" << std::endl; + + return true; +} + +bool GeneratorImpl::EmitCase(ast::CaseStatement* stmt) { + make_indent(); + + if (stmt->IsDefault()) { + out_ << "default:"; + } else { + out_ << "case "; + + if (!EmitLiteral(stmt->condition())) { + return false; + } + out_ << ":"; + } + out_ << " {" << std::endl; + increment_indent(); + + for (const auto& b : stmt->body()) { + if (!EmitStatement(b.get())) { + return false; + } + } + decrement_indent(); + + make_indent(); + out_ << "}" << std::endl; return true; } diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 6dc47e21de..7b456a90d7 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -90,6 +90,10 @@ class GeneratorImpl { /// @param expr the call expression /// @returns true if the call expression is emitted bool EmitCall(ast::CallExpression* expr); + /// 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 @@ -118,6 +122,10 @@ class GeneratorImpl { /// @param expr the initializer expression /// @returns true if the expression was emitted bool EmitInitializer(ast::InitializerExpression* expr); + /// Handles a literal + /// @param lit the literal to emit + /// @returns true if the literal was successfully emitted + bool EmitLiteral(ast::Literal* lit); /// Handles a member accessor expression /// @param expr the member accessor expression /// @returns true if the member accessor was emitted diff --git a/src/writer/wgsl/generator_impl_assign_test.cc b/src/writer/wgsl/generator_impl_assign_test.cc index a3e2ece91f..d97238a8d4 100644 --- a/src/writer/wgsl/generator_impl_assign_test.cc +++ b/src/writer/wgsl/generator_impl_assign_test.cc @@ -36,7 +36,7 @@ TEST_F(GeneratorImplTest, Emit_Assign) { g.increment_indent(); ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); - EXPECT_EQ(g.result(), " lhs = rhs;"); + EXPECT_EQ(g.result(), " lhs = rhs;\n"); } } // namespace diff --git a/src/writer/wgsl/generator_impl_break_test.cc b/src/writer/wgsl/generator_impl_break_test.cc index da057a67a2..556dcfe7d6 100644 --- a/src/writer/wgsl/generator_impl_break_test.cc +++ b/src/writer/wgsl/generator_impl_break_test.cc @@ -34,7 +34,7 @@ TEST_F(GeneratorImplTest, Emit_Break) { g.increment_indent(); ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); - EXPECT_EQ(g.result(), " break;"); + EXPECT_EQ(g.result(), " break;\n"); } TEST_F(GeneratorImplTest, Emit_BreakWithIf) { @@ -45,7 +45,7 @@ TEST_F(GeneratorImplTest, Emit_BreakWithIf) { g.increment_indent(); ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); - EXPECT_EQ(g.result(), " break if (expr);"); + EXPECT_EQ(g.result(), " break if (expr);\n"); } TEST_F(GeneratorImplTest, Emit_BreakWithUnless) { @@ -56,7 +56,7 @@ TEST_F(GeneratorImplTest, Emit_BreakWithUnless) { g.increment_indent(); ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); - EXPECT_EQ(g.result(), " break unless (expr);"); + EXPECT_EQ(g.result(), " break unless (expr);\n"); } } // namespace diff --git a/src/writer/wgsl/generator_impl_case_test.cc b/src/writer/wgsl/generator_impl_case_test.cc new file mode 100644 index 0000000000..63edbc1ff9 --- /dev/null +++ b/src/writer/wgsl/generator_impl_case_test.cc @@ -0,0 +1,71 @@ +// 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 + +#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/int_literal.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using GeneratorImplTest = testing::Test; + +TEST_F(GeneratorImplTest, Emit_Case) { + auto cond = std::make_unique(5); + + std::vector> body; + body.push_back(std::make_unique()); + + ast::CaseStatement c(std::move(cond), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( case 5: { + break; + } +)"); +} + +TEST_F(GeneratorImplTest, Emit_Case_Default) { + ast::CaseStatement c; + + std::vector> body; + body.push_back(std::make_unique()); + + c.set_body(std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&c)) << g.error(); + EXPECT_EQ(g.result(), R"( default: { + break; + } +)"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint