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 <dneto@google.com>
This commit is contained in:
parent
bf5ab65e98
commit
37f4fb0f75
|
@ -410,6 +410,7 @@ if(${TINT_BUILD_WGSL_WRITER})
|
||||||
writer/wgsl/generator_impl_assign_test.cc
|
writer/wgsl/generator_impl_assign_test.cc
|
||||||
writer/wgsl/generator_impl_break_test.cc
|
writer/wgsl/generator_impl_break_test.cc
|
||||||
writer/wgsl/generator_impl_call_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_cast_test.cc
|
||||||
writer/wgsl/generator_impl_entry_point_test.cc
|
writer/wgsl/generator_impl_entry_point_test.cc
|
||||||
writer/wgsl/generator_impl_identifier_test.cc
|
writer/wgsl/generator_impl_identifier_test.cc
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include "src/ast/break_statement.h"
|
#include "src/ast/break_statement.h"
|
||||||
#include "src/ast/builtin_decoration.h"
|
#include "src/ast/builtin_decoration.h"
|
||||||
#include "src/ast/call_expression.h"
|
#include "src/ast/call_expression.h"
|
||||||
|
#include "src/ast/case_statement.h"
|
||||||
#include "src/ast/cast_expression.h"
|
#include "src/ast/cast_expression.h"
|
||||||
#include "src/ast/const_initializer_expression.h"
|
#include "src/ast/const_initializer_expression.h"
|
||||||
#include "src/ast/decorated_variable.h"
|
#include "src/ast/decorated_variable.h"
|
||||||
|
@ -274,7 +275,10 @@ bool GeneratorImpl::EmitTypeInitializer(ast::TypeInitializerExpression* expr) {
|
||||||
|
|
||||||
bool GeneratorImpl::EmitConstInitializer(
|
bool GeneratorImpl::EmitConstInitializer(
|
||||||
ast::ConstInitializerExpression* expr) {
|
ast::ConstInitializerExpression* expr) {
|
||||||
auto lit = expr->literal();
|
return EmitLiteral(expr->literal());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitLiteral(ast::Literal* lit) {
|
||||||
if (lit->IsBool()) {
|
if (lit->IsBool()) {
|
||||||
out_ << (lit->AsBool()->IsTrue() ? "true" : "false");
|
out_ << (lit->AsBool()->IsTrue() ? "true" : "false");
|
||||||
} else if (lit->IsFloat()) {
|
} else if (lit->IsFloat()) {
|
||||||
|
@ -649,6 +653,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsBreak()) {
|
if (stmt->IsBreak()) {
|
||||||
return EmitBreak(stmt->AsBreak());
|
return EmitBreak(stmt->AsBreak());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsCase()) {
|
||||||
|
return EmitCase(stmt->AsCase());
|
||||||
|
}
|
||||||
|
|
||||||
error_ = "unknown statement type";
|
error_ = "unknown statement type";
|
||||||
return false;
|
return false;
|
||||||
|
@ -667,7 +674,7 @@ bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
out_ << ";";
|
out_ << ";" << std::endl;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -692,7 +699,36 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement* stmt) {
|
||||||
out_ << ")";
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,10 @@ class GeneratorImpl {
|
||||||
/// @param expr the call expression
|
/// @param expr the call expression
|
||||||
/// @returns true if the call expression is emitted
|
/// @returns true if the call expression is emitted
|
||||||
bool EmitCall(ast::CallExpression* expr);
|
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
|
/// Handles generating a cast expression
|
||||||
/// @param expr the cast expression
|
/// @param expr the cast expression
|
||||||
/// @returns true if the cast was emitted
|
/// @returns true if the cast was emitted
|
||||||
|
@ -118,6 +122,10 @@ class GeneratorImpl {
|
||||||
/// @param expr the initializer expression
|
/// @param expr the initializer expression
|
||||||
/// @returns true if the expression was emitted
|
/// @returns true if the expression was emitted
|
||||||
bool EmitInitializer(ast::InitializerExpression* expr);
|
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
|
/// Handles a member accessor expression
|
||||||
/// @param expr the member accessor expression
|
/// @param expr the member accessor expression
|
||||||
/// @returns true if the member accessor was emitted
|
/// @returns true if the member accessor was emitted
|
||||||
|
|
|
@ -36,7 +36,7 @@ TEST_F(GeneratorImplTest, Emit_Assign) {
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||||
EXPECT_EQ(g.result(), " lhs = rhs;");
|
EXPECT_EQ(g.result(), " lhs = rhs;\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -34,7 +34,7 @@ TEST_F(GeneratorImplTest, Emit_Break) {
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
||||||
EXPECT_EQ(g.result(), " break;");
|
EXPECT_EQ(g.result(), " break;\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(GeneratorImplTest, Emit_BreakWithIf) {
|
TEST_F(GeneratorImplTest, Emit_BreakWithIf) {
|
||||||
|
@ -45,7 +45,7 @@ TEST_F(GeneratorImplTest, Emit_BreakWithIf) {
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
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) {
|
TEST_F(GeneratorImplTest, Emit_BreakWithUnless) {
|
||||||
|
@ -56,7 +56,7 @@ TEST_F(GeneratorImplTest, Emit_BreakWithUnless) {
|
||||||
g.increment_indent();
|
g.increment_indent();
|
||||||
|
|
||||||
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
||||||
EXPECT_EQ(g.result(), " break unless (expr);");
|
EXPECT_EQ(g.result(), " break unless (expr);\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -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 <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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<ast::IntLiteral>(5);
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::Statement>> body;
|
||||||
|
body.push_back(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
|
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<std::unique_ptr<ast::Statement>> body;
|
||||||
|
body.push_back(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in New Issue