Emit assignment statement.

This CL updates the WGSL writer to emit the assignment statement nodes.

Bug: tint:4
Change-Id: I8a52f4e96c61ecb9e97cd3da41f4a1aa718e8d37
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17162
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-20 19:01:57 +00:00 committed by dan sinclair
parent daff3e2616
commit 7f9d570a5a
4 changed files with 79 additions and 0 deletions

View File

@ -407,6 +407,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_alias_type_test.cc writer/wgsl/generator_impl_alias_type_test.cc
writer/wgsl/generator_impl_array_accessor_test.cc writer/wgsl/generator_impl_array_accessor_test.cc
writer/wgsl/generator_impl_as_test.cc writer/wgsl/generator_impl_as_test.cc
writer/wgsl/generator_impl_assign_test.cc
writer/wgsl/generator_impl_call_test.cc writer/wgsl/generator_impl_call_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

View File

@ -19,6 +19,7 @@
#include "src/ast/array_accessor_expression.h" #include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h" #include "src/ast/as_expression.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/binding_decoration.h" #include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/builtin_decoration.h" #include "src/ast/builtin_decoration.h"
@ -34,6 +35,7 @@
#include "src/ast/member_accessor_expression.h" #include "src/ast/member_accessor_expression.h"
#include "src/ast/relational_expression.h" #include "src/ast/relational_expression.h"
#include "src/ast/set_decoration.h" #include "src/ast/set_decoration.h"
#include "src/ast/statement.h"
#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"
@ -639,6 +641,31 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) {
return true; return true;
} }
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign());
}
error_ = "unknown statement type";
return false;
}
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
if (!EmitExpression(stmt->lhs())) {
return false;
}
out_ << " = ";
if (!EmitExpression(stmt->rhs())) {
return false;
}
out_ << ";";
return true;
}
} // namespace wgsl } // namespace wgsl
} // namespace writer } // namespace writer
} // namespace tint } // namespace tint

View File

@ -78,6 +78,10 @@ class GeneratorImpl {
/// @param expr the as expression /// @param expr the as expression
/// @returns true if the as was emitted /// @returns true if the as was emitted
bool EmitAs(ast::AsExpression* expr); bool EmitAs(ast::AsExpression* expr);
/// Handles an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitAssign(ast::AssignmentStatement* stmt);
/// Handles generating a call expression /// Handles generating a call expression
/// @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
@ -118,6 +122,10 @@ class GeneratorImpl {
/// @param expr the relational expression /// @param expr the relational expression
/// @returns true if the expression was emitted, false otherwise /// @returns true if the expression was emitted, false otherwise
bool EmitRelational(ast::RelationalExpression* expr); bool EmitRelational(ast::RelationalExpression* expr);
/// Handles statements
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* 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

View File

@ -0,0 +1,43 @@
// 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/assignment_statement.h"
#include "src/ast/identifier_expression.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using GeneratorImplTest = testing::Test;
TEST_F(GeneratorImplTest, Emit_Assign) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
GeneratorImpl g;
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
EXPECT_EQ(g.result(), "lhs = rhs;");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint