aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-12-03 20:43:56 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-12-03 20:43:56 -0500
commit0ad1239522c70418990dc7b9da4e128da7cdd1d5 (patch)
tree3a434788633db0a3d6e30f779fc1239a7513205a /std
parent137c8f5e8a6023db24f90555e968b592a4b843e4 (diff)
downloadzig-0ad1239522c70418990dc7b9da4e128da7cdd1d5.tar.gz
zig-0ad1239522c70418990dc7b9da4e128da7cdd1d5.zip
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
Diffstat (limited to 'std')
-rw-r--r--std/build.zig42
-rw-r--r--std/debug.zig28
-rw-r--r--std/os/child_process.zig10
-rw-r--r--std/os/path.zig2
4 files changed, 40 insertions, 42 deletions
diff --git a/std/build.zig b/std/build.zig
index 009295c6ad..9bdc4b3076 100644
--- a/std/build.zig
+++ b/std/build.zig
@@ -69,8 +69,8 @@ pub const Builder = struct {
used: bool,
};
- const UserValue = enum {
- Flag,
+ const UserValue = union(enum) {
+ Flag: void,
Scalar: []const u8,
List: ArrayList([]const u8),
};
@@ -450,7 +450,7 @@ pub const Builder = struct {
pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {
if (%%self.user_input_options.put(name, UserInputOption {
.name = name,
- .value = UserValue.Scalar { value },
+ .value = UserValue { .Scalar = value },
.used = false,
})) |*prev_value| {
// option already exists
@@ -462,7 +462,7 @@ pub const Builder = struct {
%%list.append(value);
_ = %%self.user_input_options.put(name, UserInputOption {
.name = name,
- .value = UserValue.List { list },
+ .value = UserValue { .List = list },
.used = false,
});
},
@@ -471,7 +471,7 @@ pub const Builder = struct {
%%list.append(value);
_ = %%self.user_input_options.put(name, UserInputOption {
.name = name,
- .value = UserValue.List { *list },
+ .value = UserValue { .List = *list },
.used = false,
});
},
@@ -487,7 +487,7 @@ pub const Builder = struct {
pub fn addUserInputFlag(self: &Builder, name: []const u8) -> bool {
if (%%self.user_input_options.put(name, UserInputOption {
.name = name,
- .value = UserValue.Flag,
+ .value = UserValue {.Flag = {} },
.used = false,
})) |*prev_value| {
switch (prev_value.value) {
@@ -685,8 +685,8 @@ const CrossTarget = struct {
environ: builtin.Environ,
};
-const Target = enum {
- Native,
+const Target = union(enum) {
+ Native: void,
Cross: CrossTarget,
pub fn oFileExt(self: &const Target) -> []const u8 {
@@ -844,7 +844,7 @@ pub const LibExeObjStep = struct {
.kind = kind,
.root_src = root_src,
.name = name,
- .target = Target.Native,
+ .target = Target { .Native = {} },
.linker_script = null,
.link_libs = BufSet.init(builder.allocator),
.frameworks = BufSet.init(builder.allocator),
@@ -879,7 +879,7 @@ pub const LibExeObjStep = struct {
.kind = kind,
.version = *version,
.static = static,
- .target = Target.Native,
+ .target = Target { .Native = {} },
.cflags = ArrayList([]const u8).init(builder.allocator),
.source_files = ArrayList([]const u8).init(builder.allocator),
.object_files = ArrayList([]const u8).init(builder.allocator),
@@ -948,8 +948,8 @@ pub const LibExeObjStep = struct {
pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,
target_environ: builtin.Environ)
{
- self.target = Target.Cross {
- CrossTarget {
+ self.target = Target {
+ .Cross = CrossTarget {
.arch = target_arch,
.os = target_os,
.environ = target_environ,
@@ -1186,13 +1186,13 @@ pub const LibExeObjStep = struct {
Target.Native => {},
Target.Cross => |cross_target| {
%%zig_args.append("--target-arch");
- %%zig_args.append(@enumTagName(cross_target.arch));
+ %%zig_args.append(@tagName(cross_target.arch));
%%zig_args.append("--target-os");
- %%zig_args.append(@enumTagName(cross_target.os));
+ %%zig_args.append(@tagName(cross_target.os));
%%zig_args.append("--target-environ");
- %%zig_args.append(@enumTagName(cross_target.environ));
+ %%zig_args.append(@tagName(cross_target.environ));
},
}
@@ -1553,7 +1553,7 @@ pub const TestStep = struct {
.name_prefix = "",
.filter = null,
.link_libs = BufSet.init(builder.allocator),
- .target = Target.Native,
+ .target = Target { .Native = {} },
.exec_cmd_args = null,
}
}
@@ -1581,8 +1581,8 @@ pub const TestStep = struct {
pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,
target_environ: builtin.Environ)
{
- self.target = Target.Cross {
- CrossTarget {
+ self.target = Target {
+ .Cross = CrossTarget {
.arch = target_arch,
.os = target_os,
.environ = target_environ,
@@ -1620,13 +1620,13 @@ pub const TestStep = struct {
Target.Native => {},
Target.Cross => |cross_target| {
%%zig_args.append("--target-arch");
- %%zig_args.append(@enumTagName(cross_target.arch));
+ %%zig_args.append(@tagName(cross_target.arch));
%%zig_args.append("--target-os");
- %%zig_args.append(@enumTagName(cross_target.os));
+ %%zig_args.append(@tagName(cross_target.os));
%%zig_args.append("--target-environ");
- %%zig_args.append(@enumTagName(cross_target.environ));
+ %%zig_args.append(@tagName(cross_target.environ));
},
}
diff --git a/std/debug.zig b/std/debug.zig
index 50322024c3..5bfa436614 100644
--- a/std/debug.zig
+++ b/std/debug.zig
@@ -280,7 +280,7 @@ const AbbrevAttr = struct {
form_id: u64,
};
-const FormValue = enum {
+const FormValue = union(enum) {
Address: u64,
Block: []u8,
Const: Constant,
@@ -475,7 +475,7 @@ fn readAllocBytes(allocator: &mem.Allocator, in_stream: &io.InStream, size: usiz
fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {
const buf = %return readAllocBytes(allocator, in_stream, size);
- return FormValue.Block { buf };
+ return FormValue { .Block = buf };
}
fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {
@@ -484,7 +484,7 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size:
}
fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue {
- FormValue.Const { Constant {
+ FormValue { .Const = Constant {
.signed = signed,
.payload = %return readAllocBytes(allocator, in_stream, size),
}}
@@ -510,7 +510,7 @@ fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 {
fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue {
const buf = %return readAllocBytes(allocator, in_stream, size);
- return FormValue.Ref { buf };
+ return FormValue { .Ref = buf };
}
fn parseFormValueRef(allocator: &mem.Allocator, in_stream: &io.InStream, comptime T: type) -> %FormValue {
@@ -520,7 +520,7 @@ fn parseFormValueRef(allocator: &mem.Allocator, in_stream: &io.InStream, comptim
fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u64, is_64: bool) -> %FormValue {
return switch (form_id) {
- DW.FORM_addr => FormValue.Address { %return parseFormValueTargetAddrSize(in_stream) },
+ DW.FORM_addr => FormValue { .Address = %return parseFormValueTargetAddrSize(in_stream) },
DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1),
DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2),
DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4),
@@ -540,13 +540,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u
DW.FORM_exprloc => {
const size = %return readULeb128(in_stream);
const buf = %return readAllocBytes(allocator, in_stream, size);
- return FormValue.ExprLoc { buf };
- },
- DW.FORM_flag => FormValue.Flag { (%return in_stream.readByte()) != 0 },
- DW.FORM_flag_present => FormValue.Flag { true },
- DW.FORM_sec_offset => FormValue.SecOffset {
- %return parseFormValueDwarfOffsetSize(in_stream, is_64)
+ return FormValue { .ExprLoc = buf };
},
+ DW.FORM_flag => FormValue { .Flag = (%return in_stream.readByte()) != 0 },
+ DW.FORM_flag_present => FormValue { .Flag = true },
+ DW.FORM_sec_offset => FormValue { .SecOffset = %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8),
DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16),
@@ -557,11 +555,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u
parseFormValueRefLen(allocator, in_stream, ref_len)
},
- DW.FORM_ref_addr => FormValue.RefAddr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
- DW.FORM_ref_sig8 => FormValue.RefSig8 { %return in_stream.readIntLe(u64) },
+ DW.FORM_ref_addr => FormValue { .RefAddr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
+ DW.FORM_ref_sig8 => FormValue { .RefSig8 = %return in_stream.readIntLe(u64) },
- DW.FORM_string => FormValue.String { %return readStringRaw(allocator, in_stream) },
- DW.FORM_strp => FormValue.StrPtr { %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
+ DW.FORM_string => FormValue { .String = %return readStringRaw(allocator, in_stream) },
+ DW.FORM_strp => FormValue { .StrPtr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) },
DW.FORM_indirect => {
const child_form_id = %return readULeb128(in_stream);
parseFormValue(allocator, in_stream, child_form_id, is_64)
diff --git a/std/os/child_process.zig b/std/os/child_process.zig
index 005d9772e4..3a4c9410c5 100644
--- a/std/os/child_process.zig
+++ b/std/os/child_process.zig
@@ -58,7 +58,7 @@ pub const ChildProcess = struct {
err_pipe: if (is_windows) void else [2]i32,
llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
- pub const Term = enum {
+ pub const Term = union(enum) {
Exited: i32,
Signal: i32,
Stopped: i32,
@@ -281,13 +281,13 @@ pub const ChildProcess = struct {
fn statusToTerm(status: i32) -> Term {
return if (posix.WIFEXITED(status)) {
- Term.Exited { posix.WEXITSTATUS(status) }
+ Term { .Exited = posix.WEXITSTATUS(status) }
} else if (posix.WIFSIGNALED(status)) {
- Term.Signal { posix.WTERMSIG(status) }
+ Term { .Signal = posix.WTERMSIG(status) }
} else if (posix.WIFSTOPPED(status)) {
- Term.Stopped { posix.WSTOPSIG(status) }
+ Term { .Stopped = posix.WSTOPSIG(status) }
} else {
- Term.Unknown { status }
+ Term { .Unknown = status }
};
}
diff --git a/std/os/path.zig b/std/os/path.zig
index a372b5b077..3fd7b5e2db 100644
--- a/std/os/path.zig
+++ b/std/os/path.zig
@@ -1016,7 +1016,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
return os.readLink(allocator, proc_path);
},
- else => @compileError("TODO implement os.path.real for " ++ @enumTagName(builtin.os)),
+ else => @compileError("TODO implement os.path.real for " ++ @tagName(builtin.os)),
}
}