diff --git a/BUILD.gn b/BUILD.gn index c2c957bad0..6401ca3886 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1059,6 +1059,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_test.cc", "src/writer/hlsl/generator_impl_type_test.cc", "src/writer/hlsl/generator_impl_unary_op_test.cc", + "src/writer/hlsl/generator_impl_variable_decl_statement_test.cc", "src/writer/hlsl/namer_test.cc", ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d5302aec1..54db20b49f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -567,6 +567,7 @@ if (${TINT_BUILD_HLSL_WRITER}) writer/hlsl/generator_impl_test.cc writer/hlsl/generator_impl_type_test.cc writer/hlsl/generator_impl_unary_op_test.cc + writer/hlsl/generator_impl_variable_decl_statement_test.cc writer/hlsl/namer_test.cc ) endif() diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index aec13ec3f4..035b335d37 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -36,6 +36,7 @@ #include "src/ast/type/vector_type.h" #include "src/ast/uint_literal.h" #include "src/ast/unary_op_expression.h" +#include "src/ast/variable_decl_statement.h" namespace tint { namespace writer { @@ -544,6 +545,9 @@ bool GeneratorImpl::EmitStatement(ast::Statement* stmt) { if (stmt->IsSwitch()) { return EmitSwitch(stmt->AsSwitch()); } + if (stmt->IsVariableDecl()) { + return EmitVariable(stmt->AsVariableDecl()->variable()); + } error_ = "unknown statement type: " + stmt->str(); return false; @@ -685,6 +689,36 @@ bool GeneratorImpl::EmitUnaryOp(ast::UnaryOpExpression* expr) { return true; } +bool GeneratorImpl::EmitVariable(ast::Variable* var) { + make_indent(); + + // TODO(dsinclair): Handle variable decorations + if (var->IsDecorated()) { + error_ = "Variable decorations are not handled yet"; + return false; + } + + if (var->is_const()) { + out_ << "const "; + } + if (!EmitType(var->type(), var->name())) { + return false; + } + if (!var->type()->IsArray()) { + out_ << " " << var->name(); + } + + if (var->constructor() != nullptr) { + out_ << " = "; + if (!EmitExpression(var->constructor())) { + return false; + } + } + out_ << ";" << std::endl; + + return true; +} + } // namespace hlsl } // namespace writer } // namespace tint diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index d2cb4f0df3..8c60eb2b9a 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -139,6 +139,10 @@ class GeneratorImpl : public TextGenerator { /// @param type the type to emit the value for /// @returns true if the zero value was successfully emitted. bool EmitZeroValue(ast::type::Type* type); + /// Handles generating a variable + /// @param var the variable to generate + /// @returns true if the variable was emitted + bool EmitVariable(ast::Variable* var); /// Checks if the global variable is in an input or output struct /// @param var the variable to check diff --git a/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc new file mode 100644 index 0000000000..961beefeca --- /dev/null +++ b/src/writer/hlsl/generator_impl_variable_decl_statement_test.cc @@ -0,0 +1,154 @@ +// 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 + +#include "gtest/gtest.h" +#include "src/ast/identifier_expression.h" +#include "src/ast/module.h" +#include "src/ast/type/array_type.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type/vector_type.h" +#include "src/ast/variable.h" +#include "src/ast/variable_decl_statement.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslGeneratorImplTest = testing::Test; + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement) { + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &f32); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " float a;\n"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Const) { + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &f32); + var->set_is_const(true); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " const float a;\n"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) { + ast::type::F32Type f32; + ast::type::ArrayType ary(&f32, 5); + + auto var = + std::make_unique("a", ast::StorageClass::kNone, &ary); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " float a[5];\n"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Function) { + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kFunction, &f32); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " float a;\n"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Private) { + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kPrivate, &f32); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " float a;\n"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) { + auto ident = std::make_unique("initializer"); + + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &f32); + var->set_constructor(std::move(ident)); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), R"(float a = initializer; +)"); +} + +TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) { + ast::type::F32Type f32; + ast::type::VectorType vec(&f32, 3); + + ast::ExpressionList values; + auto zero_vec = + std::make_unique(&vec, std::move(values)); + + auto var = + std::make_unique("a", ast::StorageClass::kNone, &vec); + var->set_constructor(std::move(zero_vec)); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), R"(vector a = vector(0.0f); +)"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint diff --git a/src/writer/msl/generator_impl_variable_decl_statement_test.cc b/src/writer/msl/generator_impl_variable_decl_statement_test.cc index 043ef7a729..8e14e4d768 100644 --- a/src/writer/msl/generator_impl_variable_decl_statement_test.cc +++ b/src/writer/msl/generator_impl_variable_decl_statement_test.cc @@ -47,6 +47,22 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement) { EXPECT_EQ(g.result(), " float a;\n"); } +TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) { + ast::type::F32Type f32; + auto var = + std::make_unique("a", ast::StorageClass::kNone, &f32); + var->set_is_const(true); + + ast::VariableDeclStatement stmt(std::move(var)); + + ast::Module m; + GeneratorImpl g(&m); + g.increment_indent(); + + ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); + EXPECT_EQ(g.result(), " const float a;\n"); +} + TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) { ast::type::F32Type f32; ast::type::ArrayType ary(&f32, 5);