[ir] Remove operator<<

The use of `operator<<` gets much more convoluted as things are changed
over to pointers and with inheritance. This CL switches the `operator<<`
methods to `ToString` functions.

Bug: tint:1718
Change-Id: I85fd25b870d82d995eb27014c767abe071e543b0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112046
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2022-11-30 15:25:05 +00:00 committed by Dawn LUCI CQ
parent a160ccb8c3
commit 6e306d34b5
12 changed files with 63 additions and 63 deletions

View File

@ -32,22 +32,22 @@ Constant::Constant(bool b) : kind_(Kind::kBool), data_(b) {}
Constant::~Constant() = default; Constant::~Constant() = default;
std::ostream& operator<<(std::ostream& out, const Constant& r) { std::ostream& Constant::ToString(std::ostream& out) const {
switch (r.GetKind()) { switch (GetKind()) {
case Constant::Kind::kF32: case Constant::Kind::kF32:
out << std::to_string(r.AsF32().value); out << std::to_string(AsF32().value);
break; break;
case Constant::Kind::kF16: case Constant::Kind::kF16:
out << std::to_string(r.AsF16().value); out << std::to_string(AsF16().value);
break; break;
case Constant::Kind::kI32: case Constant::Kind::kI32:
out << std::to_string(r.AsI32().value); out << std::to_string(AsI32().value);
break; break;
case Constant::Kind::kU32: case Constant::Kind::kU32:
out << std::to_string(r.AsU32().value); out << std::to_string(AsU32().value);
break; break;
case Constant::Kind::kBool: case Constant::Kind::kBool:
out << (r.AsBool() ? "true" : "false"); out << (AsBool() ? "true" : "false");
break; break;
} }
return out; return out;

View File

@ -100,6 +100,11 @@ class Constant : public Castable<Constant, Value> {
/// @note, must only be called if `IsBool()` is true /// @note, must only be called if `IsBool()` is true
bool AsBool() const { return std::get<bool>(data_); } bool AsBool() const { return std::get<bool>(data_); }
/// Write the constant to the given stream
/// @param out the stream to write to
/// @returns the stream
std::ostream& ToString(std::ostream& out) const override;
private: private:
/// The type of data stored in this constant /// The type of data stored in this constant
Kind kind_; Kind kind_;
@ -107,8 +112,6 @@ class Constant : public Castable<Constant, Value> {
std::variant<f32, f16, u32, i32, bool> data_; std::variant<f32, f16, u32, i32, bool> data_;
}; };
std::ostream& operator<<(std::ostream& out, const Constant& r);
} // namespace tint::ir } // namespace tint::ir
#endif // SRC_TINT_IR_CONSTANT_H_ #endif // SRC_TINT_IR_CONSTANT_H_

View File

@ -32,7 +32,7 @@ TEST_F(IR_ConstantTest, f32) {
auto* val = b.builder.Constant(1.2_f); auto* val = b.builder.Constant(1.2_f);
EXPECT_EQ(1.2_f, val->AsF32()); EXPECT_EQ(1.2_f, val->AsF32());
str << *val; val->ToString(str);
EXPECT_EQ("1.200000", str.str()); EXPECT_EQ("1.200000", str.str());
EXPECT_TRUE(val->IsF32()); EXPECT_TRUE(val->IsF32());
@ -50,7 +50,7 @@ TEST_F(IR_ConstantTest, f16) {
auto* val = b.builder.Constant(1.1_h); auto* val = b.builder.Constant(1.1_h);
EXPECT_EQ(1.1_h, val->AsF16()); EXPECT_EQ(1.1_h, val->AsF16());
str << *val; val->ToString(str);
EXPECT_EQ("1.099609", str.str()); EXPECT_EQ("1.099609", str.str());
EXPECT_FALSE(val->IsF32()); EXPECT_FALSE(val->IsF32());
@ -68,7 +68,7 @@ TEST_F(IR_ConstantTest, i32) {
auto* val = b.builder.Constant(1_i); auto* val = b.builder.Constant(1_i);
EXPECT_EQ(1_i, val->AsI32()); EXPECT_EQ(1_i, val->AsI32());
str << *val; val->ToString(str);
EXPECT_EQ("1", str.str()); EXPECT_EQ("1", str.str());
EXPECT_FALSE(val->IsF32()); EXPECT_FALSE(val->IsF32());
@ -86,7 +86,7 @@ TEST_F(IR_ConstantTest, u32) {
auto* val = b.builder.Constant(2_u); auto* val = b.builder.Constant(2_u);
EXPECT_EQ(2_u, val->AsU32()); EXPECT_EQ(2_u, val->AsU32());
str << *val; val->ToString(str);
EXPECT_EQ("2", str.str()); EXPECT_EQ("2", str.str());
EXPECT_FALSE(val->IsF32()); EXPECT_FALSE(val->IsF32());
@ -104,14 +104,14 @@ TEST_F(IR_ConstantTest, bool) {
auto* val = b.builder.Constant(false); auto* val = b.builder.Constant(false);
EXPECT_FALSE(val->AsBool()); EXPECT_FALSE(val->AsBool());
str << *val; val->ToString(str);
EXPECT_EQ("false", str.str()); EXPECT_EQ("false", str.str());
str.str(""); str.str("");
val = b.builder.Constant(true); val = b.builder.Constant(true);
EXPECT_TRUE(val->AsBool()); EXPECT_TRUE(val->AsBool());
str << *val; val->ToString(str);
EXPECT_EQ("true", str.str()); EXPECT_EQ("true", str.str());
EXPECT_FALSE(val->IsF32()); EXPECT_FALSE(val->IsF32());

View File

@ -63,7 +63,7 @@ std::ostream& Disassembler::Indent() {
void Disassembler::EmitBlockInstructions(const Block* b) { void Disassembler::EmitBlockInstructions(const Block* b) {
for (const auto* instr : b->instructions) { for (const auto* instr : b->instructions) {
out_ << *instr << std::endl; instr->ToString(out_) << std::endl;
} }
} }

View File

@ -33,14 +33,14 @@ Instruction& Instruction::operator=(const Instruction& instr) = default;
Instruction& Instruction::operator=(Instruction&& instr) = default; Instruction& Instruction::operator=(Instruction&& instr) = default;
std::ostream& operator<<(std::ostream& out, const Instruction& instr) { std::ostream& Instruction::ToString(std::ostream& out) const {
out << *(instr.Result()) << " = "; Result()->ToString(out) << " = ";
if (instr.HasLHS()) { if (HasLHS()) {
out << *(instr.LHS()); LHS()->ToString(out);
} }
out << " "; out << " ";
switch (instr.GetKind()) { switch (GetKind()) {
case Instruction::Kind::kAdd: case Instruction::Kind::kAdd:
out << "+"; out << "+";
break; break;
@ -97,8 +97,9 @@ std::ostream& operator<<(std::ostream& out, const Instruction& instr) {
break; break;
} }
if (instr.HasRHS()) { if (HasRHS()) {
out << " " << *(instr.RHS()); out << " ";
RHS()->ToString(out);
} }
return out; return out;

View File

@ -101,6 +101,11 @@ class Instruction : public Castable<Instruction> {
return args_[1]; return args_[1];
} }
/// Write the instructino to the given stream
/// @param out the stream to write to
/// @returns the stream
std::ostream& ToString(std::ostream& out) const;
private: private:
Kind kind_; Kind kind_;
@ -108,8 +113,6 @@ class Instruction : public Castable<Instruction> {
utils::Vector<const Value*, 2> args_; utils::Vector<const Value*, 2> args_;
}; };
std::ostream& operator<<(std::ostream& out, const Instruction&);
} // namespace tint::ir } // namespace tint::ir
#endif // SRC_TINT_IR_INSTRUCTION_H_ #endif // SRC_TINT_IR_INSTRUCTION_H_

View File

@ -46,7 +46,7 @@ TEST_F(IR_InstructionTest, CreateAnd) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 & 2"); EXPECT_EQ(str.str(), "%42 = 4 & 2");
} }
@ -74,7 +74,7 @@ TEST_F(IR_InstructionTest, CreateOr) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 | 2"); EXPECT_EQ(str.str(), "%42 = 4 | 2");
} }
@ -102,7 +102,7 @@ TEST_F(IR_InstructionTest, CreateXor) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 ^ 2"); EXPECT_EQ(str.str(), "%42 = 4 ^ 2");
} }
@ -131,7 +131,7 @@ TEST_F(IR_InstructionTest, CreateLogicalAnd) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 && 2"); EXPECT_EQ(str.str(), "%42 = 4 && 2");
} }
@ -159,7 +159,7 @@ TEST_F(IR_InstructionTest, CreateLogicalOr) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 || 2"); EXPECT_EQ(str.str(), "%42 = 4 || 2");
} }
@ -187,7 +187,7 @@ TEST_F(IR_InstructionTest, CreateEqual) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 == 2"); EXPECT_EQ(str.str(), "%42 = 4 == 2");
} }
@ -215,7 +215,7 @@ TEST_F(IR_InstructionTest, CreateNotEqual) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 != 2"); EXPECT_EQ(str.str(), "%42 = 4 != 2");
} }
@ -243,7 +243,7 @@ TEST_F(IR_InstructionTest, CreateLessThan) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 < 2"); EXPECT_EQ(str.str(), "%42 = 4 < 2");
} }
@ -272,7 +272,7 @@ TEST_F(IR_InstructionTest, CreateGreaterThan) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 > 2"); EXPECT_EQ(str.str(), "%42 = 4 > 2");
} }
@ -301,7 +301,7 @@ TEST_F(IR_InstructionTest, CreateLessThanEqual) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 <= 2"); EXPECT_EQ(str.str(), "%42 = 4 <= 2");
} }
@ -330,7 +330,7 @@ TEST_F(IR_InstructionTest, CreateGreaterThanEqual) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 >= 2"); EXPECT_EQ(str.str(), "%42 = 4 >= 2");
} }
@ -358,7 +358,7 @@ TEST_F(IR_InstructionTest, CreateShiftLeft) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 << 2"); EXPECT_EQ(str.str(), "%42 = 4 << 2");
} }
@ -387,7 +387,7 @@ TEST_F(IR_InstructionTest, CreateShiftRight) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 >> 2"); EXPECT_EQ(str.str(), "%42 = 4 >> 2");
} }
@ -415,7 +415,7 @@ TEST_F(IR_InstructionTest, CreateAdd) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 + 2"); EXPECT_EQ(str.str(), "%42 = 4 + 2");
} }
@ -443,7 +443,7 @@ TEST_F(IR_InstructionTest, CreateSubtract) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 - 2"); EXPECT_EQ(str.str(), "%42 = 4 - 2");
} }
@ -471,7 +471,7 @@ TEST_F(IR_InstructionTest, CreateMultiply) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 * 2"); EXPECT_EQ(str.str(), "%42 = 4 * 2");
} }
@ -499,7 +499,7 @@ TEST_F(IR_InstructionTest, CreateDivide) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 / 2"); EXPECT_EQ(str.str(), "%42 = 4 / 2");
} }
@ -527,7 +527,7 @@ TEST_F(IR_InstructionTest, CreateModulo) {
EXPECT_EQ(i32(2), rhs->AsI32()); EXPECT_EQ(i32(2), rhs->AsI32());
std::stringstream str; std::stringstream str;
str << *instr; instr->ToString(str);
EXPECT_EQ(str.str(), "%42 = 4 % 2"); EXPECT_EQ(str.str(), "%42 = 4 % 2");
} }

