[hlsl-writer] Refactor output emission.

This CL updates the HLSL backend to take the output stream as a
parameter. This is needed because there are cases where we have to
generate the resulting stream out of order. This will allow that to
happen.

Bug: tint:7
Change-Id: Id1877a07e536a84da0555f207d1030588d44c034
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27440
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-08-26 19:05:46 +00:00 committed by Commit Bot service account
parent bdb86723e8
commit df503f0e85
37 changed files with 1467 additions and 1707 deletions

View File

@ -1088,6 +1088,8 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_unary_op_test.cc", "src/writer/hlsl/generator_impl_unary_op_test.cc",
"src/writer/hlsl/generator_impl_variable_decl_statement_test.cc", "src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
"src/writer/hlsl/namer_test.cc", "src/writer/hlsl/namer_test.cc",
"src/writer/hlsl/test_helper.cc",
"src/writer/hlsl/test_helper.h",
] ]
configs += [ configs += [

View File

@ -599,6 +599,8 @@ if (${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/generator_impl_unary_op_test.cc
writer/hlsl/generator_impl_variable_decl_statement_test.cc writer/hlsl/generator_impl_variable_decl_statement_test.cc
writer/hlsl/namer_test.cc writer/hlsl/namer_test.cc
writer/hlsl/test_helper.cc
writer/hlsl/test_helper.h
) )
endif() endif()

View File

@ -831,6 +831,7 @@ ast::type::Type* TypeDeterminer::GetImportData(
const ast::ExpressionList& params, const ast::ExpressionList& params,
uint32_t* id) { uint32_t* id) {
if (path != "GLSL.std.450") { if (path != "GLSL.std.450") {
set_error(source, "unknown import path " + path);
return nullptr; return nullptr;
} }

View File

@ -26,7 +26,7 @@ Generator::Generator(ast::Module module)
Generator::~Generator() = default; Generator::~Generator() = default;
bool Generator::Generate() { bool Generator::Generate() {
auto ret = impl_.Generate(); auto ret = impl_.Generate(out_);
if (!ret) { if (!ret) {
error_ = impl_.error(); error_ = impl_.error();
} }
@ -34,7 +34,7 @@ bool Generator::Generate() {
} }
std::string Generator::result() const { std::string Generator::result() const {
return impl_.result(); return out_.str();
} }
std::string Generator::error() const { std::string Generator::error() const {

View File

@ -15,6 +15,7 @@
#ifndef SRC_WRITER_HLSL_GENERATOR_H_ #ifndef SRC_WRITER_HLSL_GENERATOR_H_
#define SRC_WRITER_HLSL_GENERATOR_H_ #define SRC_WRITER_HLSL_GENERATOR_H_
#include <sstream>
#include <string> #include <string>
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
@ -43,6 +44,7 @@ class Generator : public Text {
std::string error() const; std::string error() const;
private: private:
std::ostringstream out_;
GeneratorImpl impl_; GeneratorImpl impl_;
}; };

File diff suppressed because it is too large Load Diff

View File

@ -21,182 +21,247 @@
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/scope_stack.h" #include "src/scope_stack.h"
#include "src/writer/hlsl/namer.h" #include "src/writer/hlsl/namer.h"
#include "src/writer/text_generator.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
/// Implementation class for HLSL generator /// Implementation class for HLSL generator
class GeneratorImpl : public TextGenerator { class GeneratorImpl {
public: public:
/// Constructor /// Constructor
/// @param module the module to generate /// @param module the module to generate
explicit GeneratorImpl(ast::Module* module); explicit GeneratorImpl(ast::Module* module);
~GeneratorImpl(); ~GeneratorImpl();
/// Increment the emitter indent level
void increment_indent() { indent_ += 2; }
/// Decrement the emiter indent level
void decrement_indent() {
if (indent_ < 2) {
indent_ = 0;
return;
}
indent_ -= 2;
}
/// Writes the current indent to the output stream
/// @param out the output stream
void make_indent(std::ostream& out);
/// @returns the error
std::string error() const { return error_; }
/// @param out the output stream
/// @returns true on successful generation; false otherwise /// @returns true on successful generation; false otherwise
bool Generate(); bool Generate(std::ostream& out);
/// Handles generating an alias /// Handles generating an alias
/// @param out the output stream
/// @param alias the alias to generate /// @param alias the alias to generate
/// @returns true if the alias was emitted /// @returns true if the alias was emitted
bool EmitAliasType(const ast::type::AliasType* alias); bool EmitAliasType(std::ostream& out, const ast::type::AliasType* alias);
/// Handles an array accessor expression /// Handles an array accessor expression
/// @param out the output stream
/// @param expr the expression to emit /// @param expr the expression to emit
/// @returns true if the array accessor was emitted /// @returns true if the array accessor was emitted
bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr); bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr);
/// Handles generating an as expression /// Handles generating an as expression
/// @param out the output stream
/// @param expr the as expression /// @param expr the as expression
/// @returns true if the as was emitted /// @returns true if the as was emitted
bool EmitAs(ast::AsExpression* expr); bool EmitAs(std::ostream& out, ast::AsExpression* expr);
/// Handles an assignment statement /// Handles an assignment statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitAssign(ast::AssignmentStatement* stmt); bool EmitAssign(std::ostream& out, ast::AssignmentStatement* stmt);
/// Handles generating a binary expression /// Handles generating a binary expression
/// @param out the output stream
/// @param expr the binary expression /// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise /// @returns true if the expression was emitted, false otherwise
bool EmitBinary(ast::BinaryExpression* expr); bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr);
/// Handles a block statement /// Handles a block statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitBlock(const ast::BlockStatement* stmt); bool EmitBlock(std::ostream& out, const ast::BlockStatement* stmt);
/// Handles a block statement with a newline at the end /// Handles a block statement with a newline at the end
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitIndentedBlockAndNewline(ast::BlockStatement* stmt); bool EmitIndentedBlockAndNewline(std::ostream& out,
ast::BlockStatement* stmt);
/// Handles a block statement with a newline at the end /// Handles a block statement with a newline at the end
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitBlockAndNewline(const ast::BlockStatement* stmt); bool EmitBlockAndNewline(std::ostream& out, const ast::BlockStatement* stmt);
/// Handles a break statement /// Handles a break statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt); bool EmitBreak(std::ostream& out, ast::BreakStatement* stmt);
/// Handles generating a call expression /// Handles generating a call expression
/// @param out the output stream
/// @param expr the call expression /// @param expr the call expression
/// @returns true if the call expression is emitted /// @returns true if the call expression is emitted
bool EmitCall(ast::CallExpression* expr); bool EmitCall(std::ostream& out, ast::CallExpression* expr);
/// Handles a case statement /// Handles a case statement
/// @param out the output stream
/// @param stmt the statement /// @param stmt the statement
/// @returns true if the statment was emitted successfully /// @returns true if the statment was emitted successfully
bool EmitCase(ast::CaseStatement* stmt); bool EmitCase(std::ostream& out, ast::CaseStatement* stmt);
/// Handles generating a cast expression /// Handles generating a cast expression
/// @param out the output stream
/// @param expr the cast expression /// @param expr the cast expression
/// @returns true if the cast was emitted /// @returns true if the cast was emitted
bool EmitCast(ast::CastExpression* expr); bool EmitCast(std::ostream& out, ast::CastExpression* expr);
/// Handles generating constructor expressions /// Handles generating constructor expressions
/// @param out the output stream
/// @param expr the constructor expression /// @param expr the constructor expression
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitConstructor(ast::ConstructorExpression* expr); bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr);
/// Handles generating a discard statement /// Handles generating a discard statement
/// @param out the output stream
/// @param stmt the discard statement /// @param stmt the discard statement
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted
bool EmitDiscard(ast::DiscardStatement* stmt); bool EmitDiscard(std::ostream& out, ast::DiscardStatement* stmt);
/// Handles generating a scalar constructor /// Handles generating a scalar constructor
/// @param out the output stream
/// @param expr the scalar constructor expression /// @param expr the scalar constructor expression
/// @returns true if the scalar constructor is emitted /// @returns true if the scalar constructor is emitted
bool EmitScalarConstructor(ast::ScalarConstructorExpression* expr); bool EmitScalarConstructor(std::ostream& out,
ast::ScalarConstructorExpression* expr);
/// Handles emitting a type constructor /// Handles emitting a type constructor
/// @param out the output stream
/// @param expr the type constructor expression /// @param expr the type constructor expression
/// @returns true if the constructor is emitted /// @returns true if the constructor is emitted
bool EmitTypeConstructor(ast::TypeConstructorExpression* expr); bool EmitTypeConstructor(std::ostream& out,
ast::TypeConstructorExpression* expr);
/// Handles a continue statement /// Handles a continue statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully /// @returns true if the statement was emitted successfully
bool EmitContinue(ast::ContinueStatement* stmt); bool EmitContinue(std::ostream& out, ast::ContinueStatement* stmt);
/// Handles generating an else statement /// Handles generating an else statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted
bool EmitElse(ast::ElseStatement* stmt); bool EmitElse(std::ostream& out, ast::ElseStatement* stmt);
/// Handles generate an Expression /// Handles generate an Expression
/// @param out the output stream
/// @param expr the expression /// @param expr the expression
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitExpression(ast::Expression* expr); bool EmitExpression(std::ostream& out, ast::Expression* expr);
/// Handles generating a function /// Handles generating a function
/// @param out the output stream
/// @param func the function to generate /// @param func the function to generate
/// @returns true if the function was emitted /// @returns true if the function was emitted
bool EmitFunction(ast::Function* func); bool EmitFunction(std::ostream& out, ast::Function* func);
/// Internal helper for emitting functions /// Internal helper for emitting functions
/// @param out the output stream
/// @param func the function to emit /// @param func the function to emit
/// @param emit_duplicate_functions set true if we need to duplicate per entry /// @param emit_duplicate_functions set true if we need to duplicate per entry
/// point /// point
/// @param ep_name the current entry point or blank if none set /// @param ep_name the current entry point or blank if none set
/// @returns true if the function was emitted. /// @returns true if the function was emitted.
bool EmitFunctionInternal(ast::Function* func, bool EmitFunctionInternal(std::ostream& out,
ast::Function* func,
bool emit_duplicate_functions, bool emit_duplicate_functions,
const std::string& ep_name); const std::string& ep_name);
/// Handles emitting information for an entry point /// Handles emitting information for an entry point
/// @param out the output stream
/// @param ep the entry point /// @param ep the entry point
/// @returns true if the entry point data was emitted /// @returns true if the entry point data was emitted
bool EmitEntryPointData(ast::EntryPoint* ep); bool EmitEntryPointData(std::ostream& out, ast::EntryPoint* ep);
/// Handles emitting the entry point function /// Handles emitting the entry point function
/// @param out the output stream
/// @param ep the entry point /// @param ep the entry point
/// @returns true if the entry point function was emitted /// @returns true if the entry point function was emitted
bool EmitEntryPointFunction(ast::EntryPoint* ep); bool EmitEntryPointFunction(std::ostream& out, ast::EntryPoint* ep);
/// Handles an if statement /// Handles an if statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted
bool EmitIf(ast::IfStatement* stmt); bool EmitIf(std::ostream& out, ast::IfStatement* stmt);
/// Handles genreating an import expression /// Handles genreating an import expression
/// @param out the output stream
/// @param expr the expression /// @param expr the expression
/// @returns true if the expression was successfully emitted. /// @returns true if the expression was successfully emitted.
bool EmitImportFunction(ast::CallExpression* expr); bool EmitImportFunction(std::ostream& out, ast::CallExpression* expr);
/// Handles a literal /// Handles a literal
/// @param out the output stream
/// @param lit the literal to emit /// @param lit the literal to emit
/// @returns true if the literal was successfully emitted /// @returns true if the literal was successfully emitted
bool EmitLiteral(ast::Literal* lit); bool EmitLiteral(std::ostream& out, ast::Literal* lit);
/// Handles a loop statement /// Handles a loop statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted
bool EmitLoop(ast::LoopStatement* stmt); bool EmitLoop(std::ostream& out, ast::LoopStatement* stmt);
/// Handles generating an identifier expression /// Handles generating an identifier expression
/// @param out the output stream
/// @param expr the identifier expression /// @param expr the identifier expression
/// @returns true if the identifeir was emitted /// @returns true if the identifeir was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr); bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr);
/// Handles a member accessor expression /// Handles a member accessor expression
/// @param out the output stream
/// @param expr the member accessor expression /// @param expr the member accessor expression
/// @returns true if the member accessor was emitted /// @returns true if the member accessor was emitted
bool EmitMemberAccessor(ast::MemberAccessorExpression* expr); bool EmitMemberAccessor(std::ostream& out,
ast::MemberAccessorExpression* expr);
/// Handles a storage buffer accessor expression /// Handles a storage buffer accessor expression
/// @param out the output stream
/// @param expr the storage buffer accessor expression /// @param expr the storage buffer accessor expression
/// @param rhs the right side of a store expression. Set to nullptr for a load /// @param rhs the right side of a store expression. Set to nullptr for a load
/// @returns true if the storage buffer accessor was emitted /// @returns true if the storage buffer accessor was emitted
bool EmitStorageBufferAccessor(ast::Expression* expr, ast::Expression* rhs); bool EmitStorageBufferAccessor(std::ostream& out,
ast::Expression* expr,
ast::Expression* rhs);
/// Handles return statements /// Handles return statements
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted /// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt); bool EmitReturn(std::ostream& out, ast::ReturnStatement* stmt);
/// Handles statement /// Handles statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt); bool EmitStatement(std::ostream& out, ast::Statement* stmt);
/// Handles generating a switch statement /// Handles generating a switch statement
/// @param out the output stream
/// @param stmt the statement to emit /// @param stmt the statement to emit
/// @returns true if the statement was emitted /// @returns true if the statement was emitted
bool EmitSwitch(ast::SwitchStatement* stmt); bool EmitSwitch(std::ostream& out, ast::SwitchStatement* stmt);
/// Handles generating type /// Handles generating type
/// @param out the output stream
/// @param type the type to generate /// @param type the type to generate
/// @param name the name of the variable, only used for array emission /// @param name the name of the variable, only used for array emission
/// @returns true if the type is emitted /// @returns true if the type is emitted
bool EmitType(ast::type::Type* type, const std::string& name); bool EmitType(std::ostream& out,
ast::type::Type* type,
const std::string& name);
/// Handles a unary op expression /// Handles a unary op expression
/// @param out the output stream
/// @param expr the expression to emit /// @param expr the expression to emit
/// @returns true if the expression was emitted /// @returns true if the expression was emitted
bool EmitUnaryOp(ast::UnaryOpExpression* expr); bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr);
/// Emits the zero value for the given type /// Emits the zero value for the given type
/// @param out the output stream
/// @param type the type to emit the value for /// @param type the type to emit the value for
/// @returns true if the zero value was successfully emitted. /// @returns true if the zero value was successfully emitted.
bool EmitZeroValue(ast::type::Type* type); bool EmitZeroValue(std::ostream& out, ast::type::Type* type);
/// Handles generating a variable /// Handles generating a variable
/// @param out the output stream
/// @param var the variable to generate /// @param var the variable to generate
/// @returns true if the variable was emitted /// @returns true if the variable was emitted
bool EmitVariable(ast::Variable* var); bool EmitVariable(std::ostream& out, ast::Variable* var);
/// Handles generating a program scope constant variable /// Handles generating a program scope constant variable
/// @param out the output stream
/// @param var the variable to emit /// @param var the variable to emit
/// @returns true if the variable was emitted /// @returns true if the variable was emitted
bool EmitProgramConstVariable(const ast::Variable* var); bool EmitProgramConstVariable(std::ostream& out, const ast::Variable* var);
/// Returns true if the accessor is accessing a storage buffer. /// Returns true if the accessor is accessing a storage buffer.
/// @param expr the expression to check /// @param expr the expression to check
@ -252,6 +317,9 @@ class GeneratorImpl : public TextGenerator {
std::string current_ep_var_name(VarType type); std::string current_ep_var_name(VarType type);
std::string error_;
size_t indent_ = 0;
Namer namer_; Namer namer_;
ast::Module* module_ = nullptr; ast::Module* module_ = nullptr;
std::string current_ep_name_; std::string current_ep_name_;

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/struct.h" #include "src/ast/struct.h"
#include "src/ast/struct_member.h" #include "src/ast/struct_member.h"
@ -21,38 +20,35 @@
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_AliasType : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitAliasType_F32) { TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_F32) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::AliasType alias("a", &f32); ast::type::AliasType alias("a", &f32);
ast::Module m; ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(typedef float a;
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
EXPECT_EQ(g.result(), R"(typedef float a;
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitAliasType_NameCollision) { TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_NameCollision) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::AliasType alias("float", &f32); ast::type::AliasType alias("float", &f32);
ast::Module m; ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(typedef float float_tint_0;
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
EXPECT_EQ(g.result(), R"(typedef float float_tint_0;
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitAliasType_Struct) { TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_Struct) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -73,8 +69,8 @@ TEST_F(HlslGeneratorImplTest, EmitAliasType_Struct) {
ast::Module m; ast::Module m;
GeneratorImpl g(&m); GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error(); ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
EXPECT_EQ(g.result(), R"(struct a { EXPECT_EQ(result(), R"(struct a {
float a; float a;
int b; int b;
}; };

View File

@ -14,23 +14,23 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/array_accessor_expression.h" #include "src/ast/array_accessor_expression.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h" #include "src/ast/scalar_constructor_expression.h"
#include "src/ast/sint_literal.h" #include "src/ast/sint_literal.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Expression : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitExpression_ArrayAccessor) { TEST_F(HlslGeneratorImplTest_Expression, EmitExpression_ArrayAccessor) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, 5); auto lit = std::make_unique<ast::SintLiteral>(&i32, 5);
auto idx = std::make_unique<ast::ScalarConstructorExpression>(std::move(lit)); auto idx = std::make_unique<ast::ScalarConstructorExpression>(std::move(lit));
@ -38,22 +38,18 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_ArrayAccessor) {
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx)); ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "ary[5]");
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), "ary[5]");
} }
TEST_F(HlslGeneratorImplTest, EmitArrayAccessor) { TEST_F(HlslGeneratorImplTest_Expression, EmitArrayAccessor) {
auto ary = std::make_unique<ast::IdentifierExpression>("ary"); auto ary = std::make_unique<ast::IdentifierExpression>("ary");
auto idx = std::make_unique<ast::IdentifierExpression>("idx"); auto idx = std::make_unique<ast::IdentifierExpression>("idx");
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx)); ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "ary[idx]");
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), "ary[idx]");
} }
} // namespace } // namespace

