Skip to content

Commit

Permalink
Rename type member functions
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 committed Jun 21, 2024
1 parent 2248cc1 commit 1c59952
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 55 deletions.
30 changes: 15 additions & 15 deletions include/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,27 @@ class RecordType : public Type {
/// @return The type id.
virtual std::string id() // NOLINT(readability-identifier-naming)
const noexcept = 0;
/// @brief Checks if `id` is a member of the record type.
virtual bool IsMember(const std::string& id) const noexcept = 0;
/// @return The type of a member in struct or union. The unknown type if the
/// `id` is not a member of the record type.
virtual std::unique_ptr<Type> MemberType(
const std::string& id) const noexcept = 0;
/// @note Every member in union shares the same offset 0.
/// @return The type offset in the record based on `id`.
/// @throw `std::runtime_error` if the `id` is not a member of the record.
virtual std::size_t offset( // NOLINT(readability-identifier-naming)
virtual std::size_t OffsetOf( // NOLINT(readability-identifier-naming)
const std::string& id) const = 0;
/// @note Every member in union shares the same offset 0.
/// @return The type offset in the record based on `index`.
/// @throw `std::out_of_range` if the `index` is out of range.
virtual std::size_t offset( // NOLINT(readability-identifier-naming)
virtual std::size_t OffsetOf( // NOLINT(readability-identifier-naming)
std::size_t index) const = 0;
/// @note For union type, if the total number of members is greater than or
/// equal to 1, return 1. Otherwise, return 0.
/// @return The total number of members in the record.
virtual std::size_t member_count() // NOLINT(readability-identifier-naming)
virtual std::size_t SlotCount() // NOLINT(readability-identifier-naming)
const noexcept = 0;
/// @brief Checks if `id` is a member of the record type.
virtual bool IsMember(const std::string& id) const noexcept = 0;
/// @return The type of a member in struct or union. The unknown type if the
/// `id` is not a member of the record type.
virtual std::unique_ptr<Type> MemberType(
const std::string& id) const noexcept = 0;
};

class StructType : public RecordType {
Expand All @@ -219,12 +219,12 @@ class StructType : public RecordType {
: id_{std::move(id)}, fields_{std::move(fields)} {}

std::string id() const noexcept override;
std::size_t offset(const std::string& id) const override;
std::size_t offset(std::size_t index) const override;
std::size_t member_count() const noexcept override;
bool IsMember(const std::string& id) const noexcept override;
std::unique_ptr<Type> MemberType(
const std::string& id) const noexcept override;
std::size_t OffsetOf(const std::string& id) const override;
std::size_t OffsetOf(std::size_t index) const override;
std::size_t SlotCount() const noexcept override;

bool IsStruct() const noexcept override {
return true;
Expand All @@ -248,12 +248,12 @@ class UnionType : public RecordType {
: id_{std::move(id)}, fields_{std::move(fields)} {}

std::string id() const noexcept override;
std::size_t offset(const std::string& id) const override;
std::size_t offset(std::size_t index) const override;
std::size_t member_count() const noexcept override;
bool IsMember(const std::string& id) const noexcept override;
std::unique_ptr<Type> MemberType(
const std::string& id) const noexcept override;
std::size_t OffsetOf(const std::string& id) const override;
std::size_t OffsetOf(std::size_t index) const override;
std::size_t SlotCount() const noexcept override;

bool IsUnion() const noexcept override {
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ void QbeIrGenerator::Visit(const RecordVarDeclNode& record_var_decl) {
// exceed the total number of members in a record. Also, it gurantees
// that accessing element in the initializers will not go out of bound.
for (auto i = std::size_t{0}, e = record_var_decl.inits.size(),
member_count = record_type->member_count();
i < member_count && i < e; ++i) {
slot_count = record_type->SlotCount();
i < slot_count && i < e; ++i) {
const auto& init = record_var_decl.inits.at(i);
init->Accept(*this);
const auto init_num = num_recorder.NumOfPrevExpr();

// res_addr = base_addr + offset
const int res_addr_num = NextLocalNum();
const auto offset = record_type->offset(i);
const auto offset = record_type->OffsetOf(i);
WriteInstr_("{} =l add {}, {}", FuncScopeTemp{res_addr_num},
FuncScopeTemp{base_addr}, offset);
WriteInstr_("storew {}, {}", FuncScopeTemp{init_num},
Expand Down Expand Up @@ -805,7 +805,7 @@ void QbeIrGenerator::Visit(const RecordMemExprNode& mem_expr) {

const auto res_addr_num = NextLocalNum();
WriteInstr_("{} =l add {}, {}", FuncScopeTemp{res_addr_num},
FuncScopeTemp{id_num}, record_type->offset(mem_expr.id));
FuncScopeTemp{id_num}, record_type->OffsetOf(mem_expr.id));

const int res_num = NextLocalNum();
WriteInstr_("{} =w loadw {}", FuncScopeTemp{res_num},
Expand Down
72 changes: 36 additions & 36 deletions src/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,28 @@ std::string StructType::id() const noexcept {
return id_;
}

std::size_t StructType::offset(const std::string& id) const {
bool StructType::IsMember(const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return true;
}
}

return false;
}

std::unique_ptr<Type> StructType::MemberType(
const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return field->type->Clone();
}
}

return std::make_unique<PrimType>(PrimitiveType::kUnknown);
}

std::size_t StructType::OffsetOf(const std::string& id) const {
std::size_t offset = 0;
for (auto i = std::size_t{0}, e = fields_.size(); i < e; ++i) {
const auto& field = fields_.at(i);
Expand All @@ -174,7 +195,7 @@ std::size_t StructType::offset(const std::string& id) const {
throw std::runtime_error{"member not found in struct!"};
}

std::size_t StructType::offset(const std::size_t index) const {
std::size_t StructType::OffsetOf(const std::size_t index) const {
if (index >= fields_.size()) {
throw std::out_of_range{"index out of bound!"};
}
Expand All @@ -185,31 +206,10 @@ std::size_t StructType::offset(const std::size_t index) const {
[](auto&& size, auto&& field) { return size + field->type->size(); });
}

std::size_t StructType::member_count() const noexcept {
std::size_t StructType::SlotCount() const noexcept {
return fields_.size();
}

bool StructType::IsMember(const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return true;
}
}

return false;
}

std::unique_ptr<Type> StructType::MemberType(
const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return field->type->Clone();
}
}

return std::make_unique<PrimType>(PrimitiveType::kUnknown);
}

bool StructType::IsEqual(const Type& that) const noexcept {
if (const auto* that_struct = dynamic_cast<const StructType*>(&that)) {
if (that_struct->size() != size()) {
Expand Down Expand Up @@ -251,18 +251,6 @@ std::unique_ptr<Type> StructType::Clone() const {
return std::make_unique<StructType>(id_, std::move(cloned_fields));
}

std::size_t UnionType::offset(const std::string& id) const {
return 0;
}

std::size_t UnionType::offset(const std::size_t index) const {
return 0;
}

std::size_t UnionType::member_count() const noexcept {
return fields_.size() > 0 ? 1 : 0;
}

std::string UnionType::id() const noexcept {
return id_;
}
Expand All @@ -288,6 +276,18 @@ std::unique_ptr<Type> UnionType::MemberType(
return std::make_unique<PrimType>(PrimitiveType::kUnknown);
}

std::size_t UnionType::OffsetOf(const std::string& id) const {
return 0;
}

std::size_t UnionType::OffsetOf(const std::size_t index) const {
return 0;
}

std::size_t UnionType::SlotCount() const noexcept {
return fields_.size() > 0 ? 1 : 0;
}

bool UnionType::IsEqual(const Type& that) const noexcept {
if (const auto* that_union = dynamic_cast<const UnionType*>(&that)) {
return that_union->size() == size();
Expand Down

0 comments on commit 1c59952

Please sign in to comment.