authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-11 20:38:13-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-05-11 20:38:13-04:00
logedcc7c72d1a684a8a16ca23ad26689f2cce4e803
treec8191ced7f0f35e70b5a824ab9f1245310c50f6b
parent6cf7fb1177848c2f2a20c1f4093b1e003c477911
parentd210628c91f2d33a168c71fc7633745ead925dfc
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2459 from LemonBoy/enum-num-um-m

Signed types for enum tags

3 files changed, 102 insertions(+), 70 deletions(-)

src/analyze.cpp+61-47
......@@ -1908,6 +1908,18 @@ static Error resolve_union_type(CodeGen *g, ZigType *union_type) {
19081908 return ErrorNone;
19091909}
19101910
1911static bool type_is_valid_extern_enum_tag(CodeGen *g, ZigType *ty) {
1912 // Only integer types are allowed by the C ABI
1913 if(ty->id != ZigTypeIdInt)
1914 return false;
1915
1916 // According to the ANSI C standard the enumeration type should be either a
1917 // signed char, a signed integer or an unsigned one. But GCC/Clang allow
1918 // other integral types as a compiler extension so let's accomodate them
1919 // aswell.
1920 return type_allowed_in_extern(g, ty);
1921}
1922
19111923static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19121924 assert(enum_type->id == ZigTypeIdEnum);
19131925
......@@ -1965,7 +1977,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19651977 enum_type->abi_size = tag_int_type->abi_size;
19661978 enum_type->abi_align = tag_int_type->abi_align;
19671979
1968 // TODO: Are extern enums allowed to have an init_arg_expr?
19691980 if (decl_node->data.container_decl.init_arg_expr != nullptr) {
19701981 ZigType *wanted_tag_int_type = analyze_type_expr(g, scope, decl_node->data.container_decl.init_arg_expr);
19711982 if (type_is_invalid(wanted_tag_int_type)) {
......@@ -1974,24 +1985,29 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
19741985 enum_type->data.enumeration.is_invalid = true;
19751986 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
19761987 buf_sprintf("expected integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));
1977 } else if (wanted_tag_int_type->data.integral.is_signed) {
1988 } else if (enum_type->data.enumeration.layout == ContainerLayoutExtern &&
1989 !type_is_valid_extern_enum_tag(g, wanted_tag_int_type)) {
19781990 enum_type->data.enumeration.is_invalid = true;
1979 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
1980 buf_sprintf("expected unsigned integer, found '%s'", buf_ptr(&wanted_tag_int_type->name)));
1981 } else if (wanted_tag_int_type->data.integral.bit_count < tag_int_type->data.integral.bit_count) {
1982 enum_type->data.enumeration.is_invalid = true;
1983 add_node_error(g, decl_node->data.container_decl.init_arg_expr,
1984 buf_sprintf("'%s' too small to hold all bits; must be at least '%s'",
1985 buf_ptr(&wanted_tag_int_type->name), buf_ptr(&tag_int_type->name)));
1991 ErrorMsg *msg = add_node_error(g, decl_node->data.container_decl.init_arg_expr,
1992 buf_sprintf("'%s' is not a valid tag type for an extern enum",
1993 buf_ptr(&wanted_tag_int_type->name)));
1994 add_error_note(g, msg, decl_node->data.container_decl.init_arg_expr,
1995 buf_sprintf("any integral type of size 8, 16, 32, 64 or 128 bit is valid"));
19861996 } else {
19871997 tag_int_type = wanted_tag_int_type;
19881998 }
19891999 }
2000
19902001 enum_type->data.enumeration.tag_int_type = tag_int_type;
19912002 enum_type->size_in_bits = tag_int_type->size_in_bits;
19922003 enum_type->abi_size = tag_int_type->abi_size;
19932004 enum_type->abi_align = tag_int_type->abi_align;
19942005
2006 BigInt bi_one;
2007 bigint_init_unsigned(&bi_one, 1);
2008
2009 TypeEnumField *last_enum_field = nullptr;
2010
19952011 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
19962012 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
19972013 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i];
......@@ -2017,60 +2033,58 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
20172033
20182034 AstNode *tag_value = field_node->data.struct_field.value;
20192035
2020 // In this first pass we resolve explicit tag values.
2021 // In a second pass we will fill in the unspecified ones.
20222036 if (tag_value != nullptr) {
2037 // A user-specified value is available
20232038 ConstExprValue *result = analyze_const_value(g, scope, tag_value, tag_int_type, nullptr);
20242039 if (type_is_invalid(result->type)) {
20252040 enum_type->data.enumeration.is_invalid = true;
20262041 continue;
20272042 }
2043
20282044 assert(result->special != ConstValSpecialRuntime);
2029 assert(result->type->id == ZigTypeIdInt ||
2030 result->type->id == ZigTypeIdComptimeInt);
2031 auto entry = occupied_tag_values.put_unique(result->data.x_bigint, tag_value);
2032 if (entry == nullptr) {
2033 bigint_init_bigint(&type_enum_field->value, &result->data.x_bigint);
2045 assert(result->type->id == ZigTypeIdInt || result->type->id == ZigTypeIdComptimeInt);
2046
2047 bigint_init_bigint(&type_enum_field->value, &result->data.x_bigint);
2048 } else {
2049 // No value was explicitly specified: allocate the last value + 1
2050 // or, if this is the first element, zero
2051 if (last_enum_field != nullptr) {
2052 bigint_add(&type_enum_field->value, &last_enum_field->value, &bi_one);
20342053 } else {
2035 Buf *val_buf = buf_alloc();
2036 bigint_append_buf(val_buf, &result->data.x_bigint, 10);
2054 bigint_init_unsigned(&type_enum_field->value, 0);
2055 }
20372056
2038 ErrorMsg *msg = add_node_error(g, tag_value,
2039 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
2040 add_error_note(g, msg, entry->value,
2041 buf_sprintf("other occurrence here"));
2057 // Make sure we can represent this number with tag_int_type
2058 if (!bigint_fits_in_bits(&type_enum_field->value,
2059 tag_int_type->size_in_bits,
2060 tag_int_type->data.integral.is_signed)) {
20422061 enum_type->data.enumeration.is_invalid = true;
2043 continue;
2062
2063 Buf *val_buf = buf_alloc();
2064 bigint_append_buf(val_buf, &type_enum_field->value, 10);
2065 add_node_error(g, field_node,
2066 buf_sprintf("enumeration value %s too large for type '%s'",
2067 buf_ptr(val_buf), buf_ptr(&tag_int_type->name)));
2068
2069 break;
20442070 }
20452071 }
2046 }
20472072
2048 // Now iterate again and populate the unspecified tag values
2049 uint32_t next_maybe_unoccupied_index = 0;
2073 // Make sure the value is unique
2074 auto entry = occupied_tag_values.put_unique(type_enum_field->value, field_node);
2075 if (entry != nullptr) {
2076 enum_type->data.enumeration.is_invalid = true;
20502077
2051 for (uint32_t field_i = 0; field_i < field_count; field_i += 1) {
2052 AstNode *field_node = decl_node->data.container_decl.fields.at(field_i);
2053 TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i];
2054 AstNode *tag_value = field_node->data.struct_field.value;
2078 Buf *val_buf = buf_alloc();
2079 bigint_append_buf(val_buf, &type_enum_field->value, 10);
20552080
2056 if (tag_value == nullptr) {
2057 if (occupied_tag_values.size() == 0) {
2058 bigint_init_unsigned(&type_enum_field->value, next_maybe_unoccupied_index);
2059 next_maybe_unoccupied_index += 1;
2060 } else {
2061 BigInt proposed_value;
2062 for (;;) {
2063 bigint_init_unsigned(&proposed_value, next_maybe_unoccupied_index);
2064 next_maybe_unoccupied_index += 1;
2065 auto entry = occupied_tag_values.put_unique(proposed_value, field_node);
2066 if (entry != nullptr) {
2067 continue;
2068 }
2069 break;
2070 }
2071 bigint_init_bigint(&type_enum_field->value, &proposed_value);
2072 }
2081 ErrorMsg *msg = add_node_error(g, field_node,
2082 buf_sprintf("enum tag value %s already taken", buf_ptr(val_buf)));
2083 add_error_note(g, msg, entry->value,
2084 buf_sprintf("other occurrence here"));
20732085 }
2086
2087 last_enum_field = type_enum_field;
20742088 }
20752089
20762090 enum_type->data.enumeration.zero_bits_loop_flag = false;
test/compile_errors.zig+17-23
......@@ -12,6 +12,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
1212 "tmp.zig:3:32: error: cast discards const qualifier",
1313 );
1414
15 cases.add(
16 "overflow in enum value allocation",
17 \\const Moo = enum(u8) {
18 \\ Last = 255,
19 \\ Over,
20 \\};
21 \\pub fn main() void {
22 \\ var y = Moo.Last;
23 \\}
24 ,
25 "tmp.zig:3:5: error: enumeration value 256 too large for type 'u8'",
26 );
27
1528 cases.add(
1629 "attempt to cast enum literal to error",
1730 \\export fn entry() void {
......@@ -5383,8 +5396,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53835396 "tmp.zig:12:20: note: referenced here",
53845397 );
53855398
5386 cases.add(
5387 "specify enum tag type that is too small",
5399 cases.add("specify enum tag type that is too small",
53885400 \\const Small = enum (u2) {
53895401 \\ One,
53905402 \\ Two,
......@@ -5396,9 +5408,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53965408 \\export fn entry() void {
53975409 \\ var x = Small.One;
53985410 \\}
5399 ,
5400 "tmp.zig:1:21: error: 'u2' too small to hold all bits; must be at least 'u3'",
5401 );
5411 , "tmp.zig:6:5: error: enumeration value 4 too large for type 'u2'");
54025412
54035413 cases.add(
54045414 "specify non-integer enum tag type",
......@@ -5448,22 +5458,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
54485458 "tmp.zig:10:31: error: expected type 'u2', found 'u3'",
54495459 );
54505460
5451 cases.add(
5452 "non unsigned integer enum tag type",
5453 \\const Small = enum(i2) {
5454 \\ One,
5455 \\ Two,
5456 \\ Three,
5457 \\ Four,
5458 \\};
5459 \\
5460 \\export fn entry() void {
5461 \\ var y = Small.Two;
5462 \\}
5463 ,
5464 "tmp.zig:1:20: error: expected unsigned integer, found 'i2'",
5465 );
5466
54675461 cases.add(
54685462 "struct fields with value assignments",
54695463 \\const MultipleChoice = struct {
......@@ -5522,8 +5516,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
55225516 \\ var x = MultipleChoice.C;
55235517 \\}
55245518 ,
5525 "tmp.zig:6:9: error: enum tag value 60 already taken",
5526 "tmp.zig:4:9: note: other occurrence here",
5519 "tmp.zig:6:5: error: enum tag value 60 already taken",
5520 "tmp.zig:4:5: note: other occurrence here",
55275521 );
55285522
55295523 cases.add(
test/stage1/behavior/enum.zig+24
......@@ -938,3 +938,27 @@ test "enum literal in array literal" {
938938 expect(array[0] == .one);
939939 expect(array[1] == .two);
940940}
941
942test "signed integer as enum tag" {
943 const SignedEnum = enum(i2) {
944 A0 = -1,
945 A1 = 0,
946 A2 = 1,
947 };
948
949 expect(@enumToInt(SignedEnum.A0) == -1);
950 expect(@enumToInt(SignedEnum.A1) == 0);
951 expect(@enumToInt(SignedEnum.A2) == 1);
952}
953
954test "enum value allocation" {
955 const LargeEnum = enum(u32) {
956 A0 = 0x80000000,
957 A1,
958 A2,
959 };
960
961 expect(@enumToInt(LargeEnum.A0) == 0x80000000);
962 expect(@enumToInt(LargeEnum.A1) == 0x80000001);
963 expect(@enumToInt(LargeEnum.A2) == 0x80000002);
964}