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 <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-19 02:53:30 +00:00 committed by dan sinclair
parent c80d5ed70a
commit 62a2002b4e
4 changed files with 67 additions and 0 deletions

View File

@ -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

View File

@ -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());

View File

@ -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

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/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<ast::IdentifierExpression>("id");
ast::CastExpression cast(&f32, std::move(id));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
EXPECT_EQ(g.result(), "cast<f32>(id)");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint