Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update fixes and comments
Browse files Browse the repository at this point in the history
leewei05 committed Jun 19, 2024
1 parent e9856c6 commit 458b76b
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/type.hpp
Original file line number Diff line number Diff line change
@@ -185,8 +185,11 @@ struct Field {

class RecordType : public Type {
public:
/// @brief Returns the type id.
virtual std::string GetId() const noexcept = 0;
/// @brief Check if id is the member of the record type.
virtual bool IsMember(const std::string& id) const noexcept = 0;
/// @brief Return the type of a member in struct or union.
virtual std::unique_ptr<Type> MemberType(
const std::string& id) const noexcept = 0;
};
4 changes: 2 additions & 2 deletions src/type.cpp
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ std::unique_ptr<Type> StructType::MemberType(
const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return std::move(field->type->Clone());
return field->type->Clone();
}
}

@@ -238,7 +238,7 @@ std::unique_ptr<Type> UnionType::MemberType(
const std::string& id) const noexcept {
for (const auto& field : fields_) {
if (field->id == id) {
return std::move(field->type->Clone());
return field->type->Clone();
}
}

9 changes: 4 additions & 5 deletions src/type_checker.cpp
Original file line number Diff line number Diff line change
@@ -118,12 +118,11 @@ void TypeChecker::Visit(RecordVarDeclNode& record_decl) {
//
// struct birth bd1 { .date = 1 }; // RecordVarDeclNode -> search type entry
// to update its type.
auto record_type = dynamic_cast<RecordType*>(record_decl.type.get());
assert(record_type);
// record_type->GetId() is "birth" in the above example.
auto real_record_type = env_.LookUpType(record_type->GetId());
auto symbol = std::make_unique<SymbolEntry>(
record_decl.id, real_record_type->type->Clone());
auto record_type = env_.LookUpType(
dynamic_cast<RecordType*>(record_decl.type.get())->GetId());
auto symbol = std::make_unique<SymbolEntry>(record_decl.id,
record_type->type->Clone());

// TODO: type check between fields and initialized members.
for (auto& init : record_decl.inits) {

0 comments on commit 458b76b

Please sign in to comment.