mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
Nested when statements within records
This commit is contained in:
+86
-47
@@ -691,50 +691,51 @@ void populate_using_entity_map(Checker *c, AstNode *node, Type *t, Map<Entity *>
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Returns filled field_count
|
void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields, Map<Entity *> *entity_map, AstNode *record_node, String context) {
|
||||||
Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
if (decl->kind == AstNode_WhenStmt) {
|
||||||
isize init_field_capacity, String context) {
|
ast_node(ws, WhenStmt, decl);
|
||||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
|
Operand operand = {Addressing_Invalid};
|
||||||
defer (gb_temp_arena_memory_end(tmp));
|
check_expr(c, &operand, ws->cond);
|
||||||
|
if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
|
||||||
Array<Entity *> fields = {};
|
error(ws->cond, "Non-constant boolean `when` condition");
|
||||||
array_init(&fields, heap_allocator(), init_field_capacity);
|
return;
|
||||||
|
|
||||||
Map<Entity *> entity_map = {};
|
|
||||||
map_init_with_reserve(&entity_map, c->tmp_allocator, 2*init_field_capacity);
|
|
||||||
|
|
||||||
Entity *using_index_expr = nullptr;
|
|
||||||
|
|
||||||
if (node != nullptr) {
|
|
||||||
GB_ASSERT(node->kind != AstNode_UnionType);
|
|
||||||
}
|
}
|
||||||
|
if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
|
||||||
check_collect_entities(c, decls, false);
|
error(ws->cond, "Invalid body for `when` statement");
|
||||||
for_array(i, c->context.scope->elements.entries) {
|
return;
|
||||||
Entity *e = c->context.scope->elements.entries[i].value;
|
|
||||||
if (e->token.string == "EnumValue") {
|
|
||||||
// gb_printf_err("EnumValue\n");
|
|
||||||
}
|
}
|
||||||
DeclInfo *d = nullptr;
|
if (operand.value.kind == ExactValue_Bool &&
|
||||||
switch (e->kind) {
|
operand.value.value_bool) {
|
||||||
default: continue;
|
for_array(i, ws->body->BlockStmt.stmts) {
|
||||||
case Entity_Constant:
|
AstNode *stmt = ws->body->BlockStmt.stmts[i];
|
||||||
case Entity_TypeName:
|
check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
|
||||||
d = decl_info_of_entity(&c->info, e);
|
|
||||||
if (d != nullptr) {
|
|
||||||
check_entity_decl(c, e, d, nullptr);
|
|
||||||
}
|
}
|
||||||
|
} else if (ws->else_stmt) {
|
||||||
|
switch (ws->else_stmt->kind) {
|
||||||
|
case AstNode_BlockStmt:
|
||||||
|
for_array(i, ws->else_stmt->BlockStmt.stmts) {
|
||||||
|
AstNode *stmt = ws->else_stmt->BlockStmt.stmts[i];
|
||||||
|
check_record_field_decl(c, stmt, fields, entity_map, record_node, context);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case AstNode_WhenStmt:
|
||||||
|
check_record_field_decl(c, ws->else_stmt, fields, entity_map, record_node, context);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error(ws->else_stmt, "Invalid `else` statement in `when` statement");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (decl->kind != AstNode_ValueDecl) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for_array(decl_index, decls) {
|
|
||||||
AstNode *decl = decls[decl_index];
|
|
||||||
if (decl->kind != AstNode_ValueDecl) continue;
|
|
||||||
|
|
||||||
ast_node(vd, ValueDecl, decl);
|
ast_node(vd, ValueDecl, decl);
|
||||||
|
|
||||||
if (!vd->is_mutable) continue;
|
if (!vd->is_mutable) return;
|
||||||
|
|
||||||
Type *type = nullptr;
|
Type *type = nullptr;
|
||||||
if (vd->type != nullptr) {
|
if (vd->type != nullptr) {
|
||||||
@@ -752,7 +753,7 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
|||||||
|
|
||||||
if (!vd->is_mutable) {
|
if (!vd->is_mutable) {
|
||||||
error(vd->names[0], "Immutable values in a %.*s are not yet supported", LIT(context));
|
error(vd->names[0], "Immutable values in a %.*s are not yet supported", LIT(context));
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vd->values.count) {
|
if (vd->values.count) {
|
||||||
@@ -762,20 +763,20 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
|||||||
for_array(name_index, vd->names) {
|
for_array(name_index, vd->names) {
|
||||||
AstNode *name = vd->names[name_index];
|
AstNode *name = vd->names[name_index];
|
||||||
if (!ast_node_expect(name, AstNode_Ident)) {
|
if (!ast_node_expect(name, AstNode_Ident)) {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Token name_token = name->Ident.token;
|
Token name_token = name->Ident.token;
|
||||||
|
|
||||||
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)fields.count);
|
Entity *e = make_entity_field(c->allocator, c->context.scope, name_token, type, is_using, cast(i32)fields->count);
|
||||||
e->identifier = name;
|
e->identifier = name;
|
||||||
if (name_token.string == "_") {
|
if (name_token.string == "_") {
|
||||||
array_add(&fields, e);
|
array_add(fields, e);
|
||||||
} else if (name_token.string == "__tag") {
|
} else if (name_token.string == "__tag") {
|
||||||
error(name, "`__tag` is a reserved identifier for fields");
|
error(name, "`__tag` is a reserved identifier for fields");
|
||||||
} else {
|
} else {
|
||||||
HashKey key = hash_string(name_token.string);
|
HashKey key = hash_string(name_token.string);
|
||||||
Entity **found = map_get(&entity_map, key);
|
Entity **found = map_get(entity_map, key);
|
||||||
if (found != nullptr) {
|
if (found != nullptr) {
|
||||||
Entity *e = *found;
|
Entity *e = *found;
|
||||||
// NOTE(bill): Scope checking already checks the declaration but in many cases, this can happen so why not?
|
// NOTE(bill): Scope checking already checks the declaration but in many cases, this can happen so why not?
|
||||||
@@ -783,14 +784,15 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
|||||||
error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
|
error(name_token, "`%.*s` is already declared in this type", LIT(name_token.string));
|
||||||
error(e->token, "\tpreviously declared");
|
error(e->token, "\tpreviously declared");
|
||||||
} else {
|
} else {
|
||||||
map_set(&entity_map, key, e);
|
map_set(entity_map, key, e);
|
||||||
array_add(&fields, e);
|
array_add(fields, e);
|
||||||
add_entity(c, c->context.scope, name, e);
|
add_entity(c, c->context.scope, name, e);
|
||||||
}
|
}
|
||||||
add_entity_use(c, name, e);
|
add_entity_use(c, name, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Entity *using_index_expr = nullptr;
|
||||||
|
|
||||||
if (is_using) {
|
if (is_using) {
|
||||||
Type *t = base_type(type_deref(type));
|
Type *t = base_type(type_deref(type));
|
||||||
@@ -800,8 +802,8 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
|||||||
Token name_token = vd->names[0]->Ident.token;
|
Token name_token = vd->names[0]->Ident.token;
|
||||||
if (is_type_indexable(t)) {
|
if (is_type_indexable(t)) {
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
for_array(emi, entity_map.entries) {
|
for_array(emi, entity_map->entries) {
|
||||||
Entity *e = entity_map.entries[emi].value;
|
Entity *e = entity_map->entries[emi].value;
|
||||||
if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
|
if (e->kind == Entity_Variable && e->flags & EntityFlag_Using) {
|
||||||
if (is_type_indexable(e->type)) {
|
if (is_type_indexable(e->type)) {
|
||||||
if (e->identifier != vd->names[0]) {
|
if (e->identifier != vd->names[0]) {
|
||||||
@@ -813,21 +815,58 @@ Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ok) {
|
if (ok) {
|
||||||
using_index_expr = fields[fields.count-1];
|
using_index_expr = (*fields)[fields->count-1];
|
||||||
} else {
|
} else {
|
||||||
fields[fields.count-1]->flags &= ~EntityFlag_Using;
|
(*fields)[fields->count-1]->flags &= ~EntityFlag_Using;
|
||||||
error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
|
error(name_token, "Previous `using` for an index expression `%.*s`", LIT(name_token.string));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
gbString type_str = type_to_string(type);
|
gbString type_str = type_to_string(type);
|
||||||
error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
|
error(name_token, "`using` cannot be applied to the field `%.*s` of type `%s`", LIT(name_token.string), type_str);
|
||||||
gb_string_free(type_str);
|
gb_string_free(type_str);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
populate_using_entity_map(c, node, type, &entity_map);
|
populate_using_entity_map(c, record_node, type, entity_map);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns filled field_count
|
||||||
|
Array<Entity *> check_fields(Checker *c, AstNode *node, Array<AstNode *> decls,
|
||||||
|
isize init_field_capacity, String context) {
|
||||||
|
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
|
||||||
|
defer (gb_temp_arena_memory_end(tmp));
|
||||||
|
|
||||||
|
Array<Entity *> fields = {};
|
||||||
|
array_init(&fields, heap_allocator(), init_field_capacity);
|
||||||
|
|
||||||
|
Map<Entity *> entity_map = {};
|
||||||
|
map_init_with_reserve(&entity_map, c->tmp_allocator, 2*init_field_capacity);
|
||||||
|
|
||||||
|
|
||||||
|
if (node != nullptr) {
|
||||||
|
GB_ASSERT(node->kind != AstNode_UnionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
check_collect_entities(c, decls, false);
|
||||||
|
for_array(i, c->context.scope->elements.entries) {
|
||||||
|
Entity *e = c->context.scope->elements.entries[i].value;
|
||||||
|
DeclInfo *d = nullptr;
|
||||||
|
switch (e->kind) {
|
||||||
|
default: continue;
|
||||||
|
case Entity_Constant:
|
||||||
|
case Entity_TypeName:
|
||||||
|
d = decl_info_of_entity(&c->info, e);
|
||||||
|
if (d != nullptr) {
|
||||||
|
check_entity_decl(c, e, d, nullptr);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for_array(decl_index, decls) {
|
||||||
|
check_record_field_decl(c, decls[decl_index], &fields, &entity_map, node, str_lit("struct"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user