aboutsummaryrefslogtreecommitdiff
path: root/src/analyze.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2016-02-04 16:09:06 -0700
committerAndrew Kelley <superjoe30@gmail.com>2016-02-04 16:09:06 -0700
commita4cba900e53154abd5595bacf709fe8fdcc86b27 (patch)
tree906b7b238f5f5d3797d04180c137f9829ca43944 /src/analyze.cpp
parent5490f907fe4b5c8323eaf917b1ead8760c9eca34 (diff)
downloadzig-a4cba900e53154abd5595bacf709fe8fdcc86b27.tar.gz
zig-a4cba900e53154abd5595bacf709fe8fdcc86b27.zip
no namespace required when switching on enum
See #43
Diffstat (limited to 'src/analyze.cpp')
-rw-r--r--src/analyze.cpp28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index 3ab41409bb..4b09c850fa 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -4508,10 +4508,30 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
if (item_node->type == NodeTypeSwitchRange) {
zig_panic("TODO range in switch statement");
}
- analyze_expression(g, import, context, expr_type, item_node);
- ConstExprValue *const_val = &get_resolved_expr(item_node)->const_val;
- if (!const_val->ok) {
- add_node_error(g, item_node, buf_sprintf("unable to resolve constant expression"));
+
+ if (expr_type->id == TypeTableEntryIdEnum) {
+ if (item_node->type == NodeTypeSymbol) {
+ Buf *field_name = &item_node->data.symbol_expr.symbol;
+ TypeEnumField *type_enum_field = get_enum_field(expr_type, field_name);
+ if (type_enum_field) {
+ item_node->data.symbol_expr.enum_field = type_enum_field;
+ } else {
+ add_node_error(g, item_node,
+ buf_sprintf("enum '%s' has no field '%s'",
+ buf_ptr(&expr_type->name), buf_ptr(field_name)));
+ }
+ } else {
+ add_node_error(g, item_node, buf_sprintf("expected enum tag name"));
+ }
+ } else {
+ TypeTableEntry *item_type = analyze_expression(g, import, context, expr_type, item_node);
+ if (item_type->id != TypeTableEntryIdInvalid) {
+ ConstExprValue *const_val = &get_resolved_expr(item_node)->const_val;
+ if (!const_val->ok) {
+ add_node_error(g, item_node,
+ buf_sprintf("unable to resolve constant expression"));
+ }
+ }
}
}
var_type = expr_type;