[msl-writer] Emit member accessors.

This CL adds emission of member accessor expressions to the MSL writer.

Bug: tint:8
Change-Id: I21d8564880c44d719c89dca2a10cd97ccb1e2cd7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24120
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-29 21:17:44 +00:00
parent 48bb366991
commit fa1659a3ef
5 changed files with 64 additions and 0 deletions

View File

@ -895,6 +895,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_identifier_test.cc",
"src/writer/msl/generator_impl_if_test.cc",
"src/writer/msl/generator_impl_kill_test.cc",
"src/writer/msl/generator_impl_member_accessor_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",

View File

@ -503,6 +503,7 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator_impl_identifier_test.cc
writer/msl/generator_impl_if_test.cc
writer/msl/generator_impl_kill_test.cc
writer/msl/generator_impl_member_accessor_test.cc
writer/msl/generator_impl_return_test.cc
writer/msl/generator_impl_test.cc
writer/msl/generator_impl_type_test.cc

View File

@ -25,6 +25,7 @@
#include "src/ast/function.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h"
#include "src/ast/member_accessor_expression.h"
#include "src/ast/return_statement.h"
#include "src/ast/sint_literal.h"
#include "src/ast/type/alias_type.h"
@ -304,6 +305,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
if (expr->IsMemberAccessor()) {
return EmitMemberAccessor(expr->AsMemberAccessor());
}
if (expr->IsUnaryOp()) {
return EmitUnaryOp(expr->AsUnaryOp());
}
@ -433,6 +437,17 @@ bool GeneratorImpl::EmitIf(ast::IfStatement* stmt) {
return true;
}
bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) {
if (!EmitExpression(expr->structure())) {
return false;
}
out_ << ".";
return EmitExpression(expr->member());
}
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
make_indent();

View File

@ -96,6 +96,10 @@ class GeneratorImpl : public TextGenerator {
/// @param lit the literal to emit
/// @returns true if the literal was successfully emitted
bool EmitLiteral(ast::Literal* lit);
/// Handles a member accessor expression
/// @param expr the member accessor expression
/// @returns true if the member accessor was emitted
bool EmitMemberAccessor(ast::MemberAccessorExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully 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 "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/member_accessor_expression.h"
#include "src/writer/msl/generator_impl.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslGeneratorImplTest = testing::Test;
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) {
auto str = std::make_unique<ast::IdentifierExpression>("str");
auto mem = std::make_unique<ast::IdentifierExpression>("mem");
ast::MemberAccessorExpression expr(std::move(str), std::move(mem));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), "str.mem");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint