Convert the WGSL generator to utils::StringStream.

This CL updates the WGSL generator to use utils::StringStream
internally.

Bug: tint:1686
Change-Id: I7387a583f9cba3c6a955f5be376adc141e3ee394
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121941
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
dan sinclair 2023-02-28 15:06:19 +00:00 committed by Dawn LUCI CQ
parent ec24bb2f0c
commit 88fea2a9c3
14 changed files with 105 additions and 90 deletions

View File

@ -13,6 +13,7 @@
// limitations under the License.
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/generator_impl.h"
namespace tint::reader::spirv::test {
@ -54,7 +55,7 @@ std::string ToString(const Program& program, const ast::Node* node) {
return Switch(
node,
[&](const ast::Expression* expr) {
std::stringstream out;
utils::StringStream out;
if (!writer.EmitExpression(out, expr)) {
return "WGSL writer error: " + writer.error();
}

View File

@ -89,7 +89,7 @@ bool GeneratorImpl::Generate() {
return true;
}
bool GeneratorImpl::EmitDiagnosticControl(std::ostream& out,
bool GeneratorImpl::EmitDiagnosticControl(utils::StringStream& out,
const ast::DiagnosticControl& diagnostic) {
out << "diagnostic(" << diagnostic.severity << ", "
<< program_->Symbols().NameFor(diagnostic.rule_name->symbol) << ")";
@ -124,7 +124,7 @@ bool GeneratorImpl::EmitTypeDecl(const ast::TypeDecl* ty) {
});
}
bool GeneratorImpl::EmitExpression(std::ostream& out, const ast::Expression* expr) {
bool GeneratorImpl::EmitExpression(utils::StringStream& out, const ast::Expression* expr) {
return Switch(
expr,
[&](const ast::IndexAccessorExpression* a) { //
@ -161,7 +161,8 @@ bool GeneratorImpl::EmitExpression(std::ostream& out, const ast::Expression* exp
});
}
bool GeneratorImpl::EmitIndexAccessor(std::ostream& out, const ast::IndexAccessorExpression* expr) {
bool GeneratorImpl::EmitIndexAccessor(utils::StringStream& out,
const ast::IndexAccessorExpression* expr) {
bool paren_lhs =
!expr->object
->IsAnyOf<ast::AccessorExpression, ast::CallExpression, ast::IdentifierExpression>();
@ -184,7 +185,7 @@ bool GeneratorImpl::EmitIndexAccessor(std::ostream& out, const ast::IndexAccesso
return true;
}
bool GeneratorImpl::EmitMemberAccessor(std::ostream& out,
bool GeneratorImpl::EmitMemberAccessor(utils::StringStream& out,
const ast::MemberAccessorExpression* expr) {
bool paren_lhs =
!expr->object
@ -203,7 +204,7 @@ bool GeneratorImpl::EmitMemberAccessor(std::ostream& out,
return true;
}
bool GeneratorImpl::EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr) {
bool GeneratorImpl::EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr) {
out << "bitcast<";
if (!EmitExpression(out, expr->type)) {
return false;
@ -218,7 +219,7 @@ bool GeneratorImpl::EmitBitcast(std::ostream& out, const ast::BitcastExpression*
return true;
}
bool GeneratorImpl::EmitCall(std::ostream& out, const ast::CallExpression* expr) {
bool GeneratorImpl::EmitCall(utils::StringStream& out, const ast::CallExpression* expr) {
if (!EmitExpression(out, expr->target)) {
return false;
}
@ -242,7 +243,7 @@ bool GeneratorImpl::EmitCall(std::ostream& out, const ast::CallExpression* expr)
return true;
}
bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression* lit) {
bool GeneratorImpl::EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* lit) {
return Switch(
lit,
[&](const ast::BoolLiteralExpression* l) { //
@ -271,11 +272,12 @@ bool GeneratorImpl::EmitLiteral(std::ostream& out, const ast::LiteralExpression*
});
}
bool GeneratorImpl::EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr) {
bool GeneratorImpl::EmitIdentifier(utils::StringStream& out,
const ast::IdentifierExpression* expr) {
return EmitIdentifier(out, expr->identifier);
}
bool GeneratorImpl::EmitIdentifier(std::ostream& out, const ast::Identifier* ident) {
bool GeneratorImpl::EmitIdentifier(utils::StringStream& out, const ast::Identifier* ident) {
if (auto* tmpl_ident = ident->As<ast::TemplatedIdentifier>()) {
if (!tmpl_ident->attributes.IsEmpty()) {
EmitAttributes(out, tmpl_ident->attributes);
@ -363,7 +365,7 @@ bool GeneratorImpl::EmitFunction(const ast::Function* func) {
return true;
}
bool GeneratorImpl::EmitImageFormat(std::ostream& out, const builtin::TexelFormat fmt) {
bool GeneratorImpl::EmitImageFormat(utils::StringStream& out, const builtin::TexelFormat fmt) {
switch (fmt) {
case builtin::TexelFormat::kUndefined:
diagnostics_.add_error(diag::System::Writer, "unknown image format");
@ -441,7 +443,7 @@ bool GeneratorImpl::EmitStructType(const ast::Struct* str) {
return true;
}
bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) {
bool GeneratorImpl::EmitVariable(utils::StringStream& out, const ast::Variable* v) {
if (!v->attributes.IsEmpty()) {
if (!EmitAttributes(out, v->attributes)) {
return false;
@ -508,7 +510,7 @@ bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) {
return true;
}
bool GeneratorImpl::EmitAttributes(std::ostream& out,
bool GeneratorImpl::EmitAttributes(utils::StringStream& out,
utils::VectorRef<const ast::Attribute*> attrs) {
bool first = true;
for (auto* attr : attrs) {
@ -650,7 +652,7 @@ bool GeneratorImpl::EmitAttributes(std::ostream& out,
return true;
}
bool GeneratorImpl::EmitBinary(std::ostream& out, const ast::BinaryExpression* expr) {
bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr) {
out << "(";
if (!EmitExpression(out, expr->lhs)) {
@ -670,7 +672,7 @@ bool GeneratorImpl::EmitBinary(std::ostream& out, const ast::BinaryExpression* e
return true;
}
bool GeneratorImpl::EmitBinaryOp(std::ostream& out, const ast::BinaryOp op) {
bool GeneratorImpl::EmitBinaryOp(utils::StringStream& out, const ast::BinaryOp op) {
switch (op) {
case ast::BinaryOp::kAnd:
out << "&";
@ -733,7 +735,7 @@ bool GeneratorImpl::EmitBinaryOp(std::ostream& out, const ast::BinaryOp op) {
return true;
}
bool GeneratorImpl::EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr) {
bool GeneratorImpl::EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpression* expr) {
switch (expr->op) {
case ast::UnaryOp::kAddressOf:
out << "&";
@ -777,7 +779,7 @@ bool GeneratorImpl::EmitBlock(const ast::BlockStatement* stmt) {
return true;
}
bool GeneratorImpl::EmitBlockHeader(std::ostream& out, const ast::BlockStatement* stmt) {
bool GeneratorImpl::EmitBlockHeader(utils::StringStream& out, const ast::BlockStatement* stmt) {
if (!stmt->attributes.IsEmpty()) {
if (!EmitAttributes(out, stmt->attributes)) {
return false;

View File

@ -35,6 +35,7 @@
#include "src/tint/ast/unary_op_expression.h"
#include "src/tint/program.h"
#include "src/tint/sem/struct.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/text_generator.h"
namespace tint::writer::wgsl {
@ -55,7 +56,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param diagnostic the diagnostic control node
/// @returns true if the diagnostic control was emitted
bool EmitDiagnosticControl(std::ostream& out, const ast::DiagnosticControl& diagnostic);
bool EmitDiagnosticControl(utils::StringStream& out, const ast::DiagnosticControl& diagnostic);
/// Handles generating an enable directive
/// @param enable the enable node
/// @returns true if the enable directive was emitted
@ -68,7 +69,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the expression to emit
/// @returns true if the index accessor was emitted
bool EmitIndexAccessor(std::ostream& out, const ast::IndexAccessorExpression* expr);
bool EmitIndexAccessor(utils::StringStream& out, const ast::IndexAccessorExpression* expr);
/// Handles an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
@ -77,17 +78,17 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the binary expression
/// @returns true if the expression was emitted, false otherwise
bool EmitBinary(std::ostream& out, const ast::BinaryExpression* expr);
bool EmitBinary(utils::StringStream& out, const ast::BinaryExpression* expr);
/// Handles generating a binary operator
/// @param out the output stream
/// @param op the binary operator
/// @returns true if the operator was emitted, false otherwise
bool EmitBinaryOp(std::ostream& out, const ast::BinaryOp op);
bool EmitBinaryOp(utils::StringStream& out, const ast::BinaryOp op);
/// Handles generating a bitcast expression
/// @param out the output stream
/// @param expr the bitcast expression
/// @returns true if the bitcast was emitted
bool EmitBitcast(std::ostream& out, const ast::BitcastExpression* expr);
bool EmitBitcast(utils::StringStream& out, const ast::BitcastExpression* expr);
/// Handles a block statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
@ -96,7 +97,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream to write the header to
/// @param stmt the block statement to emit the header for
/// @returns true if the statement was emitted successfully
bool EmitBlockHeader(std::ostream& out, const ast::BlockStatement* stmt);
bool EmitBlockHeader(utils::StringStream& out, const ast::BlockStatement* stmt);
/// Handles a break statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
@ -109,7 +110,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the call expression
/// @returns true if the call expression is emitted
bool EmitCall(std::ostream& out, const ast::CallExpression* expr);
bool EmitCall(utils::StringStream& out, const ast::CallExpression* expr);
/// Handles a case statement
/// @param stmt the statement
/// @returns true if the statment was emitted successfully
@ -122,7 +123,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the literal expression expression
/// @returns true if the literal expression is emitted
bool EmitLiteral(std::ostream& out, const ast::LiteralExpression* expr);
bool EmitLiteral(utils::StringStream& out, const ast::LiteralExpression* expr);
/// Handles a continue statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
@ -131,7 +132,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the expression
/// @returns true if the expression was emitted
bool EmitExpression(std::ostream& out, const ast::Expression* expr);
bool EmitExpression(utils::StringStream& out, const ast::Expression* expr);
/// Handles generating a function
/// @param func the function to generate
/// @returns true if the function was emitted
@ -140,12 +141,12 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the identifier expression
/// @returns true if the identifier was emitted
bool EmitIdentifier(std::ostream& out, const ast::IdentifierExpression* expr);
bool EmitIdentifier(utils::StringStream& out, const ast::IdentifierExpression* expr);
/// Handles generating an identifier
/// @param out the output of the expression stream
/// @param ident the identifier
/// @returns true if the identifier was emitted
bool EmitIdentifier(std::ostream& out, const ast::Identifier* ident);
bool EmitIdentifier(utils::StringStream& out, const ast::Identifier* ident);
/// Handles an if statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
@ -174,7 +175,7 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param expr the member accessor expression
/// @returns true if the member accessor was emitted
bool EmitMemberAccessor(std::ostream& out, const ast::MemberAccessorExpression* expr);
bool EmitMemberAccessor(utils::StringStream& out, const ast::MemberAccessorExpression* expr);
/// Handles return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
@ -207,22 +208,22 @@ class GeneratorImpl : public TextGenerator {
/// @param out the output stream
/// @param fmt the format to generate
/// @returns true if the format is emitted
bool EmitImageFormat(std::ostream& out, const builtin::TexelFormat fmt);
bool EmitImageFormat(utils::StringStream& out, const builtin::TexelFormat fmt);
/// Handles a unary op expression
/// @param out the output stream
/// @param expr the expression to emit
/// @returns true if the expression was emitted
bool EmitUnaryOp(std::ostream& out, const ast::UnaryOpExpression* expr);
bool EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpression* expr);
/// Handles generating a variable
/// @param out the output stream
/// @param var the variable to generate
/// @returns true if the variable was emitted
bool EmitVariable(std::ostream& out, const ast::Variable* var);
bool EmitVariable(utils::StringStream& out, const ast::Variable* var);
/// Handles generating a attribute list
/// @param out the output stream
/// @param attrs the attribute list
/// @returns true if the attributes were emitted
bool EmitAttributes(std::ostream& out, utils::VectorRef<const ast::Attribute*> attrs);
bool EmitAttributes(utils::StringStream& out, utils::VectorRef<const ast::Attribute*> attrs);
};
} // namespace tint::writer::wgsl

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -28,7 +29,7 @@ TEST_F(WgslGeneratorImplTest, IndexAccessor) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "ary[5i]");
}
@ -42,7 +43,7 @@ TEST_F(WgslGeneratorImplTest, IndexAccessor_OfDref) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "(*(p))[5i]");
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
namespace tint::writer::wgsl {
@ -47,7 +48,7 @@ TEST_P(WgslBinaryTest, Emit) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), params.result);
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -27,7 +28,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Bitcast) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error();
EXPECT_EQ(out.str(), "bitcast<f32>(1i)");
}

View File

@ -13,6 +13,7 @@
// limitations under the License.
#include "src/tint/ast/call_statement.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -33,7 +34,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "my_func()");
}
@ -56,7 +57,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Call_WithParams) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "my_func(param1, param2)");
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -27,7 +28,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Scalar_F32_From_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "f32(1i)");
}
@ -40,7 +41,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Scalar_F16_From_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "f16(1i)");
}
@ -51,7 +52,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Vector_F32_From_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "vec3<f32>(vec3<i32>(1i, 2i, 3i))");
}
@ -64,7 +65,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_Cast_Vector_F16_From_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "vec3<f16>(vec3<i32>(1i, 2i, 3i))");
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
namespace tint::writer::wgsl {
@ -26,7 +27,7 @@ TEST_F(WgslGeneratorImplTest, EmitIdentifierExpression_Single) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, i)) << gen.error();
EXPECT_EQ(out.str(), "glsl");
}

View File

@ -14,6 +14,7 @@
#include <cstring>
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -114,7 +115,7 @@ TEST_P(WgslGenerator_F32LiteralTest, Emit) {
SetResolveOnBuild(false);
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitLiteral(out, v)) << gen.error();
EXPECT_EQ(out.str(), GetParam().expected);
}
@ -162,7 +163,7 @@ TEST_P(WgslGenerator_F16LiteralTest, Emit) {
SetResolveOnBuild(false);
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitLiteral(out, v)) << gen.error();
EXPECT_EQ(out.str(), GetParam().expected);
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
namespace tint::writer::wgsl {
@ -28,7 +29,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "str.mem");
}
@ -43,7 +44,7 @@ TEST_F(WgslGeneratorImplTest, EmitExpression_MemberAccessor_OfDref) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "(*(p)).mem");
}

View File

@ -17,6 +17,7 @@
#include "src/tint/type/multisampled_texture.h"
#include "src/tint/type/sampled_texture.h"
#include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -32,7 +33,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Alias) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "alias");
}
@ -42,7 +43,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "array<bool, 4u>");
}
@ -53,7 +54,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Array_Attribute) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "@stride(16) array<bool, 4u>");
}
@ -63,7 +64,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_RuntimeArray) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "array<bool>");
}
@ -73,7 +74,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Bool) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "bool");
}
@ -83,7 +84,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_F32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "f32");
}
@ -95,7 +96,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_F16) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "f16");
}
@ -105,7 +106,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "i32");
}
@ -115,7 +116,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Matrix_F32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "mat2x3<f32>");
}
@ -127,7 +128,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Matrix_F16) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "mat2x3<f16>");
}
@ -138,7 +139,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Pointer) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "ptr<workgroup, f32>");
}
@ -150,7 +151,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_PointerAccessMode) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "ptr<storage, f32, read_write>");
}
@ -164,7 +165,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Struct) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "S");
}
@ -291,7 +292,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_U32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "u32");
}
@ -301,7 +302,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Vector_F32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "vec3<f32>");
}
@ -313,7 +314,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Vector_F16) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "vec3<f16>");
}
@ -335,7 +336,7 @@ TEST_P(WgslGenerator_DepthTextureTest, EmitType_DepthTexture) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), param.name);
}
@ -356,7 +357,7 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_F32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<f32>");
}
@ -369,7 +370,7 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<i32>");
}
@ -382,7 +383,7 @@ TEST_P(WgslGenerator_SampledTextureTest, EmitType_SampledTexture_U32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<u32>");
}
@ -405,7 +406,7 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_F32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<f32>");
}
@ -418,7 +419,7 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_I32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<i32>");
}
@ -431,7 +432,7 @@ TEST_P(WgslGenerator_MultiampledTextureTest, EmitType_MultisampledTexture_U32) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.name) + "<u32>");
}
@ -459,7 +460,7 @@ TEST_P(WgslGenerator_StorageTextureTest, EmitType_StorageTexture) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), param.name);
}
@ -490,7 +491,7 @@ TEST_P(WgslGenerator_ImageFormatTest, EmitType_StorageTexture_ImageFormat) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitImageFormat(out, param.fmt)) << gen.error();
EXPECT_EQ(out.str(), param.name);
}
@ -521,7 +522,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_Sampler) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "sampler");
}
@ -532,7 +533,7 @@ TEST_F(WgslGeneratorImplTest, EmitType_SamplerComparison) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, type)) << gen.error();
EXPECT_EQ(out.str(), "sampler_comparison");
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
namespace tint::writer::wgsl {
@ -26,7 +27,7 @@ TEST_F(WgslUnaryOpTest, AddressOf) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "&(expr)");
}
@ -38,7 +39,7 @@ TEST_F(WgslUnaryOpTest, Complement) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "~(expr)");
}
@ -51,7 +52,7 @@ TEST_F(WgslUnaryOpTest, Indirection) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "*(expr)");
}
@ -63,7 +64,7 @@ TEST_F(WgslUnaryOpTest, Not) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "!(expr)");
}
@ -75,7 +76,7 @@ TEST_F(WgslUnaryOpTest, Negation) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "-(expr)");
}

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/tint/utils/string_stream.h"
#include "src/tint/writer/wgsl/test_helper.h"
using namespace tint::number_suffixes; // NOLINT
@ -26,7 +27,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(var<private> a : f32;)");
}
@ -36,7 +37,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_AddressSpace) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(var<private> a : f32;)");
}
@ -48,7 +49,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_Read) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(@binding(0) @group(0) var<storage, read> a : S;)");
}
@ -60,7 +61,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Access_ReadWrite) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(@binding(0) @group(0) var<storage, read_write> a : S;)");
}
@ -70,7 +71,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(@group(1) @binding(2) var a : sampler;)");
}
@ -80,7 +81,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Initializer) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(var<private> a : f32 = 1.0f;)");
}
@ -91,7 +92,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Explicit) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(let a : f32 = 1.0f;)");
}
@ -102,7 +103,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Inferred) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(let a = 1.0f;)");
}
@ -113,7 +114,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Explicit) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(const a : f32 = 1.0f;)");
}
@ -124,7 +125,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Inferred) {
GeneratorImpl& gen = Build();
std::stringstream out;
utils::StringStream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(const a = 1.0f;)");
}