View File

@ -14,53 +14,46 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/as_expression.h" #include "src/ast/as_expression.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_As : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Float) { TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Float) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto id = std::make_unique<ast::IdentifierExpression>("id"); auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::AsExpression as(&f32, std::move(id)); ast::AsExpression as(&f32, std::move(id));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "asfloat(id)");
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
EXPECT_EQ(g.result(), "asfloat(id)");
} }
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Int) { TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Int) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto id = std::make_unique<ast::IdentifierExpression>("id"); auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::AsExpression as(&i32, std::move(id)); ast::AsExpression as(&i32, std::move(id));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "asint(id)");
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
EXPECT_EQ(g.result(), "asint(id)");
} }
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Uint) { TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Uint) {
ast::type::U32Type u32; ast::type::U32Type u32;
auto id = std::make_unique<ast::IdentifierExpression>("id"); auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::AsExpression as(&u32, std::move(id)); ast::AsExpression as(&u32, std::move(id));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "asuint(id)");
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
EXPECT_EQ(g.result(), "asuint(id)");
} }
} // namespace } // namespace

View File

@ -15,30 +15,27 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Assign : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Assign) { TEST_F(HlslGeneratorImplTest_Assign, Emit_Assign) {
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs"); auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs"); auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ(g.result(), " lhs = rhs;\n"); EXPECT_EQ(result(), " lhs = rhs;\n");
} }
} // namespace } // namespace

View File

@ -14,11 +14,10 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -33,7 +32,9 @@ inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
out << data.op; out << data.op;
return out; return out;
} }
using HlslBinaryTest = testing::TestWithParam<BinaryData>;
class HlslBinaryTest : public TestHelper,
public testing::TestWithParam<BinaryData> {};
TEST_P(HlslBinaryTest, Emit) { TEST_P(HlslBinaryTest, Emit) {
auto params = GetParam(); auto params = GetParam();
@ -42,10 +43,8 @@ TEST_P(HlslBinaryTest, Emit) {
ast::BinaryExpression expr(params.op, std::move(left), std::move(right)); ast::BinaryExpression expr(params.op, std::move(left), std::move(right));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), params.result);
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest, HlslGeneratorImplTest,

View File

@ -14,43 +14,38 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/block_statement.h" #include "src/ast/block_statement.h"
#include "src/ast/discard_statement.h" #include "src/ast/discard_statement.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Block : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Block) { TEST_F(HlslGeneratorImplTest_Block, Emit_Block) {
ast::BlockStatement b; ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>()); b.append(std::make_unique<ast::DiscardStatement>());
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &b)) << gen().error();
EXPECT_EQ(g.result(), R"( { EXPECT_EQ(result(), R"( {
discard; discard;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Block_WithoutNewline) { TEST_F(HlslGeneratorImplTest_Block, Emit_Block_WithoutNewline) {
ast::BlockStatement b; ast::BlockStatement b;
b.append(std::make_unique<ast::DiscardStatement>()); b.append(std::make_unique<ast::DiscardStatement>());
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitBlock(&b)) << g.error(); ASSERT_TRUE(gen().EmitBlock(out(), &b)) << gen().error();
EXPECT_EQ(g.result(), R"({ EXPECT_EQ(result(), R"({
discard; discard;
})"); })");
} }

View File

@ -15,27 +15,24 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Break : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Break) { TEST_F(HlslGeneratorImplTest_Break, Emit_Break) {
ast::BreakStatement b; ast::BreakStatement b;
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&b)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &b)) << gen().error();
EXPECT_EQ(g.result(), " break;\n"); EXPECT_EQ(result(), " break;\n");
} }
} // namespace } // namespace

View File

@ -14,23 +14,22 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/call_expression.h" #include "src/ast/call_expression.h"
#include "src/ast/call_statement.h" #include "src/ast/call_statement.h"
#include "src/ast/function.h" #include "src/ast/function.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Call : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithoutParams) { TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithoutParams) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto id = std::make_unique<ast::IdentifierExpression>("my_func"); auto id = std::make_unique<ast::IdentifierExpression>("my_func");
@ -38,16 +37,13 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{}, auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
&void_type); &void_type);
mod()->AddFunction(std::move(func));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
m.AddFunction(std::move(func)); EXPECT_EQ(result(), "my_func()");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), "my_func()");
} }
TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithParams) { TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithParams) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto id = std::make_unique<ast::IdentifierExpression>("my_func"); auto id = std::make_unique<ast::IdentifierExpression>("my_func");
@ -58,16 +54,13 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithParams) {
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{}, auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
&void_type); &void_type);
mod()->AddFunction(std::move(func));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
m.AddFunction(std::move(func)); EXPECT_EQ(result(), "my_func(param1, param2)");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), "my_func(param1, param2)");
} }
TEST_F(HlslGeneratorImplTest, EmitStatement_Call) { TEST_F(HlslGeneratorImplTest_Call, EmitStatement_Call) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto id = std::make_unique<ast::IdentifierExpression>("my_func"); auto id = std::make_unique<ast::IdentifierExpression>("my_func");
@ -79,14 +72,10 @@ TEST_F(HlslGeneratorImplTest, EmitStatement_Call) {
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{}, auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
&void_type); &void_type);
mod()->AddFunction(std::move(func));
ast::Module m; gen().increment_indent();
m.AddFunction(std::move(func)); ASSERT_TRUE(gen().EmitStatement(out(), &call)) << gen().error();
EXPECT_EQ(result(), " my_func(param1, param2);\n");
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&call)) << g.error();
EXPECT_EQ(g.result(), " my_func(param1, param2);\n");
} }
} // namespace } // namespace

View File

@ -14,7 +14,6 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
#include "src/ast/fallthrough_statement.h" #include "src/ast/fallthrough_statement.h"
@ -22,16 +21,16 @@
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/sint_literal.h" #include "src/ast/sint_literal.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Case : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Case) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
@ -41,36 +40,32 @@ TEST_F(HlslGeneratorImplTest, Emit_Case) {
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5)); lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body)); ast::CaseStatement c(std::move(lit), std::move(body));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error(); ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), R"( case 5: { EXPECT_EQ(result(), R"( case 5: {
break; break;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Case_BreaksByDefault) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::CaseSelectorList lit; ast::CaseSelectorList lit;
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5)); lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::make_unique<ast::BlockStatement>()); ast::CaseStatement c(std::move(lit), std::make_unique<ast::BlockStatement>());
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error(); ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), R"( case 5: { EXPECT_EQ(result(), R"( case 5: {
break; break;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Case_WithFallthrough) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
@ -80,18 +75,16 @@ TEST_F(HlslGeneratorImplTest, Emit_Case_WithFallthrough) {
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5)); lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
ast::CaseStatement c(std::move(lit), std::move(body)); ast::CaseStatement c(std::move(lit), std::move(body));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error(); ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), R"( case 5: { EXPECT_EQ(result(), R"( case 5: {
/* fallthrough */ /* fallthrough */
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Case_MultipleSelectors) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
@ -102,31 +95,27 @@ TEST_F(HlslGeneratorImplTest, Emit_Case_MultipleSelectors) {
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 6)); lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 6));
ast::CaseStatement c(std::move(lit), std::move(body)); ast::CaseStatement c(std::move(lit), std::move(body));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error(); ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), R"( case 5: EXPECT_EQ(result(), R"( case 5:
case 6: { case 6: {
break; break;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Case_Default) { TEST_F(HlslGeneratorImplTest_Case, Emit_Case_Default) {
ast::CaseStatement c; ast::CaseStatement c;
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::BreakStatement>()); body->append(std::make_unique<ast::BreakStatement>());
c.set_body(std::move(body)); c.set_body(std::move(body));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitCase(&c)) << g.error(); ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), R"( default: { EXPECT_EQ(result(), R"( default: {
break; break;
} }
)"); )");

View File

@ -14,43 +14,38 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/cast_expression.h" #include "src/ast/cast_expression.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Cast : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitExpression_Cast_Scalar) { TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Scalar) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto id = std::make_unique<ast::IdentifierExpression>("id"); auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::CastExpression cast(&f32, std::move(id)); ast::CastExpression cast(&f32, std::move(id));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &cast)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "float(id)");
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
EXPECT_EQ(g.result(), "float(id)");
} }
TEST_F(HlslGeneratorImplTest, EmitExpression_Cast_Vector) { TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Vector) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3); ast::type::VectorType vec3(&f32, 3);
auto id = std::make_unique<ast::IdentifierExpression>("id"); auto id = std::make_unique<ast::IdentifierExpression>("id");
ast::CastExpression cast(&vec3, std::move(id)); ast::CastExpression cast(&vec3, std::move(id));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &cast)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "vector<float, 3>(id)");
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
EXPECT_EQ(g.result(), "vector<float, 3>(id)");
} }
} // namespace } // namespace

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/bool_literal.h" #include "src/ast/bool_literal.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
#include "src/ast/module.h" #include "src/ast/module.h"
@ -27,61 +26,54 @@
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/ast/uint_literal.h" #include "src/ast/uint_literal.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Constructor : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitConstructor_Bool) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Bool) {
ast::type::BoolType bool_type; ast::type::BoolType bool_type;
auto lit = std::make_unique<ast::BoolLiteral>(&bool_type, false); auto lit = std::make_unique<ast::BoolLiteral>(&bool_type, false);
ast::ScalarConstructorExpression expr(std::move(lit)); ast::ScalarConstructorExpression expr(std::move(lit));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "false");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "false");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Int) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Int) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345); auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
ast::ScalarConstructorExpression expr(std::move(lit)); ast::ScalarConstructorExpression expr(std::move(lit));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "-12345");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "-12345");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_UInt) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_UInt) {
ast::type::U32Type u32; ast::type::U32Type u32;
auto lit = std::make_unique<ast::UintLiteral>(&u32, 56779); auto lit = std::make_unique<ast::UintLiteral>(&u32, 56779);
ast::ScalarConstructorExpression expr(std::move(lit)); ast::ScalarConstructorExpression expr(std::move(lit));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "56779u");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "56779u");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Float) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Float) {
ast::type::F32Type f32; ast::type::F32Type f32;
// Use a number close to 1<<30 but whose decimal representation ends in 0. // Use a number close to 1<<30 but whose decimal representation ends in 0.
auto lit = std::make_unique<ast::FloatLiteral>(&f32, float((1 << 30) - 4)); auto lit = std::make_unique<ast::FloatLiteral>(&f32, float((1 << 30) - 4));
ast::ScalarConstructorExpression expr(std::move(lit)); ast::ScalarConstructorExpression expr(std::move(lit));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "1.07374182e+09f");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "1.07374182e+09f");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Float) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Float) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto lit = std::make_unique<ast::FloatLiteral>(&f32, -1.2e-5); auto lit = std::make_unique<ast::FloatLiteral>(&f32, -1.2e-5);
@ -91,13 +83,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Float) {
ast::TypeConstructorExpression expr(&f32, std::move(values)); ast::TypeConstructorExpression expr(&f32, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "float(-1.20000004e-05f)");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "float(-1.20000004e-05f)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Bool) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Bool) {
ast::type::BoolType b; ast::type::BoolType b;
auto lit = std::make_unique<ast::BoolLiteral>(&b, true); auto lit = std::make_unique<ast::BoolLiteral>(&b, true);
@ -107,13 +97,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Bool) {
ast::TypeConstructorExpression expr(&b, std::move(values)); ast::TypeConstructorExpression expr(&b, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool(true)");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "bool(true)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Int) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Int) {
ast::type::I32Type i32; ast::type::I32Type i32;
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345); auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
@ -123,13 +111,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Int) {
ast::TypeConstructorExpression expr(&i32, std::move(values)); ast::TypeConstructorExpression expr(&i32, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "int(-12345)");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "int(-12345)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Uint) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Uint) {
ast::type::U32Type u32; ast::type::U32Type u32;
auto lit = std::make_unique<ast::UintLiteral>(&u32, 12345); auto lit = std::make_unique<ast::UintLiteral>(&u32, 12345);
@ -139,13 +125,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Uint) {
ast::TypeConstructorExpression expr(&u32, std::move(values)); ast::TypeConstructorExpression expr(&u32, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "uint(12345u)");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "uint(12345u)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3); ast::type::VectorType vec(&f32, 3);
@ -162,27 +146,23 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec) {
ast::TypeConstructorExpression expr(&vec, std::move(values)); ast::TypeConstructorExpression expr(&vec, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(),
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(),
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)"); "vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec_Empty) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3); ast::type::VectorType vec(&f32, 3);
ast::ExpressionList values; ast::ExpressionList values;
ast::TypeConstructorExpression expr(&vec, std::move(values)); ast::TypeConstructorExpression expr(&vec, std::move(values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "vector<float, 3>(0.0f)");
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(), "vector<float, 3>(0.0f)");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Mat) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::MatrixType mat(&f32, 3, 2); // 3 ROWS, 2 COLUMNS ast::type::MatrixType mat(&f32, 3, 2); // 3 ROWS, 2 COLUMNS
ast::type::VectorType vec(&f32, 3); ast::type::VectorType vec(&f32, 3);
@ -214,19 +194,17 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Mat) {
ast::TypeConstructorExpression expr(&mat, std::move(mat_values)); ast::TypeConstructorExpression expr(&mat, std::move(mat_values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
// A matrix of type T with n columns and m rows can also be constructed from // A matrix of type T with n columns and m rows can also be constructed from
// n vectors of type T with m components. // n vectors of type T with m components.
EXPECT_EQ(g.result(), EXPECT_EQ(result(),
std::string("matrix<float, 3, 2>(vector<float, 3>(1.00000000f, " std::string("matrix<float, 3, 2>(vector<float, 3>(1.00000000f, "
"2.00000000f, 3.00000000f), ") + "2.00000000f, 3.00000000f), ") +
"vector<float, 3>(3.00000000f, 4.00000000f, 5.00000000f))"); "vector<float, 3>(3.00000000f, 4.00000000f, 5.00000000f))");
} }
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) { TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3); ast::type::VectorType vec(&f32, 3);
ast::type::ArrayType ary(&vec, 3); ast::type::ArrayType ary(&vec, 3);
@ -255,10 +233,8 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
ast::TypeConstructorExpression expr(&ary, std::move(ary_values)); ast::TypeConstructorExpression expr(&ary, std::move(ary_values));
ast::Module m; ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(),
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
EXPECT_EQ(g.result(),
std::string("{") + std::string("{") +
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " + "vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " +
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f), " + "vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f), " +
@ -266,7 +242,8 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
} }
// TODO(dsinclair): Add struct constructor test. // TODO(dsinclair): Add struct constructor test.
TEST_F(HlslGeneratorImplTest, DISABLED_EmitConstructor_Type_Struct) {} TEST_F(HlslGeneratorImplTest_Constructor,
DISABLED_EmitConstructor_Type_Struct) {}
} // namespace } // namespace
} // namespace hlsl } // namespace hlsl

View File

@ -15,27 +15,25 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/continue_statement.h" #include "src/ast/continue_statement.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Continue : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Continue) { TEST_F(HlslGeneratorImplTest_Continue, Emit_Continue) {
ast::ContinueStatement c; ast::ContinueStatement c;
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&c)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &c)) << gen().error();
EXPECT_EQ(g.result(), " continue;\n"); EXPECT_EQ(result(), " continue;\n");
} }
} // namespace } // namespace

View File

@ -12,27 +12,25 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/discard_statement.h" #include "src/ast/discard_statement.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Discard : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Discard) { TEST_F(HlslGeneratorImplTest_Discard, Emit_Discard) {
ast::DiscardStatement stmt; ast::DiscardStatement stmt;
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
EXPECT_EQ(g.result(), " discard;\n"); EXPECT_EQ(result(), " discard;\n");
} }
} // namespace } // namespace

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/decorated_variable.h" #include "src/ast/decorated_variable.h"
#include "src/ast/entry_point.h" #include "src/ast/entry_point.h"
@ -27,16 +26,17 @@
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/context.h" #include "src/context.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_EntryPoint : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Vertex_Input) {
// [[location 0]] var<in> foo : f32; // [[location 0]] var<in> foo : f32;
// [[location 1]] var<in> bar : i32; // [[location 1]] var<in> bar : i32;
// //
@ -60,14 +60,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -82,19 +79,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "",
"vtx_main"); "vtx_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct vtx_main_in {
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.result(), R"(struct vtx_main_in {
float foo : TEXCOORD0; float foo : TEXCOORD0;
int bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
@ -102,7 +97,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Vertex_Output) {
// [[location 0]] var<out> foo : f32; // [[location 0]] var<out> foo : f32;
// [[location 1]] var<out> bar : i32; // [[location 1]] var<out> bar : i32;
// //
@ -126,14 +121,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -148,19 +140,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "",
"vtx_main"); "vtx_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct vtx_main_out {
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.result(), R"(struct vtx_main_out {
float foo : TEXCOORD0; float foo : TEXCOORD0;
int bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
@ -168,7 +158,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Fragment_Input) {
// [[location 0]] var<in> foo : f32; // [[location 0]] var<in> foo : f32;
// [[location 1]] var<in> bar : i32; // [[location 1]] var<in> bar : i32;
// //
@ -192,14 +182,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -214,19 +201,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"main", "frag_main"); "main", "frag_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct main_in {
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.result(), R"(struct main_in {
float foo : TEXCOORD0; float foo : TEXCOORD0;
int bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
@ -234,7 +219,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Fragment_Output) {
// [[location 0]] var<out> foo : f32; // [[location 0]] var<out> foo : f32;
// [[location 1]] var<out> bar : i32; // [[location 1]] var<out> bar : i32;
// //
@ -258,14 +243,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -280,19 +262,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"main", "frag_main"); "main", "frag_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct main_out {
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.result(), R"(struct main_out {
float foo : SV_Target0; float foo : SV_Target0;
int bar : SV_Target1; int bar : SV_Target1;
}; };
@ -300,7 +280,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Compute_Input) {
// [[location 0]] var<in> foo : f32; // [[location 0]] var<in> foo : f32;
// [[location 1]] var<in> bar : i32; // [[location 1]] var<in> bar : i32;
// //
@ -321,14 +301,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -343,22 +320,20 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
"main", "comp_main"); "main", "comp_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_FALSE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(gen().error(), R"(invalid location variable for pipeline stage)");
ASSERT_FALSE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.error(), R"(invalid location variable for pipeline stage)");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Compute_Output) {
// [[location 0]] var<out> foo : f32; // [[location 0]] var<out> foo : f32;
// [[location 1]] var<out> bar : i32; // [[location 1]] var<out> bar : i32;
// //
@ -379,14 +354,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = auto func =
@ -401,22 +373,20 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) {
std::make_unique<ast::IdentifierExpression>("bar"))); std::make_unique<ast::IdentifierExpression>("bar")));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
"main", "comp_main"); "main", "comp_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_FALSE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(gen().error(), R"(invalid location variable for pipeline stage)");
ASSERT_FALSE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.error(), R"(invalid location variable for pipeline stage)");
} }
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) { TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Builtins) {
// [[builtin frag_coord]] var<in> coord : vec4<f32>; // [[builtin frag_coord]] var<in> coord : vec4<f32>;
// [[builtin frag_depth]] var<out> depth : f32; // [[builtin frag_depth]] var<out> depth : f32;
// //
@ -448,14 +418,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) {
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth)); std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
depth_var->set_decorations(std::move(decos)); depth_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; td().RegisterVariableForTesting(depth_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
td.RegisterVariableForTesting(depth_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
mod.AddGlobalVariable(std::move(depth_var)); mod()->AddGlobalVariable(std::move(depth_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -469,19 +436,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) {
std::make_unique<ast::IdentifierExpression>("x")))); std::make_unique<ast::IdentifierExpression>("x"))));
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"main", "frag_main"); "main", "frag_main");
auto* ep_ptr = ep.get(); auto* ep_ptr = ep.get();
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct main_in {
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
EXPECT_EQ(g.result(), R"(struct main_in {
vector<float, 4> coord : SV_Position; vector<float, 4> coord : SV_Position;
}; };

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
#include "src/ast/binding_decoration.h" #include "src/ast/binding_decoration.h"
@ -42,16 +41,17 @@
#include "src/ast/variable_decl_statement.h" #include "src/ast/variable_decl_statement.h"
#include "src/context.h" #include "src/context.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Function : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Function) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{}, auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
@ -61,21 +61,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); gen().increment_indent();
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), R"( void my_func() {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( void my_func() {
return; return;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_Name_Collision) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_Name_Collision) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto func = std::make_unique<ast::Function>("GeometryShader", auto func = std::make_unique<ast::Function>("GeometryShader",
@ -85,21 +82,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_Name_Collision) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); gen().increment_indent();
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), R"( void GeometryShader_tint_0() {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( void GeometryShader_tint_0() {
return; return;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_WithParams) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithParams) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::I32Type i32; ast::type::I32Type i32;
@ -117,21 +111,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_WithParams) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); gen().increment_indent();
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), R"( void my_func(float a, int b) {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( void my_func(float a, int b) {
return; return;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_NoName) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_NoName) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto func = std::make_unique<ast::Function>("frag_main", ast::VariableList{}, auto func = std::make_unique<ast::Function>("frag_main", ast::VariableList{},
@ -139,19 +130,17 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_NoName) {
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); mod()->AddEntryPoint(std::move(ep));
m.AddEntryPoint(std::move(ep));
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
ASSERT_TRUE(g.Generate()) << g.error(); EXPECT_EQ(result(), R"(void frag_main() {
EXPECT_EQ(g.result(), R"(void frag_main() {
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_WithInOutVars) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -167,14 +156,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) {
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -187,17 +173,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct frag_main_in {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct frag_main_in {
float foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
@ -214,7 +198,8 @@ frag_main_out frag_main(frag_main_in tint_in) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) { TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_EntryPoint_WithInOut_Builtins) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec4(&f32, 4); ast::type::VectorType vec4(&f32, 4);
@ -235,14 +220,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) {
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth)); std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
depth_var->set_decorations(std::move(decos)); depth_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; td().RegisterVariableForTesting(depth_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
td.RegisterVariableForTesting(depth_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
mod.AddGlobalVariable(std::move(depth_var)); mod()->AddGlobalVariable(std::move(depth_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -257,17 +239,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct frag_main_in {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct frag_main_in {
vector<float, 4> coord : SV_Position; vector<float, 4> coord : SV_Position;
}; };
@ -284,7 +264,7 @@ frag_main_out frag_main(frag_main_in tint_in) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_With_Uniform) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec4(&f32, 4); ast::type::VectorType vec4(&f32, 4);
@ -298,12 +278,8 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) {
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; mod()->AddGlobalVariable(std::move(coord_var));
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -320,17 +296,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(cbuffer : register(b0) {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(cbuffer : register(b0) {
vector<float, 4> coord; vector<float, 4> coord;
}; };
@ -342,7 +316,8 @@ void frag_main() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) { TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_EntryPoint_With_UniformStruct) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec4(&f32, 4); ast::type::VectorType vec4(&f32, 4);
@ -358,23 +333,19 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) {
s.set_name("Uniforms"); s.set_name("Uniforms");
auto alias = std::make_unique<ast::type::AliasType>("Uniforms", &s); auto alias = std::make_unique<ast::type::AliasType>("Uniforms", &s);
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
auto coord_var = auto coord_var =
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"uniforms", ast::StorageClass::kUniform, alias.get())); "uniforms", ast::StorageClass::kUniform, alias.get()));
mod.AddAliasType(alias.get()); mod()->AddAliasType(alias.get());
ast::VariableDecorationList decos; ast::VariableDecorationList decos;
decos.push_back(std::make_unique<ast::BindingDecoration>(0)); decos.push_back(std::make_unique<ast::BindingDecoration>(0));
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
td.RegisterVariableForTesting(coord_var.get()); td().RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -393,17 +364,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct Uniforms {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct Uniforms {
vector<float, 4> coord; vector<float, 4> coord;
}; };
@ -417,7 +386,7 @@ void frag_main() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_EntryPoint_With_StorageBuffer_Read) { Emit_Function_EntryPoint_With_StorageBuffer_Read) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -449,11 +418,8 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; mod()->AddGlobalVariable(std::move(coord_var));
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -470,17 +436,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(RWByteAddressBuffer coord : register(u0);
void frag_main() { void frag_main() {
float v = asfloat(coord.Load(4)); float v = asfloat(coord.Load(4));
@ -490,7 +454,7 @@ void frag_main() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_EntryPoint_With_StorageBuffer_Store) { Emit_Function_EntryPoint_With_StorageBuffer_Store) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -522,12 +486,9 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
@ -545,17 +506,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(RWByteAddressBuffer coord : register(u0);
void frag_main() { void frag_main() {
coord.Store(4, asuint(2.00000000f)); coord.Store(4, asuint(2.00000000f));
@ -565,7 +524,7 @@ void frag_main() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_Called_By_EntryPoints_WithLocationGlobals_And_Params) { Emit_Function_Called_By_EntryPoints_WithLocationGlobals_And_Params) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -587,16 +546,13 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::LocationDecoration>(0)); decos.push_back(std::make_unique<ast::LocationDecoration>(0));
val_var->set_decorations(std::move(decos)); val_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod); td().RegisterVariableForTesting(val_var.get());
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
td.RegisterVariableForTesting(val_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
mod.AddGlobalVariable(std::move(val_var)); mod()->AddGlobalVariable(std::move(val_var));
ast::VariableList params; ast::VariableList params;
params.push_back(std::make_unique<ast::Variable>( params.push_back(std::make_unique<ast::Variable>(
@ -615,7 +571,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("foo"))); std::make_unique<ast::IdentifierExpression>("foo")));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
std::move(params), &void_type); std::move(params), &void_type);
@ -633,17 +589,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct ep_1_in {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct ep_1_in {
float foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
@ -667,7 +621,7 @@ ep_1_out ep_1(ep_1_in tint_in) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_Called_By_EntryPoints_NoUsedGlobals) { Emit_Function_Called_By_EntryPoints_NoUsedGlobals) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -682,12 +636,9 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth)); std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
depth_var->set_decorations(std::move(decos)); depth_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(depth_var.get());
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(depth_var.get());
mod.AddGlobalVariable(std::move(depth_var)); mod()->AddGlobalVariable(std::move(depth_var));
ast::VariableList params; ast::VariableList params;
params.push_back(std::make_unique<ast::Variable>( params.push_back(std::make_unique<ast::Variable>(
@ -700,7 +651,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("param"))); std::make_unique<ast::IdentifierExpression>("param")));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
std::move(params), &void_type); std::move(params), &void_type);
@ -718,17 +669,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct ep_1_out {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct ep_1_out {
float depth : SV_Depth; float depth : SV_Depth;
}; };
@ -745,7 +694,7 @@ ep_1_out ep_1() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_Called_By_EntryPoints_WithBuiltinGlobals_And_Params) { Emit_Function_Called_By_EntryPoints_WithBuiltinGlobals_And_Params) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -767,14 +716,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth)); std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
depth_var->set_decorations(std::move(decos)); depth_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; td().RegisterVariableForTesting(depth_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
td.RegisterVariableForTesting(depth_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
mod.AddGlobalVariable(std::move(depth_var)); mod()->AddGlobalVariable(std::move(depth_var));
ast::VariableList params; ast::VariableList params;
params.push_back(std::make_unique<ast::Variable>( params.push_back(std::make_unique<ast::Variable>(
@ -792,7 +738,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("param"))); std::make_unique<ast::IdentifierExpression>("param")));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
std::move(params), &void_type); std::move(params), &void_type);
@ -810,17 +756,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct ep_1_in {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct ep_1_in {
vector<float, 4> coord : SV_Position; vector<float, 4> coord : SV_Position;
}; };
@ -842,7 +786,7 @@ ep_1_out ep_1(ep_1_in tint_in) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
DISABLED_Emit_Function_Called_By_EntryPoint_With_Uniform) { DISABLED_Emit_Function_Called_By_EntryPoint_With_Uniform) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -857,12 +801,9 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
params.push_back(std::make_unique<ast::Variable>( params.push_back(std::make_unique<ast::Variable>(
@ -877,7 +818,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("x")))); std::make_unique<ast::IdentifierExpression>("x"))));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
&void_type); &void_type);
@ -897,20 +838,18 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"( ... )");
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( ... )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
DISABLED_Emit_Function_Called_By_EntryPoint_With_StorageBuffer) { DISABLED_Emit_Function_Called_By_EntryPoint_With_StorageBuffer) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -925,12 +864,9 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::SetDecoration>(1)); decos.push_back(std::make_unique<ast::SetDecoration>(1));
coord_var->set_decorations(std::move(decos)); coord_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var)); mod()->AddGlobalVariable(std::move(coord_var));
ast::VariableList params; ast::VariableList params;
params.push_back(std::make_unique<ast::Variable>( params.push_back(std::make_unique<ast::Variable>(
@ -945,7 +881,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("x")))); std::make_unique<ast::IdentifierExpression>("x"))));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func = std::make_unique<ast::Function>("frag_main", std::move(params), auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
&void_type); &void_type);
@ -965,20 +901,18 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
mod.AddFunction(std::move(func)); mod()->AddFunction(std::move(func));
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "", auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
"frag_main"); "frag_main");
mod.AddEntryPoint(std::move(ep)); mod()->AddEntryPoint(std::move(ep));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"( ... )");
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( ... )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
DISABLED_Emit_Function_Called_Two_EntryPoints_WithGlobals) { DISABLED_Emit_Function_Called_Two_EntryPoints_WithGlobals) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -995,14 +929,11 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(foo_var.get());
ast::Module mod; td().RegisterVariableForTesting(bar_var.get());
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(foo_var.get());
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(foo_var)); mod()->AddGlobalVariable(std::move(foo_var));
mod.AddGlobalVariable(std::move(bar_var)); mod()->AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto sub_func = auto sub_func =
@ -1016,7 +947,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("foo"))); std::make_unique<ast::IdentifierExpression>("foo")));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
std::move(params), &void_type); std::move(params), &void_type);
@ -1030,20 +961,18 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_2", "frag_1_main"); "ep_2", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
mod.AddEntryPoint(std::move(ep2)); mod()->AddEntryPoint(std::move(ep2));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct ep_1_in {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct ep_1_in {
float foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
@ -1084,7 +1013,7 @@ ep_2_out ep_2(ep_2_in tint_in) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
DISABLED_Emit_Function_EntryPoints_WithGlobal_Nested_Return) { DISABLED_Emit_Function_EntryPoints_WithGlobal_Nested_Return) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -1096,11 +1025,8 @@ TEST_F(HlslGeneratorImplTest,
decos.push_back(std::make_unique<ast::LocationDecoration>(1)); decos.push_back(std::make_unique<ast::LocationDecoration>(1));
bar_var->set_decorations(std::move(decos)); bar_var->set_decorations(std::move(decos));
Context ctx; td().RegisterVariableForTesting(bar_var.get());
ast::Module mod; mod()->AddGlobalVariable(std::move(bar_var));
TypeDeterminer td(&ctx, &mod);
td.RegisterVariableForTesting(bar_var.get());
mod.AddGlobalVariable(std::move(bar_var));
ast::VariableList params; ast::VariableList params;
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
@ -1127,17 +1053,15 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(gen().Generate(out())) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), R"(struct ep_1_out {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"(struct ep_1_out {
float bar : SV_Target0; float bar : SV_Target0;
}; };
@ -1153,15 +1077,11 @@ ep_1_out ep_1() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_Called_Two_EntryPoints_WithoutGlobals) { Emit_Function_Called_Two_EntryPoints_WithoutGlobals) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::type::F32Type f32; ast::type::F32Type f32;
Context ctx;
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
ast::VariableList params; ast::VariableList params;
auto sub_func = auto sub_func =
std::make_unique<ast::Function>("sub_func", std::move(params), &f32); std::make_unique<ast::Function>("sub_func", std::move(params), &f32);
@ -1172,7 +1092,7 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::FloatLiteral>(&f32, 1.0)))); std::make_unique<ast::FloatLiteral>(&f32, 1.0))));
sub_func->set_body(std::move(body)); sub_func->set_body(std::move(body));
mod.AddFunction(std::move(sub_func)); mod()->AddFunction(std::move(sub_func));
auto func_1 = std::make_unique<ast::Function>("frag_1_main", auto func_1 = std::make_unique<ast::Function>("frag_1_main",
std::move(params), &void_type); std::move(params), &void_type);
@ -1188,20 +1108,19 @@ TEST_F(HlslGeneratorImplTest,
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func_1->set_body(std::move(body)); func_1->set_body(std::move(body));
mod.AddFunction(std::move(func_1)); mod()->AddFunction(std::move(func_1));
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_1", "frag_1_main"); "ep_1", "frag_1_main");
auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
"ep_2", "frag_1_main"); "ep_2", "frag_1_main");
mod.AddEntryPoint(std::move(ep1)); mod()->AddEntryPoint(std::move(ep1));
mod.AddEntryPoint(std::move(ep2)); mod()->AddEntryPoint(std::move(ep2));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
GeneratorImpl g(&mod); ASSERT_TRUE(gen().Generate(out())) << gen().error();
ASSERT_TRUE(g.Generate()) << g.error(); EXPECT_EQ(result(), R"(float sub_func() {
EXPECT_EQ(g.result(), R"(float sub_func() {
return 1.00000000f; return 1.00000000f;
} }
@ -1217,7 +1136,7 @@ void ep_2() {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithName) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_WithName) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{}, auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{},
@ -1225,19 +1144,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithName) {
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
"my_main", "comp_main"); "my_main", "comp_main");
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); mod()->AddEntryPoint(std::move(ep));
m.AddEntryPoint(std::move(ep));
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
ASSERT_TRUE(g.Generate()) << g.error(); EXPECT_EQ(result(), R"(void my_main() {
EXPECT_EQ(g.result(), R"(void my_main() {
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithNameCollision) { TEST_F(HlslGeneratorImplTest_Function,
Emit_Function_EntryPoint_WithNameCollision) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{}, auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{},
@ -1245,19 +1163,17 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithNameCollision) {
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute, auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
"GeometryShader", "comp_main"); "GeometryShader", "comp_main");
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); mod()->AddEntryPoint(std::move(ep));
m.AddEntryPoint(std::move(ep));
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
ASSERT_TRUE(g.Generate()) << g.error(); EXPECT_EQ(result(), R"(void GeometryShader_tint_0() {
EXPECT_EQ(g.result(), R"(void GeometryShader_tint_0() {
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_Function_WithArrayParams) { TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithArrayParams) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::ArrayType ary(&f32, 5); ast::type::ArrayType ary(&f32, 5);
@ -1273,14 +1189,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_WithArrayParams) {
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
func->set_body(std::move(body)); func->set_body(std::move(body));
ast::Module m; mod()->AddFunction(std::move(func));
m.AddFunction(std::move(func)); gen().increment_indent();
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), R"( void my_func(float a[5]) {
ASSERT_TRUE(g.Generate()) << g.error();
EXPECT_EQ(g.result(), R"( void my_func(float a[5]) {
return; return;
} }

View File

@ -12,53 +12,43 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Identifier : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, DISABLED_EmitExpression_Identifier) { TEST_F(HlslGeneratorImplTest_Identifier, DISABLED_EmitExpression_Identifier) {
ast::IdentifierExpression i(std::vector<std::string>{"std", "glsl"}); ast::IdentifierExpression i(std::vector<std::string>{"std", "glsl"});
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
ast::Module m; EXPECT_EQ(result(), "std::glsl");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "std::glsl");
} }
TEST_F(HlslGeneratorImplTest, EmitIdentifierExpression_Single) { TEST_F(HlslGeneratorImplTest_Identifier, EmitIdentifierExpression_Single) {
ast::IdentifierExpression i("foo"); ast::IdentifierExpression i("foo");
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
ast::Module m; EXPECT_EQ(result(), "foo");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "foo");
} }
TEST_F(HlslGeneratorImplTest, EmitIdentifierExpression_Single_WithCollision) { TEST_F(HlslGeneratorImplTest_Identifier,
EmitIdentifierExpression_Single_WithCollision) {
ast::IdentifierExpression i("virtual"); ast::IdentifierExpression i("virtual");
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
ast::Module m; EXPECT_EQ(result(), "virtual_tint_0");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "virtual_tint_0");
} }
// TODO(dsinclair): Handle import names // TODO(dsinclair): Handle import names
TEST_F(HlslGeneratorImplTest, DISABLED_EmitIdentifierExpression_MultipleNames) { TEST_F(HlslGeneratorImplTest_Identifier,
DISABLED_EmitIdentifierExpression_MultipleNames) {
ast::IdentifierExpression i({"std", "glsl", "init"}); ast::IdentifierExpression i({"std", "glsl", "init"});
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
ast::Module m; EXPECT_EQ(result(), "std::glsl::init");
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
EXPECT_EQ(g.result(), "std::glsl::init");
} }
} // namespace } // namespace

View File

@ -12,40 +12,36 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/else_statement.h" #include "src/ast/else_statement.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/if_statement.h" #include "src/ast/if_statement.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/return_statement.h" #include "src/ast/return_statement.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_If : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_If) { TEST_F(HlslGeneratorImplTest_If, Emit_If) {
auto cond = std::make_unique<ast::IdentifierExpression>("cond"); auto cond = std::make_unique<ast::IdentifierExpression>("cond");
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::ReturnStatement>()); body->append(std::make_unique<ast::ReturnStatement>());
ast::IfStatement i(std::move(cond), std::move(body)); ast::IfStatement i(std::move(cond), std::move(body));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( if (cond) {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
EXPECT_EQ(g.result(), R"( if (cond) {
return; return;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) { TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond"); auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
auto else_body = std::make_unique<ast::BlockStatement>(); auto else_body = std::make_unique<ast::BlockStatement>();
else_body->append(std::make_unique<ast::ReturnStatement>()); else_body->append(std::make_unique<ast::ReturnStatement>());
@ -61,12 +57,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) {
ast::IfStatement i(std::move(cond), std::move(body)); ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses)); i.set_else_statements(std::move(elses));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else if (else_cond) { } else if (else_cond) {
return; return;
@ -74,7 +68,7 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) { TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) {
auto else_body = std::make_unique<ast::BlockStatement>(); auto else_body = std::make_unique<ast::BlockStatement>();
else_body->append(std::make_unique<ast::ReturnStatement>()); else_body->append(std::make_unique<ast::ReturnStatement>());
@ -88,12 +82,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) {
ast::IfStatement i(std::move(cond), std::move(body)); ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses)); i.set_else_statements(std::move(elses));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else { } else {
return; return;
@ -101,7 +93,7 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_IfWithMultiple) { TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) {
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond"); auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
auto else_body = std::make_unique<ast::BlockStatement>(); auto else_body = std::make_unique<ast::BlockStatement>();
@ -122,12 +114,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithMultiple) {
ast::IfStatement i(std::move(cond), std::move(body)); ast::IfStatement i(std::move(cond), std::move(body));
i.set_else_statements(std::move(elses)); i.set_else_statements(std::move(elses));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m);
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&i)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
EXPECT_EQ(g.result(), R"( if (cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else if (else_cond) { } else if (else_cond) {
return; return;

View File

@ -16,7 +16,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/call_expression.h" #include "src/ast/call_expression.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
@ -30,14 +29,14 @@
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/context.h" #include "src/context.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Import : public TestHelper, public testing::Test {};
struct HlslImportData { struct HlslImportData {
const char* name; const char* name;
@ -47,7 +46,10 @@ inline std::ostream& operator<<(std::ostream& out, HlslImportData data) {
out << data.name; out << data.name;
return out; return out;
} }
using HlslImportData_SingleParamTest = testing::TestWithParam<HlslImportData>;
class HlslImportData_SingleParamTest
: public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_SingleParamTest, FloatScalar) { TEST_P(HlslImportData_SingleParamTest, FloatScalar) {
auto param = GetParam(); auto param = GetParam();
@ -61,19 +63,14 @@ TEST_P(HlslImportData_SingleParamTest, FloatScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1.00000000f)");
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1.00000000f)");
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest, HlslGeneratorImplTest_Import,
HlslImportData_SingleParamTest, HlslImportData_SingleParamTest,
testing::Values(HlslImportData{"acos", "acos"}, testing::Values(HlslImportData{"acos", "acos"},
HlslImportData{"asin", "asin"}, HlslImportData{"asin", "asin"},
@ -104,20 +101,21 @@ INSTANTIATE_TEST_SUITE_P(
HlslImportData{"tanh", "tanh"}, HlslImportData{"tanh", "tanh"},
HlslImportData{"trunc", "trunc"})); HlslImportData{"trunc", "trunc"}));
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_Acosh) { TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_Acosh) {
FAIL(); FAIL();
} }
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ASinh) { TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_ASinh) {
FAIL(); FAIL();
} }
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ATanh) { TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_ATanh) {
FAIL(); FAIL();
} }
using HlslImportData_SingleIntParamTest = class HlslImportData_SingleIntParamTest
testing::TestWithParam<HlslImportData>; : public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_SingleIntParamTest, IntScalar) { TEST_P(HlslImportData_SingleIntParamTest, IntScalar) {
auto param = GetParam(); auto param = GetParam();
@ -131,23 +129,20 @@ TEST_P(HlslImportData_SingleIntParamTest, IntScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1)");
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_SingleIntParamTest, HlslImportData_SingleIntParamTest,
testing::Values(HlslImportData{"sabs", "abs"}, testing::Values(HlslImportData{"sabs", "abs"},
HlslImportData{"ssign", "sign"})); HlslImportData{"ssign", "sign"}));
using HlslImportData_DualParamTest = testing::TestWithParam<HlslImportData>; class HlslImportData_DualParamTest
: public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_DualParamTest, FloatScalar) { TEST_P(HlslImportData_DualParamTest, FloatScalar) {
auto param = GetParam(); auto param = GetParam();
@ -163,19 +158,14 @@ TEST_P(HlslImportData_DualParamTest, FloatScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(),
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(),
std::string(param.hlsl_name) + "(1.00000000f, 2.00000000f)"); std::string(param.hlsl_name) + "(1.00000000f, 2.00000000f)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_DualParamTest, HlslImportData_DualParamTest,
testing::Values(HlslImportData{"atan2", "atan2"}, testing::Values(HlslImportData{"atan2", "atan2"},
HlslImportData{"distance", "distance"}, HlslImportData{"distance", "distance"},
@ -187,8 +177,9 @@ INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
HlslImportData{"reflect", "reflect"}, HlslImportData{"reflect", "reflect"},
HlslImportData{"step", "step"})); HlslImportData{"step", "step"}));
using HlslImportData_DualParam_VectorTest = class HlslImportData_DualParam_VectorTest
testing::TestWithParam<HlslImportData>; : public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) { TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
auto param = GetParam(); auto param = GetParam();
@ -220,26 +211,22 @@ TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(),
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(),
std::string(param.hlsl_name) + std::string(param.hlsl_name) +
"(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " "(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), "
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f))"); "vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f))");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_DualParam_VectorTest, HlslImportData_DualParam_VectorTest,
testing::Values(HlslImportData{"cross", "cross"})); testing::Values(HlslImportData{"cross", "cross"}));
using HlslImportData_DualParam_Int_Test = class HlslImportData_DualParam_Int_Test
testing::TestWithParam<HlslImportData>; : public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) { TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) {
auto param = GetParam(); auto param = GetParam();
@ -255,25 +242,22 @@ TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1, 2)");
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_DualParam_Int_Test, HlslImportData_DualParam_Int_Test,
testing::Values(HlslImportData{"smax", "max"}, testing::Values(HlslImportData{"smax", "max"},
HlslImportData{"smin", "min"}, HlslImportData{"smin", "min"},
HlslImportData{"umax", "max"}, HlslImportData{"umax", "max"},
HlslImportData{"umin", "min"})); HlslImportData{"umin", "min"}));
using HlslImportData_TripleParamTest = testing::TestWithParam<HlslImportData>; class HlslImportData_TripleParamTest
: public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_TripleParamTest, FloatScalar) { TEST_P(HlslImportData_TripleParamTest, FloatScalar) {
auto param = GetParam(); auto param = GetParam();
@ -291,20 +275,15 @@ TEST_P(HlslImportData_TripleParamTest, FloatScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string(param.hlsl_name) +
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error(); "(1.00000000f, 2.00000000f, 3.00000000f)");
EXPECT_EQ(g.result(), std::string(param.hlsl_name) +
"(1.00000000f, 2.00000000f, 3.00000000f)");
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest, HlslGeneratorImplTest_Import,
HlslImportData_TripleParamTest, HlslImportData_TripleParamTest,
testing::Values(HlslImportData{"faceforward", "faceforward"}, testing::Values(HlslImportData{"faceforward", "faceforward"},
HlslImportData{"fma", "fma"}, HlslImportData{"fma", "fma"},
@ -312,12 +291,13 @@ INSTANTIATE_TEST_SUITE_P(
HlslImportData{"nclamp", "clamp"}, HlslImportData{"nclamp", "clamp"},
HlslImportData{"smoothstep", "smoothstep"})); HlslImportData{"smoothstep", "smoothstep"}));
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_FMix) { TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_FMix) {
FAIL(); FAIL();
} }
using HlslImportData_TripleParam_Int_Test = class HlslImportData_TripleParam_Int_Test
testing::TestWithParam<HlslImportData>; : public TestHelper,
public testing::TestWithParam<HlslImportData> {};
TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) { TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) {
auto param = GetParam(); auto param = GetParam();
@ -335,23 +315,18 @@ TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) {
std::vector<std::string>{"std", param.name}), std::vector<std::string>{"std", param.name}),
std::move(params)); std::move(params));
Context ctx; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ast::Module mod;
TypeDeterminer td(&ctx, &mod);
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1, 2, 3)");
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2, 3)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
HlslImportData_TripleParam_Int_Test, HlslImportData_TripleParam_Int_Test,
testing::Values(HlslImportData{"sclamp", "clamp"}, testing::Values(HlslImportData{"sclamp", "clamp"},
HlslImportData{"uclamp", "clamp"})); HlslImportData{"uclamp", "clamp"}));
TEST_F(HlslGeneratorImplTest, HlslImportData_Determinant) { TEST_F(HlslGeneratorImplTest_Import, HlslImportData_Determinant) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::MatrixType mat(&f32, 3, 3); ast::type::MatrixType mat(&f32, 3, 3);
@ -365,19 +340,14 @@ TEST_F(HlslGeneratorImplTest, HlslImportData_Determinant) {
std::vector<std::string>{"std", "determinant"}), std::vector<std::string>{"std", "determinant"}),
std::move(params)); std::move(params));
Context ctx; mod()->AddGlobalVariable(std::move(var));
ast::Module mod; mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
mod.AddGlobalVariable(std::move(var));
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
TypeDeterminer td(&ctx, &mod);
// Register the global // Register the global
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), std::string("determinant(var)"));
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
EXPECT_EQ(g.result(), std::string("determinant(var)"));
} }
} // namespace } // namespace

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/call_expression.h" #include "src/ast/call_expression.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
@ -20,14 +19,15 @@
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/context.h" #include "src/context.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Intrinsic : public TestHelper,
public testing::Test {};
struct IntrinsicData { struct IntrinsicData {
const char* name; const char* name;
@ -37,16 +37,14 @@ inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) {
out << data.name; out << data.name;
return out; return out;
} }
using HlslIntrinsicTest = testing::TestWithParam<IntrinsicData>; class HlslIntrinsicTest : public TestHelper,
public testing::TestWithParam<IntrinsicData> {};
TEST_P(HlslIntrinsicTest, Emit) { TEST_P(HlslIntrinsicTest, Emit) {
auto param = GetParam(); auto param = GetParam();
EXPECT_EQ(gen().generate_intrinsic_name(param.name), param.hlsl_name);
ast::Module m;
GeneratorImpl g(&m);
EXPECT_EQ(g.generate_intrinsic_name(param.name), param.hlsl_name);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest, HlslGeneratorImplTest_Intrinsic,
HlslIntrinsicTest, HlslIntrinsicTest,
testing::Values(IntrinsicData{"any", "any"}, testing::Values(IntrinsicData{"any", "any"},
IntrinsicData{"all", "all"}, IntrinsicData{"all", "all"},
@ -64,15 +62,15 @@ INSTANTIATE_TEST_SUITE_P(
IntrinsicData{"is_inf", "isinf"}, IntrinsicData{"is_inf", "isinf"},
IntrinsicData{"is_nan", "isnan"})); IntrinsicData{"is_nan", "isnan"}));
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_IsNormal) { TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_IsNormal) {
FAIL(); FAIL();
} }
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_Select) { TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_Select) {
FAIL(); FAIL();
} }
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) { TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_OuterProduct) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec2(&f32, 2); ast::type::VectorType vec2(&f32, 2);
ast::type::VectorType vec3(&f32, 3); ast::type::VectorType vec3(&f32, 3);
@ -90,32 +88,25 @@ TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) {
std::make_unique<ast::IdentifierExpression>("outer_product"), std::make_unique<ast::IdentifierExpression>("outer_product"),
std::move(params)); std::move(params));
Context ctx; td().RegisterVariableForTesting(a.get());
ast::Module m; td().RegisterVariableForTesting(b.get());
TypeDeterminer td(&ctx, &m);
td.RegisterVariableForTesting(a.get());
td.RegisterVariableForTesting(b.get());
m.AddGlobalVariable(std::move(a)); mod()->AddGlobalVariable(std::move(a));
m.AddGlobalVariable(std::move(b)); mod()->AddGlobalVariable(std::move(b));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(td.DetermineResultType(&call)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&call)) << td().error();
GeneratorImpl g(&m); gen().increment_indent();
ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), " float3x2(a * b[0], a * b[1], a * b[2])");
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), " float3x2(a * b[0], a * b[1], a * b[2])");
} }
TEST_F(HlslGeneratorImplTest, Intrinsic_Bad_Name) { TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Bad_Name) {
ast::Module m; EXPECT_EQ(gen().generate_intrinsic_name("unknown name"), "");
GeneratorImpl g(&m);
EXPECT_EQ(g.generate_intrinsic_name("unknown name"), "");
} }
TEST_F(HlslGeneratorImplTest, Intrinsic_Call) { TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Call) {
ast::ExpressionList params; ast::ExpressionList params;
params.push_back(std::make_unique<ast::IdentifierExpression>("param1")); params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
params.push_back(std::make_unique<ast::IdentifierExpression>("param2")); params.push_back(std::make_unique<ast::IdentifierExpression>("param2"));
@ -123,11 +114,9 @@ TEST_F(HlslGeneratorImplTest, Intrinsic_Call) {
ast::CallExpression call(std::make_unique<ast::IdentifierExpression>("dot"), ast::CallExpression call(std::make_unique<ast::IdentifierExpression>("dot"),
std::move(params)); std::move(params));
ast::Module m; gen().increment_indent();
GeneratorImpl g(&m); ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
g.increment_indent(); EXPECT_EQ(result(), " dot(param1, param2)");
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
EXPECT_EQ(g.result(), " dot(param1, param2)");
} }
} // namespace } // namespace

View File

@ -14,7 +14,6 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/discard_statement.h" #include "src/ast/discard_statement.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
@ -25,33 +24,30 @@
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h" #include "src/ast/variable_decl_statement.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Loop : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Loop) { TEST_F(HlslGeneratorImplTest_Loop, Emit_Loop) {
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>()); body->append(std::make_unique<ast::DiscardStatement>());
ast::LoopStatement l(std::move(body), {}); ast::LoopStatement l(std::move(body), {});
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &l)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( for(;;) {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
EXPECT_EQ(g.result(), R"( for(;;) {
discard; discard;
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) { TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithContinuing) {
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
body->append(std::make_unique<ast::DiscardStatement>()); body->append(std::make_unique<ast::DiscardStatement>());
@ -59,13 +55,10 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
continuing->append(std::make_unique<ast::ReturnStatement>()); continuing->append(std::make_unique<ast::ReturnStatement>());
ast::LoopStatement l(std::move(body), std::move(continuing)); ast::LoopStatement l(std::move(body), std::move(continuing));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &l)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
EXPECT_EQ(g.result(), R"( {
bool tint_hlsl_is_first_1 = true; bool tint_hlsl_is_first_1 = true;
for(;;) { for(;;) {
if (!tint_hlsl_is_first_1) { if (!tint_hlsl_is_first_1) {
@ -79,7 +72,7 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
)"); )");
} }
TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) { TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto body = std::make_unique<ast::BlockStatement>(); auto body = std::make_unique<ast::BlockStatement>();
@ -102,13 +95,10 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
std::move(lhs), std::move(rhs))); std::move(lhs), std::move(rhs)));
ast::LoopStatement outer(std::move(body), std::move(continuing)); ast::LoopStatement outer(std::move(body), std::move(continuing));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &outer)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
EXPECT_EQ(g.result(), R"( {
bool tint_hlsl_is_first_1 = true; bool tint_hlsl_is_first_1 = true;
for(;;) { for(;;) {
if (!tint_hlsl_is_first_1) { if (!tint_hlsl_is_first_1) {
@ -134,7 +124,7 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
// TODO(dsinclair): Handle pulling declared variables up and out of the for() if // TODO(dsinclair): Handle pulling declared variables up and out of the for() if
// there is a continuing block. // there is a continuing block.
TEST_F(HlslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) { TEST_F(HlslGeneratorImplTest_Loop, DISABLED_Emit_LoopWithVarUsedInContinuing) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto var = std::make_unique<ast::Variable>( auto var = std::make_unique<ast::Variable>(
@ -156,13 +146,10 @@ TEST_F(HlslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
std::move(lhs), std::move(rhs))); std::move(lhs), std::move(rhs)));
ast::LoopStatement outer(std::move(body), std::move(continuing)); ast::LoopStatement outer(std::move(body), std::move(continuing));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &outer)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
EXPECT_EQ(g.result(), R"( {
float lhs; float lhs;
bool tint_hlsl_is_first_1 = true; bool tint_hlsl_is_first_1 = true;
for(;;) { for(;;) {

View File

@ -14,7 +14,6 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/array_accessor_expression.h" #include "src/ast/array_accessor_expression.h"
#include "src/ast/assignment_statement.h" #include "src/ast/assignment_statement.h"
#include "src/ast/binary_expression.h" #include "src/ast/binary_expression.h"
@ -36,16 +35,17 @@
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/context.h" #include "src/context.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_MemberAccessor : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitExpression_MemberAccessor) { TEST_F(HlslGeneratorImplTest_MemberAccessor, EmitExpression_MemberAccessor) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::StructMemberList members; ast::StructMemberList members;
@ -68,20 +68,16 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_MemberAccessor) {
ast::MemberAccessorExpression expr(std::move(str), std::move(mem)); ast::MemberAccessorExpression expr(std::move(str), std::move(mem));
Context ctx; td().RegisterVariableForTesting(str_var.get());
ast::Module mod; gen().register_global(str_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(str_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(str_var.get());
g.register_global(str_var.get());
mod.AddGlobalVariable(std::move(str_var));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "str.mem"); EXPECT_EQ(result(), "str.mem");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load) { EmitExpression_MemberAccessor_StorageBuffer_Load) {
// struct Data { // struct Data {
// [[offset 0]] a : i32; // [[offset 0]] a : i32;
@ -119,23 +115,18 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("data"), std::make_unique<ast::IdentifierExpression>("data"),
std::make_unique<ast::IdentifierExpression>("b")); std::make_unique<ast::IdentifierExpression>("b"));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get()); ASSERT_TRUE(td().Determine()) << td().error();
g.register_global(coord_var.get()); ASSERT_TRUE(td().DetermineResultType(&expr));
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
ASSERT_TRUE(td.DetermineResultType(&expr)); EXPECT_EQ(result(), "asfloat(data.Load(4))");
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
EXPECT_EQ(g.result(), "asfloat(data.Load(4))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_Int) { EmitExpression_MemberAccessor_StorageBuffer_Load_Int) {
// struct Data { // struct Data {
// [[offset 0]] a : i32; // [[offset 0]] a : i32;
@ -173,22 +164,18 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("data"), std::make_unique<ast::IdentifierExpression>("data"),
std::make_unique<ast::IdentifierExpression>("a")); std::make_unique<ast::IdentifierExpression>("a"));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asint(data.Load(0))"); EXPECT_EQ(result(), "asint(data.Load(0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray) { EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray) {
// struct Data { // struct Data {
// [[offset 0]] a : [[stride 4]] array<i32, 5>; // [[offset 0]] a : [[stride 4]] array<i32, 5>;
@ -225,22 +212,18 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::ScalarConstructorExpression>( std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 2))); std::make_unique<ast::SintLiteral>(&i32, 2)));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asint(data.Load((4 * 2) + 0))"); EXPECT_EQ(result(), "asint(data.Load((4 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray_ExprIdx) { EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray_ExprIdx) {
// struct Data { // struct Data {
// [[offset 0]] a : [[stride 4]] array<i32, 5>; // [[offset 0]] a : [[stride 4]] array<i32, 5>;
@ -285,22 +268,18 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::ScalarConstructorExpression>( std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 3)))); std::make_unique<ast::SintLiteral>(&i32, 3))));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asint(data.Load((4 * ((2 + 4) - 3)) + 0))"); EXPECT_EQ(result(), "asint(data.Load((4 * ((2 + 4) - 3)) + 0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store) { EmitExpression_MemberAccessor_StorageBuffer_Store) {
// struct Data { // struct Data {
// [[offset 0]] a : i32; // [[offset 0]] a : i32;
@ -335,15 +314,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &s)); "data", ast::StorageClass::kStorageBuffer, &s));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lhs = std::make_unique<ast::MemberAccessorExpression>( auto lhs = std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::IdentifierExpression>("data"), std::make_unique<ast::IdentifierExpression>("data"),
@ -352,13 +327,13 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::FloatLiteral>(&f32, 2.0f)); std::make_unique<ast::FloatLiteral>(&f32, 2.0f));
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(td().DetermineResultType(&assign));
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ(g.result(), R"(data.Store(4, asuint(2.00000000f)); EXPECT_EQ(result(), R"(data.Store(4, asuint(2.00000000f));
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_ToArray) { EmitExpression_MemberAccessor_StorageBuffer_Store_ToArray) {
// struct Data { // struct Data {
// [[offset 0]] a : [[stride 4]] array<i32, 5>; // [[offset 0]] a : [[stride 4]] array<i32, 5>;
@ -389,15 +364,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &s)); "data", ast::StorageClass::kStorageBuffer, &s));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lhs = std::make_unique<ast::ArrayAccessorExpression>( auto lhs = std::make_unique<ast::ArrayAccessorExpression>(
std::make_unique<ast::MemberAccessorExpression>( std::make_unique<ast::MemberAccessorExpression>(
@ -409,13 +380,13 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::SintLiteral>(&i32, 2)); std::make_unique<ast::SintLiteral>(&i32, 2));
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)) << td.error(); ASSERT_TRUE(td().DetermineResultType(&assign)) << td().error();
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ(g.result(), R"(data.Store((4 * 2) + 0, asuint(2)); EXPECT_EQ(result(), R"(data.Store((4 * 2) + 0, asuint(2));
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_Int) { EmitExpression_MemberAccessor_StorageBuffer_Store_Int) {
// struct Data { // struct Data {
// [[offset 0]] a : i32; // [[offset 0]] a : i32;
@ -450,15 +421,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &s)); "data", ast::StorageClass::kStorageBuffer, &s));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lhs = std::make_unique<ast::MemberAccessorExpression>( auto lhs = std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::IdentifierExpression>("data"), std::make_unique<ast::IdentifierExpression>("data"),
@ -467,13 +434,13 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::SintLiteral>(&i32, 2)); std::make_unique<ast::SintLiteral>(&i32, 2));
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(td().DetermineResultType(&assign));
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ(g.result(), R"(data.Store(0, asuint(2)); EXPECT_EQ(result(), R"(data.Store(0, asuint(2));
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_Vec3) { EmitExpression_MemberAccessor_StorageBuffer_Load_Vec3) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -510,26 +477,22 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &s)); "data", ast::StorageClass::kStorageBuffer, &s));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ast::MemberAccessorExpression expr( ast::MemberAccessorExpression expr(
std::make_unique<ast::IdentifierExpression>("data"), std::make_unique<ast::IdentifierExpression>("data"),
std::make_unique<ast::IdentifierExpression>("b")); std::make_unique<ast::IdentifierExpression>("b"));
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asfloat(data.Load3(16))"); EXPECT_EQ(result(), "asfloat(data.Load3(16))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_Vec3) { EmitExpression_MemberAccessor_StorageBuffer_Store_Vec3) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -566,15 +529,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &s)); "data", ast::StorageClass::kStorageBuffer, &s));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lit1 = std::make_unique<ast::FloatLiteral>(&f32, 1.f); auto lit1 = std::make_unique<ast::FloatLiteral>(&f32, 1.f);
auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f); auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f);
@ -595,15 +554,15 @@ TEST_F(HlslGeneratorImplTest,
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(td().DetermineResultType(&assign));
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ( EXPECT_EQ(
g.result(), result(),
R"(data.Store3(16, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f))); R"(data.Store3(16, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)));
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel) { EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -656,15 +615,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ast::MemberAccessorExpression expr( ast::MemberAccessorExpression expr(
std::make_unique<ast::ArrayAccessorExpression>( std::make_unique<ast::ArrayAccessorExpression>(
@ -675,12 +630,12 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::SintLiteral>(&i32, 2))), std::make_unique<ast::SintLiteral>(&i32, 2))),
std::make_unique<ast::IdentifierExpression>("b")); std::make_unique<ast::IdentifierExpression>("b"));
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asfloat(data.Load3(16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle) { EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -733,15 +688,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ast::MemberAccessorExpression expr( ast::MemberAccessorExpression expr(
std::make_unique<ast::MemberAccessorExpression>( std::make_unique<ast::MemberAccessorExpression>(
@ -754,13 +705,13 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::IdentifierExpression>("b")), std::make_unique<ast::IdentifierExpression>("b")),
std::make_unique<ast::IdentifierExpression>("xy")); std::make_unique<ast::IdentifierExpression>("xy"));
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asfloat(data.Load3(16 + (32 * 2) + 0)).xy"); EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0)).xy");
} }
TEST_F( TEST_F(
HlslGeneratorImplTest, HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle_SingleLetter) { EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle_SingleLetter) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -813,15 +764,11 @@ TEST_F(
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ast::MemberAccessorExpression expr( ast::MemberAccessorExpression expr(
std::make_unique<ast::MemberAccessorExpression>( std::make_unique<ast::MemberAccessorExpression>(
@ -834,12 +781,12 @@ TEST_F(
std::make_unique<ast::IdentifierExpression>("b")), std::make_unique<ast::IdentifierExpression>("b")),
std::make_unique<ast::IdentifierExpression>("g")); std::make_unique<ast::IdentifierExpression>("g"));
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Index) { EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Index) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -892,15 +839,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
ast::ArrayAccessorExpression expr( ast::ArrayAccessorExpression expr(
std::make_unique<ast::MemberAccessorExpression>( std::make_unique<ast::MemberAccessorExpression>(
@ -914,12 +857,12 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::ScalarConstructorExpression>( std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::SintLiteral>(&i32, 1))); std::make_unique<ast::SintLiteral>(&i32, 1)));
ASSERT_TRUE(td.DetermineResultType(&expr)); ASSERT_TRUE(td().DetermineResultType(&expr));
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error(); ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
EXPECT_EQ(g.result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_MultiLevel) { EmitExpression_MemberAccessor_StorageBuffer_Store_MultiLevel) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -972,15 +915,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lhs = std::make_unique<ast::MemberAccessorExpression>( auto lhs = std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::ArrayAccessorExpression>( std::make_unique<ast::ArrayAccessorExpression>(
@ -1007,15 +946,15 @@ TEST_F(HlslGeneratorImplTest,
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(td().DetermineResultType(&assign));
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ( EXPECT_EQ(
g.result(), result(),
R"(data.Store3(16 + (32 * 2) + 0, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f))); R"(data.Store3(16 + (32 * 2) + 0, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)));
)"); )");
} }
TEST_F(HlslGeneratorImplTest, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_Swizzle_SingleLetter) { EmitExpression_MemberAccessor_StorageBuffer_Store_Swizzle_SingleLetter) {
// struct Data { // struct Data {
// [[offset 0]] a : vec3<i32>; // [[offset 0]] a : vec3<i32>;
@ -1068,15 +1007,11 @@ TEST_F(HlslGeneratorImplTest,
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>( std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
"data", ast::StorageClass::kStorageBuffer, &pre)); "data", ast::StorageClass::kStorageBuffer, &pre));
Context ctx; td().RegisterVariableForTesting(coord_var.get());
ast::Module mod; gen().register_global(coord_var.get());
TypeDeterminer td(&ctx, &mod); mod()->AddGlobalVariable(std::move(coord_var));
GeneratorImpl g(&mod);
td.RegisterVariableForTesting(coord_var.get());
g.register_global(coord_var.get());
mod.AddGlobalVariable(std::move(coord_var));
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td().Determine()) << td().error();
auto lhs = std::make_unique<ast::MemberAccessorExpression>( auto lhs = std::make_unique<ast::MemberAccessorExpression>(
std::make_unique<ast::MemberAccessorExpression>( std::make_unique<ast::MemberAccessorExpression>(
@ -1094,9 +1029,9 @@ TEST_F(HlslGeneratorImplTest,
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs)); ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&assign)); ASSERT_TRUE(td().DetermineResultType(&assign));
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error(); ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
EXPECT_EQ(g.result(), EXPECT_EQ(result(),
R"(data.Store((4 * 1) + 16 + (32 * 2) + 0, asuint(1.00000000f)); R"(data.Store((4 * 1) + 16 + (32 * 2) + 0, asuint(1.00000000f));
)"); )");
} }

View File

@ -15,7 +15,6 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/float_literal.h" #include "src/ast/float_literal.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/scalar_constructor_expression.h" #include "src/ast/scalar_constructor_expression.h"
@ -23,16 +22,17 @@
#include "src/ast/type/f32_type.h" #include "src/ast/type/f32_type.h"
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_ModuleConstant : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_ModuleConstant) { TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_ModuleConstant) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::ArrayType ary(&f32, 3); ast::type::ArrayType ary(&f32, 3);
@ -50,11 +50,10 @@ TEST_F(HlslGeneratorImplTest, Emit_ModuleConstant) {
var->set_constructor( var->set_constructor(
std::make_unique<ast::TypeConstructorExpression>(&ary, std::move(exprs))); std::make_unique<ast::TypeConstructorExpression>(&ary, std::move(exprs)));
ast::Module m; ASSERT_TRUE(gen().EmitProgramConstVariable(out(), var.get()))
GeneratorImpl g(&m); << gen().error();
ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error();
EXPECT_EQ( EXPECT_EQ(
g.result(), result(),
"static const float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n"); "static const float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n");
} }

