operand: Use pure virtual functions

This commit is contained in:
ReinUsesLisp 2019-11-01 06:07:53 -03:00
parent 4c2981eab5
commit b4eeadfd9b
8 changed files with 39 additions and 51 deletions

View file

@ -22,16 +22,16 @@ void LiteralNumber::Fetch(Stream& stream) const {
}
}
u16 LiteralNumber::GetWordCount() const {
u16 LiteralNumber::GetWordCount() const noexcept {
return is_32 ? 1 : 2;
}
bool LiteralNumber::operator==(const Operand& other) const {
if (GetType() == other.GetType()) {
const auto& o{static_cast<const LiteralNumber&>(other)};
return o.raw == raw && o.is_32 == is_32;
bool LiteralNumber::operator==(const Operand& other) const noexcept {
if (!EqualType(other)) {
return false;
}
return false;
const auto& o{static_cast<const LiteralNumber&>(other)};
return o.raw == raw && o.is_32 == is_32;
}
} // namespace Sirit

View file

@ -13,15 +13,16 @@
namespace Sirit {
class LiteralNumber : public Operand {
class LiteralNumber final : public Operand {
public:
explicit LiteralNumber(u64 raw, bool is_32);
~LiteralNumber() override;
void Fetch(Stream& stream) const override;
u16 GetWordCount() const override;
bool operator==(const Operand& other) const override;
u16 GetWordCount() const noexcept override;
bool operator==(const Operand& other) const noexcept override;
template <typename T>
static std::unique_ptr<LiteralNumber> Create(T value) {

View file

@ -19,15 +19,15 @@ void LiteralString::Fetch(Stream& stream) const {
stream.Write(string);
}
u16 LiteralString::GetWordCount() const {
u16 LiteralString::GetWordCount() const noexcept {
return static_cast<u16>(string.size() / 4 + 1);
}
bool LiteralString::operator==(const Operand& other) const {
if (GetType() == other.GetType()) {
return static_cast<const LiteralString&>(other).string == string;
bool LiteralString::operator==(const Operand& other) const noexcept {
if (!EqualType(other)) {
return false;
}
return false;
return static_cast<const LiteralString&>(other).string == string;
}
} // namespace Sirit

View file

@ -12,18 +12,19 @@
namespace Sirit {
class LiteralString : public Operand {
class LiteralString final : public Operand {
public:
LiteralString(std::string string);
~LiteralString() override;
void Fetch(Stream& stream) const override;
u16 GetWordCount() const override;
bool operator==(const Operand& other) const override;
u16 GetWordCount() const noexcept override;
bool operator==(const Operand& other) const noexcept override;
private:
const std::string string;
std::string string;
};
} // namespace Sirit

View file

@ -25,12 +25,12 @@ void Op::Fetch(Stream& stream) const {
stream.Write(id.value());
}
u16 Op::GetWordCount() const {
u16 Op::GetWordCount() const noexcept {
return 1;
}
bool Op::operator==(const Operand& other) const {
if (GetType() != other.GetType()) {
bool Op::operator==(const Operand& other) const noexcept {
if (!EqualType(other)) {
return false;
}
const auto& op = static_cast<const Op&>(other);

View file

@ -14,15 +14,16 @@
namespace Sirit {
class Op : public Operand {
class Op final : public Operand {
public:
explicit Op(spv::Op opcode, std::optional<u32> id = {}, Id result_type = nullptr);
~Op() override;
void Fetch(Stream& stream) const override;
u16 GetWordCount() const override;
bool operator==(const Operand& other) const override;
u16 GetWordCount() const noexcept override;
bool operator==(const Operand& other) const noexcept override;
void Write(Stream& stream) const;

View file

@ -13,25 +13,4 @@ Operand::Operand(OperandType operand_type) : operand_type{operand_type} {}
Operand::~Operand() = default;
void Operand::Fetch([[maybe_unused]] Stream& stream) const {
assert(!"Fetching unimplemented operand");
}
u16 Operand::GetWordCount() const {
assert(!"Fetching unimplemented operand");
return 0;
}
bool Operand::operator==([[maybe_unused]] const Operand& other) const {
return false;
}
bool Operand::operator!=(const Operand& other) const {
return !(*this == other);
}
OperandType Operand::GetType() const {
return operand_type;
}
} // namespace Sirit

View file

@ -17,16 +17,22 @@ public:
explicit Operand(OperandType operand_type);
virtual ~Operand();
virtual void Fetch(Stream& stream) const;
virtual u16 GetWordCount() const;
virtual void Fetch(Stream& stream) const = 0;
virtual bool operator==(const Operand& other) const;
bool operator!=(const Operand& other) const;
virtual u16 GetWordCount() const noexcept = 0;
OperandType GetType() const;
virtual bool operator==(const Operand& other) const noexcept = 0;
bool operator!=(const Operand& other) const noexcept {
return !operator==(other);
}
bool EqualType(const Operand& other) const noexcept {
return operand_type == other.operand_type;
}
private:
OperandType operand_type{};
OperandType operand_type;
};
} // namespace Sirit