diff --git a/BUILD.gn b/BUILD.gn index e90aa057b1..ccff5dd703 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -852,7 +852,6 @@ source_set("tint_unittests_wgsl_writer_src") { "src/writer/wgsl/generator_impl_cast_test.cc", "src/writer/wgsl/generator_impl_constructor_test.cc", "src/writer/wgsl/generator_impl_continue_test.cc", - "src/writer/wgsl/generator_impl_else_test.cc", "src/writer/wgsl/generator_impl_entry_point_test.cc", "src/writer/wgsl/generator_impl_fallthrough_test.cc", "src/writer/wgsl/generator_impl_function_test.cc", @@ -892,6 +891,7 @@ source_set("tint_unittests_msl_writer_src") { "src/writer/msl/generator_impl_constructor_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", "src/writer/msl/generator_impl_return_test.cc", "src/writer/msl/generator_impl_test.cc", "src/writer/msl/generator_impl_type_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1cd7a4991..27d1bfeee1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -472,7 +472,6 @@ if(${TINT_BUILD_WGSL_WRITER}) writer/wgsl/generator_impl_cast_test.cc writer/wgsl/generator_impl_constructor_test.cc writer/wgsl/generator_impl_continue_test.cc - writer/wgsl/generator_impl_else_test.cc writer/wgsl/generator_impl_entry_point_test.cc writer/wgsl/generator_impl_fallthrough_test.cc writer/wgsl/generator_impl_function_test.cc @@ -500,6 +499,7 @@ if(${TINT_BUILD_MSL_WRITER}) writer/msl/generator_impl_constructor_test.cc writer/msl/generator_impl_function_test.cc writer/msl/generator_impl_identifier_test.cc + writer/msl/generator_impl_if_test.cc writer/msl/generator_impl_return_test.cc writer/msl/generator_impl_test.cc writer/msl/generator_impl_type_test.cc diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 252e90290c..79f5afb6b0 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -19,9 +19,11 @@ #include "src/ast/binary_expression.h" #include "src/ast/bool_literal.h" #include "src/ast/cast_expression.h" +#include "src/ast/else_statement.h" #include "src/ast/float_literal.h" #include "src/ast/function.h" #include "src/ast/identifier_expression.h" +#include "src/ast/if_statement.h" #include "src/ast/return_statement.h" #include "src/ast/sint_literal.h" #include "src/ast/type/alias_type.h" @@ -345,6 +347,42 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) { return true; } +bool GeneratorImpl::EmitElse(ast::ElseStatement* stmt) { + if (stmt->HasCondition()) { + out_ << " else if ("; + if (!EmitExpression(stmt->condition())) { + return false; + } + out_ << ")"; + } else { + out_ << " else"; + } + + return EmitStatementBlock(stmt->body()); +} + +bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) { + make_indent(); + + out_ << "if ("; + if (!EmitExpression(stmt->condition())) { + return false; + } + out_ << ")"; + + if (!EmitStatementBlock(stmt->body())) { + return false; + } + + for (const auto& e : stmt->else_statements()) { + if (!EmitElse(e.get())) { + return false; + } + } + out_ << std::endl; + + return true; +} bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) { make_indent(); @@ -390,6 +428,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsAssign()) { return EmitAssign(stmt->AsAssign()); } + if (stmt->IsIf()) { + return EmitIf(stmt->AsIf()); + } if (stmt->IsReturn()) { return EmitReturn(stmt->AsReturn()); } diff --git a/src/writer/msl/generator_impl.h b/src/writer/msl/generator_impl.h index 3c3b1b5c0e..9459089549 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 constructor expression /// @returns true if the expression was emitted bool EmitConstructor(ast::ConstructorExpression* expr); + /// Handles generating an else statement + /// @param stmt the statement to emit + /// @returns true if the statement was emitted + bool EmitElse(ast::ElseStatement* stmt); /// Handles generate an Expression /// @param expr the expression /// @returns true if the expression was emitted @@ -72,6 +76,10 @@ class GeneratorImpl : public TextGenerator { /// @param expr the identifier expression /// @returns true if the identifeir was emitted bool EmitIdentifier(ast::IdentifierExpression* expr); + /// Handles an if statement + /// @param stmt the statement to emit + /// @returns true if the statement was successfully emitted + bool EmitIf(ast::IfStatement* stmt); /// Handles a literal /// @param lit the literal to emit /// @returns true if the literal was successfully emitted diff --git a/src/writer/msl/generator_impl_if_test.cc b/src/writer/msl/generator_impl_if_test.cc new file mode 100644 index 0000000000..b7a78b7ae6 --- /dev/null +++ b/src/writer/msl/generator_impl_if_test.cc @@ -0,0 +1,139 @@ +// 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 "gtest/gtest.h" +#include "src/ast/else_statement.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/if_statement.h" +#include "src/ast/return_statement.h" +#include "src/writer/msl/generator_impl.h" + +namespace tint { +namespace writer { +namespace msl { +namespace { + +using MslGeneratorImplTest = testing::Test; + +TEST_F(MslGeneratorImplTest, Emit_If) { + auto cond = std::make_unique("cond"); + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::IfStatement i(std::move(cond), std::move(body)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); + EXPECT_EQ(g.result(), R"( if (cond) { + return; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) { + auto else_cond = std::make_unique("else_cond"); + + ast::StatementList else_body; + else_body.push_back(std::make_unique()); + + ast::ElseStatementList elses; + elses.push_back(std::make_unique(std::move(else_cond), + std::move(else_body))); + + auto cond = std::make_unique("cond"); + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::IfStatement i(std::move(cond), std::move(body)); + i.set_else_statements(std::move(elses)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); + EXPECT_EQ(g.result(), R"( if (cond) { + return; + } else if (else_cond) { + return; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_IfWithElse) { + ast::StatementList else_body; + else_body.push_back(std::make_unique()); + + ast::ElseStatementList elses; + elses.push_back(std::make_unique(std::move(else_body))); + + auto cond = std::make_unique("cond"); + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::IfStatement i(std::move(cond), std::move(body)); + i.set_else_statements(std::move(elses)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); + EXPECT_EQ(g.result(), R"( if (cond) { + return; + } else { + return; + } +)"); +} + +TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) { + auto else_cond = std::make_unique("else_cond"); + + ast::StatementList else_body; + else_body.push_back(std::make_unique()); + + ast::StatementList else_body_2; + else_body_2.push_back(std::make_unique()); + + ast::ElseStatementList elses; + elses.push_back(std::make_unique(std::move(else_cond), + std::move(else_body))); + elses.push_back(std::make_unique(std::move(else_body_2))); + + auto cond = std::make_unique("cond"); + ast::StatementList body; + body.push_back(std::make_unique()); + + ast::IfStatement i(std::move(cond), std::move(body)); + i.set_else_statements(std::move(elses)); + + GeneratorImpl g; + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); + EXPECT_EQ(g.result(), R"( if (cond) { + return; + } else if (else_cond) { + return; + } else { + return; + } +)"); +} + +} // namespace +} // namespace msl +} // namespace writer +} // namespace tint diff --git a/src/writer/wgsl/generator_impl_else_test.cc b/src/writer/wgsl/generator_impl_else_test.cc deleted file mode 100644 index 47869baf7e..0000000000 --- a/src/writer/wgsl/generator_impl_else_test.cc +++ /dev/null @@ -1,65 +0,0 @@ -// 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/else_statement.h" -#include "src/ast/identifier_expression.h" -#include "src/ast/kill_statement.h" -#include "src/writer/wgsl/generator_impl.h" - -namespace tint { -namespace writer { -namespace wgsl { -namespace { - -using WgslGeneratorImplTest = testing::Test; - -TEST_F(WgslGeneratorImplTest, Emit_Else) { - ast::StatementList body; - body.push_back(std::make_unique()); - - ast::ElseStatement e(std::move(body)); - - GeneratorImpl g; - g.increment_indent(); - - ASSERT_TRUE(g.EmitElse(&e)) << g.error(); - EXPECT_EQ(g.result(), R"( else { - kill; - })"); -} - -TEST_F(WgslGeneratorImplTest, Emit_ElseWithCondition) { - auto cond = std::make_unique("cond"); - - ast::StatementList body; - body.push_back(std::make_unique()); - - ast::ElseStatement e(std::move(cond), std::move(body)); - - GeneratorImpl g; - g.increment_indent(); - - ASSERT_TRUE(g.EmitElse(&e)) << g.error(); - EXPECT_EQ(g.result(), R"( elseif (cond) { - kill; - })"); -} - -} // namespace -} // namespace wgsl -} // namespace writer -} // namespace tint