From 0ad1239522c70418990dc7b9da4e128da7cdd1d5 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 3 Dec 2017 20:43:56 -0500 Subject: [PATCH] rework enums and unions and their relationship to each other * @enumTagName renamed to @tagName and it works on enums and union-enums * Remove the EnumTag type. Now there is only enum and union, and the tag type of a union is always an enum. * unions support specifying the tag enum type, and they support inferring an enum tag type. * Enums no longer support field types but they do support setting the tag values. Likewise union-enums when inferring an enum tag type support setting the tag values. * It is now an error for enums and unions to have 0 fields. * switch statements support union-enums closes #618 --- doc/langref.html.in | 21 +- src/all_types.hpp | 59 +-- src/analyze.cpp | 607 +++++++++++----------- src/analyze.hpp | 6 +- src/ast_render.cpp | 7 + src/codegen.cpp | 245 +++------ src/ir.cpp | 488 ++++++++--------- src/ir_print.cpp | 31 +- src/parser.cpp | 29 +- std/build.zig | 42 +- std/debug.zig | 28 +- std/os/child_process.zig | 10 +- std/os/path.zig | 2 +- test/cases/bugs/394.zig | 4 +- test/cases/enum.zig | 24 +- test/cases/enum_with_members.zig | 6 +- test/cases/misc.zig | 16 +- test/cases/reflection.zig | 4 +- test/cases/switch.zig | 16 +- test/cases/switch_prong_err_enum.zig | 4 +- test/cases/switch_prong_implicit_cast.zig | 8 +- test/cases/union.zig | 35 +- test/compile_errors.zig | 54 +- test/tests.zig | 10 +- 24 files changed, 803 insertions(+), 953 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 20b8ae1eee..76bf0ce237 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -136,7 +136,7 @@
  • @divFloor
  • @divTrunc
  • @embedFile
  • -
  • @enumTagName
  • +
  • @tagName
  • @EnumTagType
  • @errorName
  • @fence
  • @@ -2165,7 +2165,7 @@ test "enum variant switch" { }; } -// The @enumTagName and @memberCount builtin functions can be used to +// The @memberName and @memberCount builtin functions can be used to // the string representation and number of members respectively. const BuiltinType = enum { A: f32, @@ -2174,8 +2174,8 @@ const BuiltinType = enum { }; test "enum builtins" { - assert(mem.eql(u8, @enumTagName(BuiltinType.A { 0 }), "A")); - assert(mem.eql(u8, @enumTagName(BuiltinType.C), "C")); + assert(mem.eql(u8, @memberName(BuiltinType.A { 0 }), "A")); + assert(mem.eql(u8, @memberName(BuiltinType.C), "C")); assert(@memberCount(BuiltinType) == 3); }
    $ zig test enum.zig
    @@ -2189,8 +2189,9 @@ Test 4/4 enum builtins...OK

    See also:

    union

    TODO union documentation

    @@ -4252,10 +4253,10 @@ test.zig:6:2: error: found compile log statement -

    @enumTagName

    -
    @enumTagName(value: var) -> []const u8
    +

    @tagName

    +
    @tagName(value: var) -> []const u8

    - Converts an enum tag name to a slice of bytes. + Converts an enum value or union value to a slice of bytes representing the name.

    @EnumTagType

    @EnumTagType(T: type) -> type
    @@ -5843,7 +5844,9 @@ GroupedExpression = "(" Expression ")" KeywordLiteral = "true" | "false" | "null" | "continue" | "undefined" | "error" | "this" | "unreachable" -ContainerDecl = option("extern" | "packed") ("struct" | "union" | ("enum" option(GroupedExpression))) "{" many(ContainerMember) "}" +ContainerDecl = option("extern" | "packed") + ("struct" option(GroupedExpression) | "union" option("enum" option(GroupedExpression) | GroupedExpression) | ("enum" option(GroupedExpression))) + "{" many(ContainerMember) "}"

    Zen