diff options
Diffstat (limited to 'src/check_stmt.cpp')
| -rw-r--r-- | src/check_stmt.cpp | 364 |
1 files changed, 181 insertions, 183 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 8ec5ac9f4..8e6406cc4 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -1,10 +1,10 @@ -void check_stmt_list(Checker *c, Array<AstNode *> stmts, u32 flags) { +void check_stmt_list(CheckerContext *ctx, Array<AstNode *> stmts, u32 flags) { if (stmts.count == 0) { return; } if (flags&Stmt_CheckScopeDecls) { - check_scope_decls(c, stmts, cast(isize)(1.2*stmts.count)); + check_scope_decls(ctx, stmts, cast(isize)(1.2*stmts.count)); } bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0; @@ -39,7 +39,7 @@ void check_stmt_list(Checker *c, Array<AstNode *> stmts, u32 flags) { } } - check_stmt(c, n, new_flags); + check_stmt(ctx, n, new_flags); } } @@ -176,7 +176,7 @@ bool check_is_terminating(AstNode *node) { return false; } -Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { +Type *check_assignment_variable(CheckerContext *ctx, Operand *lhs, Operand *rhs) { if (rhs->mode == Addressing_Invalid) { return nullptr; } @@ -190,7 +190,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { // NOTE(bill): Ignore assignments to '_' if (is_blank_ident(node)) { - check_assignment(c, rhs, nullptr, str_lit("assignment to '_' identifier")); + check_assignment(ctx, rhs, nullptr, str_lit("assignment to '_' identifier")); if (rhs->mode == Addressing_Invalid) { return nullptr; } @@ -208,7 +208,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { } if (rhs->mode == Addressing_ProcGroup) { - Array<Entity *> procs = proc_group_entities(c, *rhs); + Array<Entity *> procs = proc_group_entities(ctx, *rhs); GB_ASSERT(procs.count > 0); // NOTE(bill): These should be done @@ -220,9 +220,9 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { Operand x = {}; x.mode = Addressing_Value; x.type = t; - if (check_is_assignable_to(c, &x, lhs->type)) { + if (check_is_assignable_to(ctx, &x, lhs->type)) { e = procs[i]; - add_entity_use(c, rhs->expr, e); + add_entity_use(ctx, rhs->expr, e); break; } } @@ -236,7 +236,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { } else { if (node->kind == AstNode_Ident) { ast_node(i, Ident, node); - e = scope_lookup_entity(c->context.scope, i->token.string); + e = scope_lookup_entity(ctx->scope, i->token.string); if (e != nullptr && e->kind == Entity_Variable) { used = (e->flags & EntityFlag_Used) != 0; // TODO(bill): Make backup just in case } @@ -292,7 +292,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { AstNode *ln = unparen_expr(lhs->expr); if (ln->kind == AstNode_IndexExpr) { AstNode *x = ln->IndexExpr.expr; - TypeAndValue tav = type_and_value_of_expr(&c->info, x); + TypeAndValue tav = type_and_value_of_expr(&ctx->checker->info, x); GB_ASSERT(tav.mode != Addressing_Invalid); if (tav.mode != Addressing_Variable) { if (!is_type_pointer(tav.type)) { @@ -312,7 +312,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { // NOTE(bill): Extra error checks Operand op_c = {Addressing_Invalid}; ast_node(se, SelectorExpr, lhs->expr); - check_expr(c, &op_c, se->expr); + check_expr(ctx, &op_c, se->expr); if (op_c.mode == Addressing_MapIndex) { gbString str = expr_to_string(lhs->expr); error(lhs->expr, "Cannot assign to struct field '%s' in map", str); @@ -333,7 +333,7 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { } } - check_assignment(c, rhs, assignment_type, str_lit("assignment")); + check_assignment(ctx, rhs, assignment_type, str_lit("assignment")); if (rhs->mode == Addressing_Invalid) { return nullptr; } @@ -342,13 +342,13 @@ Type *check_assignment_variable(Checker *c, Operand *lhs, Operand *rhs) { } -void check_stmt_internal(Checker *c, AstNode *node, u32 flags); -void check_stmt(Checker *c, AstNode *node, u32 flags) { - u32 prev_stmt_state_flags = c->context.stmt_state_flags; +void check_stmt_internal(CheckerContext *ctx, AstNode *node, u32 flags); +void check_stmt(CheckerContext *ctx, AstNode *node, u32 flags) { + u32 prev_stmt_state_flags = ctx->stmt_state_flags; if (node->stmt_state_flags != 0) { u32 in = node->stmt_state_flags; - u32 out = c->context.stmt_state_flags; + u32 out = ctx->stmt_state_flags; if (in & StmtStateFlag_no_bounds_check) { out |= StmtStateFlag_no_bounds_check; @@ -359,18 +359,18 @@ void check_stmt(Checker *c, AstNode *node, u32 flags) { out &= ~StmtStateFlag_no_bounds_check; } - c->context.stmt_state_flags = out; + ctx->stmt_state_flags = out; } - check_stmt_internal(c, node, flags); + check_stmt_internal(ctx, node, flags); - c->context.stmt_state_flags = prev_stmt_state_flags; + ctx->stmt_state_flags = prev_stmt_state_flags; } -void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) { +void check_when_stmt(CheckerContext *ctx, AstNodeWhenStmt *ws, u32 flags) { Operand operand = {Addressing_Invalid}; - check_expr(c, &operand, ws->cond); + check_expr(ctx, &operand, ws->cond); if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) { error(ws->cond, "Non-constant boolean 'when' condition"); return; @@ -381,14 +381,14 @@ void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) { } if (operand.value.kind == ExactValue_Bool && operand.value.value_bool) { - check_stmt_list(c, ws->body->BlockStmt.stmts, flags); + check_stmt_list(ctx, ws->body->BlockStmt.stmts, flags); } else if (ws->else_stmt) { switch (ws->else_stmt->kind) { case AstNode_BlockStmt: - check_stmt_list(c, ws->else_stmt->BlockStmt.stmts, flags); + check_stmt_list(ctx, ws->else_stmt->BlockStmt.stmts, flags); break; case AstNode_WhenStmt: - check_when_stmt(c, &ws->else_stmt->WhenStmt, flags); + check_when_stmt(ctx, &ws->else_stmt->WhenStmt, flags); break; default: error(ws->else_stmt, "Invalid 'else' statement in 'when' statement"); @@ -397,7 +397,7 @@ void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) { } } -void check_label(Checker *c, AstNode *label) { +void check_label(CheckerContext *ctx, AstNode *label) { if (label == nullptr) { return; } @@ -413,15 +413,15 @@ void check_label(Checker *c, AstNode *label) { } - if (c->context.curr_proc_decl == nullptr) { + if (ctx->curr_proc_decl == nullptr) { error(l->name, "A label is only allowed within a procedure"); return; } - GB_ASSERT(c->context.decl != nullptr); + GB_ASSERT(ctx->decl != nullptr); bool ok = true; - for_array(i, c->context.decl->labels) { - BlockLabel bl = c->context.decl->labels[i]; + for_array(i, ctx->decl->labels) { + BlockLabel bl = ctx->decl->labels[i]; if (bl.name == name) { error(label, "Duplicate label with the name '%.*s'", LIT(name)); ok = false; @@ -429,24 +429,24 @@ void check_label(Checker *c, AstNode *label) { } } - Entity *e = alloc_entity_label(c->context.scope, l->name->Ident.token, t_invalid, label); - add_entity(c, c->context.scope, l->name, e); - e->parent_proc_decl = c->context.curr_proc_decl; + Entity *e = alloc_entity_label(ctx->scope, l->name->Ident.token, t_invalid, label); + add_entity(ctx->checker, ctx->scope, l->name, e); + e->parent_proc_decl = ctx->curr_proc_decl; if (ok) { BlockLabel bl = {name, label}; - array_add(&c->context.decl->labels, bl); + array_add(&ctx->decl->labels, bl); } } // Returns 'true' for 'continue', 'false' for 'return' -bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bool is_selector, Entity *e) { +bool check_using_stmt_entity(CheckerContext *ctx, AstNodeUsingStmt *us, AstNode *expr, bool is_selector, Entity *e) { if (e == nullptr) { error(us->token, "'using' applied to an unknown entity"); return true; } - add_entity_use(c, expr, e); + add_entity_use(ctx, expr, e); switch (e->kind) { case Entity_TypeName: { @@ -456,7 +456,7 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo Entity *f = t->Enum.fields[i]; if (!is_entity_exported(f)) continue; - Entity *found = scope_insert_entity(c->context.scope, f); + Entity *found = scope_insert_entity(ctx->scope, f); if (found != nullptr) { gbString expr_str = expr_to_string(expr); error(us->token, "Namespace collision while 'using' '%s' of: %.*s", expr_str, LIT(found->token.string)); @@ -478,7 +478,7 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo Entity *decl = scope->elements.entries[i].value; if (!is_entity_exported(decl)) continue; - Entity *found = scope_insert_entity(c->context.scope, decl); + Entity *found = scope_insert_entity(ctx->scope, decl); if (found != nullptr) { gbString expr_str = expr_to_string(expr); error(us->token, @@ -501,13 +501,13 @@ bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bo Type *t = base_type(type_deref(e->type)); if (t->kind == Type_Struct) { // TODO(bill): Make it work for unions too - Scope *found = scope_of_node(&c->info, t->Struct.node); + Scope *found = scope_of_node(&ctx->checker->info, t->Struct.node); for_array(i, found->elements.entries) { Entity *f = found->elements.entries[i].value; if (f->kind == Entity_Variable) { Entity *uvar = alloc_entity_using_variable(e, f->token, f->type); uvar->using_expr = expr; - Entity *prev = scope_insert_entity(c->context.scope, uvar); + Entity *prev = scope_insert_entity(ctx->scope, uvar); if (prev != nullptr) { gbString expr_str = expr_to_string(expr); error(us->token, "Namespace collision while using '%s' of: '%.*s'", expr_str, LIT(prev->token.string)); @@ -559,7 +559,7 @@ struct TypeAndToken { Token token; }; -void add_constant_switch_case(Checker *c, Map<TypeAndToken> *seen, Operand operand, bool use_expr = true) { +void add_constant_switch_case(CheckerContext *ctx, Map<TypeAndToken> *seen, Operand operand, bool use_expr = true) { if (operand.mode != Addressing_Constant) { return; } @@ -570,8 +570,8 @@ void add_constant_switch_case(Checker *c, Map<TypeAndToken> *seen, Operand opera TypeAndToken *found = map_get(seen, key); if (found != nullptr) { isize count = multi_map_count(seen, key); - TypeAndToken *taps = gb_alloc_array(c->allocator, TypeAndToken, count); - defer (gb_free(c->allocator, taps)); + TypeAndToken *taps = gb_alloc_array(ctx->allocator, TypeAndToken, count); + defer (gb_free(ctx->allocator, taps)); multi_map_get_all(seen, key, taps); for (isize i = 0; i < count; i++) { @@ -600,23 +600,23 @@ void add_constant_switch_case(Checker *c, Map<TypeAndToken> *seen, Operand opera multi_map_insert(seen, key, tap); } -void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { +void check_switch_stmt(CheckerContext *ctx, AstNode *node, u32 mod_flags) { ast_node(ss, SwitchStmt, node); Operand x = {}; mod_flags |= Stmt_BreakAllowed | Stmt_FallthroughAllowed; - check_open_scope(c, node); - defer (check_close_scope(c)); + check_open_scope(ctx, node); + defer (check_close_scope(ctx)); - check_label(c, ss->label); // TODO(bill): What should the label's "scope" be? + check_label(ctx, ss->label); // TODO(bill): What should the label's "scope" be? if (ss->init != nullptr) { - check_stmt(c, ss->init, 0); + check_stmt(ctx, ss->init, 0); } if (ss->tag != nullptr) { - check_expr(c, &x, ss->tag); - check_assignment(c, &x, nullptr, str_lit("switch expression")); + check_expr(ctx, &x, ss->tag); + check_assignment(ctx, &x, nullptr, str_lit("switch expression")); } else { x.mode = Addressing_Constant; x.type = t_bool; @@ -626,7 +626,7 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { token.pos = ast_node_token(ss->body).pos; token.string = str_lit("true"); - x.expr = gb_alloc_item(c->allocator, AstNode); + x.expr = gb_alloc_item(ctx->allocator, AstNode); x.expr->kind = AstNode_Ident; x.expr->Ident.token = token; } @@ -687,14 +687,14 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { ast_node(ie, BinaryExpr, expr); Operand lhs = {}; Operand rhs = {}; - check_expr(c, &lhs, ie->left); + check_expr(ctx, &lhs, ie->left); if (x.mode == Addressing_Invalid) { continue; } if (lhs.mode == Addressing_Invalid) { continue; } - check_expr(c, &rhs, ie->right); + check_expr(ctx, &rhs, ie->right); if (rhs.mode == Addressing_Invalid) { continue; } @@ -711,7 +711,7 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { Operand a = lhs; Operand b = rhs; - check_comparison(c, &a, &x, Token_LtEq); + check_comparison(ctx, &a, &x, Token_LtEq); if (a.mode == Addressing_Invalid) { continue; } @@ -721,7 +721,7 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { default: error(ie->op, "Invalid interval operator"); continue; } - check_comparison(c, &b, &x, op); + check_comparison(ctx, &b, &x, op); if (b.mode == Addressing_Invalid) { continue; } @@ -736,32 +736,32 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { Operand a1 = lhs; Operand b1 = rhs; - check_comparison(c, &a1, &b1, op); + check_comparison(ctx, &a1, &b1, op); if (complete) { error(lhs.expr, "#complete switch statement does not allow ranges"); } - add_constant_switch_case(c, &seen, lhs); + add_constant_switch_case(ctx, &seen, lhs); if (op == Token_LtEq) { - add_constant_switch_case(c, &seen, rhs); + add_constant_switch_case(ctx, &seen, rhs); } } else { Operand y = {}; - check_expr(c, &y, expr); + check_expr(ctx, &y, expr); if (x.mode == Addressing_Invalid || y.mode == Addressing_Invalid) { continue; } - convert_to_typed(c, &y, x.type); + convert_to_typed(ctx, &y, x.type); if (y.mode == Addressing_Invalid) { continue; } // NOTE(bill): the ordering here matters Operand z = y; - check_comparison(c, &z, &x, Token_CmpEq); + check_comparison(ctx, &z, &x, Token_CmpEq); if (z.mode == Addressing_Invalid) { continue; } @@ -772,13 +772,13 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { continue; } - add_constant_switch_case(c, &seen, y); + add_constant_switch_case(ctx, &seen, y); } } - check_open_scope(c, stmt); - check_stmt_list(c, cc->stmts, mod_flags); - check_close_scope(c); + check_open_scope(ctx, stmt); + check_stmt_list(ctx, cc->stmts, mod_flags); + check_close_scope(ctx); } if (complete) { @@ -786,7 +786,7 @@ void check_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { GB_ASSERT(is_type_enum(et)); auto fields = et->Enum.fields; - auto unhandled = array_make<Entity *>(c->allocator, 0, fields.count); + auto unhandled = array_make<Entity *>(ctx->allocator, 0, fields.count); defer (array_free(&unhandled)); for_array(i, fields) { @@ -838,15 +838,15 @@ TypeSwitchKind check_valid_type_switch_type(Type *type) { return TypeSwitch_Invalid; } -void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { +void check_type_switch_stmt(CheckerContext *ctx, AstNode *node, u32 mod_flags) { ast_node(ss, TypeSwitchStmt, node); Operand x = {}; mod_flags |= Stmt_BreakAllowed; - check_open_scope(c, node); - defer (check_close_scope(c)); + check_open_scope(ctx, node); + defer (check_close_scope(ctx)); - check_label(c, ss->label); // TODO(bill): What should the label's "scope" be? + check_label(ctx, ss->label); // TODO(bill): What should the label's "scope" be? if (ss->tag->kind != AstNode_AssignStmt) { error(ss->tag, "Expected an 'in' assignment for this type switch statement"); @@ -866,9 +866,9 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { AstNode *lhs = as->lhs[0]; AstNode *rhs = as->rhs[0]; - check_expr(c, &x, rhs); - check_assignment(c, &x, nullptr, str_lit("type switch expression")); - add_type_info_type(c, x.type); + check_expr(ctx, &x, rhs); + check_assignment(ctx, &x, nullptr, str_lit("type switch expression")); + add_type_info_type(ctx, x.type); TypeSwitchKind switch_kind = check_valid_type_switch_type(x.type); if (switch_kind == TypeSwitch_Invalid) { @@ -941,7 +941,7 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { AstNode *type_expr = cc->list[type_index]; if (type_expr != nullptr) { // Otherwise it's a default expression Operand y = {}; - check_expr_or_type(c, &y, type_expr); + check_expr_or_type(ctx, &y, type_expr); if (switch_kind == TypeSwitch_Union) { GB_ASSERT(is_type_union(bt)); @@ -960,10 +960,10 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { continue; } case_type = y.type; - add_type_info_type(c, y.type); + add_type_info_type(ctx, y.type); } else if (switch_kind == TypeSwitch_Any) { case_type = y.type; - add_type_info_type(c, y.type); + add_type_info_type(ctx, y.type); } else { GB_PANIC("Unknown type to type switch statement"); } @@ -996,19 +996,19 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { if (case_type == nullptr) { case_type = x.type; } - add_type_info_type(c, case_type); + add_type_info_type(ctx, case_type); - check_open_scope(c, stmt); + check_open_scope(ctx, stmt); { - Entity *tag_var = alloc_entity_variable(c->context.scope, lhs->Ident.token, case_type, false, EntityState_Resolved); + Entity *tag_var = alloc_entity_variable(ctx->scope, lhs->Ident.token, case_type, false, EntityState_Resolved); tag_var->flags |= EntityFlag_Used; tag_var->flags |= EntityFlag_Value; - add_entity(c, c->context.scope, lhs, tag_var); - add_entity_use(c, lhs, tag_var); - add_implicit_entity(c, stmt, tag_var); + add_entity(ctx->checker, ctx->scope, lhs, tag_var); + add_entity_use(ctx, lhs, tag_var); + add_implicit_entity(ctx, stmt, tag_var); } - check_stmt_list(c, cc->stmts, mod_flags); - check_close_scope(c); + check_stmt_list(ctx, cc->stmts, mod_flags); + check_close_scope(ctx); } if (complete) { @@ -1016,7 +1016,7 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { GB_ASSERT(is_type_union(ut)); auto variants = ut->Union.variants; - auto unhandled = array_make<Type *>(c->allocator, 0, variants.count); + auto unhandled = array_make<Type *>(ctx->allocator, 0, variants.count); defer (array_free(&unhandled)); for_array(i, variants) { @@ -1046,7 +1046,7 @@ void check_type_switch_stmt(Checker *c, AstNode *node, u32 mod_flags) { } } -void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { +void check_stmt_internal(CheckerContext *ctx, AstNode *node, u32 flags) { u32 mod_flags = flags & (~Stmt_FallthroughAllowed); switch (node->kind) { case_ast_node(_, EmptyStmt, node); case_end; @@ -1055,7 +1055,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { case_ast_node(es, ExprStmt, node) Operand operand = {Addressing_Invalid}; - ExprKind kind = check_expr_base(c, &operand, es->expr, nullptr); + ExprKind kind = check_expr_base(ctx, &operand, es->expr, nullptr); switch (operand.mode) { case Addressing_Type: { gbString str = type_to_string(operand.type); @@ -1072,7 +1072,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } if (operand.expr->kind == AstNode_CallExpr) { AstNodeCallExpr *ce = &operand.expr->CallExpr; - Type *t = type_of_expr(&c->info, ce->proc); + Type *t = type_of_expr(&ctx->checker->info, ce->proc); if (is_type_proc(t)) { if (t->Proc.require_results) { gbString expr_str = expr_to_string(ce->proc); @@ -1094,7 +1094,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { case_ast_node(ts, TagStmt, node); // TODO(bill): Tag Statements error(node, "Tag statements are not supported yet"); - check_stmt(c, ts->stmt, flags); + check_stmt(ctx, ts->stmt, flags); case_end; case_ast_node(as, AssignStmt, node); @@ -1110,8 +1110,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { // NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be // an extra allocation - auto lhs_operands = array_make<Operand>(c->allocator, lhs_count); - auto rhs_operands = array_make<Operand>(c->allocator, 0, 2*lhs_count); + auto lhs_operands = array_make<Operand>(ctx->allocator, lhs_count); + auto rhs_operands = array_make<Operand>(ctx->allocator, 0, 2*lhs_count); defer (array_free(&lhs_operands)); defer (array_free(&rhs_operands)); @@ -1121,11 +1121,11 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { o->expr = as->lhs[i]; o->mode = Addressing_Value; } else { - check_expr(c, &lhs_operands[i], as->lhs[i]); + check_expr(ctx, &lhs_operands[i], as->lhs[i]); } } - check_unpack_arguments(c, nullptr, lhs_operands.count, &rhs_operands, as->rhs, true); + check_unpack_arguments(ctx, nullptr, lhs_operands.count, &rhs_operands, as->rhs, true); isize rhs_count = rhs_operands.count; for_array(i, rhs_operands) { @@ -1136,7 +1136,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { isize max = gb_min(lhs_count, rhs_count); for (isize i = 0; i < max; i++) { - check_assignment_variable(c, &lhs_operands[i], &rhs_operands[i]); + check_assignment_variable(ctx, &lhs_operands[i], &rhs_operands[i]); } if (lhs_count != rhs_count) { error(as->lhs[0], "Assignment count mismatch '%td' = '%td'", lhs_count, rhs_count); @@ -1165,13 +1165,13 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { be->left = as->lhs[0]; be->right = as->rhs[0]; - check_expr(c, &lhs, as->lhs[0]); - check_binary_expr(c, &rhs, &binary_expr); + check_expr(ctx, &lhs, as->lhs[0]); + check_binary_expr(ctx, &rhs, &binary_expr); if (rhs.mode == Addressing_Invalid) { return; } // NOTE(bill): Only use the first one will be used - check_assignment_variable(c, &lhs, &rhs); + check_assignment_variable(ctx, &lhs, &rhs); break; } @@ -1179,31 +1179,31 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { case_end; case_ast_node(bs, BlockStmt, node); - check_open_scope(c, node); - check_stmt_list(c, bs->stmts, flags); - check_close_scope(c); + check_open_scope(ctx, node); + check_stmt_list(ctx, bs->stmts, flags); + check_close_scope(ctx); case_end; case_ast_node(is, IfStmt, node); - check_open_scope(c, node); + check_open_scope(ctx, node); if (is->init != nullptr) { - check_stmt(c, is->init, 0); + check_stmt(ctx, is->init, 0); } Operand operand = {Addressing_Invalid}; - check_expr(c, &operand, is->cond); + check_expr(ctx, &operand, is->cond); if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) { error(is->cond, "Non-boolean condition in 'if' statement"); } - check_stmt(c, is->body, mod_flags); + check_stmt(ctx, is->body, mod_flags); if (is->else_stmt != nullptr) { switch (is->else_stmt->kind) { case AstNode_IfStmt: case AstNode_BlockStmt: - check_stmt(c, is->else_stmt, mod_flags); + check_stmt(ctx, is->else_stmt, mod_flags); break; default: error(is->else_stmt, "Invalid 'else' statement in 'if' statement"); @@ -1211,22 +1211,22 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } } - check_close_scope(c); + check_close_scope(ctx); case_end; case_ast_node(ws, WhenStmt, node); - check_when_stmt(c, ws, flags); + check_when_stmt(ctx, ws, flags); case_end; case_ast_node(rs, ReturnStmt, node); - GB_ASSERT(c->context.curr_proc_sig != nullptr); + GB_ASSERT(ctx->curr_proc_sig != nullptr); - if (c->context.in_defer) { + if (ctx->in_defer) { error(rs->token, "You cannot 'return' within a defer statement"); break; } - Type *proc_type = c->context.curr_proc_sig; + Type *proc_type = ctx->curr_proc_sig; GB_ASSERT(proc_type != nullptr); GB_ASSERT(proc_type->kind == Type_Proc); // Type *proc_type = c->proc_stack[c->proc_stack.count-1]; @@ -1241,7 +1241,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { auto operands = array_make<Operand>(heap_allocator(), 0, 2*rs->results.count); defer (array_free(&operands)); - check_unpack_arguments(c, nullptr, -1, &operands, rs->results, false); + check_unpack_arguments(ctx, nullptr, -1, &operands, rs->results, false); if (result_count == 0 && rs->results.count > 0) { error(rs->results[0], "No return values expected"); @@ -1253,7 +1253,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { isize max_count = rs->results.count; for (isize i = 0; i < max_count; i++) { Entity *e = pt->results->Tuple.variables[i]; - check_assignment(c, &operands[i], e->type, str_lit("return statement")); + check_assignment(ctx, &operands[i], e->type, str_lit("return statement")); } } case_end; @@ -1261,37 +1261,37 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { case_ast_node(fs, ForStmt, node); u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed; - check_open_scope(c, node); - check_label(c, fs->label); // TODO(bill): What should the label's "scope" be? + check_open_scope(ctx, node); + check_label(ctx, fs->label); // TODO(bill): What should the label's "scope" be? if (fs->init != nullptr) { - check_stmt(c, fs->init, 0); + check_stmt(ctx, fs->init, 0); } if (fs->cond != nullptr) { Operand o = {Addressing_Invalid}; - check_expr(c, &o, fs->cond); + check_expr(ctx, &o, fs->cond); if (o.mode != Addressing_Invalid && !is_type_boolean(o.type)) { error(fs->cond, "Non-boolean condition in 'for' statement"); } } if (fs->post != nullptr) { - check_stmt(c, fs->post, 0); + check_stmt(ctx, fs->post, 0); if (fs->post->kind != AstNode_AssignStmt && fs->post->kind != AstNode_IncDecStmt) { error(fs->post, "'for' statement post statement must be a simple statement"); } } - check_stmt(c, fs->body, new_flags); + check_stmt(ctx, fs->body, new_flags); - check_close_scope(c); + check_close_scope(ctx); case_end; case_ast_node(rs, RangeStmt, node); u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed; - check_open_scope(c, node); - check_label(c, rs->label); + check_open_scope(ctx, node); + check_label(ctx, rs->label); Type *val0 = nullptr; Type *val1 = nullptr; @@ -1307,29 +1307,29 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Operand x = {Addressing_Invalid}; Operand y = {Addressing_Invalid}; - check_expr(c, &x, ie->left); + check_expr(ctx, &x, ie->left); if (x.mode == Addressing_Invalid) { goto skip_expr; } - check_expr(c, &y, ie->right); + check_expr(ctx, &y, ie->right); if (y.mode == Addressing_Invalid) { goto skip_expr; } - convert_to_typed(c, &x, y.type); + convert_to_typed(ctx, &x, y.type); if (x.mode == Addressing_Invalid) { goto skip_expr; } - convert_to_typed(c, &y, x.type); + convert_to_typed(ctx, &y, x.type); if (y.mode == Addressing_Invalid) { goto skip_expr; } - convert_to_typed(c, &x, default_type(y.type)); + convert_to_typed(ctx, &x, default_type(y.type)); if (x.mode == Addressing_Invalid) { goto skip_expr; } - convert_to_typed(c, &y, default_type(x.type)); + convert_to_typed(ctx, &y, default_type(x.type)); if (y.mode == Addressing_Invalid) { goto skip_expr; } @@ -1383,13 +1383,13 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } - add_type_and_value(&c->info, ie->left, x.mode, x.type, x.value); - add_type_and_value(&c->info, ie->right, y.mode, y.type, y.value); + add_type_and_value(&ctx->checker->info, ie->left, x.mode, x.type, x.value); + add_type_and_value(&ctx->checker->info, ie->right, y.mode, y.type, y.value); val0 = type; val1 = t_int; } else { Operand operand = {Addressing_Invalid}; - check_expr_or_type(c, &operand, rs->expr); + check_expr_or_type(ctx, &operand, rs->expr); if (operand.mode == Addressing_Type) { if (!is_type_enum(operand.type)) { @@ -1400,7 +1400,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } else { val0 = operand.type; val1 = t_int; - add_type_info_type(c, operand.type); + add_type_info_type(ctx, operand.type); goto skip_expr; } } else if (operand.mode != Addressing_Invalid) { @@ -1411,7 +1411,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (is_type_string(t)) { val0 = t_rune; val1 = t_int; - add_package_dependency(c, "runtime", "__string_decode_rune"); + add_package_dependency(ctx, "runtime", "__string_decode_rune"); } break; case Type_Array: @@ -1464,12 +1464,12 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Entity *found = nullptr; if (!is_blank_ident(str)) { - found = current_scope_lookup_entity(c->context.scope, str); + found = current_scope_lookup_entity(ctx->scope, str); } if (found == nullptr) { bool is_immutable = true; - entity = alloc_entity_variable(c->context.scope, token, type, is_immutable, EntityState_Resolved); - add_entity_definition(&c->info, name, entity); + entity = alloc_entity_variable(ctx->scope, token, type, is_immutable, EntityState_Resolved); + add_entity_definition(&ctx->checker->info, name, entity); } else { TokenPos pos = found->token.pos; error(token, @@ -1495,20 +1495,20 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } for (isize i = 0; i < entity_count; i++) { - add_entity(c, c->context.scope, entities[i]->identifier, entities[i]); + add_entity(ctx->checker, ctx->scope, entities[i]->identifier, entities[i]); } - check_stmt(c, rs->body, new_flags); + check_stmt(ctx, rs->body, new_flags); - check_close_scope(c); + check_close_scope(ctx); case_end; case_ast_node(ss, SwitchStmt, node); - check_switch_stmt(c, node, mod_flags); + check_switch_stmt(ctx, node, mod_flags); case_end; case_ast_node(ss, TypeSwitchStmt, node); - check_type_switch_stmt(c, node, mod_flags); + check_type_switch_stmt(ctx, node, mod_flags); case_end; @@ -1516,10 +1516,10 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (is_ast_node_decl(ds->stmt)) { error(ds->token, "You cannot defer a declaration"); } else { - bool out_in_defer = c->context.in_defer; - c->context.in_defer = true; - check_stmt(c, ds->stmt, 0); - c->context.in_defer = out_in_defer; + bool out_in_defer = ctx->in_defer; + ctx->in_defer = true; + check_stmt(ctx, ds->stmt, 0); + ctx->in_defer = out_in_defer; } case_end; @@ -1554,12 +1554,12 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { AstNode *ident = bs->label; String name = ident->Ident.token.string; Operand o = {}; - Entity *e = check_ident(c, &o, ident, nullptr, nullptr, false); + Entity *e = check_ident(ctx, &o, ident, nullptr, nullptr, false); if (e == nullptr) { error(ident, "Undeclared label name: %.*s", LIT(name)); return; } - add_entity_use(c, ident, e); + add_entity_use(ctx, ident, e); if (e->kind != Entity_Label) { error(ident, "'%.*s' is not a label", LIT(name)); return; @@ -1581,10 +1581,10 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Operand o = {}; switch (expr->kind) { case AstNode_Ident: - e = check_ident(c, &o, expr, nullptr, nullptr, true); + e = check_ident(ctx, &o, expr, nullptr, nullptr, true); break; case AstNode_SelectorExpr: - e = check_selector(c, &o, expr, nullptr); + e = check_selector(ctx, &o, expr, nullptr); is_selector = true; break; case AstNode_Implicit: @@ -1595,7 +1595,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { continue; } - if (!check_using_stmt_entity(c, us, expr, is_selector, e)) { + if (!check_using_stmt_entity(ctx, us, expr, is_selector, e)) { return; } } @@ -1610,15 +1610,15 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Entity *e = nullptr; Operand o = {}; if (expr->kind == AstNode_Ident) { - e = check_ident(c, &o, expr, nullptr, nullptr, true); + e = check_ident(ctx, &o, expr, nullptr, nullptr, true); } else if (expr->kind == AstNode_SelectorExpr) { - e = check_selector(c, &o, expr, nullptr); + e = check_selector(ctx, &o, expr, nullptr); } if (e == nullptr) { error(expr, "'using' applied to an unknown entity"); return; } - add_entity_use(c, expr, e); + add_entity_use(ctx, expr, e); switch (e->kind) { @@ -1641,8 +1641,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { continue; } - add_entity_use(c, node, f); - add_entity(c, c->context.scope, node, f); + add_entity_use(ctx, node, f); + add_entity(ctx->checker, ctx->scope, node, f); } } else { error(node, "'using' can be only applied to enum type entities"); @@ -1670,8 +1670,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { bool implicit_is_found = ptr_set_exists(&scope->implicit, f); if (is_entity_exported(f) && !implicit_is_found) { - add_entity_use(c, node, f); - add_entity(c, c->context.scope, node, f); + add_entity_use(ctx, node, f); + add_entity(ctx->checker, ctx->scope, node, f); } else { error(node, "'%.*s' is exported from '%.*s'", LIT(f->token.string), LIT(e->token.string)); continue; @@ -1685,7 +1685,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Type *t = base_type(type_deref(e->type)); if (t->kind == Type_Struct) { // TODO(bill): Make it work for unions too - Scope *found = scope_of_node(&c->info, t->Struct.node); + Scope *found = scope_of_node(&ctx->checker->info, t->Struct.node); for_array(list_index, uis->list) { AstNode *node = uis->list[list_index]; ast_node(ident, Ident, node); @@ -1703,7 +1703,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Entity *uvar = alloc_entity_using_variable(e, f->token, f->type); uvar->using_expr = expr; - Entity *prev = scope_insert_entity(c->context.scope, uvar); + Entity *prev = scope_insert_entity(ctx->scope, uvar); if (prev != nullptr) { gbString expr_str = expr_to_string(expr); error(node, "Namespace collision while using '%s' of: '%.*s'", expr_str, LIT(prev->token.string)); @@ -1750,36 +1750,34 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { case_ast_node(pa, PushContext, node); Operand op = {}; - check_expr(c, &op, pa->expr); - check_assignment(c, &op, t_context, str_lit("argument to context <-")); - check_stmt(c, pa->body, mod_flags); + check_expr(ctx, &op, pa->expr); + check_assignment(ctx, &op, t_context, str_lit("argument to context <-")); + check_stmt(ctx, pa->body, mod_flags); case_end; case_ast_node(fb, ForeignBlockDecl, node); AstNode *foreign_library = fb->foreign_library; - CheckerContext prev_context = c->context; - defer (c->context = prev_context); - + CheckerContext c = *ctx; if (foreign_library->kind != AstNode_Ident) { error(foreign_library, "foreign library name must be an identifier"); } else { - c->context.foreign_context.curr_library = foreign_library; - c->context.foreign_context.default_cc = ProcCC_CDecl; + c.foreign_context.curr_library = foreign_library; + c.foreign_context.default_cc = ProcCC_CDecl; } - check_decl_attributes(c, fb->attributes, foreign_block_decl_attribute, nullptr); + check_decl_attributes(&c, fb->attributes, foreign_block_decl_attribute, nullptr); for_array(i, fb->decls) { AstNode *decl = fb->decls[i]; if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) { - check_stmt(c, decl, flags); + check_stmt(&c, decl, flags); } } case_end; case_ast_node(vd, ValueDecl, node); if (vd->is_mutable) { - Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count); + Entity **entities = gb_alloc_array(ctx->allocator, Entity *, vd->names.count); isize entity_count = 0; isize new_name_count = 0; @@ -1794,14 +1792,14 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Entity *found = nullptr; // NOTE(bill): Ignore assignments to '_' if (!is_blank_ident(str)) { - found = current_scope_lookup_entity(c->context.scope, str); + found = current_scope_lookup_entity(ctx->scope, str); new_name_count += 1; } if (found == nullptr) { - entity = alloc_entity_variable(c->context.scope, token, nullptr, false); + entity = alloc_entity_variable(ctx->scope, token, nullptr, false); entity->identifier = name; - AstNode *fl = c->context.foreign_context.curr_library; + AstNode *fl = ctx->foreign_context.curr_library; if (fl != nullptr) { GB_ASSERT(fl->kind == AstNode_Ident); entity->Variable.is_foreign = true; @@ -1819,7 +1817,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (entity == nullptr) { entity = alloc_entity_dummy_variable(universal_scope, ast_node_token(name)); } - entity->parent_proc_decl = c->context.curr_proc_decl; + entity->parent_proc_decl = ctx->curr_proc_decl; entities[entity_count++] = entity; } @@ -1829,7 +1827,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { Type *init_type = nullptr; if (vd->type != nullptr) { - init_type = check_type(c, vd->type); + init_type = check_type(ctx, vd->type); if (init_type == nullptr) { init_type = t_invalid; } else if (is_type_polymorphic(base_type(init_type))) { @@ -1847,8 +1845,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { // TODO NOTE(bill): This technically checks things multple times - AttributeContext ac = make_attribute_context(c->context.foreign_context.link_prefix); - check_decl_attributes(c, vd->attributes, var_decl_attribute, &ac); + AttributeContext ac = make_attribute_context(ctx->foreign_context.link_prefix); + check_decl_attributes(ctx, vd->attributes, var_decl_attribute, &ac); for (isize i = 0; i < entity_count; i++) { Entity *e = entities[i]; @@ -1864,7 +1862,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { e->type = init_type; e->state = EntityState_Resolved; } - ac.link_name = handle_link_name(c, e->token, ac.link_name, ac.link_prefix); + ac.link_name = handle_link_name(ctx, e->token, ac.link_name, ac.link_prefix); e->Variable.thread_local_model = ac.thread_local_model; if (ac.link_name.len > 0) { @@ -1872,8 +1870,8 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { } } - check_arity_match(c, vd); - check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration")); + check_arity_match(ctx, vd); + check_init_variables(ctx, entities, entity_count, vd->values, str_lit("variable declaration")); for (isize i = 0; i < entity_count; i++) { Entity *e = entities[i]; @@ -1890,9 +1888,9 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (vd->values.count > 0) { error(e->token, "A foreign variable declaration cannot have a default value"); } - init_entity_foreign_library(c, e); + init_entity_foreign_library(ctx, e); - auto *fp = &c->info.foreigns; + auto *fp = &ctx->checker->info.foreigns; HashKey key = hash_string(name); Entity **found = map_get(fp, key); if (found) { @@ -1910,7 +1908,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { map_set(fp, key, e); } } - add_entity(c, c->context.scope, e->identifier, e); + add_entity(ctx->checker, ctx->scope, e->identifier, e); } if (vd->is_using != 0) { @@ -1935,13 +1933,13 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) { if (is_blank_ident(name)) { error(token, "'using' cannot be applied variable declared as '_'"); } else if (is_type_struct(t) || is_type_raw_union(t)) { - Scope *scope = scope_of_node(&c->info, t->Struct.node); + Scope *scope = scope_of_node(&ctx->checker->info, t->Struct.node); for_array(i, scope->elements.entries) { Entity *f = scope->elements.entries[i].value; if (f->kind == Entity_Variable) { Entity *uvar = alloc_entity_using_variable(e, f->token, f->type); uvar->Variable.is_immutable = is_immutable; - Entity *prev = scope_insert_entity(c->context.scope, uvar); + Entity *prev = scope_insert_entity(ctx->scope, uvar); if (prev != nullptr) { error(token, "Namespace collision while 'using' '%.*s' of: %.*s", LIT(name), LIT(prev->token.string)); return; |