From 62a2002b4e2b99180e1214b9076fcecdfc0397b5 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Thu, 19 Mar 2020 02:53:30 +0000 Subject: [PATCH] Emit Cast expression. This CL adds Cast Expression emission to the WGSL generator. Bug: tint:4 Change-Id: Ib48290d53423be5770cfb8f1b5f1ccb93b53a4df Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/17062 Reviewed-by: David Neto --- src/CMakeLists.txt | 1 + src/writer/wgsl/generator_impl.cc | 19 +++++++++ src/writer/wgsl/generator_impl.h | 4 ++ src/writer/wgsl/generator_impl_cast_test.cc | 43 +++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/writer/wgsl/generator_impl_cast_test.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7279687ef5..b992b6071e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -408,6 +408,7 @@ if(${TINT_BUILD_WGSL_WRITER}) 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_cast_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 ed3a1bf8b5..b72039e7b7 100644 --- a/src/writer/wgsl/generator_impl.cc +++ b/src/writer/wgsl/generator_impl.cc @@ -23,6 +23,7 @@ #include "src/ast/bool_literal.h" #include "src/ast/builtin_decoration.h" #include "src/ast/call_expression.h" +#include "src/ast/cast_expression.h" #include "src/ast/const_initializer_expression.h" #include "src/ast/decorated_variable.h" #include "src/ast/float_literal.h" @@ -126,6 +127,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) { if (expr->IsCall()) { return EmitCall(expr->AsCall()); } + if (expr->IsCast()) { + return EmitCast(expr->AsCast()); + } if (expr->IsIdentifier()) { return EmitIdentifier(expr->AsIdentifier()); } @@ -190,6 +194,21 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) { return true; } +bool GeneratorImpl::EmitCast(ast::CastExpression* expr) { + out_ << "cast<"; + if (!EmitType(expr->type())) { + return false; + } + + out_ << ">("; + if (!EmitExpression(expr->expr())) { + 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 d03b8f6ec2..188b6eecf3 100644 --- a/src/writer/wgsl/generator_impl.h +++ b/src/writer/wgsl/generator_impl.h @@ -82,6 +82,10 @@ class GeneratorImpl { /// @param expr the call expression /// @returns true if the call expression is emitted bool EmitCall(ast::CallExpression* expr); + /// Handles generating a cast expression + /// @param expr the cast expression + /// @returns true if the cast was emitted + bool EmitCast(ast::CastExpression* 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_cast_test.cc b/src/writer/wgsl/generator_impl_cast_test.cc new file mode 100644 index 0000000000..f011758106 --- /dev/null +++ b/src/writer/wgsl/generator_impl_cast_test.cc @@ -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 + +#include "gtest/gtest.h" +#include "src/ast/cast_expression.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/type/f32_type.h" +#include "src/writer/wgsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace wgsl { +namespace { + +using GeneratorImplTest = testing::Test; + +TEST_F(GeneratorImplTest, EmitExpression_Cast) { + ast::type::F32Type f32; + auto id = std::make_unique("id"); + ast::CastExpression cast(&f32, std::move(id)); + + GeneratorImpl g; + ASSERT_TRUE(g.EmitExpression(&cast)) << g.error(); + EXPECT_EQ(g.result(), "cast(id)"); +} + +} // namespace +} // namespace wgsl +} // namespace writer +} // namespace tint