aboutsummaryrefslogtreecommitdiff
path: root/src/tilde_stmt.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2023-07-25 00:33:43 +0100
committergingerBill <bill@gingerbill.org>2023-07-25 00:33:43 +0100
commitb934e4b564e58c62b8c6848a71fe99b02c588a94 (patch)
treeb23f80418a5e33cf5c9595ad7b4b3aa9c567e215 /src/tilde_stmt.cpp
parent28c97a94670b2b1cad801e9e9d3b5d465f9435d8 (diff)
Implement basic runtime type information
This allows for `runtime.println_any` to work!
Diffstat (limited to 'src/tilde_stmt.cpp')
-rw-r--r--src/tilde_stmt.cpp43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/tilde_stmt.cpp b/src/tilde_stmt.cpp
index 8b577dfeb..a663a401d 100644
--- a/src/tilde_stmt.cpp
+++ b/src/tilde_stmt.cpp
@@ -409,11 +409,9 @@ gb_internal cgValue cg_emit_struct_ep(cgProcedure *p, cgValue s, i64 index) {
switch (t->kind) {
case Type_Struct:
- {
- type_set_offsets(t);
- result_type = t->Struct.fields[index]->type;
- offset = t->Struct.offsets[index];
- }
+ type_set_offsets(t);
+ result_type = t->Struct.fields[index]->type;
+ offset = t->Struct.offsets[index];
break;
case Type_Union:
GB_ASSERT(index == -1);
@@ -421,7 +419,10 @@ gb_internal cgValue cg_emit_struct_ep(cgProcedure *p, cgValue s, i64 index) {
break;
// return cg_emit_union_tag_ptr(p, s);
case Type_Tuple:
- GB_PANIC("TODO(bill): cg_emit_tuple_ep");
+ type_set_offsets(t);
+ result_type = t->Tuple.variables[index]->type;
+ offset = t->Tuple.offsets[index];
+ GB_PANIC("TODO(bill): cg_emit_tuple_ep %d", s.kind);
break;
// return cg_emit_tuple_ep(p, s, index);
case Type_Slice:
@@ -1799,8 +1800,11 @@ gb_internal void cg_build_switch_stmt(cgProcedure *p, Ast *node) {
expr = unparen_expr(expr);
GB_ASSERT(!is_ast_range(expr));
if (expr->tav.mode == Addressing_Type) {
- GB_PANIC("TODO(bill): cg_typeid as i64");
- // key = cg_typeid(p, expr->tav.value.value_typeid);
+ Type *type = expr->tav.value.value_typeid;
+ if (type == nullptr || type == t_invalid) {
+ type = expr->tav.type;
+ }
+ key = cg_typeid_as_u64(p->module, type);
} else {
auto tv = type_and_value_of_expr(expr);
GB_ASSERT(tv.mode == Addressing_Constant);
@@ -1912,21 +1916,16 @@ gb_internal void cg_build_switch_stmt(cgProcedure *p, Ast *node) {
cg_scope_close(p, cgDeferExit_Default, done);
}
-gb_internal void cg_type_case_body(cgProcedure *p, Ast *label, Ast *clause, TB_Node *body_region, TB_Node *done_region) {
- // ast_node(cc, CaseClause, clause);
-
- // cg_push_target_list(p, label, done, nullptr, nullptr);
- // cg_build_stmt_list(p, cc->stmts);
- // cg_scope_close(p, cgDeferExit_Default, body_region);
- // cg_pop_target_list(p);
-
- // cg_emit_goto(p, done_region);
-}
-
gb_internal void cg_build_type_switch_stmt(cgProcedure *p, Ast *node) {
ast_node(ss, TypeSwitchStmt, node);
+ TB_Node *done_region = cg_control_region(p, "typeswitch_done");
+ TB_Node *else_region = done_region;
+ TB_Node *default_region = nullptr;
+ isize num_cases = 0;
+
cg_scope_open(p, ss->scope);
+ defer (cg_scope_close(p, cgDeferExit_Default, done_region));
ast_node(as, AssignStmt, ss->tag);
GB_ASSERT(as->lhs.count == 1);
@@ -1969,11 +1968,6 @@ gb_internal void cg_build_type_switch_stmt(cgProcedure *p, Ast *node) {
ast_node(body, BlockStmt, ss->body);
- TB_Node *done_region = cg_control_region(p, "typeswitch_done");
- TB_Node *else_region = done_region;
- TB_Node *default_region = nullptr;
- isize num_cases = 0;
-
for (Ast *clause : body->stmts) {
ast_node(cc, CaseClause, clause);
num_cases += cc->list.count;
@@ -2158,7 +2152,6 @@ gb_internal void cg_build_type_switch_stmt(cgProcedure *p, Ast *node) {
cg_emit_goto(p, done_region);
tb_inst_set_control(p->func, done_region);
- cg_scope_close(p, cgDeferExit_Default, done_region);
}