View File

@ -15,40 +15,33 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/return_statement.h" #include "src/ast/return_statement.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Return : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Return) { TEST_F(HlslGeneratorImplTest_Return, Emit_Return) {
ast::ReturnStatement r; ast::ReturnStatement r;
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &r)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " return;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return;\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_ReturnWithValue) { TEST_F(HlslGeneratorImplTest_Return, Emit_ReturnWithValue) {
auto expr = std::make_unique<ast::IdentifierExpression>("expr"); auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::ReturnStatement r(std::move(expr)); ast::ReturnStatement r(std::move(expr));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &r)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " return expr;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
EXPECT_EQ(g.result(), " return expr;\n");
} }
} // namespace } // namespace

View File

@ -14,7 +14,6 @@
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/break_statement.h" #include "src/ast/break_statement.h"
#include "src/ast/case_statement.h" #include "src/ast/case_statement.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
@ -22,16 +21,16 @@
#include "src/ast/sint_literal.h" #include "src/ast/sint_literal.h"
#include "src/ast/switch_statement.h" #include "src/ast/switch_statement.h"
#include "src/ast/type/i32_type.h" #include "src/ast/type/i32_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Switch : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_Switch) { TEST_F(HlslGeneratorImplTest_Switch, Emit_Switch) {
auto def = std::make_unique<ast::CaseStatement>(); auto def = std::make_unique<ast::CaseStatement>();
auto def_body = std::make_unique<ast::BlockStatement>(); auto def_body = std::make_unique<ast::BlockStatement>();
def_body->append(std::make_unique<ast::BreakStatement>()); def_body->append(std::make_unique<ast::BreakStatement>());
@ -53,13 +52,10 @@ TEST_F(HlslGeneratorImplTest, Emit_Switch) {
auto cond = std::make_unique<ast::IdentifierExpression>("cond"); auto cond = std::make_unique<ast::IdentifierExpression>("cond");
ast::SwitchStatement s(std::move(cond), std::move(body)); ast::SwitchStatement s(std::move(cond), std::move(body));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &s)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"( switch(cond) {
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&s)) << g.error();
EXPECT_EQ(g.result(), R"( switch(cond) {
case 5: { case 5: {
break; break;
} }

View File

@ -12,35 +12,31 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "src/writer/hlsl/generator_impl.h"
#include <memory> #include <memory>
#include "gtest/gtest.h"
#include "src/ast/entry_point.h" #include "src/ast/entry_point.h"
#include "src/ast/function.h" #include "src/ast/function.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
#include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, DISABLED_Generate) { TEST_F(HlslGeneratorImplTest, DISABLED_Generate) {
ast::type::VoidType void_type; ast::type::VoidType void_type;
ast::Module m; mod()->AddFunction(std::make_unique<ast::Function>(
m.AddFunction(std::make_unique<ast::Function>("my_func", ast::VariableList{}, "my_func", ast::VariableList{}, &void_type));
&void_type)); mod()->AddEntryPoint(std::make_unique<ast::EntryPoint>(
m.AddEntryPoint(std::make_unique<ast::EntryPoint>(
ast::PipelineStage::kFragment, "my_func", "")); ast::PipelineStage::kFragment, "my_func", ""));
GeneratorImpl g(&m); ASSERT_TRUE(gen().Generate(out())) << gen().error();
ASSERT_TRUE(g.Generate()) << g.error(); EXPECT_EQ(result(), R"(#import <metal_lib>
EXPECT_EQ(g.result(), R"(#import <metal_lib>
void my_func() { void my_func() {
} }
@ -48,30 +44,23 @@ void my_func() {
} }
TEST_F(HlslGeneratorImplTest, InputStructName) { TEST_F(HlslGeneratorImplTest, InputStructName) {
ast::Module m; ASSERT_EQ(gen().generate_name("func_main_in"), "func_main_in");
GeneratorImpl g(&m);
ASSERT_EQ(g.generate_name("func_main_in"), "func_main_in");
} }
TEST_F(HlslGeneratorImplTest, InputStructName_ConflictWithExisting) { TEST_F(HlslGeneratorImplTest, InputStructName_ConflictWithExisting) {
ast::Module m;
GeneratorImpl g(&m);
// Register the struct name as existing. // Register the struct name as existing.
auto* namer = g.namer_for_testing(); auto* namer = gen().namer_for_testing();
namer->NameFor("func_main_out"); namer->NameFor("func_main_out");
ASSERT_EQ(g.generate_name("func_main_out"), "func_main_out_0"); ASSERT_EQ(gen().generate_name("func_main_out"), "func_main_out_0");
} }
TEST_F(HlslGeneratorImplTest, NameConflictWith_InputStructName) { TEST_F(HlslGeneratorImplTest, NameConflictWith_InputStructName) {
ast::Module m; ASSERT_EQ(gen().generate_name("func_main_in"), "func_main_in");
GeneratorImpl g(&m);
ASSERT_EQ(g.generate_name("func_main_in"), "func_main_in");
ast::IdentifierExpression ident("func_main_in"); ast::IdentifierExpression ident("func_main_in");
ASSERT_TRUE(g.EmitIdentifier(&ident)); ASSERT_TRUE(gen().EmitIdentifier(out(), &ident));
EXPECT_EQ(g.result(), "func_main_in_0"); EXPECT_EQ(result(), "func_main_in_0");
} }
struct HlslBuiltinData { struct HlslBuiltinData {
@ -82,13 +71,12 @@ inline std::ostream& operator<<(std::ostream& out, HlslBuiltinData data) {
out << data.builtin; out << data.builtin;
return out; return out;
} }
using HlslBuiltinConversionTest = testing::TestWithParam<HlslBuiltinData>; class HlslBuiltinConversionTest
: public TestHelper,
public testing::TestWithParam<HlslBuiltinData> {};
TEST_P(HlslBuiltinConversionTest, Emit) { TEST_P(HlslBuiltinConversionTest, Emit) {
auto params = GetParam(); auto params = GetParam();
EXPECT_EQ(gen().builtin_to_attribute(params.builtin),
ast::Module m;
GeneratorImpl g(&m);
EXPECT_EQ(g.builtin_to_attribute(params.builtin),
std::string(params.attribute_name)); std::string(params.attribute_name));
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(

View File

@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "gtest/gtest.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/struct.h" #include "src/ast/struct.h"
#include "src/ast/struct_decoration.h" #include "src/ast/struct_decoration.h"
@ -29,170 +28,142 @@
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_Type : public TestHelper, public testing::Test {};
TEST_F(HlslGeneratorImplTest, EmitType_Alias) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::AliasType alias("alias", &f32); ast::type::AliasType alias("alias", &f32);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &alias, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "alias");
ASSERT_TRUE(g.EmitType(&alias, "")) << g.error();
EXPECT_EQ(g.result(), "alias");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Alias_NameCollision) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias_NameCollision) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::AliasType alias("bool", &f32); ast::type::AliasType alias("bool", &f32);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &alias, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool_tint_0");
ASSERT_TRUE(g.EmitType(&alias, "")) << g.error();
EXPECT_EQ(g.result(), "bool_tint_0");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Array) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &a, "ary")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool ary[4]");
ASSERT_TRUE(g.EmitType(&a, "ary")) << g.error();
EXPECT_EQ(g.result(), "bool ary[4]");
} }
TEST_F(HlslGeneratorImplTest, EmitType_ArrayOfArray) { TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::type::ArrayType c(&a, 5); ast::type::ArrayType c(&a, 5);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &c, "ary")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool ary[5][4]");
ASSERT_TRUE(g.EmitType(&c, "ary")) << g.error();
EXPECT_EQ(g.result(), "bool ary[5][4]");
} }
// TODO(dsinclair): Is this possible? What order should it output in? // TODO(dsinclair): Is this possible? What order should it output in?
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) { TEST_F(HlslGeneratorImplTest_Type,
DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::type::ArrayType c(&a, 5); ast::type::ArrayType c(&a, 5);
ast::type::ArrayType d(&c); ast::type::ArrayType d(&c);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &c, "ary")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool ary[5][4][1]");
ASSERT_TRUE(g.EmitType(&c, "ary")) << g.error();
EXPECT_EQ(g.result(), "bool ary[5][4][1]");
} }
TEST_F(HlslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) { TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::type::ArrayType c(&a, 5); ast::type::ArrayType c(&a, 5);
ast::type::ArrayType d(&c, 6); ast::type::ArrayType d(&c, 6);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &d, "ary")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool ary[6][5][4]");
ASSERT_TRUE(g.EmitType(&d, "ary")) << g.error();
EXPECT_EQ(g.result(), "bool ary[6][5][4]");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Array_NameCollision) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_NameCollision) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &a, "bool")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool bool_tint_0[4]");
ASSERT_TRUE(g.EmitType(&a, "bool")) << g.error();
EXPECT_EQ(g.result(), "bool bool_tint_0[4]");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Array_WithoutName) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b, 4); ast::type::ArrayType a(&b, 4);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &a, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool[4]");
ASSERT_TRUE(g.EmitType(&a, "")) << g.error();
EXPECT_EQ(g.result(), "bool[4]");
} }
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_RuntimeArray) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_RuntimeArray) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b); ast::type::ArrayType a(&b);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &a, "ary")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool ary[]");
ASSERT_TRUE(g.EmitType(&a, "ary")) << g.error();
EXPECT_EQ(g.result(), "bool ary[]");
} }
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_RuntimeArray_NameCollision) { TEST_F(HlslGeneratorImplTest_Type,
DISABLED_EmitType_RuntimeArray_NameCollision) {
ast::type::BoolType b; ast::type::BoolType b;
ast::type::ArrayType a(&b); ast::type::ArrayType a(&b);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &a, "double")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool double_tint_0[]");
ASSERT_TRUE(g.EmitType(&a, "double")) << g.error();
EXPECT_EQ(g.result(), "bool double_tint_0[]");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Bool) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Bool) {
ast::type::BoolType b; ast::type::BoolType b;
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &b, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "bool");
ASSERT_TRUE(g.EmitType(&b, "")) << g.error();
EXPECT_EQ(g.result(), "bool");
} }
TEST_F(HlslGeneratorImplTest, EmitType_F32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_F32) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &f32, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "float");
ASSERT_TRUE(g.EmitType(&f32, "")) << g.error();
EXPECT_EQ(g.result(), "float");
} }
TEST_F(HlslGeneratorImplTest, EmitType_I32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_I32) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &i32, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "int");
ASSERT_TRUE(g.EmitType(&i32, "")) << g.error();
EXPECT_EQ(g.result(), "int");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Matrix) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::MatrixType m(&f32, 3, 2); ast::type::MatrixType m(&f32, 3, 2);
ast::Module mod; ASSERT_TRUE(gen().EmitType(out(), &m, "")) << gen().error();
GeneratorImpl g(&mod); EXPECT_EQ(result(), "matrix<float, 3, 2>");
ASSERT_TRUE(g.EmitType(&m, "")) << g.error();
EXPECT_EQ(g.result(), "matrix<float, 3, 2>");
} }
// TODO(dsinclair): How to annotate as workgroup? // TODO(dsinclair): How to annotate as workgroup?
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Pointer) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Pointer) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::PointerType p(&f32, ast::StorageClass::kWorkgroup); ast::type::PointerType p(&f32, ast::StorageClass::kWorkgroup);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &p, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "float*");
ASSERT_TRUE(g.EmitType(&p, "")) << g.error();
EXPECT_EQ(g.result(), "float*");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Struct) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -210,16 +181,14 @@ TEST_F(HlslGeneratorImplTest, EmitType_Struct) {
ast::type::StructType s(std::move(str)); ast::type::StructType s(std::move(str));
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(struct {
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
EXPECT_EQ(g.result(), R"(struct {
int a; int a;
float b; float b;
})"); })");
} }
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -243,10 +212,8 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) {
ast::type::StructType s(std::move(str)); ast::type::StructType s(std::move(str));
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(struct {
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
EXPECT_EQ(g.result(), R"(struct {
int8_t pad_0[4]; int8_t pad_0[4];
int a; int a;
int8_t pad_1[24]; int8_t pad_1[24];
@ -256,7 +223,7 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) {
})"); })");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Struct_NameCollision) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct_NameCollision) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -273,17 +240,15 @@ TEST_F(HlslGeneratorImplTest, EmitType_Struct_NameCollision) {
ast::type::StructType s(std::move(str)); ast::type::StructType s(std::move(str));
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(struct {
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
EXPECT_EQ(g.result(), R"(struct {
int double_tint_0; int double_tint_0;
float float_tint_0; float float_tint_0;
})"); })");
} }
// TODO(dsinclair): How to translate [[block]] // TODO(dsinclair): How to translate [[block]]
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_WithDecoration) {
ast::type::I32Type i32; ast::type::I32Type i32;
ast::type::F32Type f32; ast::type::F32Type f32;
@ -302,41 +267,33 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
ast::type::StructType s(std::move(str)); ast::type::StructType s(std::move(str));
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), R"(struct {
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
EXPECT_EQ(g.result(), R"(struct {
int a; int a;
float b; float b;
})"); })");
} }
TEST_F(HlslGeneratorImplTest, EmitType_U32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_U32) {
ast::type::U32Type u32; ast::type::U32Type u32;
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &u32, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "uint");
ASSERT_TRUE(g.EmitType(&u32, "")) << g.error();
EXPECT_EQ(g.result(), "uint");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Vector) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Vector) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType v(&f32, 3); ast::type::VectorType v(&f32, 3);
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &v, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "vector<float, 3>");
ASSERT_TRUE(g.EmitType(&v, "")) << g.error();
EXPECT_EQ(g.result(), "vector<float, 3>");
} }
TEST_F(HlslGeneratorImplTest, EmitType_Void) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) {
ast::type::VoidType v; ast::type::VoidType v;
ast::Module m; ASSERT_TRUE(gen().EmitType(out(), &v, "")) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), "void");
ASSERT_TRUE(g.EmitType(&v, "")) << g.error();
EXPECT_EQ(g.result(), "void");
} }
} // namespace } // namespace

