Convert TextGenerator over to utils::StringStream.

This CL updates the TextGenerator base class to use utils::StringStream.

Bug: tint:1686
Change-Id: Ie36f55cf32e63773c4bee2cf89cdef19ab1fba28
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121960
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:14:09 +00:00 committed by Dawn LUCI CQ
parent 88fea2a9c3
commit b2ba57b15d
6 changed files with 60 additions and 68 deletions

View File

@ -103,10 +103,6 @@ class StringStream {
/// @returns the string contents of the stream
std::string str() const { return sstream_.str(); }
/// [DEPRECATED] This should not be called.
/// @returns the underlying stream
std::ostream& stream() { return sstream_; }
private:
std::stringstream sstream_;
};

View File

@ -381,7 +381,7 @@ bool GeneratorImpl::EmitBitcast(utils::StringStream& out, const ast::BitcastExpr
return false;
}
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->expr)) {
return false;
}
@ -425,7 +425,7 @@ bool GeneratorImpl::EmitVectorRelational(utils::StringStream& out,
default:
break;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -445,14 +445,14 @@ bool GeneratorImpl::EmitBitwiseBoolOp(utils::StringStream& out, const ast::Binar
"")) {
return false;
}
ScopedParen outerCastParen(out.stream());
ScopedParen outerCastParen(out);
// Cast LHS to uint scalar or vector type.
if (!EmitType(out, uint_type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite,
"")) {
return false;
}
{
ScopedParen innerCastParen(out.stream());
ScopedParen innerCastParen(out);
// Emit LHS.
if (!EmitExpression(out, expr->lhs)) {
return false;
@ -473,7 +473,7 @@ bool GeneratorImpl::EmitBitwiseBoolOp(utils::StringStream& out, const ast::Binar
return false;
}
{
ScopedParen innerCastParen(out.stream());
ScopedParen innerCastParen(out);
// Emit RHS.
if (!EmitExpression(out, expr->rhs)) {
return false;
@ -532,7 +532,7 @@ bool GeneratorImpl::EmitFloatModulo(utils::StringStream& out, const ast::BinaryE
// Call the helper
out << fn;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -592,7 +592,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
return EmitFloatModulo(out, expr);
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -731,7 +731,7 @@ bool GeneratorImpl::EmitFunctionCall(utils::StringStream& out,
auto* ident = fn->Declaration()->name;
out << builder_.Symbols().NameFor(ident->symbol);
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : args) {
@ -813,7 +813,7 @@ bool GeneratorImpl::EmitBuiltinCall(utils::StringStream& out,
}
out << name;
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->Arguments()) {
@ -837,7 +837,7 @@ bool GeneratorImpl::EmitValueConversion(utils::StringStream& out,
builtin::Access::kReadWrite, "")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, call->Arguments()[0]->Declaration())) {
return false;
@ -860,7 +860,7 @@ bool GeneratorImpl::EmitValueConstructor(utils::StringStream& out,
if (!EmitType(out, type, builtin::AddressSpace::kUndefined, builtin::Access::kReadWrite, "")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->Arguments()) {
@ -883,7 +883,7 @@ bool GeneratorImpl::EmitWorkgroupAtomicCall(utils::StringStream& out,
auto call = [&](const char* name) {
out << name;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < expr->args.Length(); i++) {
auto* arg = expr->args[i];
if (i > 0) {
@ -903,7 +903,7 @@ bool GeneratorImpl::EmitWorkgroupAtomicCall(utils::StringStream& out,
// atomicOr using 0 as the OR value
out << "atomicOr";
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
}
@ -1101,7 +1101,7 @@ bool GeneratorImpl::EmitSelectCall(utils::StringStream& out, const ast::CallExpr
out << ")";
return true;
}
ScopedParen paren(out.stream());
ScopedParen paren(out);
if (!EmitExpression(out, expr_cond)) {
return false;
}
@ -1182,7 +1182,7 @@ bool GeneratorImpl::EmitDotCall(utils::StringStream& out,
}
out << fn;
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
@ -1378,7 +1378,7 @@ bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
return EmitExpression(out, e);
}
emit_signed_int_type(ty);
ScopedParen sp(out.stream());
ScopedParen sp(out);
return EmitExpression(out, e);
};
@ -1388,7 +1388,7 @@ bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
// textureSize() / imageSize() returns a signed scalar / vector in GLSL.
// Cast.
emit_unsigned_int_type(call->Type());
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (texture_type->Is<type::StorageTexture>()) {
out << "imageSize(";
@ -1427,7 +1427,7 @@ bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
// textureSize() / imageSize() returns a signed scalar / vector in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (texture_type->Is<type::StorageTexture>()) {
out << "imageSize(";
@ -1461,7 +1461,7 @@ bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
// textureQueryLevels() returns a signed scalar in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out.stream());
ScopedParen sp(out);
out << "textureQueryLevels(";
if (!EmitExpression(out, texture)) {
@ -1475,7 +1475,7 @@ bool GeneratorImpl::EmitTextureCall(utils::StringStream& out,
// textureSamples() returns a signed scalar in GLSL.
// Cast.
out << "uint";
ScopedParen sp(out.stream());
ScopedParen sp(out);
out << "textureSamples(";
if (!EmitExpression(out, texture)) {
@ -2367,7 +2367,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (constant->AllEqual()) {
return EmitConstant(out, constant->Index(0));
@ -2389,7 +2389,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t column_idx = 0; column_idx < m->columns(); column_idx++) {
if (column_idx > 0) {
@ -2407,7 +2407,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
auto count = a->ConstantCount();
if (!count) {
@ -2434,7 +2434,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
out << StructName(s);
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < s->Members().Length(); i++) {
if (i > 0) {
@ -2499,7 +2499,7 @@ bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* ty
"")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (uint32_t i = 0; i < vec->Width(); i++) {
if (i != 0) {
out << ", ";
@ -2513,7 +2513,7 @@ bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* ty
"")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) {
if (i != 0) {
out << ", ";
@ -2528,7 +2528,7 @@ bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* ty
return false;
}
bool first = true;
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (auto* member : str->Members()) {
if (!first) {
out << ", ";
@ -2542,7 +2542,7 @@ bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* ty
"")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
auto count = arr->ConstantCount();
if (!count) {
@ -3111,7 +3111,7 @@ bool GeneratorImpl::EmitUnaryOp(utils::StringStream& out, const ast::UnaryOpExpr
break;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->expr)) {
return false;
}
@ -3243,7 +3243,7 @@ bool GeneratorImpl::CallBuiltinHelper(utils::StringStream& out,
// Call the helper
out << fn;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->args) {
if (!first) {

View File

@ -757,7 +757,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
return true;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
@ -1419,7 +1419,7 @@ bool GeneratorImpl::EmitStorageBufferAccess(
if (n > 1) {
out << n;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, offset)) {
return false;
}
@ -1433,7 +1433,7 @@ bool GeneratorImpl::EmitStorageBufferAccess(
// to emit `buffer.Load<float16_t>(offset)`.
auto templated_load = [&](const char* type) {
out << buffer << ".Load<" << type << ">"; // templated load
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, offset)) {
return false;
}
@ -1485,12 +1485,12 @@ bool GeneratorImpl::EmitStorageBufferAccess(
if (n > 1) {
out << n;
}
ScopedParen sp1(out.stream());
ScopedParen sp1(out);
if (!EmitExpression(out, offset)) {
return false;
}
out << ", asuint";
ScopedParen sp2(out.stream());
ScopedParen sp2(out);
if (!EmitExpression(out, value)) {
return false;
}
@ -1501,7 +1501,7 @@ bool GeneratorImpl::EmitStorageBufferAccess(
// to emit `buffer.Store<float16_t>(offset)`.
auto templated_store = [&](const char* type) {
out << buffer << ".Store<" << type << ">"; // templated store
ScopedParen sp1(out.stream());
ScopedParen sp1(out);
if (!EmitExpression(out, offset)) {
return false;
}
@ -1849,7 +1849,7 @@ bool GeneratorImpl::EmitWorkgroupAtomicCall(utils::StringStream& out,
out << "InterlockedExchange";
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
}
@ -1944,7 +1944,7 @@ bool GeneratorImpl::EmitSelectCall(utils::StringStream& out, const ast::CallExpr
auto* expr_false = expr->args[0];
auto* expr_true = expr->args[1];
auto* expr_cond = expr->args[2];
ScopedParen paren(out.stream());
ScopedParen paren(out);
if (!EmitExpression(out, expr_cond)) {
return false;
}
@ -3312,7 +3312,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out,
[&](const type::Vector* v) {
if (constant->AllEqual()) {
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitConstant(out, constant->Index(0), is_variable_initializer)) {
return false;
}
@ -3329,7 +3329,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out,
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < v->Width(); i++) {
if (i > 0) {
@ -3347,7 +3347,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out,
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < m->columns(); i++) {
if (i > 0) {
@ -3507,7 +3507,7 @@ bool GeneratorImpl::EmitValue(utils::StringStream& out, const type::Type* type,
"")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (uint32_t i = 0; i < vec->Width(); i++) {
if (i != 0) {
out << ", ";
@ -3523,7 +3523,7 @@ bool GeneratorImpl::EmitValue(utils::StringStream& out, const type::Type* type,
"")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (uint32_t i = 0; i < (mat->rows() * mat->columns()); i++) {
if (i != 0) {
out << ", ";
@ -4382,7 +4382,7 @@ bool GeneratorImpl::CallBuiltinHelper(utils::StringStream& out,
// Call the helper
out << fn;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->args) {
if (!first) {

View File

@ -525,7 +525,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
// Handle fmod
if (expr->op == ast::BinaryOp::kModulo && lhs_type->is_float_scalar_or_vector()) {
out << "fmod";
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -549,7 +549,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
// WGSL defines behaviour for signed overflow, MSL does not. For these
// cases, bitcast operands to unsigned, then cast result to signed.
ScopedBitCast outer_int_cast(this, out, target_type, signed_type_of(target_type));
ScopedParen sp(out.stream());
ScopedParen sp(out);
{
ScopedBitCast lhs_uint_cast(this, out, lhs_type, unsigned_type_of(target_type));
if (!EmitExpression(out, expr->lhs)) {
@ -576,7 +576,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
// Shift left: discards top bits, so convert first operand to unsigned
// first, then convert result back to signed
ScopedBitCast outer_int_cast(this, out, lhs_type, signed_type_of(lhs_type));
ScopedParen sp(out.stream());
ScopedParen sp(out);
{
ScopedBitCast lhs_uint_cast(this, out, lhs_type, unsigned_type_of(lhs_type));
if (!EmitExpression(out, expr->lhs)) {
@ -595,7 +595,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
// Handle '&' and '|' of booleans.
if ((expr->IsAnd() || expr->IsOr()) && lhs_type->Is<type::Bool>()) {
out << "bool";
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -609,7 +609,7 @@ bool GeneratorImpl::EmitBinary(utils::StringStream& out, const ast::BinaryExpres
}
// Emit as usual
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->lhs)) {
return false;
}
@ -748,7 +748,7 @@ bool GeneratorImpl::EmitBuiltinCall(utils::StringStream& out,
if (sem->Type()->UnwrapRef()->is_scalar()) {
// Emulate scalar overload using fabs(x - y);
out << "fabs";
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (!EmitExpression(out, expr->args[0])) {
return false;
}
@ -866,7 +866,7 @@ bool GeneratorImpl::EmitAtomicCall(utils::StringStream& out,
auto call = [&](const std::string& name, bool append_memory_order_relaxed) {
out << name;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < expr->args.Length(); i++) {
auto* arg = expr->args[i];
if (i > 0) {
@ -1646,7 +1646,7 @@ bool GeneratorImpl::EmitZeroValue(utils::StringStream& out, const type::Type* ty
if (!EmitType(out, mat, "")) {
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
return EmitZeroValue(out, mat->type());
},
[&](const type::Array*) {
@ -1693,7 +1693,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
if (constant->AllEqual()) {
if (!EmitConstant(out, constant->Index(0))) {
@ -1717,7 +1717,7 @@ bool GeneratorImpl::EmitConstant(utils::StringStream& out, const constant::Value
return false;
}
ScopedParen sp(out.stream());
ScopedParen sp(out);
for (size_t i = 0; i < m->columns(); i++) {
if (i > 0) {
@ -3286,7 +3286,7 @@ bool GeneratorImpl::CallBuiltinHelper(utils::StringStream& out,
// Call the helper
out << fn;
{
ScopedParen sp(out.stream());
ScopedParen sp(out);
bool first = true;
for (auto* arg : call->args) {
if (!first) {

View File

@ -114,7 +114,7 @@ void TextGenerator::TextBuffer::Insert(const TextBuffer& tb, size_t before, uint
}
std::string TextGenerator::TextBuffer::String(uint32_t indent /* = 0 */) const {
std::stringstream ss;
utils::StringStream ss;
for (auto& line : lines) {
if (!line.content.empty()) {
for (uint32_t i = 0; i < indent + line.indent; i++) {
@ -127,7 +127,7 @@ std::string TextGenerator::TextBuffer::String(uint32_t indent /* = 0 */) const {
return ss.str();
}
TextGenerator::ScopedParen::ScopedParen(std::ostream& stream) : s(stream) {
TextGenerator::ScopedParen::ScopedParen(utils::StringStream& stream) : s(stream) {
s << "(";
}

View File

@ -140,9 +140,6 @@ class TextGenerator {
/// Destructor
~LineWriter();
/// [DEPRECATED] Remove when utils::StringStream conversion is done
/// @returns the utils::StringStream
operator std::ostream&() { return os.stream(); }
/// @returns the utils::StringStream
operator utils::StringStream&() { return os; }
@ -164,9 +161,8 @@ class TextGenerator {
/// Helper for writing a '(' on construction and a ')' destruction.
struct ScopedParen {
/// Constructor
/// [DEPRECATED] This should be utils::StringStream when conversion is done
/// @param stream the std::ostream that will be written to
explicit ScopedParen(std::ostream& stream);
/// @param stream the utils::StringStream that will be written to
explicit ScopedParen(utils::StringStream& stream);
/// Destructor
~ScopedParen();
@ -174,7 +170,7 @@ class TextGenerator {
ScopedParen(ScopedParen&& rhs) = delete;
ScopedParen(const ScopedParen&) = delete;
ScopedParen& operator=(const ScopedParen&) = delete;
std::ostream& s;
utils::StringStream& s;
};
/// Helper for incrementing indentation on construction and decrementing