1 #include "analysis/register_declarations.h"
2 #include "ast/intrinsic.h"
5 #include "ast/package.h"
10 void RegisterDeclarations::run_impl(Program *p) { visit(p); }
12 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, Program) {
15 for (
const auto &c : p->get_children()) {
22 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, Intrinsic) {
24 if (p->get_intrinsic_type() == IntrinsicType::TEST_COMP_ERROR) {
25 _within_test_comp_error =
true;
27 auto *tce = pcast<TestCompError>(p->get_sub());
34 for (
auto *c : tce->get_children())
36 }
catch (
const CompileException &e) {
37 std::cerr << fmt::format(
"Caught expected compile error: {}\nContinue compilation...\n", e.what());
42 _within_test_comp_error =
false;
46 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, Parenthesis) { visit(p->get_sub()); }
48 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, If) {
49 size_t n = p->get_num_branches();
50 for (
size_t i = 0; i < n; ++i) {
51 auto *cond = p->get_predicate(i);
56 visit(p->get_branch(i));
60 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, VarDecl) {
61 str name = p->get_name();
62 if (ctx()->get_decl(name)) {
63 error(ErrorType::SEMANTIC_ERROR, p, fmt::format(
"Cannot redeclare variable named {}", name));
66 ctx()->set_decl(name, p);
69 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, ArgDecl) {
70 str name = p->get_name();
71 if (ctx()->get_decl(name)) {
72 error(ErrorType::SEMANTIC_ERROR, p, fmt::format(
"Cannot redeclare argument named {}", name));
75 ctx()->set_decl(name, p);
78 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, CompoundStmt) {
80 for (
const auto &c : p->get_children()) {
86 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, BinaryOrUnary) { visit(p->get_expr_ptr()); }
88 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, BinaryOperator) {
93 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, UnaryOperator) { visit(p->get_rhs()); }
95 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, Assignment) {
100 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, FunctionDecl) {
101 register_public_func_decl(p);
105 size_t n = p->get_n_args();
106 const auto &arg_decls = p->get_arg_decls();
107 for (
size_t i = 0; i < n; ++i) {
111 if (!p->is_external()) {
112 visit(p->get_body());
118 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, StructDecl) {
120 str struct_name = p->get_name();
121 auto *root_ctx = top_ctx();
122 auto *prev_decl = root_ctx->get_decl(struct_name);
123 if (prev_decl && prev_decl != p) {
124 error(ErrorType::SEMANTIC_ERROR, p,
"Cannot redeclare a struct");
127 register_public_type_decl(struct_name, p);
130 p->set_type(Type::GetStructType(p));
133 DEFINE_AST_VISITOR_IMPL(RegisterDeclarations, Loop) {
136 if (p->_loop_type == ASTLoopType::FOR) {
137 visit(p->_initialization);
146 void RegisterDeclarations::register_public_type_decl(
const str &name, TypeDecl *decl) {
147 if (!_within_test_comp_error) {
152 void RegisterDeclarations::register_public_func_decl(FunctionDecl *decl) {
153 if (!_within_test_comp_error) {
void set_decl(const str &name, Decl *decl)
Register a type declaration.
void set_function_decl(FunctionDecl *func)
Register a function declaration.