Emit member accessor in WGSL generator.

This CL adds MemberAccessor handling into the WGSL generator.

Bug: tint:4
Change-Id: Ib84f9ca1969fec48f167433af957dc1ed4de3415
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17063
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-19 02:55:19 +00:00 committed by dan sinclair
parent 62a2002b4e
commit 2c56dd9099
4 changed files with 62 additions and 0 deletions

View File

@ -413,6 +413,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_identifier_test.cc writer/wgsl/generator_impl_identifier_test.cc
writer/wgsl/generator_impl_import_test.cc writer/wgsl/generator_impl_import_test.cc
writer/wgsl/generator_impl_initializer_test.cc writer/wgsl/generator_impl_initializer_test.cc
writer/wgsl/generator_impl_member_accessor_test.cc
writer/wgsl/generator_impl_type_test.cc writer/wgsl/generator_impl_type_test.cc
writer/wgsl/generator_impl_variable_test.cc writer/wgsl/generator_impl_variable_test.cc
) )

View File

@ -31,6 +31,7 @@
#include "src/ast/initializer_expression.h" #include "src/ast/initializer_expression.h"
#include "src/ast/int_literal.h" #include "src/ast/int_literal.h"
#include "src/ast/location_decoration.h" #include "src/ast/location_decoration.h"
#include "src/ast/member_accessor_expression.h"
#include "src/ast/set_decoration.h" #include "src/ast/set_decoration.h"
#include "src/ast/struct.h" #include "src/ast/struct.h"
#include "src/ast/struct_member.h" #include "src/ast/struct_member.h"
@ -136,6 +137,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsInitializer()) { if (expr->IsInitializer()) {
return EmitInitializer(expr->AsInitializer()); return EmitInitializer(expr->AsInitializer());
} }
if (expr->IsMemberAccessor()) {
return EmitMemberAccessor(expr->AsMemberAccessor());
}
error_ = "unknown expression type"; error_ = "unknown expression type";
return false; return false;
@ -155,6 +159,16 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) {
return true; return true;
} }
bool GeneratorImpl::EmitMemberAccessor(ast::MemberAccessorExpression* expr) {
if (!EmitExpression(expr->structure())) {
return false;
}
out_ << ".";
return EmitExpression(expr->member());
}
bool GeneratorImpl::EmitAs(ast::AsExpression* expr) { bool GeneratorImpl::EmitAs(ast::AsExpression* expr) {
out_ << "as<"; out_ << "as<";
if (!EmitType(expr->type())) { if (!EmitType(expr->type())) {

View File

@ -110,6 +110,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 member accessor expression
/// @param expr the member accessor expression
/// @returns true if the member accessor was emitted
bool EmitMemberAccessor(ast::MemberAccessorExpression* expr);
/// 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 "gtest/gtest.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/member_accessor_expression.h"
#include "src/writer/wgsl/generator_impl.h"
namespace tint {
namespace writer {
namespace wgsl {
namespace {
using GeneratorImplTest = testing::Test;
TEST_F(GeneratorImplTest, 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 wgsl
} // namespace writer
} // namespace tint