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>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8731960..7279687 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt
@@ -407,6 +407,7 @@ 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
diff --git a/src/writer/wgsl/generator_impl.cc b/src/writer/wgsl/generator_impl.cc index e0257f3..ed3a1bf 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc
@@ -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 @@ 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 @@ 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());
diff --git a/src/writer/wgsl/generator_impl.h b/src/writer/wgsl/generator_impl.h index 25203d9..d03b8f6 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h
@@ -78,6 +78,10 @@ /// @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
diff --git a/src/writer/wgsl/generator_impl_call_test.cc b/src/writer/wgsl/generator_impl_call_test.cc new file mode 100644 index 0000000..639c231 --- /dev/null +++ b/src/writer/wgsl/generator_impl_call_test.cc
@@ -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