Add WGSL writer support for call.

This CL adds call expressions to the WGSL writer support.

Bug: tint:4
Change-Id: I1caa2f5f81ac2e2ab89755c1721e96d44c331853
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17060
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-19 02:51:27 +00:00 committed by dan sinclair
parent cd49b59c78
commit c80d5ed70a
4 changed files with 86 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_array_accessor_test.cc
writer/wgsl/generator_impl_as_test.cc
writer/wgsl/generator_impl_call_test.cc
writer/wgsl/generator_impl_entry_point_test.cc
writer/wgsl/generator_impl_identifier_test.cc
writer/wgsl/generator_impl_import_test.cc

View File

@ -22,6 +22,7 @@
#include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h"
#include "src/ast/builtin_decoration.h"
#include "src/ast/call_expression.h"
#include "src/ast/const_initializer_expression.h"
#include "src/ast/decorated_variable.h"
#include "src/ast/float_literal.h"
@ -122,6 +123,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsAs()) {
return EmitAs(expr->AsAs());
}
if (expr->IsCall()) {
return EmitCall(expr->AsCall());
}
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
@ -162,6 +166,30 @@ bool GeneratorImpl::EmitAs(ast::AsExpression* expr) {
return true;
}
bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
if (!EmitExpression(expr->func())) {
return false;
}
out_ << "(";
bool first = true;
const auto& params = expr->params();
for (const auto& param : params) {
if (!first) {
out_ << ", ";
}
first = false;
if (!EmitExpression(param.get())) {
return false;
}
}
out_ << ")";
return true;
}
bool GeneratorImpl::EmitInitializer(ast::InitializerExpression* expr) {
if (expr->IsConstInitializer()) {
return EmitConstInitializer(expr->AsConstInitializer());

View File

@ -78,6 +78,10 @@ class GeneratorImpl {
/// @param expr the as expression
/// @returns true if the as was emitted
bool EmitAs(ast::AsExpression* expr);
/// Handles generating a call expression
/// @param expr the call expression
/// @returns true if the call expression is emitted
bool EmitCall(ast::CallExpression* expr);
/// Handles generating a const initializer
/// @param expr the const initializer expression
/// @returns true if the initializer is emitted

View File

@ -0,0 +1,53 @@
// 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/call_expression.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, EmitExpression_Call_WithoutParams) {
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
ast::CallExpression call(std::move(id), {});
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), "my_func()");
}
TEST_F(GeneratorImplTest, EmitExpression_Call_WithParams) {
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
std::vector<std::unique_ptr<ast::Expression>> params;
params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
params.push_back(std::make_unique<ast::IdentifierExpression>("param2"));
ast::CallExpression call(std::move(id), std::move(params));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), "my_func(param1, param2)");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint