Skip to content

Commit

Permalink
Fix missing declaration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei05 committed Jun 16, 2024
1 parent 2f6edee commit 4166ce8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
11 changes: 11 additions & 0 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ decl: declaration_specifiers init_declarator_list_opt SEMICOLON {
auto decl_list = std::vector<std::unique_ptr<DeclNode>>{};
if (std::holds_alternative<std::unique_ptr<Type>>(decl_specifiers)) {
auto type = std::move(std::get<std::unique_ptr<Type>>(decl_specifiers));
if (init_decl_list.empty()) {
// A stand-alone type that doesn't declare any identifier, e.g., `int;`.
decl_list.push_back(std::make_unique<VarDeclNode>(Loc(@1), "", type->Clone()));
}

for (auto& init_decl : init_decl_list) {
if (init_decl) {
init_decl->type = ResolveType(type->Clone(), std::move(init_decl->type));
Expand All @@ -405,7 +410,13 @@ 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));
}

auto* rec_decl = dynamic_cast<RecordDeclNode*>(decl.get());
// Initialize record variable.
for (auto& init_decl : init_decl_list) {
if (init_decl) {
init_decl->type = ResolveType(rec_decl->type->Clone(), std::move(init_decl->type));
Expand Down
1 change: 1 addition & 0 deletions test/typecheck/decl.exp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ProgramNode <1:1>
DeclStmtNode <3:3>
VarDeclNode <3:7> j: int
DeclStmtNode <4:3>
VarDeclNode <4:3> : int
DeclStmtNode <5:3>
VarDeclNode <5:7> a: int
VarDeclNode <5:10> b: int
Expand Down
9 changes: 9 additions & 0 deletions test/typecheck/struct.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ ProgramNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
DeclStmtNode <2:3>
RecordDeclNode <2:10> struct ss definition
DeclStmtNode <4:3>
RecordDeclNode <4:10> struct birth definition
FieldNode <5:9> date: int
FieldNode <6:9> month: int
FieldNode <7:9> year: int
DeclStmtNode <10:3>
RecordDeclNode <10:9> struct definition
FieldNode <11:9> quarter: int
FieldNode <12:9> dime: int
FieldNode <13:9> penny: int
DeclStmtNode <16:3>
RecordVarDeclNode <16:16> bd1: struct birth
InitExprNode <17:5>
Expand Down
11 changes: 11 additions & 0 deletions test/typecheck/union.exp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ ProgramNode <1:1>
FuncDefNode <1:5> main: int ()
CompoundStmtNode <1:12>
DeclStmtNode <2:3>
RecordDeclNode <2:9> union u definition
DeclStmtNode <4:3>
RecordDeclNode <4:9> union shape definition
FieldNode <5:9> square: int
FieldNode <6:9> circle: int
FieldNode <7:9> triangle: int
DeclStmtNode <10:3>
RecordDeclNode <10:8> union definition
FieldNode <11:9> a: int
FieldNode <12:9> b: int
FieldNode <13:9> c: int
FieldNode <14:9> d: int
FieldNode <15:9> e: int
DeclStmtNode <20:3>
RecordVarDeclNode <20:15> s: union shape
InitExprNode <20:20>
Expand Down

0 comments on commit 4166ce8

Please sign in to comment.