Emit switch statements.
This CL updates the WGSL generator to output switch statements. Bug: tint:4 Change-Id: I4450b10932960691c76e76869105ade01fdd5a79 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17282 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
4202d7d016
commit
5f64aee763
|
@ -424,6 +424,7 @@ if(${TINT_BUILD_WGSL_WRITER})
|
||||||
writer/wgsl/generator_impl_regardless_test.cc
|
writer/wgsl/generator_impl_regardless_test.cc
|
||||||
writer/wgsl/generator_impl_relational_test.cc
|
writer/wgsl/generator_impl_relational_test.cc
|
||||||
writer/wgsl/generator_impl_return_test.cc
|
writer/wgsl/generator_impl_return_test.cc
|
||||||
|
writer/wgsl/generator_impl_switch_test.cc
|
||||||
writer/wgsl/generator_impl_type_test.cc
|
writer/wgsl/generator_impl_type_test.cc
|
||||||
writer/wgsl/generator_impl_unary_derivative_test.cc
|
writer/wgsl/generator_impl_unary_derivative_test.cc
|
||||||
writer/wgsl/generator_impl_unary_method_test.cc
|
writer/wgsl/generator_impl_unary_method_test.cc
|
||||||
|
|
|
@ -44,6 +44,7 @@
|
||||||
#include "src/ast/struct.h"
|
#include "src/ast/struct.h"
|
||||||
#include "src/ast/struct_member.h"
|
#include "src/ast/struct_member.h"
|
||||||
#include "src/ast/struct_member_offset_decoration.h"
|
#include "src/ast/struct_member_offset_decoration.h"
|
||||||
|
#include "src/ast/switch_statement.h"
|
||||||
#include "src/ast/type/array_type.h"
|
#include "src/ast/type/array_type.h"
|
||||||
#include "src/ast/type/matrix_type.h"
|
#include "src/ast/type/matrix_type.h"
|
||||||
#include "src/ast/type/pointer_type.h"
|
#include "src/ast/type/pointer_type.h"
|
||||||
|
@ -679,6 +680,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
|
||||||
if (stmt->IsReturn()) {
|
if (stmt->IsReturn()) {
|
||||||
return EmitReturn(stmt->AsReturn());
|
return EmitReturn(stmt->AsReturn());
|
||||||
}
|
}
|
||||||
|
if (stmt->IsSwitch()) {
|
||||||
|
return EmitSwitch(stmt->AsSwitch());
|
||||||
|
}
|
||||||
if (stmt->IsVariable()) {
|
if (stmt->IsVariable()) {
|
||||||
return EmitVariable(stmt->AsVariable()->variable());
|
return EmitVariable(stmt->AsVariable()->variable());
|
||||||
}
|
}
|
||||||
|
@ -840,6 +844,30 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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::EmitUnless(ast::UnlessStatement* stmt) {
|
bool GeneratorImpl::EmitUnless(ast::UnlessStatement* stmt) {
|
||||||
make_indent();
|
make_indent();
|
||||||
|
|
||||||
|
|
|
@ -162,6 +162,10 @@ class GeneratorImpl {
|
||||||
/// @param stmt the statement to emit
|
/// @param stmt the statement to emit
|
||||||
/// @returns true if the statement was emitted
|
/// @returns true if the statement was emitted
|
||||||
bool EmitStatement(ast::Statement* stmt);
|
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
|
/// Handles generating type
|
||||||
/// @param type the type to generate
|
/// @param type the type to generate
|
||||||
/// @returns true if the type is emitted
|
/// @returns true if the type is emitted
|
||||||
|
|
|
@ -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/ast/switch_statement.h"
|
||||||
|
#include "src/writer/wgsl/generator_impl.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace wgsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using GeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, Emit_Switch) {
|
||||||
|
auto def = std::make_unique<ast::CaseStatement>();
|
||||||
|
std::vector<std::unique_ptr<ast::Statement>> def_body;
|
||||||
|
def_body.push_back(std::make_unique<ast::BreakStatement>());
|
||||||
|
def->set_body(std::move(def_body));
|
||||||
|
|
||||||
|
auto case_val = std::make_unique<ast::IntLiteral>(5);
|
||||||
|
std::vector<std::unique_ptr<ast::Statement>> case_body;
|
||||||
|
case_body.push_back(std::make_unique<ast::BreakStatement>());
|
||||||
|
|
||||||
|
auto case_stmt = std::make_unique<ast::CaseStatement>(std::move(case_val),
|
||||||
|
std::move(case_body));
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::CaseStatement>> body;
|
||||||
|
body.push_back(std::move(case_stmt));
|
||||||
|
body.push_back(std::move(def));
|
||||||
|
|
||||||
|
auto cond = std::make_unique<ast::IdentifierExpression>("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 wgsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
Reference in New Issue