View File

@ -15,11 +15,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/unary_op_expression.h" #include "src/ast/unary_op_expression.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -34,19 +33,18 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
out << data.op; out << data.op;
return out; return out;
} }
using HlslUnaryOpTest = testing::TestWithParam<UnaryOpData>; class HlslUnaryOpTest : public TestHelper,
public testing::TestWithParam<UnaryOpData> {};
TEST_P(HlslUnaryOpTest, Emit) { TEST_P(HlslUnaryOpTest, Emit) {
auto params = GetParam(); auto params = GetParam();
auto expr = std::make_unique<ast::IdentifierExpression>("expr"); auto expr = std::make_unique<ast::IdentifierExpression>("expr");
ast::UnaryOpExpression op(params.op, std::move(expr)); ast::UnaryOpExpression op(params.op, std::move(expr));
ast::Module m; ASSERT_TRUE(gen().EmitExpression(out(), &op)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), std::string(params.name) + "(expr)");
ASSERT_TRUE(g.EmitExpression(&op)) << g.error();
EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_UnaryOp,
HlslUnaryOpTest, HlslUnaryOpTest,
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot}, testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", UnaryOpData{"-",

View File

@ -15,7 +15,6 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "gtest/gtest.h"
#include "src/ast/identifier_expression.h" #include "src/ast/identifier_expression.h"
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/ast/type/array_type.h" #include "src/ast/type/array_type.h"
@ -23,47 +22,42 @@
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/variable.h" #include "src/ast/variable.h"
#include "src/ast/variable_decl_statement.h" #include "src/ast/variable_decl_statement.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/test_helper.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
using HlslGeneratorImplTest = testing::Test; class HlslGeneratorImplTest_VariableDecl : public TestHelper,
public testing::Test {};
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto var = auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32); std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " float a;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Const) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto var = auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32); std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
var->set_is_const(true); var->set_is_const(true);
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " const float a;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " const float a;\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::ArrayType ary(&f32, 5); ast::type::ArrayType ary(&f32, 5);
@ -71,46 +65,39 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary); std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary);
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " float a[5];\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a[5];\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Function) { TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Function) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto var = auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32); std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " float a;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Private) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
ast::type::F32Type f32; ast::type::F32Type f32;
auto var = auto var =
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32); std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
gen().increment_indent();
ast::Module m; ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
GeneratorImpl g(&m); EXPECT_EQ(result(), " float a;\n");
g.increment_indent();
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) { TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Initializer_Private) {
auto ident = std::make_unique<ast::IdentifierExpression>("initializer"); auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
ast::type::F32Type f32; ast::type::F32Type f32;
@ -119,15 +106,13 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
var->set_constructor(std::move(ident)); var->set_constructor(std::move(ident));
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
ast::Module m; EXPECT_EQ(result(), R"(float a = initializer;
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) { TEST_F(HlslGeneratorImplTest_VariableDecl,
Emit_VariableDeclStatement_Initializer_ZeroVec) {
ast::type::F32Type f32; ast::type::F32Type f32;
ast::type::VectorType vec(&f32, 3); ast::type::VectorType vec(&f32, 3);
@ -140,11 +125,8 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
var->set_constructor(std::move(zero_vec)); var->set_constructor(std::move(zero_vec));
ast::VariableDeclStatement stmt(std::move(var)); ast::VariableDeclStatement stmt(std::move(var));
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
ast::Module m; EXPECT_EQ(result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
GeneratorImpl g(&m);
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
EXPECT_EQ(g.result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
)"); )");
} }

View File

@ -0,0 +1,27 @@
// 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 "src/writer/hlsl/test_helper.h"
namespace tint {
namespace writer {
namespace hlsl {
TestHelper::TestHelper() : td_(&ctx_, &mod_), impl_(&mod_) {}
TestHelper::~TestHelper() = default;
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -0,0 +1,63 @@
// 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.
#ifndef SRC_WRITER_HLSL_TEST_HELPER_H_
#define SRC_WRITER_HLSL_TEST_HELPER_H_
#include <sstream>
#include "gtest/gtest.h"
#include "src/ast/module.h"
#include "src/context.h"
#include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h"
namespace tint {
namespace writer {
namespace hlsl {
/// Helper class for testing
class TestHelper {
public:
TestHelper();
~TestHelper();
/// @returns the generator implementation
GeneratorImpl& gen() { return impl_; }
/// @returns the module
ast::Module* mod() { return &mod_; }
/// @returns the type determiner
TypeDeterminer& td() { return td_; }
/// @returns the output stream
std::ostream& out() { return out_; }
/// @returns the result string
std::string result() const { return out_.str(); }
private:
Context ctx_;
ast::Module mod_;
TypeDeterminer td_;
GeneratorImpl impl_;
std::ostringstream out_;
};
} // namespace hlsl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_HLSL_TEST_HELPER_H_