View File

@ -24,8 +24,8 @@ Temp::Temp(Id id) : id_(id) {}
Temp::~Temp() = default; Temp::~Temp() = default;
std::ostream& operator<<(std::ostream& out, const Temp& r) { std::ostream& Temp::ToString(std::ostream& out) const {
out << "%" << std::to_string(r.AsId()); out << "%" << std::to_string(AsId());
return out; return out;
} }

View File

@ -43,12 +43,15 @@ class Temp : public Castable<Temp, Value> {
/// @returns the value data as an `Id`. /// @returns the value data as an `Id`.
Id AsId() const { return id_; } Id AsId() const { return id_; }
/// Write the temp to the given stream
/// @param out the stream to write to
/// @returns the stream
std::ostream& ToString(std::ostream& out) const override;
private: private:
Id id_ = 0; Id id_ = 0;
}; };
std::ostream& operator<<(std::ostream& out, const Temp& r);
} // namespace tint::ir } // namespace tint::ir
#endif // SRC_TINT_IR_TEMP_H_ #endif // SRC_TINT_IR_TEMP_H_

View File

@ -33,7 +33,7 @@ TEST_F(IR_TempTest, id) {
auto* val = b.builder.Temp(); auto* val = b.builder.Temp();
EXPECT_EQ(4u, val->AsId()); EXPECT_EQ(4u, val->AsId());
str << *val; val->ToString(str);
EXPECT_EQ("%4", str.str()); EXPECT_EQ("%4", str.str());
} }

View File

@ -25,17 +25,4 @@ Value::Value() = default;
Value::~Value() = default; Value::~Value() = default;
std::ostream& operator<<(std::ostream& out, const Value& v) {
const auto* ptr = &v;
if (auto* c = ptr->As<Constant>()) {
out << *c;
} else if (auto* t = ptr->As<Temp>()) {
out << *t;
} else {
out << "Unknown value";
}
return out;
}
} // namespace tint::ir } // namespace tint::ir

View File

@ -33,13 +33,16 @@ class Value : public Castable<Value> {
Value& operator=(const Value&) = delete; Value& operator=(const Value&) = delete;
Value& operator=(Value&&) = delete; Value& operator=(Value&&) = delete;
/// Write the value to the given stream
/// @param out the stream to write to
/// @returns the stream
virtual std::ostream& ToString(std::ostream& out) const = 0;
protected: protected:
/// Constructor /// Constructor
Value(); Value();
}; };
std::ostream& operator<<(std::ostream& out, const Value& v);
} // namespace tint::ir } // namespace tint::ir
#endif // SRC_TINT_IR_VALUE_H_ #endif // SRC_TINT_IR_VALUE_H_