Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse record declaration while declaring variable #178

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
leewei05 marked this conversation as resolved.
Show resolved Hide resolved
custom_types.insert(rec_type_id);
}
}
$$ = std::make_unique<DeclStmtNode>(Loc(@1), std::move(decl_list));
}
Expand Down
14 changes: 14 additions & 0 deletions test/codegen/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,19 @@ int main() {
bd1.date = bd1.date + 2;
__builtin_print(bd1.date);

struct book {
int fiction;
int sci_fi;
int history;
} library = {
.fiction = 100,
.sci_fi = 50,
.history = 500,
};

__builtin_print(library.fiction);
__builtin_print(library.sci_fi);
__builtin_print(library.history);

return 0;
}
3 changes: 3 additions & 0 deletions test/codegen/struct.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
5
1998
5
100
50
500
9 changes: 9 additions & 0 deletions test/codegen/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,14 @@ int main() {
__builtin_print(s.circle);
__builtin_print(s.triangle);

union gender {
int male;
int female;
} girl = {
.female = 1,
};

__builtin_print(girl.female);

return 0;
}
1 change: 1 addition & 0 deletions test/codegen/union.exp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
4
4
4
1
16 changes: 16 additions & 0 deletions test/typecheck/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,21 @@ int main() {

bd1.date;

struct animal {
int lion;
int tiger;
int giraffe;
} zoo;

struct book {
int fiction;
int sci_fi;
int history;
} library = {
.fiction = 100,
.sci_fi = 50,
.history = 500,
};

return 0;
}
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
14 changes: 14 additions & 0 deletions test/typecheck/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,19 @@ int main() {

s.circle;

union gender {
int male;
int female;
} girl = {
.female = 1,
};

union birth_place {
int tw;
int us;
} yt = {
.tw = 1,
};

return 0;
}
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
Loading