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; return is_32 ? 1 : 2;
} }
bool LiteralNumber::operator==(const Operand& other) const { bool LiteralNumber::operator==(const Operand& other) const noexcept {
if (GetType() == other.GetType()) { if (!EqualType(other)) {
const auto& o{static_cast<const LiteralNumber&>(other)}; return false;
return o.raw == raw && o.is_32 == is_32;
} }
return false; const auto& o{static_cast<const LiteralNumber&>(other)};
return o.raw == raw && o.is_32 == is_32;
} }
} // namespace Sirit } // namespace Sirit

View File

@ -13,15 +13,16 @@
namespace Sirit { namespace Sirit {
class LiteralNumber : public Operand { class LiteralNumber final : public Operand {
public: public:
explicit LiteralNumber(u64 raw, bool is_32); explicit LiteralNumber(u64 raw, bool is_32);
~LiteralNumber() override; ~LiteralNumber() override;
void Fetch(Stream& stream) const 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> template <typename T>
static std::unique_ptr<LiteralNumber> Create(T value) { static std::unique_ptr<LiteralNumber> Create(T value) {

View File

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

View File

@ -12,18 +12,19 @@
namespace Sirit { namespace Sirit {
class LiteralString : public Operand { class LiteralString final : public Operand {
public: public:
LiteralString(std::string string); LiteralString(std::string string);
~LiteralString() override; ~LiteralString() override;
void Fetch(Stream& stream) const 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: private:
const std::string string; std::string string;
}; };
} // namespace Sirit } // namespace Sirit

View File

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

View File

@ -14,15 +14,16 @@
namespace Sirit { namespace Sirit {
class Op : public Operand { class Op final : public Operand {
public: public:
explicit Op(spv::Op opcode, std::optional<u32> id = {}, Id result_type = nullptr); explicit Op(spv::Op opcode, std::optional<u32> id = {}, Id result_type = nullptr);
~Op() override; ~Op() override;
void Fetch(Stream& stream) const 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; void Write(Stream& stream) const;

View File

@ -13,25 +13,4 @@ Operand::Operand(OperandType operand_type) : operand_type{operand_type} {}
Operand::~Operand() = default; 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 } // namespace Sirit

View File

@ -17,16 +17,22 @@ public:
explicit Operand(OperandType operand_type); explicit Operand(OperandType operand_type);
virtual ~Operand(); virtual ~Operand();
virtual void Fetch(Stream& stream) const; virtual void Fetch(Stream& stream) const = 0;
virtual u16 GetWordCount() const;
virtual bool operator==(const Operand& other) const; virtual u16 GetWordCount() const noexcept = 0;
bool operator!=(const Operand& other) const;
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: private:
OperandType operand_type{}; OperandType operand_type;
}; };
} // namespace Sirit } // namespace Sirit