[metal-writer] Emit assignment statements.

This CL adds assignment statements to the Metal backend.

Bug: tint:8
Change-Id: Iaf4faa62124948fd0e785d5bebd20aae778ba050
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23705
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-23 17:54:33 +00:00
parent e66d6a6745
commit 70c605fc47
5 changed files with 73 additions and 0 deletions

View File

@ -885,6 +885,7 @@ source_set("tint_unittests_wgsl_writer_src") {
source_set("tint_unittests_msl_writer_src") {
sources = [
"src/writer/msl/generator_impl_assign_test.cc",
"src/writer/msl/generator_impl_function_test.cc",
"src/writer/msl/generator_impl_identifier_test.cc",
"src/writer/msl/generator_impl_return_test.cc",

View File

@ -493,6 +493,7 @@ endif()
if(${TINT_BUILD_MSL_WRITER})
list(APPEND TINT_TEST_SRCS
writer/msl/generator_impl_assign_test.cc
writer/msl/generator_impl_function_test.cc
writer/msl/generator_impl_identifier_test.cc
writer/msl/generator_impl_return_test.cc

View File

@ -14,6 +14,7 @@
#include "src/writer/msl/generator_impl.h"
#include "src/ast/assignment_statement.h"
#include "src/ast/function.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/return_statement.h"
@ -47,6 +48,24 @@ bool GeneratorImpl::Generate(const ast::Module& module) {
return true;
}
bool GeneratorImpl::EmitAssign(ast::AssignmentStatement* stmt) {
make_indent();
if (!EmitExpression(stmt->lhs())) {
return false;
}
out_ << " = ";
if (!EmitExpression(stmt->rhs())) {
return false;
}
out_ << ";" << std::endl;
return true;
}
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
@ -138,6 +157,9 @@ bool GeneratorImpl::EmitStatementBlockAndNewline(
}
bool GeneratorImpl::EmitStatement(ast::Statement* stmt) {
if (stmt->IsAssign()) {
return EmitAssign(stmt->AsAssign());
}
if (stmt->IsReturn()) {
return EmitReturn(stmt->AsReturn());
}

View File

@ -37,6 +37,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true on successful generation; false otherwise
bool Generate(const ast::Module& module);
/// Handles an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitAssign(ast::AssignmentStatement* stmt);
/// Handles generate an Expression
/// @param expr the expression
/// @returns true if the expression was emitted

View File

@ -0,0 +1,45 @@
// 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/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, 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;
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
EXPECT_EQ(g.result(), " lhs = rhs;\n");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint