diff --git a/BUILD.gn b/BUILD.gn index 5c67ee8764..4643fca889 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -889,8 +889,10 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_as_test.cc", "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_cast_test.cc", "src/writer/msl/generator_impl_constructor_test.cc", + "src/writer/msl/generator_impl_continue_test.cc", "src/writer/msl/generator_impl_function_test.cc", "src/writer/msl/generator_impl_identifier_test.cc", "src/writer/msl/generator_impl_if_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab015667c6..5a64fba7c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -497,8 +497,10 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_as_test.cc 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_cast_test.cc writer/msl/generator_impl_constructor_test.cc + writer/msl/generator_impl_continue_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc writer/msl/generator_impl_if_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 8d05206208..04eba00e63 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -19,7 +19,9 @@ #include "src/ast/assignment_statement.h" #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" +#include "src/ast/break_statement.h" #include "src/ast/cast_expression.h" +#include "src/ast/continue_statement.h" #include "src/ast/else_statement.h" #include "src/ast/float_literal.h" #include "src/ast/function.h" @@ -213,6 +215,12 @@ bool GeneratorImpl::EmitBinary(ast::BinaryExpression* expr) { return true; } +bool GeneratorImpl::EmitBreak(ast::BreakStatement*) { + make_indent(); + out_ << "break;" << std::endl; + return true; +} + bool GeneratorImpl::EmitCast(ast::CastExpression* expr) { if (!EmitType(expr->type(), "")) { return false; @@ -233,6 +241,12 @@ bool GeneratorImpl::EmitConstructor(ast::ConstructorExpression* expr) { return EmitTypeConstructor(expr->AsTypeConstructor()); } +bool GeneratorImpl::EmitContinue(ast::ContinueStatement*) { + make_indent(); + out_ << "continue;" << std::endl; + return true; +} + bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) { if (!EmitType(expr->type(), "")) { return false; @@ -493,6 +507,12 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsAssign()) { return EmitAssign(stmt->AsAssign()); } + if (stmt->IsBreak()) { + return EmitBreak(stmt->AsBreak()); + } + if (stmt->IsContinue()) { + return EmitContinue(stmt->AsContinue()); + } if (stmt->IsIf()) { return EmitIf(stmt->AsIf()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index e27a0716e6..77391b5cfa 100644 --- a/src/writer/msl/generator_impl.h +++ b/src/writer/msl/generator_impl.h @@ -60,6 +60,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the binary expression /// @returns true if the expression was emitted, false otherwise bool EmitBinary(ast::BinaryExpression* expr); + /// Handles a break statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitBreak(ast::BreakStatement* stmt); /// Handles generating a cast expression /// @param expr the cast expression /// @returns true if the cast was emitted @@ -68,6 +72,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the constructor expression /// @returns true if the expression was emitted bool EmitConstructor(ast::ConstructorExpression* expr); + /// Handles a continue statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted successfully + bool EmitContinue(ast::ContinueStatement* stmt); /// Handles generating an else statement /// @param stmt the statement to emit /// @returns true if the statement was emitted diff --git a/src/writer/msl/generator_impl_break_test.cc b/src/writer/msl/generator_impl_break_test.cc new file mode 100644 index 0000000000..8f33da009f --- /dev/null +++ b/src/writer/msl/generator_impl_break_test.cc @@ -0,0 +1,42 @@ +// 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/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Break) { + ast::BreakStatement b; + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); + EXPECT_EQ(g.result(), " break;\n"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint diff --git a/src/writer/msl/generator_impl_continue_test.cc b/src/writer/msl/generator_impl_continue_test.cc new file mode 100644 index 0000000000..550c29f59a --- /dev/null +++ b/src/writer/msl/generator_impl_continue_test.cc @@ -0,0 +1,42 @@ +// 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/continue_statement.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_Continue) { + ast::ContinueStatement c; + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&c)) << g.error(); + EXPECT_EQ(g.result(), " continue;\n"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint