Skip to content

Commit

Permalink
Update record type declaration while declaring variables
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 authored and Lai-YT committed Oct 19, 2024
1 parent e158376 commit e8a2b6b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
22 changes: 16 additions & 6 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
// inserts verbatim to the header file.
%code requires {
#include <memory>
#include <set>
#include <string>
#include <variant>
#include <vector>
Expand All @@ -47,6 +48,9 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
Location Loc(const yy::location& loc) {
return Location{loc.begin.line, loc.begin.column};
}

/// @brief A set that stores unique custom types for every file.
std::set<std::string> custom_types;
}

%skeleton "lalr1.cc"
Expand Down Expand Up @@ -420,19 +424,25 @@ decl: declaration_specifiers init_declarator_list_opt SEMICOLON {
}
} else {
auto decl = std::move(std::get<std::unique_ptr<DeclNode>>(decl_specifiers));
// A record declaration that doesn't declare any identifier, e.g., `struct point {int x, int y};`.
if (init_decl_list.empty()) {
decl_list.push_back(std::move(decl));
} else {
auto& rec_decl = dynamic_cast<RecordDeclNode&>(*decl);
// Initialize record variable.
auto& rec_decl = dynamic_cast<RecordDeclNode&>(*decl);

if (!init_decl_list.empty()) {
for (auto& init_decl : init_decl_list) {
if (init_decl) {
init_decl->type = ResolveType(rec_decl.type->Clone(), std::move(init_decl->type));
}
decl_list.push_back(std::move(init_decl));
}
}

auto rec_type_id = dynamic_cast<RecordType&>(*rec_decl.type).id();
// Insert RecordDeclNode if it is a newly create type.
if (custom_types.find(rec_type_id) == custom_types.end()) {
// To dump type declarations in front of variable declarations,
// insert decl at the beginning of the vector.
decl_list.insert(decl_list.begin(), std::move(decl));
custom_types.insert(rec_type_id);
}
}
$$ = std::make_unique<DeclStmtNode>(Loc(@1), std::move(decl_list));
}
Expand Down
25 changes: 23 additions & 2 deletions test/typecheck/struct.exp
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,26 @@ TransUnitNode <1:1>
ExprStmtNode <28:3>
RecordMemExprNode <28:3> .date: int
IdExprNode <28:3> bd1: struct birth
ReturnStmtNode <30:3>
IntConstExprNode <30:10> 0: int
DeclStmtNode <30:3>
RecordDeclNode <30:10> struct animal definition
FieldNode <31:9> lion: int
FieldNode <32:9> tiger: int
FieldNode <33:9> giraffe: int
VarDeclNode <34:5> zoo: struct animal
DeclStmtNode <36:3>
RecordDeclNode <36:10> struct book definition
FieldNode <37:9> fiction: int
FieldNode <38:9> sci_fi: int
FieldNode <39:9> history: int
RecordVarDeclNode <40:5> library: struct book
InitExprNode <41:5> int
IdDesNode <41:6> fiction
IntConstExprNode <41:16> 100: int
InitExprNode <41:5> int
IdDesNode <42:6> sci_fi
IntConstExprNode <42:15> 50: int
InitExprNode <41:5> int
IdDesNode <43:6> history
IntConstExprNode <43:16> 500: int
ReturnStmtNode <46:3>
IntConstExprNode <46:10> 0: int
20 changes: 18 additions & 2 deletions test/typecheck/union.exp
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,21 @@ TransUnitNode <1:1>
ExprStmtNode <27:3>
RecordMemExprNode <27:3> .circle: int
IdExprNode <27:3> s: union shape
ReturnStmtNode <29:3>
IntConstExprNode <29:10> 0: int
DeclStmtNode <29:3>
RecordDeclNode <29:9> union gender definition
FieldNode <30:9> male: int
FieldNode <31:9> female: int
RecordVarDeclNode <32:5> girl: union gender
InitExprNode <33:5> int
IdDesNode <33:6> female
IntConstExprNode <33:15> 1: int
DeclStmtNode <36:3>
RecordDeclNode <36:9> union birth_place definition
FieldNode <37:9> tw: int
FieldNode <38:9> us: int
RecordVarDeclNode <39:5> yt: union birth_place
InitExprNode <40:5> int
IdDesNode <40:6> tw
IntConstExprNode <40:11> 1: int
ReturnStmtNode <43:3>
IntConstExprNode <43:10> 0: int

0 comments on commit e8a2b6b

Please sign in to comment.