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

Support multiple declarations in a single statement #159

Merged
merged 4 commits into from
Jun 13, 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
24 changes: 17 additions & 7 deletions include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ struct DesNode // NOLINT(cppcoreguidelines-special-member-functions)
std::make_unique<PrimType>(PrimitiveType::kUnknown);
};

/// @brief A declaration statement may declare multiple identifiers.
struct DeclStmtNode : public StmtNode {
DeclStmtNode(Location loc, std::vector<std::unique_ptr<DeclNode>> decls)
: StmtNode{loc}, decls{std::move(decls)} {}

void Accept(NonModifyingVisitor&) const override;
void Accept(ModifyingVisitor&) override;

std::vector<std::unique_ptr<DeclNode>> decls;
};

struct VarDeclNode : public DeclNode {
VarDeclNode(Location loc, std::string id, std::unique_ptr<Type> type,
std::unique_ptr<ExprNode> init = {})
Expand Down Expand Up @@ -189,25 +200,24 @@ struct FuncDefNode : public DeclNode {
struct LoopInitNode : public AstNode {
LoopInitNode(
Location loc,
std::variant<std::unique_ptr<DeclNode>, std::unique_ptr<ExprNode>> clause)
std::variant<std::unique_ptr<DeclStmtNode>, std::unique_ptr<ExprNode>>
clause)
: AstNode{loc}, clause{std::move(clause)} {}

void Accept(NonModifyingVisitor&) const override;
void Accept(ModifyingVisitor&) override;

std::variant<std::unique_ptr<DeclNode>, std::unique_ptr<ExprNode>> clause;
std::variant<std::unique_ptr<DeclStmtNode>, std::unique_ptr<ExprNode>> clause;
};

struct CompoundStmtNode : public StmtNode {
using Item =
std::variant<std::unique_ptr<DeclNode>, std::unique_ptr<StmtNode>>;
CompoundStmtNode(Location loc, std::vector<Item> items)
: StmtNode{loc}, items{std::move(items)} {}
CompoundStmtNode(Location loc, std::vector<std::unique_ptr<StmtNode>> stmts)
: StmtNode{loc}, stmts{std::move(stmts)} {}

void Accept(NonModifyingVisitor&) const override;
void Accept(ModifyingVisitor&) override;

std::vector<Item> items;
std::vector<std::unique_ptr<StmtNode>> stmts;
};

/// @brief Root of the entire program.
Expand Down
1 change: 1 addition & 0 deletions include/ast_dumper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

class AstDumper : public NonModifyingVisitor {
public:
void Visit(const DeclStmtNode&) override;
void Visit(const LoopInitNode&) override;
void Visit(const VarDeclNode&) override;
void Visit(const ArrDeclNode&) override;
Expand Down
1 change: 1 addition & 0 deletions include/qbe_ir_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class QbeIrGenerator : public NonModifyingVisitor {
public:
void Visit(const DeclStmtNode&) override;
void Visit(const LoopInitNode&) override;
void Visit(const VarDeclNode&) override;
void Visit(const ArrDeclNode&) override;
Expand Down
1 change: 1 addition & 0 deletions include/type_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TypeChecker : public ModifyingVisitor {
public:
TypeChecker(ScopeStack& env) : env_{env} {}

void Visit(DeclStmtNode&) override;
void Visit(LoopInitNode&) override;
void Visit(VarDeclNode&) override;
void Visit(ArrDeclNode&) override;
Expand Down
2 changes: 2 additions & 0 deletions include/visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct StmtNode;
struct ExprNode;
struct DeclNode;
struct DesNode;
struct DeclStmtNode;
struct VarDeclNode;
struct ArrDeclNode;
struct RecordDeclNode;
Expand Down Expand Up @@ -69,6 +70,7 @@ class Visitor {
virtual void Visit(CondMut<AstNode>&){};
virtual void Visit(CondMut<StmtNode>&){};
virtual void Visit(CondMut<ExprNode>&){};
virtual void Visit(CondMut<DeclStmtNode>&){};
virtual void Visit(CondMut<DeclNode>&){};
virtual void Visit(CondMut<DesNode>&){};
virtual void Visit(CondMut<VarDeclNode>&){};
Expand Down
82 changes: 48 additions & 34 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
%nterm <std::unique_ptr<ExprNode>> expr assign_expr expr_opt unary_expr postfix_expr primary_expr
%nterm <std::unique_ptr<ExprNode>> const_expr cond_expr logic_or_expr logic_and_expr inclusive_or_expr exclusive_or_expr
%nterm <std::unique_ptr<ExprNode>> and_expr eq_expr relational_expr shift_expr add_expr mul_expr cast_expr
%nterm <std::unique_ptr<DeclNode>> decl id_opt
%nterm <std::unique_ptr<DeclNode>> id_opt
%nterm <std::unique_ptr<DeclStmtNode>> decl
%nterm <std::unique_ptr<ParamNode>> parameter_declaration
%nterm <std::vector<std::unique_ptr<ParamNode>>> parameter_type_list_opt parameter_type_list parameter_list
%nterm <std::unique_ptr<FieldNode>> struct_declaration struct_declarator struct_declarator_list
%nterm <std::vector<std::unique_ptr<FieldNode>>> struct_declaration_list
// The followings also declare an identifier, however, their types are not yet fully resolved.
%nterm <std::unique_ptr<DeclNode>> declarator direct_declarator init_declarator_opt init_declarator
%nterm <std::unique_ptr<DeclNode>> declarator direct_declarator init_declarator
%nterm <std::vector<std::unique_ptr<DeclNode>>> init_declarator_list_opt init_declarator_list
// The abstract declarator is a declarator without an identifier, which are actually types.
%nterm <std::unique_ptr<Type>> abstract_declarator_opt abstract_declarator direct_abstract_declarator_opt direct_abstract_declarator
%nterm <std::unique_ptr<Type>> struct_or_union specifier_qualifier_list
Expand All @@ -127,10 +129,9 @@ std::unique_ptr<Type> ResolveType(std::unique_ptr<Type> resolved_type,
%nterm <std::unique_ptr<FuncDefNode>> func_def
%nterm <std::vector<std::unique_ptr<FuncDefNode>>> func_def_list_opt
%nterm <std::unique_ptr<LoopInitNode>> loop_init
%nterm <std::unique_ptr<StmtNode>> stmt jump_stmt selection_stmt labeled_stmt
%nterm <std::unique_ptr<StmtNode>> stmt jump_stmt selection_stmt labeled_stmt block_item
%nterm <std::unique_ptr<CompoundStmtNode>> compound_stmt
%nterm <std::vector<CompoundStmtNode::Item>> block_item_list block_item_list_opt
%nterm <CompoundStmtNode::Item> block_item
%nterm <std::vector<std::unique_ptr<StmtNode>>> block_item_list block_item_list_opt

// Resolve the ambiguity in the "dangling-else" grammar.
// Example: IF LEFT_PAREN expr RIGHT_PAREN IF LEFT_PAREN expr RIGHT_PAREN stmt • ELSE stmt
Expand Down Expand Up @@ -197,12 +198,12 @@ compound_stmt: LEFT_CURLY block_item_list_opt RIGHT_CURLY {

block_item_list_opt: block_item_list { $$ = $1; }
| epsilon {
$$ = std::vector<CompoundStmtNode::Item>{};
$$ = std::vector<std::unique_ptr<StmtNode>>{};
}
;

block_item_list: block_item {
$$ = std::vector<CompoundStmtNode::Item>{};
$$ = std::vector<std::unique_ptr<StmtNode>>{};
$$.push_back($1);
}
| block_item_list block_item {
Expand Down Expand Up @@ -387,30 +388,47 @@ arg: expr {

/* 6.7 Declarations */
/* Declaration specifiers can be either a 'type' or a 'declaration of type'. */
/* TODO: init declarator list */
decl: declaration_specifiers init_declarator_opt SEMICOLON {
auto decl_specifers = $1;
auto init_decl = $2;
if (std::holds_alternative<std::unique_ptr<Type>>(decl_specifers)) {
auto type = std::move(std::get<std::unique_ptr<Type>>(decl_specifers));
if (init_decl) {
init_decl->type = ResolveType(std::move(type), std::move(init_decl->type));
} else { // unnamed primitive type
init_decl = std::make_unique<VarDeclNode>(Loc(@1), "", std::move(type));
decl: declaration_specifiers init_declarator_list_opt SEMICOLON {
auto decl_specifiers = $1;
auto init_decl_list = $2;
// A single declaration may declare multiple identifiers.
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));
for (auto& init_decl : init_decl_list) {
if (init_decl) {
init_decl->type = ResolveType(type->Clone(), std::move(init_decl->type));
} else { // unnamed primitive type
init_decl = std::make_unique<VarDeclNode>(Loc(@1), "", type->Clone());
}
decl_list.push_back(std::move(init_decl));
}

$$ = std::move(init_decl);
} else {
auto decl = std::move(std::get<std::unique_ptr<DeclNode>>(decl_specifers));
auto decl = std::move(std::get<std::unique_ptr<DeclNode>>(decl_specifiers));
auto* rec_decl = dynamic_cast<RecordDeclNode*>(decl.get());
assert(rec_decl);
if (init_decl) {
init_decl->type = ResolveType(std::move(rec_decl->type), std::move(init_decl->type));
decl = std::move(init_decl);
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));
}

$$ = std::move(decl);
}
$$ = std::make_unique<DeclStmtNode>(Loc(@1), std::move(decl_list));
}
;

init_declarator_list_opt: init_declarator_list { $$ = $1; }
| epsilon { $$ = std::vector<std::unique_ptr<DeclNode>>{}; }
;

init_declarator_list: init_declarator {
$$ = std::vector<std::unique_ptr<DeclNode>>{};
$$.push_back($1);
}
| init_declarator_list COMMA init_declarator {
auto init_decl_list = $1;
init_decl_list.push_back($3);
$$ = std::move(init_decl_list);
}
;

Expand All @@ -423,10 +441,6 @@ declaration_specifiers: type_specifier declaration_specifiers {
| type_specifier { $$ = $1; }
;

init_declarator_opt: init_declarator { $$ = $1; }
| epsilon { $$ = nullptr; }
;

/* A init declarator is a declarator with an optional initializer. */
init_declarator: declarator { $$ = $1; }
| declarator ASSIGN initializer {
Expand Down Expand Up @@ -476,9 +490,9 @@ struct_or_union_specifier: struct_or_union id_opt LEFT_CURLY struct_declaration_

auto type_id = decl_id ? decl_id->id : "";
if (type->IsStruct()) {
type = std::make_unique<StructType>(std::move(type_id), std::move(field_types));
type = std::make_unique<StructType>(type_id, std::move(field_types));
} else {
type = std::make_unique<UnionType>(std::move(type_id), std::move(field_types));
type = std::make_unique<UnionType>(type_id, std::move(field_types));
}

$$ = std::make_unique<RecordDeclNode>(Loc(@2), std::move(type_id), std::move(type), std::move(field_list));
Expand All @@ -490,9 +504,9 @@ struct_or_union_specifier: struct_or_union id_opt LEFT_CURLY struct_declaration_
auto field_types = std::vector<std::unique_ptr<Type>>{};

if (type->IsStruct()) {
type = std::make_unique<StructType>(std::move(decl_id), std::move(field_types));
type = std::make_unique<StructType>(decl_id, std::move(field_types));
} else {
type = std::make_unique<UnionType>(std::move(decl_id), std::move(field_types));
type = std::make_unique<UnionType>(decl_id, std::move(field_types));
}

$$ = std::make_unique<RecordDeclNode>(Loc(@2), std::move(decl_id), std::move(type), std::move(field_list));
Expand Down
8 changes: 8 additions & 0 deletions src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ void DesNode::Accept(ModifyingVisitor& v) {

DesNode::~DesNode() = default;

void DeclStmtNode::Accept(NonModifyingVisitor& v) const {
v.Visit(*this);
}

void DeclStmtNode::Accept(ModifyingVisitor& v) {
v.Visit(*this);
}

void VarDeclNode::Accept(NonModifyingVisitor& v) const {
v.Visit(*this);
}
Expand Down
13 changes: 11 additions & 2 deletions src/ast_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ std::string GetPostfixOperator(PostfixOperator op) {

} // namespace

void AstDumper::Visit(const DeclStmtNode& decl_stmt) {
std::cout << indenter_.Indent() << "DeclStmtNode <" << decl_stmt.loc << ">\n";
indenter_.IncreaseLevel();
for (const auto& decl : decl_stmt.decls) {
decl->Accept(*this);
}
indenter_.DecreaseLevel();
}

void AstDumper::Visit(const VarDeclNode& decl) {
std::cout << indenter_.Indent() << "VarDeclNode <" << decl.loc << "> "
<< decl.id << ": " << decl.type->ToString() << '\n';
Expand Down Expand Up @@ -169,8 +178,8 @@ void AstDumper::Visit(const CompoundStmtNode& compound_stmt) {
std::cout << indenter_.Indent() << "CompoundStmtNode <" << compound_stmt.loc
<< ">\n";
indenter_.IncreaseLevel();
for (const auto& item : compound_stmt.items) {
std::visit([this](auto&& item) { item->Accept(*this); }, item);
for (const auto& stmt : compound_stmt.stmts) {
stmt->Accept(*this);
}
indenter_.DecreaseLevel();
}
Expand Down
10 changes: 8 additions & 2 deletions src/qbe_ir_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ auto

} // namespace

void QbeIrGenerator::Visit(const DeclStmtNode& decl_stmt) {
for (const auto& decl : decl_stmt.decls) {
decl->Accept(*this);
}
}

void QbeIrGenerator::Visit(const VarDeclNode& decl) {
int id_num = NextLocalNum();
WriteInstr_("{} =l alloc{} {}", FuncScopeTemp{id_num}, decl.type->size(),
Expand Down Expand Up @@ -267,8 +273,8 @@ void QbeIrGenerator::Visit(const CompoundStmtNode& compound_stmt) {
// because it doesn't know whether it is a if statement body or a function.
// Thus, by moving label creation to an upper level, each block can have its
// correct starting label.
for (const auto& item : compound_stmt.items) {
std::visit([this](auto&& item) { item->Accept(*this); }, item);
for (const auto& stmt : compound_stmt.stmts) {
stmt->Accept(*this);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ bool IsInBodyOf(BodyType type) {

} // namespace

void TypeChecker::Visit(DeclStmtNode& decl_stmt) {
for (auto& decl : decl_stmt.decls) {
decl->Accept(*this);
}
}

void TypeChecker::Visit(VarDeclNode& decl) {
if (decl.init) {
decl.init->Accept(*this);
Expand Down Expand Up @@ -194,8 +200,8 @@ void TypeChecker::Visit(LoopInitNode& loop_init) {

void TypeChecker::Visit(CompoundStmtNode& compound_stmt) {
env_.PushScope(ScopeKind::kBlock);
for (auto& item : compound_stmt.items) {
std::visit([this](auto&& item) { item->Accept(*this); }, item);
for (auto& stmt : compound_stmt.stmts) {
stmt->Accept(*this);
}
env_.PopScope();
}
Expand Down
4 changes: 1 addition & 3 deletions test/codegen/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ int main() {
a[3] = 2 + a[2];
__builtin_print(a[3]);

int b = 2;
int c = 0;
int d[4] = {1, b, c = 3, 4};
int b = 2, c = 0, d[4] = {1, b, c = 3, 4};
__builtin_print(d[0]);
__builtin_print(d[1]);
__builtin_print(d[2]);
Expand Down
2 changes: 2 additions & 0 deletions test/codegen/decl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ int main() {
int i;
int j = 2;
__builtin_print(j);
int a = 3, b;
__builtin_print(a);
return 0;
}
1 change: 1 addition & 0 deletions test/codegen/decl.exp
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
2
3
2 changes: 1 addition & 1 deletion test/codegen/for_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ int main() {
// Nested for loop
//
int k = 0;
for (int i = 0; i < 5; i = i + 1) {
for (int i = 0, e = 5; i < e; i = i + 1) {
for (int j = 0; j < 5; j = j + 1) {
k = k + 1;
}
Expand Down
3 changes: 1 addition & 2 deletions test/codegen/pointer.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
int main() {
int a = 10;
int* b = &a;
int* c;
int *b = &a, *c;
__builtin_print(a);

*b = 5;
Expand Down
3 changes: 1 addition & 2 deletions test/typecheck/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ int main() {
a[1] = 2;
a[2] = a[1] + 1;

int b = 0;
int c[4] = {1, b = 2, 3, 4};
int b = 0, c[4] = {1, b = 2, 3, 4};

return 0;
}
Loading
Loading