aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2018-10-13 11:07:56 +0100
committergingerBill <bill@gingerbill.org>2018-10-13 11:07:56 +0100
commit73e9dbbf8c4a68dc6c512eb2de568d59df046494 (patch)
treebb2806ad8b6b078ff3b901cd2e5dba857dc23f32 /src
parent0971a59493d601458ccb386a3752a75f6d880b8f (diff)
switch on typeid with type cases
Diffstat (limited to 'src')
-rw-r--r--src/check_stmt.cpp48
-rw-r--r--src/ir.cpp9
2 files changed, 39 insertions, 18 deletions
diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp
index 94de84f86..0ae83f1ed 100644
--- a/src/check_stmt.cpp
+++ b/src/check_stmt.cpp
@@ -729,32 +729,46 @@ void check_switch_stmt(CheckerContext *ctx, Ast *node, u32 mod_flags) {
add_constant_switch_case(ctx, &seen, rhs);
} else {
Operand y = {};
- check_expr(ctx, &y, expr);
+ if (is_type_typeid(x.type)) {
+ check_expr_or_type(ctx, &y, expr, x.type);
+ } else {
+ check_expr(ctx, &y, expr);
+ }
if (x.mode == Addressing_Invalid ||
y.mode == Addressing_Invalid) {
continue;
}
- convert_to_typed(ctx, &y, x.type);
- if (y.mode == Addressing_Invalid) {
- continue;
- }
+ if (y.mode == Addressing_Type) {
+ Type *t = y.type;
+ if (t == nullptr || t == t_invalid || is_type_polymorphic(t)) {
+ error(y.expr, "Invalid type for case clause");
+ continue;
+ }
+ t = default_type(t);
+ add_type_info_type(ctx, t);
+ } else {
+ convert_to_typed(ctx, &y, x.type);
+ if (y.mode == Addressing_Invalid) {
+ continue;
+ }
- // NOTE(bill): the ordering here matters
- Operand z = y;
- check_comparison(ctx, &z, &x, Token_CmpEq);
- if (z.mode == Addressing_Invalid) {
- continue;
- }
- if (y.mode != Addressing_Constant) {
- if (complete) {
- error(y.expr, "#complete switch statement only allows constant case clauses");
+ // NOTE(bill): the ordering here matters
+ Operand z = y;
+ check_comparison(ctx, &z, &x, Token_CmpEq);
+ if (z.mode == Addressing_Invalid) {
+ continue;
+ }
+ if (y.mode != Addressing_Constant) {
+ if (complete) {
+ error(y.expr, "#complete switch statement only allows constant case clauses");
+ }
+ continue;
}
- continue;
- }
- add_constant_switch_case(ctx, &seen, y);
+ add_constant_switch_case(ctx, &seen, y);
+ }
}
}
diff --git a/src/ir.cpp b/src/ir.cpp
index ffd9f4f67..b51b9c184 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -7145,7 +7145,14 @@ void ir_build_stmt_internal(irProcedure *proc, Ast *node) {
irValue *cond_rhs = ir_emit_comp(proc, op, tag, rhs);
cond = ir_emit_arith(proc, Token_And, cond_lhs, cond_rhs, t_bool);
} else {
- cond = ir_emit_comp(proc, Token_CmpEq, tag, ir_build_expr(proc, expr));
+ if (expr->tav.mode == Addressing_Type) {
+ GB_ASSERT(is_type_typeid(ir_type(tag)));
+ irValue *e = ir_typeid(proc->module, expr->tav.type);
+ e = ir_emit_conv(proc, e, ir_type(tag));
+ cond = ir_emit_comp(proc, Token_CmpEq, tag, e);
+ } else {
+ cond = ir_emit_comp(proc, Token_CmpEq, tag, ir_build_expr(proc, expr));
+ }
}
ir_emit_if(proc, cond, body, next_cond);
ir_start_block(proc, next_cond);