authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 15:18:02-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-23 15:18:02-04:00
loge06885d64e3569719e9479c7069da7ad426a70d3
tree075258e85acce6c4d5fb3a29225fc0d97fabfb26
parent9ec6a78f121c8f61c10ea02f6949f27b0228ba16

enums support member functions


7 files changed, 103 insertions(+), 43 deletions(-)

src/all_types.hpp+1-1
......@@ -924,7 +924,7 @@ struct TypeTableEntryError {
924924
925925struct TypeTableEntryEnum {
926926 AstNode *decl_node;
927 uint32_t field_count;
927 uint32_t src_field_count;
928928 uint32_t gen_field_count;
929929 TypeEnumField *fields;
930930 bool is_invalid; // true if any fields are invalid
src/analyze.cpp+63-34
......@@ -1325,7 +1325,7 @@ static void resolve_enum_type(CodeGen *g, ImportTableEntry *import, TypeTableEnt
13251325
13261326 uint32_t field_count = decl_node->data.struct_decl.fields.length;
13271327
1328 enum_type->data.enumeration.field_count = field_count;
1328 enum_type->data.enumeration.src_field_count = field_count;
13291329 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
13301330 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
13311331
......@@ -2451,8 +2451,8 @@ static LabelTableEntry *find_label(CodeGen *g, BlockContext *orig_context, Buf *
24512451 return nullptr;
24522452}
24532453
2454static TypeEnumField *get_enum_field(TypeTableEntry *enum_type, Buf *name) {
2455 for (uint32_t i = 0; i < enum_type->data.enumeration.field_count; i += 1) {
2454static TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) {
2455 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
24562456 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
24572457 if (buf_eql_buf(type_enum_field->name, name)) {
24582458 return type_enum_field;
......@@ -2467,7 +2467,7 @@ static TypeTableEntry *analyze_enum_value_expr(CodeGen *g, ImportTableEntry *imp
24672467{
24682468 assert(field_access_node->type == NodeTypeFieldAccessExpr);
24692469
2470 TypeEnumField *type_enum_field = get_enum_field(enum_type, field_name);
2470 TypeEnumField *type_enum_field = find_enum_type_field(enum_type, field_name);
24712471 if (type_enum_field->type_entry->id == TypeTableEntryIdInvalid) {
24722472 return g->builtin_types.entry_invalid;
24732473 }
......@@ -2715,6 +2715,41 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
27152715 }
27162716}
27172717
2718static TypeTableEntry *analyze_member_access(CodeGen *g, bool wrapped_in_fn_call,
2719 TypeTableEntry *bare_struct_type, Buf *field_name, AstNode *node, TypeTableEntry *struct_type)
2720{
2721 assert(node->type == NodeTypeFieldAccessExpr);
2722 if (wrapped_in_fn_call && !is_slice(bare_struct_type)) {
2723 BlockContext *container_block_context = get_container_block_context(bare_struct_type);
2724 assert(container_block_context);
2725 auto entry = container_block_context->decl_table.maybe_get(field_name);
2726 AstNode *fn_decl_node = entry ? entry->value : nullptr;
2727 if (fn_decl_node && fn_decl_node->type == NodeTypeFnProto) {
2728 resolve_top_level_decl(g, fn_decl_node, false);
2729 TopLevelDecl *tld = get_as_top_level_decl(fn_decl_node);
2730 if (tld->resolution == TldResolutionInvalid) {
2731 return g->builtin_types.entry_invalid;
2732 }
2733
2734 node->data.field_access_expr.is_member_fn = true;
2735 FnTableEntry *fn_entry = fn_decl_node->data.fn_proto.fn_table_entry;
2736 if (fn_entry->type_entry->id == TypeTableEntryIdGenericFn) {
2737 return resolve_expr_const_val_as_generic_fn(g, node, fn_entry->type_entry, false);
2738 } else {
2739 return resolve_expr_const_val_as_fn(g, node, fn_entry, false);
2740 }
2741 } else {
2742 add_node_error(g, node, buf_sprintf("no function named '%s' in '%s'",
2743 buf_ptr(field_name), buf_ptr(&bare_struct_type->name)));
2744 return g->builtin_types.entry_invalid;
2745 }
2746 } else {
2747 add_node_error(g, node,
2748 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), buf_ptr(&struct_type->name)));
2749 return g->builtin_types.entry_invalid;
2750 }
2751}
2752
27182753static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
27192754 TypeTableEntry *expected_type, AstNode *node)
27202755{
......@@ -2742,34 +2777,28 @@ static TypeTableEntry *analyze_field_access_expr(CodeGen *g, ImportTableEntry *i
27422777 node->data.field_access_expr.type_struct_field = find_struct_type_field(bare_struct_type, field_name);
27432778 if (node->data.field_access_expr.type_struct_field) {
27442779 return node->data.field_access_expr.type_struct_field->type_entry;
2745 } else if (wrapped_in_fn_call && !is_slice(bare_struct_type)) {
2746 BlockContext *container_block_context = get_container_block_context(bare_struct_type);
2747 assert(container_block_context);
2748 auto entry = container_block_context->decl_table.maybe_get(field_name);
2749 AstNode *fn_decl_node = entry ? entry->value : nullptr;
2750 if (fn_decl_node && fn_decl_node->type == NodeTypeFnProto) {
2751 resolve_top_level_decl(g, fn_decl_node, false);
2752 TopLevelDecl *tld = get_as_top_level_decl(fn_decl_node);
2753 if (tld->resolution == TldResolutionInvalid) {
2754 return g->builtin_types.entry_invalid;
2755 }
2780 } else {
2781 return analyze_member_access(g, wrapped_in_fn_call, bare_struct_type, field_name,
2782 node, struct_type);
2783 }
2784 } else if (struct_type->id == TypeTableEntryIdEnum || (struct_type->id == TypeTableEntryIdPointer &&
2785 struct_type->data.pointer.child_type->id == TypeTableEntryIdEnum))
2786 {
2787 TypeTableEntry *bare_struct_type = (struct_type->id == TypeTableEntryIdEnum) ?
2788 struct_type : struct_type->data.pointer.child_type;
27562789
2757 node->data.field_access_expr.is_member_fn = true;
2758 FnTableEntry *fn_entry = fn_decl_node->data.fn_proto.fn_table_entry;
2759 if (fn_entry->type_entry->id == TypeTableEntryIdGenericFn) {
2760 return resolve_expr_const_val_as_generic_fn(g, node, fn_entry->type_entry, false);
2761 } else {
2762 return resolve_expr_const_val_as_fn(g, node, fn_entry, false);
2763 }
2764 } else {
2765 add_node_error(g, node, buf_sprintf("no function named '%s' in '%s'",
2766 buf_ptr(field_name), buf_ptr(&bare_struct_type->name)));
2767 return g->builtin_types.entry_invalid;
2768 }
2790 if (!bare_struct_type->data.enumeration.complete) {
2791 resolve_struct_type(g, bare_struct_type->data.enumeration.decl_node->owner, bare_struct_type);
2792 }
2793
2794 node->data.field_access_expr.bare_struct_type = bare_struct_type;
2795 node->data.field_access_expr.type_enum_field = find_enum_type_field(bare_struct_type, field_name);
2796
2797 if (node->data.field_access_expr.type_enum_field) {
2798 return node->data.field_access_expr.type_enum_field->type_entry;
27692799 } else {
2770 add_node_error(g, node,
2771 buf_sprintf("no member named '%s' in '%s'", buf_ptr(field_name), buf_ptr(&struct_type->name)));
2772 return g->builtin_types.entry_invalid;
2800 return analyze_member_access(g, wrapped_in_fn_call, bare_struct_type, field_name,
2801 node, struct_type);
27732802 }
27742803 } else if (struct_type->id == TypeTableEntryIdArray) {
27752804 if (buf_eql_str(field_name, "len")) {
......@@ -5269,7 +5298,7 @@ static TypeTableEntry *analyze_builtin_fn_call_expr(CodeGen *g, ImportTableEntry
52695298 if (type_entry->id == TypeTableEntryIdInvalid) {
52705299 return type_entry;
52715300 } else if (type_entry->id == TypeTableEntryIdEnum) {
5272 uint64_t value_count = type_entry->data.enumeration.field_count;
5301 uint64_t value_count = type_entry->data.enumeration.src_field_count;
52735302 return resolve_expr_const_val_as_unsigned_num_lit(g, node, expected_type,
52745303 value_count, false);
52755304 } else {
......@@ -6130,7 +6159,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
61306159 size_t *field_use_counts = nullptr;
61316160 HashMap<int, AstNode *, int_hash, int_eq> err_use_nodes = {};
61326161 if (expr_type->id == TypeTableEntryIdEnum) {
6133 field_use_counts = allocate<size_t>(expr_type->data.enumeration.field_count);
6162 field_use_counts = allocate<size_t>(expr_type->data.enumeration.src_field_count);
61346163 } else if (expr_type->id == TypeTableEntryIdErrorUnion) {
61356164 err_use_nodes.init(10);
61366165 }
......@@ -6168,7 +6197,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
61686197 if (expr_type->id == TypeTableEntryIdEnum) {
61696198 if (item_node->type == NodeTypeSymbol) {
61706199 Buf *field_name = item_node->data.symbol_expr.symbol;
6171 TypeEnumField *type_enum_field = get_enum_field(expr_type, field_name);
6200 TypeEnumField *type_enum_field = find_enum_type_field(expr_type, field_name);
61726201 if (type_enum_field) {
61736202 item_node->data.symbol_expr.enum_field = type_enum_field;
61746203 if (!var_type) {
......@@ -6300,7 +6329,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
63006329 }
63016330
63026331 if (expr_type->id == TypeTableEntryIdEnum && !else_prong) {
6303 for (uint32_t i = 0; i < expr_type->data.enumeration.field_count; i += 1) {
6332 for (uint32_t i = 0; i < expr_type->data.enumeration.src_field_count; i += 1) {
63046333 if (field_use_counts[i] == 0) {
63056334 add_node_error(g, node,
63066335 buf_sprintf("enumeration value '%s' not handled in switch",
src/codegen.cpp+5-5
......@@ -4605,7 +4605,7 @@ static void define_builtin_types(CodeGen *g) {
46054605 entry->zero_bits = true; // only allowed at compile time
46064606 buf_init_from_str(&entry->name, "@OS");
46074607 uint32_t field_count = target_os_count();
4608 entry->data.enumeration.field_count = field_count;
4608 entry->data.enumeration.src_field_count = field_count;
46094609 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
46104610 for (uint32_t i = 0; i < field_count; i += 1) {
46114611 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
......@@ -4631,7 +4631,7 @@ static void define_builtin_types(CodeGen *g) {
46314631 entry->zero_bits = true; // only allowed at compile time
46324632 buf_init_from_str(&entry->name, "@Arch");
46334633 uint32_t field_count = target_arch_count();
4634 entry->data.enumeration.field_count = field_count;
4634 entry->data.enumeration.src_field_count = field_count;
46354635 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
46364636 for (uint32_t i = 0; i < field_count; i += 1) {
46374637 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
......@@ -4663,7 +4663,7 @@ static void define_builtin_types(CodeGen *g) {
46634663 entry->zero_bits = true; // only allowed at compile time
46644664 buf_init_from_str(&entry->name, "@Environ");
46654665 uint32_t field_count = target_environ_count();
4666 entry->data.enumeration.field_count = field_count;
4666 entry->data.enumeration.src_field_count = field_count;
46674667 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
46684668 for (uint32_t i = 0; i < field_count; i += 1) {
46694669 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
......@@ -4690,7 +4690,7 @@ static void define_builtin_types(CodeGen *g) {
46904690 entry->zero_bits = true; // only allowed at compile time
46914691 buf_init_from_str(&entry->name, "@ObjectFormat");
46924692 uint32_t field_count = target_oformat_count();
4693 entry->data.enumeration.field_count = field_count;
4693 entry->data.enumeration.src_field_count = field_count;
46944694 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
46954695 for (uint32_t i = 0; i < field_count; i += 1) {
46964696 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
......@@ -4716,7 +4716,7 @@ static void define_builtin_types(CodeGen *g) {
47164716 entry->deep_const = true;
47174717 buf_init_from_str(&entry->name, "AtomicOrder");
47184718 uint32_t field_count = 6;
4719 entry->data.enumeration.field_count = field_count;
4719 entry->data.enumeration.src_field_count = field_count;
47204720 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
47214721 entry->data.enumeration.fields[0].name = buf_create_from_str("Unordered");
47224722 entry->data.enumeration.fields[0].value = AtomicOrderUnordered;
src/eval.cpp+1-1
......@@ -683,7 +683,7 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
683683 {
684684 uint64_t value = other_val->data.x_bignum.data.x_uint;
685685 assert(new_type->id == TypeTableEntryIdEnum);
686 assert(value < new_type->data.enumeration.field_count);
686 assert(value < new_type->data.enumeration.src_field_count);
687687 const_val->data.x_enum.tag = value;
688688 const_val->data.x_enum.payload = NULL;
689689 const_val->ok = true;
src/parseh.cpp+2-2
......@@ -870,7 +870,7 @@ static TypeTableEntry *resolve_enum_decl(Context *c, const EnumDecl *enum_decl)
870870 enum_type->data.enumeration.complete = true;
871871 enum_type->data.enumeration.tag_type = tag_type_entry;
872872
873 enum_type->data.enumeration.field_count = field_count;
873 enum_type->data.enumeration.src_field_count = field_count;
874874 enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
875875 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
876876
......@@ -977,7 +977,7 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
977977 enum_node->data.struct_decl.top_level_decl.visib_mod = VisibModExport;
978978 enum_node->data.struct_decl.type_entry = enum_type;
979979
980 for (uint32_t i = 0; i < enum_type->data.enumeration.field_count; i += 1) {
980 for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) {
981981 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i];
982982 AstNode *type_node = make_type_node(c, type_enum_field->type_entry);
983983 AstNode *field_node = create_struct_field_node(c, buf_ptr(type_enum_field->name), type_node);
test/cases/enum_with_members.zig created+30
......@@ -0,0 +1,30 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const io = std.io;
4const str = std.str;
5
6enum ET {
7 SINT: i32,
8 UINT: u32,
9
10 pub fn print(a: &ET, buf: []u8) -> %usize {
11 return switch (*a) {
12 SINT => |x| { io.bufPrintInt(i32, buf, x) },
13 UINT => |x| { io.bufPrintInt(u32, buf, x) },
14 }
15 }
16}
17
18#attribute("test")
19fn enumWithMembers() {
20 const a = ET.SINT { -42 };
21 const b = ET.UINT { 42 };
22 var buf: [20]u8 = undefined;
23
24 assert(%%a.print(buf) == 3);
25 assert(str.eql(buf[0...3], "-42"));
26
27 assert(%%b.print(buf) == 2);
28 assert(str.eql(buf[0...2], "42"));
29}
30
test/self_hosted.zig+1
......@@ -13,6 +13,7 @@ const test_var_params = @import("cases/var_params.zig");
1313const test_const_slice_child = @import("cases/const_slice_child.zig");
1414const test_switch_prong_implicit_cast = @import("cases/switch_prong_implicit_cast.zig");
1515const test_switch_prong_err_enum = @import("cases/switch_prong_err_enum.zig");
16const test_enum_with_members = @import("cases/enum_with_members.zig");
1617
1718// normal comment
1819/// this is a documentation comment