[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:
parent
a160ccb8c3
commit
6e306d34b5
|
@ -32,22 +32,22 @@ Constant::Constant(bool b) : kind_(Kind::kBool), data_(b) {}
|
|||
|
||||
Constant::~Constant() = default;
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Constant& r) {
|
||||
switch (r.GetKind()) {
|
||||
std::ostream& Constant::ToString(std::ostream& out) const {
|
||||
switch (GetKind()) {
|
||||
case Constant::Kind::kF32:
|
||||
out << std::to_string(r.AsF32().value);
|
||||
out << std::to_string(AsF32().value);
|
||||
break;
|
||||
case Constant::Kind::kF16:
|
||||
out << std::to_string(r.AsF16().value);
|
||||
out << std::to_string(AsF16().value);
|
||||
break;
|
||||
case Constant::Kind::kI32:
|
||||
out << std::to_string(r.AsI32().value);
|
||||
out << std::to_string(AsI32().value);
|
||||
break;
|
||||
case Constant::Kind::kU32:
|
||||
out << std::to_string(r.AsU32().value);
|
||||
out << std::to_string(AsU32().value);
|
||||
break;
|
||||
case Constant::Kind::kBool:
|
||||
out << (r.AsBool() ? "true" : "false");
|
||||
out << (AsBool() ? "true" : "false");
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
|
|
|
@ -100,6 +100,11 @@ class Constant : public Castable<Constant, Value> {
|
|||
/// @note, must only be called if `IsBool()` is true
|
||||
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:
|
||||
/// The type of data stored in this constant
|
||||
Kind kind_;
|
||||
|
@ -107,8 +112,6 @@ class Constant : public Castable<Constant, Value> {
|
|||
std::variant<f32, f16, u32, i32, bool> data_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Constant& r);
|
||||
|
||||
} // namespace tint::ir
|
||||
|
||||
#endif // SRC_TINT_IR_CONSTANT_H_
|
||||
|
|
|
@ -32,7 +32,7 @@ TEST_F(IR_ConstantTest, f32) {
|
|||
auto* val = b.builder.Constant(1.2_f);
|
||||
EXPECT_EQ(1.2_f, val->AsF32());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("1.200000", str.str());
|
||||
|
||||
EXPECT_TRUE(val->IsF32());
|
||||
|
@ -50,7 +50,7 @@ TEST_F(IR_ConstantTest, f16) {
|
|||
auto* val = b.builder.Constant(1.1_h);
|
||||
EXPECT_EQ(1.1_h, val->AsF16());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("1.099609", str.str());
|
||||
|
||||
EXPECT_FALSE(val->IsF32());
|
||||
|
@ -68,7 +68,7 @@ TEST_F(IR_ConstantTest, i32) {
|
|||
auto* val = b.builder.Constant(1_i);
|
||||
EXPECT_EQ(1_i, val->AsI32());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("1", str.str());
|
||||
|
||||
EXPECT_FALSE(val->IsF32());
|
||||
|
@ -86,7 +86,7 @@ TEST_F(IR_ConstantTest, u32) {
|
|||
auto* val = b.builder.Constant(2_u);
|
||||
EXPECT_EQ(2_u, val->AsU32());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("2", str.str());
|
||||
|
||||
EXPECT_FALSE(val->IsF32());
|
||||
|
@ -104,14 +104,14 @@ TEST_F(IR_ConstantTest, bool) {
|
|||
auto* val = b.builder.Constant(false);
|
||||
EXPECT_FALSE(val->AsBool());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("false", str.str());
|
||||
|
||||
str.str("");
|
||||
val = b.builder.Constant(true);
|
||||
EXPECT_TRUE(val->AsBool());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("true", str.str());
|
||||
|
||||
EXPECT_FALSE(val->IsF32());
|
||||
|
|
|
@ -63,7 +63,7 @@ std::ostream& Disassembler::Indent() {
|
|||
|
||||
void Disassembler::EmitBlockInstructions(const Block* b) {
|
||||
for (const auto* instr : b->instructions) {
|
||||
out_ << *instr << std::endl;
|
||||
instr->ToString(out_) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -33,14 +33,14 @@ Instruction& Instruction::operator=(const Instruction& instr) = default;
|
|||
|
||||
Instruction& Instruction::operator=(Instruction&& instr) = default;
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Instruction& instr) {
|
||||
out << *(instr.Result()) << " = ";
|
||||
if (instr.HasLHS()) {
|
||||
out << *(instr.LHS());
|
||||
std::ostream& Instruction::ToString(std::ostream& out) const {
|
||||
Result()->ToString(out) << " = ";
|
||||
if (HasLHS()) {
|
||||
LHS()->ToString(out);
|
||||
}
|
||||
out << " ";
|
||||
|
||||
switch (instr.GetKind()) {
|
||||
switch (GetKind()) {
|
||||
case Instruction::Kind::kAdd:
|
||||
out << "+";
|
||||
break;
|
||||
|
@ -97,8 +97,9 @@ std::ostream& operator<<(std::ostream& out, const Instruction& instr) {
|
|||
break;
|
||||
}
|
||||
|
||||
if (instr.HasRHS()) {
|
||||
out << " " << *(instr.RHS());
|
||||
if (HasRHS()) {
|
||||
out << " ";
|
||||
RHS()->ToString(out);
|
||||
}
|
||||
|
||||
return out;
|
||||
|
|
|
@ -101,6 +101,11 @@ class Instruction : public Castable<Instruction> {
|
|||
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:
|
||||
Kind kind_;
|
||||
|
||||
|
@ -108,8 +113,6 @@ class Instruction : public Castable<Instruction> {
|
|||
utils::Vector<const Value*, 2> args_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Instruction&);
|
||||
|
||||
} // namespace tint::ir
|
||||
|
||||
#endif // SRC_TINT_IR_INSTRUCTION_H_
|
||||
|
|
|
@ -46,7 +46,7 @@ TEST_F(IR_InstructionTest, CreateAnd) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 & 2");
|
||||
}
|
||||
|
||||
|
@ -74,7 +74,7 @@ TEST_F(IR_InstructionTest, CreateOr) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 | 2");
|
||||
}
|
||||
|
||||
|
@ -102,7 +102,7 @@ TEST_F(IR_InstructionTest, CreateXor) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 ^ 2");
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ TEST_F(IR_InstructionTest, CreateLogicalAnd) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 && 2");
|
||||
}
|
||||
|
||||
|
@ -159,7 +159,7 @@ TEST_F(IR_InstructionTest, CreateLogicalOr) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 || 2");
|
||||
}
|
||||
|
||||
|
@ -187,7 +187,7 @@ TEST_F(IR_InstructionTest, CreateEqual) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 == 2");
|
||||
}
|
||||
|
||||
|
@ -215,7 +215,7 @@ TEST_F(IR_InstructionTest, CreateNotEqual) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 != 2");
|
||||
}
|
||||
|
||||
|
@ -243,7 +243,7 @@ TEST_F(IR_InstructionTest, CreateLessThan) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 < 2");
|
||||
}
|
||||
|
||||
|
@ -272,7 +272,7 @@ TEST_F(IR_InstructionTest, CreateGreaterThan) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 > 2");
|
||||
}
|
||||
|
||||
|
@ -301,7 +301,7 @@ TEST_F(IR_InstructionTest, CreateLessThanEqual) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 <= 2");
|
||||
}
|
||||
|
||||
|
@ -330,7 +330,7 @@ TEST_F(IR_InstructionTest, CreateGreaterThanEqual) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 >= 2");
|
||||
}
|
||||
|
||||
|
@ -358,7 +358,7 @@ TEST_F(IR_InstructionTest, CreateShiftLeft) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 << 2");
|
||||
}
|
||||
|
||||
|
@ -387,7 +387,7 @@ TEST_F(IR_InstructionTest, CreateShiftRight) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 >> 2");
|
||||
}
|
||||
|
||||
|
@ -415,7 +415,7 @@ TEST_F(IR_InstructionTest, CreateAdd) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 + 2");
|
||||
}
|
||||
|
||||
|
@ -443,7 +443,7 @@ TEST_F(IR_InstructionTest, CreateSubtract) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 - 2");
|
||||
}
|
||||
|
||||
|
@ -471,7 +471,7 @@ TEST_F(IR_InstructionTest, CreateMultiply) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 * 2");
|
||||
}
|
||||
|
||||
|
@ -499,7 +499,7 @@ TEST_F(IR_InstructionTest, CreateDivide) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 / 2");
|
||||
}
|
||||
|
||||
|
@ -527,7 +527,7 @@ TEST_F(IR_InstructionTest, CreateModulo) {
|
|||
EXPECT_EQ(i32(2), rhs->AsI32());
|
||||
|
||||
std::stringstream str;
|
||||
str << *instr;
|
||||
instr->ToString(str);
|
||||
EXPECT_EQ(str.str(), "%42 = 4 % 2");
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ Temp::Temp(Id id) : id_(id) {}
|
|||
|
||||
Temp::~Temp() = default;
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Temp& r) {
|
||||
out << "%" << std::to_string(r.AsId());
|
||||
std::ostream& Temp::ToString(std::ostream& out) const {
|
||||
out << "%" << std::to_string(AsId());
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,12 +43,15 @@ class Temp : public Castable<Temp, Value> {
|
|||
/// @returns the value data as an `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:
|
||||
Id id_ = 0;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Temp& r);
|
||||
|
||||
} // namespace tint::ir
|
||||
|
||||
#endif // SRC_TINT_IR_TEMP_H_
|
||||
|
|
|
@ -33,7 +33,7 @@ TEST_F(IR_TempTest, id) {
|
|||
auto* val = b.builder.Temp();
|
||||
EXPECT_EQ(4u, val->AsId());
|
||||
|
||||
str << *val;
|
||||
val->ToString(str);
|
||||
EXPECT_EQ("%4", str.str());
|
||||
}
|
||||
|
||||
|
|
|
@ -25,17 +25,4 @@ 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
|
||||
|
|
|
@ -33,13 +33,16 @@ class Value : public Castable<Value> {
|
|||
Value& operator=(const 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:
|
||||
/// Constructor
|
||||
Value();
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const Value& v);
|
||||
|
||||
} // namespace tint::ir
|
||||
|
||||
#endif // SRC_TINT_IR_VALUE_H_
|
||||
|
|
Loading…
Reference in New Issue