[metal-writer] Add identifier expression.

This CL adds the start of identifier expressions to the Metal backend.
Identifiers with paths are currently not supported.

Bug: tint:8
Change-Id: I4df8b6a3c32251d454d3dae5fa8933dad36094f8
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/23703
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-06-23 17:49:08 +00:00
parent 8cd87513b3
commit 646fb23c1f
6 changed files with 78 additions and 2 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_identifier_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

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

View File

@ -14,6 +14,7 @@
#include "src/writer/msl/generator_impl.h"
#include "src/ast/identifier_expression.h"
#include "src/ast/return_statement.h"
#include "src/ast/type/alias_type.h"
#include "src/ast/type/array_type.h"
@ -39,11 +40,26 @@ bool GeneratorImpl::Generate(const ast::Module&) {
return true;
}
bool GeneratorImpl::EmitExpression(ast::Expression*) {
bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
error_ = "unknown expression type";
return false;
}
bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
auto* ident = expr->AsIdentifier();
if (ident->has_path()) {
// TODO(dsinclair): Handle identifier with path
error_ = "Identifier paths not handled yet.";
return false;
}
out_ << ident->name();
return true;
}
bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
make_indent();

View File

@ -41,6 +41,10 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the expression
/// @returns true if the expression was emitted
bool EmitExpression(ast::Expression* expr);
/// Handles generating an identifier expression
/// @param expr the identifier expression
/// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted

View File

@ -0,0 +1,54 @@
// 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 "gtest/gtest.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, DISABLED_EmitExpression_Identifier) {
ast::IdentifierExpression i(std::vector<std::string>{"std", "glsl"});
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "std::glsl");
}
TEST_F(MslGeneratorImplTest, EmitIdentifierExpression_Single) {
ast::IdentifierExpression i("foo");
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "foo");
}
// TODO(dsinclair): Handle import names
TEST_F(MslGeneratorImplTest, DISABLED_EmitIdentifierExpression_MultipleNames) {
ast::IdentifierExpression i({"std", "glsl", "init"});
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "std::glsl::init");
}
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -37,7 +37,7 @@ TEST_F(MslGeneratorImplTest, Emit_Return) {
EXPECT_EQ(g.result(), " return;\n");
}
TEST_F(MslGeneratorImplTest, DISABLED_Emit_ReturnWithValue) {
TEST_F(MslGeneratorImplTest, Emit_ReturnWithValue) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ReturnStatement r(std::move(expr));