Add generation of the 'as' expression.

This CL adds 'as' generation into the WGSL generator.

Bug: tint:4
Change-Id: Ica9e615e30aae1882681d8da74b26a51cff6dc30
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16880
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-03-18 20:33:00 +00:00 committed by dan sinclair
parent 4a19eeff2c
commit cd49b59c78
4 changed files with 67 additions and 0 deletions

View File

@ -406,6 +406,7 @@ if(${TINT_BUILD_WGSL_WRITER})
writer/wgsl/generator_impl_test.cc
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_entry_point_test.cc
writer/wgsl/generator_impl_identifier_test.cc
writer/wgsl/generator_impl_import_test.cc

View File

@ -18,6 +18,7 @@
#include <limits>
#include "src/ast/array_accessor_expression.h"
#include "src/ast/as_expression.h"
#include "src/ast/binding_decoration.h"
#include "src/ast/bool_literal.h"
#include "src/ast/builtin_decoration.h"
@ -118,6 +119,9 @@ bool GeneratorImpl::EmitExpression(ast::Expression* expr) {
if (expr->IsArrayAccessor()) {
return EmitArrayAccessor(expr->AsArrayAccessor());
}
if (expr->IsAs()) {
return EmitAs(expr->AsAs());
}
if (expr->IsIdentifier()) {
return EmitIdentifier(expr->AsIdentifier());
}
@ -143,6 +147,21 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) {
return true;
}
bool GeneratorImpl::EmitAs(ast::AsExpression* expr) {
out_ << "as<";
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

@ -74,6 +74,10 @@ class GeneratorImpl {
/// @param expr the expression to emit
/// @returns true if the array accessor was emitted
bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr);
/// Handles generating an as expression
/// @param expr the as expression
/// @returns true if the as was emitted
bool EmitAs(ast::AsExpression* 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/as_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_As) {
ast::type::F32Type f32;
auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::AsExpression as(&f32, std::move(id));
GeneratorImpl g;
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
EXPECT_EQ(g.result(), "as<f32>(id)");
}
} // namespace
} // namespace wgsl
} // namespace writer
} // namespace tint