diff --git a/.travis.yml b/.travis.yml index c5299e914e..731202f5f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,22 @@ sudo: required services: - - docker +- docker os: - - linux - - osx +- linux +- osx dist: trusty osx_image: xcode8.3 language: cpp before_install: - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_before_install; fi - - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_before_install; fi +- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_before_install; fi +- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_before_install; fi install: - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_install; fi - - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_install; fi +- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_install; fi +- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_install; fi script: - - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_script; fi - - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_script; fi +- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then ci/travis_linux_script; fi +- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ci/travis_osx_script; fi +env: + global: + - secure: QmJ+eLOxj3Irl5SHxt6lQvrj7++1AIz8bYri6RScAQGHQPIztkmbpBjAkpFgYaWPkZ04ROtamFXdS7oHtJHSECesgPoqM/CHIychQkgpDq30+TsFyYbBpDGHY+N6r2WnQTvg+9EuAp6P365us6qFS0D5zQ3P40c56uMbazFu3J4W1HZP+pLWlLjEXaN88ePhHWqNZyvwGMkLpYl3ghcrE9H4vGZQ7jenRW4UmskLEkuhUPJbQiow3Td8arJiRmLVISzWqneqNraLUpGyUVr4F3Rbjzacfoo3r9ZZynhY0mFsEye82x6TMGgH2xsNGkd91zpQuckWUT+pQv/G6FXpnEnjIJSO2Z5WAxXrx6xB1k2HZ17/4NWLF3fJVhdQJm3mS6odeGzUjgGrl1A42evxU+7VbcofEJq1aMiLgU1jUT2pt+pefCwmKJYLpEsSzuyrVxgvskQz0QpC053TAYSNf2Jj6Qhg9YDWyOeemYmDgffTqErF7AYhc6NKH0s0XKkIiNFSxorkEsfG/Ck1o+15slHNmWZXlmXToxDqFkLDoPvfGKg7koU5YTGvci/F9ZKb1juhGLxZbwap/18zN40BqA+Ip2yDBJAKxsIiwSjSIguy6g/Z1I50s0xNGOr36urfRRQX5H+rqr/xCZ63B6WSe6qBcZboWAQMDn8HLS9Xiwc= + - secure: dnb7r5guUeMOX9e7XlPUSZzmga8VW3G9Q1aa7LxEKiTjSnWhu5KpPDe8o1X3Rj6nc5iXDqmBH/C/7eNXPDyXJJWPvpE2YRpGymyUkRaakul0QBKJEaMvwy2SuAfS69CWC+TSzfGRvtSYkdpBhhLvs0h5S819S5jYbCNSCmOKfFucaP5NsHNIZ/I19oIeTPTa0/UnVm7DLFZXZjvbS+czkdyH1DhbT85sLj+XqNTzLePImE68efrjaHnlSy/CzBVJzj55UgD5i9fxNCQWzGWim/SD5xZ0zKtLycSOf6wQN2lCo0lkjw9rDlYz69mM5L9ikfYL9oHDPZnh84oXKglQ5miOHCgqs/qs4439I05lIu8i/EfbFA55YG4NyO3rL9YVOOt5gwiwvJYhDcnkVVzSl0o5bsoZgQfYvPWaIQKNkl3C53zfDQjgqS54CeDzlZpFrQTDQ1RrH8oeVC1gfYAeMabMDadox5rfZmLIN5JTf/F8iD/QdxGcoUvkEENcQgfP9PnubExtexgHGsEmqbm6ORSZ1MkEh2m3fo0f8KE6TbN1UigmcQ8nTkWBHsSmfHnB8HwJQp8mwQmDamXA+Hl3e3w4LOdYkJVlNW1/TTyJJOOvjMQCjF8SJmPHuh+QpqKbSaT9XM/vBhxbIZEufH8kawJKCBBcCNspGMNjhXfNjM0= diff --git a/CMakeLists.txt b/CMakeLists.txt index 021fd43cf0..9bf4bdd709 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -498,7 +498,28 @@ set(ZIG_STD_FILES "math/tan.zig" "math/tanh.zig" "math/trunc.zig" - "math/x86_64/sqrt.zig" + "math/complex/abs.zig" + "math/complex/acosh.zig" + "math/complex/acos.zig" + "math/complex/arg.zig" + "math/complex/asinh.zig" + "math/complex/asin.zig" + "math/complex/atanh.zig" + "math/complex/atan.zig" + "math/complex/conj.zig" + "math/complex/cosh.zig" + "math/complex/cos.zig" + "math/complex/exp.zig" + "math/complex/index.zig" + "math/complex/ldexp.zig" + "math/complex/log.zig" + "math/complex/pow.zig" + "math/complex/proj.zig" + "math/complex/sinh.zig" + "math/complex/sin.zig" + "math/complex/sqrt.zig" + "math/complex/tanh.zig" + "math/complex/tan.zig" "mem.zig" "net.zig" "os/child_process.zig" @@ -509,13 +530,17 @@ set(ZIG_STD_FILES "os/index.zig" "os/linux/errno.zig" "os/linux/index.zig" + "os/linux/vdso.zig" "os/linux/x86_64.zig" "os/path.zig" + "os/time.zig" + "os/epoch.zig" "os/windows/error.zig" "os/windows/index.zig" "os/windows/util.zig" "os/zen.zig" "rand/index.zig" + "rand/ziggurat.zig" "sort.zig" "special/bootstrap.zig" "special/bootstrap_lib.zig" diff --git a/ci/travis_linux_script b/ci/travis_linux_script index 8c090b237c..1c449bc8a8 100755 --- a/ci/travis_linux_script +++ b/ci/travis_linux_script @@ -19,5 +19,5 @@ if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then echo "secret_key = $AWS_SECRET_ACCESS_KEY" >> ~/.s3cfg s3cmd put -P $TRAVIS_BUILD_DIR/artifacts/* s3://ziglang.org/builds/ touch empty - s3cmd put -P empty s3://ziglang.org/builds/zig-linux-x86_64-$TRAVIS_BRANCH.tar.xz --add-header=x-amz-website-redirect-location:/builds/$(ls $TRAVIS_BUILD_DIR/artifacts) + s3cmd put -P empty s3://ziglang.org/builds/zig-linux-x86_64-$TRAVIS_BRANCH.tar.xz --add-header="Cache-Control: max-age=0, must-revalidate" --add-header=x-amz-website-redirect-location:/builds/$(ls $TRAVIS_BUILD_DIR/artifacts) fi diff --git a/deps/lld/ELF/MarkLive.cpp b/deps/lld/ELF/MarkLive.cpp index 88f558c7a3..9fca927437 100644 --- a/deps/lld/ELF/MarkLive.cpp +++ b/deps/lld/ELF/MarkLive.cpp @@ -301,6 +301,15 @@ template void elf::markLive() { // Follow the graph to mark all live sections. doGcSections(); + // If all references to a DSO happen to be weak, the DSO is removed from + // DT_NEEDED, which creates dangling shared symbols to non-existent DSO. + // We'll replace such symbols with undefined ones to fix it. + for (Symbol *Sym : Symtab->getSymbols()) + if (auto *S = dyn_cast(Sym)) + if (S->isWeak() && !S->getFile().IsNeeded) + replaceSymbol(S, nullptr, S->getName(), STB_WEAK, S->StOther, + S->Type); + // Report garbage-collected sections. if (Config->PrintGcSections) for (InputSectionBase *Sec : InputSections) diff --git a/doc/docgen.zig b/doc/docgen.zig index 56d9a04412..bd9dc6c147 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -749,6 +749,10 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var try build_args.append("--release-fast"); try out.print(" --release-fast"); }, + builtin.Mode.ReleaseSmall => { + try build_args.append("--release-small"); + try out.print(" --release-small"); + }, } for (code.link_objects) |link_object| { const name_with_ext = try std.fmt.allocPrint(allocator, "{}{}", link_object, obj_ext); @@ -810,6 +814,10 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var try test_args.append("--release-fast"); try out.print(" --release-fast"); }, + builtin.Mode.ReleaseSmall => { + try test_args.append("--release-small"); + try out.print(" --release-small"); + }, } if (code.target_windows) { try test_args.appendSlice([][]const u8{ @@ -840,6 +848,10 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var try test_args.append("--release-fast"); try out.print(" --release-fast"); }, + builtin.Mode.ReleaseSmall => { + try test_args.append("--release-small"); + try out.print(" --release-small"); + }, } const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, null, max_doc_file_size); switch (result.term) { @@ -874,6 +886,7 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var builtin.Mode.Debug => {}, builtin.Mode.ReleaseSafe => try test_args.append("--release-safe"), builtin.Mode.ReleaseFast => try test_args.append("--release-fast"), + builtin.Mode.ReleaseSmall => try test_args.append("--release-small"), } const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, null, max_doc_file_size); @@ -927,6 +940,12 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var try out.print(" --release-fast"); } }, + builtin.Mode.ReleaseSmall => { + try build_args.append("--release-small"); + if (!code.is_inline) { + try out.print(" --release-small"); + } + }, } if (maybe_error_match) |error_match| { diff --git a/doc/langref.html.in b/doc/langref.html.in index 856d62f142..16fafdaad9 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -3880,6 +3880,25 @@ pub fn main() void { {#header_open|@ArgType#}

TODO

{#header_close#} + {#header_open|@atomicLoad#} +
@atomicLoad(comptime T: type, ptr: &const T, comptime ordering: builtin.AtomicOrder) -> T
+

+ This builtin function atomically dereferences a pointer and returns the value. +

+

+ T must be a pointer type, a bool, + or an integer whose bit count meets these requirements: +

+
    +
  • At least 8
  • +
  • At most the same as usize
  • +
  • Power of 2
  • +
+

+ TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe + we can remove this restriction +

+ {#header_close#} {#header_open|@atomicRmw#}
@atomicRmw(comptime T: type, ptr: &T, comptime op: builtin.AtomicRmwOp, operand: T, comptime ordering: builtin.AtomicOrder) -> T

@@ -4046,16 +4065,60 @@ comptime {

{#header_close#} - {#header_open|@cmpxchg#} -
@cmpxchg(ptr: &T, cmp: T, new: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> bool
+ {#header_open|@cmpxchgStrong#} +
@cmpxchgStrong(comptime T: type, ptr: &T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> ?T

- This function performs an atomic compare exchange operation. + This function performs a strong atomic compare exchange operation. It's the equivalent of this code, + except atomic: +

+ {#code_begin|syntax#} +fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T { + const old_value = *ptr; + if (old_value == expected_value) { + *ptr = new_value; + return null; + } else { + return old_value; + } +} + {#code_end#} +

+ If you are using cmpxchg in a loop, {#link|@cmpxchgWeak#} is the better choice, because it can be implemented + more efficiently in machine instructions.

AtomicOrder can be found with @import("builtin").AtomicOrder.

@typeOf(ptr).alignment must be >= @sizeOf(T).

- {#see_also|Compile Variables#} + {#see_also|Compile Variables|cmpxchgWeak#} + {#header_close#} + {#header_open|@cmpxchgWeak#} +
@cmpxchgWeak(comptime T: type, ptr: &T, expected_value: T, new_value: T, success_order: AtomicOrder, fail_order: AtomicOrder) -> ?T
+

+ This function performs a weak atomic compare exchange operation. It's the equivalent of this code, + except atomic: +

+ {#code_begin|syntax#} +fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: &T, expected_value: T, new_value: T) ?T { + const old_value = *ptr; + if (old_value == expected_value and usuallyTrueButSometimesFalse()) { + *ptr = new_value; + return null; + } else { + return old_value; + } +} + {#code_end#} +

+ If you are using cmpxchg in a loop, the sporadic failure will be no problem, and cmpxchgWeak + is the better choice, because it can be implemented more efficiently in machine instructions. + However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}. +

+

+ AtomicOrder can be found with @import("builtin").AtomicOrder. +

+

@typeOf(ptr).alignment must be >= @sizeOf(T).

+ {#see_also|Compile Variables|cmpxchgStrong#} {#header_close#} {#header_open|@compileError#}
@compileError(comptime msg: []u8)
@@ -4349,6 +4412,10 @@ fn add(a: i32, b: i32) i32 { return a + b; } It does not include functions, variables, or constants.

{#header_close#} + {#header_open|@field#} +
@field(lhs: var, comptime field_name: []const u8) -> (field)
+

Preforms field access equivalent to lhs.->field_name-<.

+ {#header_close#} {#header_open|@memberType#}
@memberType(comptime T: type, comptime index: usize) -> type

Returns the field type of a struct or union.

@@ -4669,6 +4736,16 @@ pub const FloatMode = enum { The result is a target-specific compile time constant.

{#header_close#} + {#header_open|@sqrt#} +
@sqrt(comptime T: type, value: T) -> T
+

+ Performs the square root of a floating point number. Uses a dedicated hardware instruction + when available. Currently only supports f32 and f64 at runtime. f128 at runtime is TODO. +

+

+ This is a low-level intrinsic. Most code can use std.math.sqrt instead. +

+ {#header_close#} {#header_open|@subWithOverflow#}
@subWithOverflow(comptime T: type, a: T, b: T, result: &T) -> bool

@@ -5845,7 +5922,7 @@ Defer(body) = ("defer" | "deferror") body IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body)) -SuspendExpression(body) = "suspend" option(("|" Symbol "|" body)) +SuspendExpression(body) = option(Symbol ":") "suspend" option(("|" Symbol "|" body)) IfErrorExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body) @@ -5991,7 +6068,7 @@ hljs.registerLanguage("zig", function(t) { a = t.IR + "\\s*\\(", c = { keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong", - built_in: "breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchg fence divExact truncate atomicRmw", + built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic canImplicitCast ptrCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field", literal: "true false null undefined" }, n = [e, t.CLCM, t.CBCM, s, r]; diff --git a/src-self-hosted/arg.zig b/src-self-hosted/arg.zig new file mode 100644 index 0000000000..707f208287 --- /dev/null +++ b/src-self-hosted/arg.zig @@ -0,0 +1,284 @@ +const std = @import("std"); +const debug = std.debug; +const mem = std.mem; + +const Allocator = mem.Allocator; +const ArrayList = std.ArrayList; +const HashMap = std.HashMap; + +fn trimStart(slice: []const u8, ch: u8) []const u8 { + var i: usize = 0; + for (slice) |b| { + if (b != '-') break; + i += 1; + } + + return slice[i..]; +} + +fn argInAllowedSet(maybe_set: ?[]const []const u8, arg: []const u8) bool { + if (maybe_set) |set| { + for (set) |possible| { + if (mem.eql(u8, arg, possible)) { + return true; + } + } + return false; + } else { + return true; + } +} + +// Modifies the current argument index during iteration +fn readFlagArguments(allocator: &Allocator, args: []const []const u8, required: usize, + allowed_set: ?[]const []const u8, index: &usize) !FlagArg { + + switch (required) { + 0 => return FlagArg { .None = undefined }, // TODO: Required to force non-tag but value? + 1 => { + if (*index + 1 >= args.len) { + return error.MissingFlagArguments; + } + + *index += 1; + const arg = args[*index]; + + if (!argInAllowedSet(allowed_set, arg)) { + return error.ArgumentNotInAllowedSet; + } + + return FlagArg { .Single = arg }; + }, + else => |needed| { + var extra = ArrayList([]const u8).init(allocator); + errdefer extra.deinit(); + + var j: usize = 0; + while (j < needed) : (j += 1) { + if (*index + 1 >= args.len) { + return error.MissingFlagArguments; + } + + *index += 1; + const arg = args[*index]; + + if (!argInAllowedSet(allowed_set, arg)) { + return error.ArgumentNotInAllowedSet; + } + + try extra.append(arg); + } + + return FlagArg { .Many = extra }; + }, + } +} + +const HashMapFlags = HashMap([]const u8, FlagArg, std.hash.Fnv1a_32.hash, mem.eql_slice_u8); + +// A store for querying found flags and positional arguments. +pub const Args = struct { + flags: HashMapFlags, + positionals: ArrayList([]const u8), + + pub fn parse(allocator: &Allocator, comptime spec: []const Flag, args: []const []const u8) !Args { + var parsed = Args { + .flags = HashMapFlags.init(allocator), + .positionals = ArrayList([]const u8).init(allocator), + }; + + var i: usize = 0; + next: while (i < args.len) : (i += 1) { + const arg = args[i]; + + if (arg.len != 0 and arg[0] == '-') { + // TODO: hashmap, although the linear scan is okay for small argument sets as is + for (spec) |flag| { + if (mem.eql(u8, arg, flag.name)) { + const flag_name_trimmed = trimStart(flag.name, '-'); + const flag_args = readFlagArguments(allocator, args, flag.required, flag.allowed_set, &i) catch |err| { + switch (err) { + error.ArgumentNotInAllowedSet => { + std.debug.warn("argument '{}' is invalid for flag '{}'\n", args[i], arg); + std.debug.warn("allowed options are "); + for (??flag.allowed_set) |possible| { + std.debug.warn("'{}' ", possible); + } + std.debug.warn("\n"); + }, + error.MissingFlagArguments => { + std.debug.warn("missing argument for flag: {}\n", arg); + }, + else => {}, + } + + return err; + }; + + if (flag.mergable) { + var prev = + if (parsed.flags.get(flag_name_trimmed)) |entry| + entry.value.Many + else + ArrayList([]const u8).init(allocator); + + // MergeN creation disallows 0 length flag entry (doesn't make sense) + switch (flag_args) { + FlagArg.None => unreachable, + FlagArg.Single => |inner| try prev.append(inner), + FlagArg.Many => |inner| try prev.appendSlice(inner.toSliceConst()), + } + + _ = try parsed.flags.put(flag_name_trimmed, FlagArg { .Many = prev }); + } else { + _ = try parsed.flags.put(flag_name_trimmed, flag_args); + } + + continue :next; + } + } + + // TODO: Better errors with context, global error state and return is sufficient. + std.debug.warn("could not match flag: {}\n", arg); + return error.UnknownFlag; + } else { + try parsed.positionals.append(arg); + } + } + + return parsed; + } + + pub fn deinit(self: &Args) void { + self.flags.deinit(); + self.positionals.deinit(); + } + + // e.g. --help + pub fn present(self: &Args, name: []const u8) bool { + return self.flags.contains(name); + } + + // e.g. --name value + pub fn single(self: &Args, name: []const u8) ?[]const u8 { + if (self.flags.get(name)) |entry| { + switch (entry.value) { + FlagArg.Single => |inner| { return inner; }, + else => @panic("attempted to retrieve flag with wrong type"), + } + } else { + return null; + } + } + + // e.g. --names value1 value2 value3 + pub fn many(self: &Args, name: []const u8) ?[]const []const u8 { + if (self.flags.get(name)) |entry| { + switch (entry.value) { + FlagArg.Many => |inner| { return inner.toSliceConst(); }, + else => @panic("attempted to retrieve flag with wrong type"), + } + } else { + return null; + } + } +}; + +// Arguments for a flag. e.g. arg1, arg2 in `--command arg1 arg2`. +const FlagArg = union(enum) { + None, + Single: []const u8, + Many: ArrayList([]const u8), +}; + +// Specification for how a flag should be parsed. +pub const Flag = struct { + name: []const u8, + required: usize, + mergable: bool, + allowed_set: ?[]const []const u8, + + pub fn Bool(comptime name: []const u8) Flag { + return ArgN(name, 0); + } + + pub fn Arg1(comptime name: []const u8) Flag { + return ArgN(name, 1); + } + + pub fn ArgN(comptime name: []const u8, comptime n: usize) Flag { + return Flag { + .name = name, + .required = n, + .mergable = false, + .allowed_set = null, + }; + } + + pub fn ArgMergeN(comptime name: []const u8, comptime n: usize) Flag { + if (n == 0) { + @compileError("n must be greater than 0"); + } + + return Flag { + .name = name, + .required = n, + .mergable = true, + .allowed_set = null, + }; + } + + pub fn Option(comptime name: []const u8, comptime set: []const []const u8) Flag { + return Flag { + .name = name, + .required = 1, + .mergable = false, + .allowed_set = set, + }; + } +}; + +test "parse arguments" { + const spec1 = comptime []const Flag { + Flag.Bool("--help"), + Flag.Bool("--init"), + Flag.Arg1("--build-file"), + Flag.Option("--color", []const []const u8 { "on", "off", "auto" }), + Flag.ArgN("--pkg-begin", 2), + Flag.ArgMergeN("--object", 1), + Flag.ArgN("--library", 1), + }; + + const cliargs = []const []const u8 { + "build", + "--help", + "pos1", + "--build-file", "build.zig", + "--object", "obj1", + "--object", "obj2", + "--library", "lib1", + "--library", "lib2", + "--color", "on", + "pos2", + }; + + var args = try Args.parse(std.debug.global_allocator, spec1, cliargs); + + debug.assert(args.present("help")); + debug.assert(!args.present("help2")); + debug.assert(!args.present("init")); + + debug.assert(mem.eql(u8, ??args.single("build-file"), "build.zig")); + debug.assert(mem.eql(u8, ??args.single("color"), "on")); + + const objects = ??args.many("object"); + debug.assert(mem.eql(u8, objects[0], "obj1")); + debug.assert(mem.eql(u8, objects[1], "obj2")); + + debug.assert(mem.eql(u8, ??args.single("library"), "lib2")); + + const pos = args.positionals.toSliceConst(); + debug.assert(mem.eql(u8, pos[0], "build")); + debug.assert(mem.eql(u8, pos[1], "pos1")); + debug.assert(mem.eql(u8, pos[2], "pos2")); +} diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig new file mode 100644 index 0000000000..3f1fefdd5a --- /dev/null +++ b/src-self-hosted/introspect.zig @@ -0,0 +1,57 @@ +// Introspection and determination of system libraries needed by zig. + +const std = @import("std"); +const mem = std.mem; +const os = std.os; + +const warn = std.debug.warn; + +/// Caller must free result +pub fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) ![]u8 { + const test_zig_dir = try os.path.join(allocator, test_path, "lib", "zig"); + errdefer allocator.free(test_zig_dir); + + const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig"); + defer allocator.free(test_index_file); + + var file = try os.File.openRead(allocator, test_index_file); + file.close(); + + return test_zig_dir; +} + +/// Caller must free result +pub fn findZigLibDir(allocator: &mem.Allocator) ![]u8 { + const self_exe_path = try os.selfExeDirPath(allocator); + defer allocator.free(self_exe_path); + + var cur_path: []const u8 = self_exe_path; + while (true) { + const test_dir = os.path.dirname(cur_path); + + if (mem.eql(u8, test_dir, cur_path)) { + break; + } + + return testZigInstallPrefix(allocator, test_dir) catch |err| { + cur_path = test_dir; + continue; + }; + } + + return error.FileNotFound; +} + +pub fn resolveZigLibDir(allocator: &mem.Allocator) ![]u8 { + return findZigLibDir(allocator) catch |err| { + warn( + \\Unable to find zig lib directory: {}. + \\Reinstall Zig or use --zig-install-prefix. + \\ + , + @errorName(err) + ); + + return error.ZigLibDirNotFound; + }; +} diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index d125b05b24..c1a6bbe99a 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -1,613 +1,165 @@ const std = @import("std"); -const mem = std.mem; -const io = std.io; -const os = std.os; -const heap = std.heap; -const warn = std.debug.warn; -const assert = std.debug.assert; -const target = @import("target.zig"); -const Target = target.Target; -const Module = @import("module.zig").Module; -const ErrColor = Module.ErrColor; -const Emit = Module.Emit; const builtin = @import("builtin"); + +const os = std.os; +const io = std.io; +const mem = std.mem; +const Allocator = mem.Allocator; const ArrayList = std.ArrayList; +const Buffer = std.Buffer; + +const arg = @import("arg.zig"); const c = @import("c.zig"); +const introspect = @import("introspect.zig"); +const Args = arg.Args; +const Flag = arg.Flag; +const Module = @import("module.zig").Module; +const Target = @import("target.zig").Target; -const default_zig_cache_name = "zig-cache"; +var stderr: &io.OutStream(io.FileOutStream.Error) = undefined; +var stdout: &io.OutStream(io.FileOutStream.Error) = undefined; -const Cmd = enum { - None, - Build, - Test, - Version, - Zen, - TranslateC, - Targets, +const usage = + \\usage: zig [command] [options] + \\ + \\Commands: + \\ + \\ build Build project from build.zig + \\ build-exe [source] Create executable from source or object files + \\ build-lib [source] Create library from source or object files + \\ build-obj [source] Create object from source or assembly + \\ fmt [source] Parse file and render in canonical zig format + \\ run [source] Create executable and run immediately + \\ targets List available compilation targets + \\ test [source] Create and run a test build + \\ translate-c [source] Convert c code to zig code + \\ version Print version number and exit + \\ zen Print zen of zig and exit + \\ + \\ + ; + +const Command = struct { + name: []const u8, + exec: fn(&Allocator, []const []const u8) error!void, }; -fn badArgs(comptime format: []const u8, args: ...) noreturn { - var stderr = io.getStdErr() catch std.os.exit(1); - var stderr_stream_adapter = io.FileOutStream.init(&stderr); - const stderr_stream = &stderr_stream_adapter.stream; - stderr_stream.print(format ++ "\n\n", args) catch std.os.exit(1); - printUsage(&stderr_stream_adapter.stream) catch std.os.exit(1); - std.os.exit(1); -} - pub fn main() !void { - const allocator = std.heap.c_allocator; + var allocator = std.heap.c_allocator; + + var stdout_file = try std.io.getStdOut(); + var stdout_out_stream = std.io.FileOutStream.init(&stdout_file); + stdout = &stdout_out_stream.stream; + + var stderr_file = try std.io.getStdErr(); + var stderr_out_stream = std.io.FileOutStream.init(&stderr_file); + stderr = &stderr_out_stream.stream; const args = try os.argsAlloc(allocator); defer os.argsFree(allocator, args); - if (args.len >= 2 and mem.eql(u8, args[1], "build")) { - return buildMain(allocator, args[2..]); + if (args.len <= 1) { + try stderr.write(usage); + os.exit(1); } - if (args.len >= 2 and mem.eql(u8, args[1], "fmt")) { - return fmtMain(allocator, args[2..]); - } + const commands = []Command { + Command { .name = "build", .exec = cmdBuild }, + Command { .name = "build-exe", .exec = cmdBuildExe }, + Command { .name = "build-lib", .exec = cmdBuildLib }, + Command { .name = "build-obj", .exec = cmdBuildObj }, + Command { .name = "fmt", .exec = cmdFmt }, + Command { .name = "run", .exec = cmdRun }, + Command { .name = "targets", .exec = cmdTargets }, + Command { .name = "test", .exec = cmdTest }, + Command { .name = "translate-c", .exec = cmdTranslateC }, + Command { .name = "version", .exec = cmdVersion }, + Command { .name = "zen", .exec = cmdZen }, - var cmd = Cmd.None; - var build_kind: Module.Kind = undefined; - var build_mode: builtin.Mode = builtin.Mode.Debug; - var color = ErrColor.Auto; - var emit_file_type = Emit.Binary; + // undocumented commands + Command { .name = "help", .exec = cmdHelp }, + Command { .name = "internal", .exec = cmdInternal }, + }; - var strip = false; - var is_static = false; - var verbose_tokenize = false; - var verbose_ast_tree = false; - var verbose_ast_fmt = false; - var verbose_link = false; - var verbose_ir = false; - var verbose_llvm_ir = false; - var verbose_cimport = false; - var mwindows = false; - var mconsole = false; - var rdynamic = false; - var each_lib_rpath = false; - var timing_info = false; - - var in_file_arg: ?[]u8 = null; - var out_file: ?[]u8 = null; - var out_file_h: ?[]u8 = null; - var out_name_arg: ?[]u8 = null; - var libc_lib_dir_arg: ?[]u8 = null; - var libc_static_lib_dir_arg: ?[]u8 = null; - var libc_include_dir_arg: ?[]u8 = null; - var msvc_lib_dir_arg: ?[]u8 = null; - var kernel32_lib_dir_arg: ?[]u8 = null; - var zig_install_prefix: ?[]u8 = null; - var dynamic_linker_arg: ?[]u8 = null; - var cache_dir_arg: ?[]const u8 = null; - var target_arch: ?[]u8 = null; - var target_os: ?[]u8 = null; - var target_environ: ?[]u8 = null; - var mmacosx_version_min: ?[]u8 = null; - var mios_version_min: ?[]u8 = null; - var linker_script_arg: ?[]u8 = null; - var test_name_prefix_arg: ?[]u8 = null; - - var test_filters = ArrayList([]const u8).init(allocator); - defer test_filters.deinit(); - - var lib_dirs = ArrayList([]const u8).init(allocator); - defer lib_dirs.deinit(); - - var clang_argv = ArrayList([]const u8).init(allocator); - defer clang_argv.deinit(); - - var llvm_argv = ArrayList([]const u8).init(allocator); - defer llvm_argv.deinit(); - - var link_libs = ArrayList([]const u8).init(allocator); - defer link_libs.deinit(); - - var frameworks = ArrayList([]const u8).init(allocator); - defer frameworks.deinit(); - - var objects = ArrayList([]const u8).init(allocator); - defer objects.deinit(); - - var asm_files = ArrayList([]const u8).init(allocator); - defer asm_files.deinit(); - - var rpath_list = ArrayList([]const u8).init(allocator); - defer rpath_list.deinit(); - - var ver_major: u32 = 0; - var ver_minor: u32 = 0; - var ver_patch: u32 = 0; - - var arg_i: usize = 1; - while (arg_i < args.len) : (arg_i += 1) { - const arg = args[arg_i]; - - if (arg.len != 0 and arg[0] == '-') { - if (mem.eql(u8, arg, "--release-fast")) { - build_mode = builtin.Mode.ReleaseFast; - } else if (mem.eql(u8, arg, "--release-safe")) { - build_mode = builtin.Mode.ReleaseSafe; - } else if (mem.eql(u8, arg, "--strip")) { - strip = true; - } else if (mem.eql(u8, arg, "--static")) { - is_static = true; - } else if (mem.eql(u8, arg, "--verbose-tokenize")) { - verbose_tokenize = true; - } else if (mem.eql(u8, arg, "--verbose-ast-tree")) { - verbose_ast_tree = true; - } else if (mem.eql(u8, arg, "--verbose-ast-fmt")) { - verbose_ast_fmt = true; - } else if (mem.eql(u8, arg, "--verbose-link")) { - verbose_link = true; - } else if (mem.eql(u8, arg, "--verbose-ir")) { - verbose_ir = true; - } else if (mem.eql(u8, arg, "--verbose-llvm-ir")) { - verbose_llvm_ir = true; - } else if (mem.eql(u8, arg, "--verbose-cimport")) { - verbose_cimport = true; - } else if (mem.eql(u8, arg, "-mwindows")) { - mwindows = true; - } else if (mem.eql(u8, arg, "-mconsole")) { - mconsole = true; - } else if (mem.eql(u8, arg, "-rdynamic")) { - rdynamic = true; - } else if (mem.eql(u8, arg, "--each-lib-rpath")) { - each_lib_rpath = true; - } else if (mem.eql(u8, arg, "--enable-timing-info")) { - timing_info = true; - } else if (mem.eql(u8, arg, "--test-cmd-bin")) { - @panic("TODO --test-cmd-bin"); - } else if (arg[1] == 'L' and arg.len > 2) { - // alias for --library-path - try lib_dirs.append(arg[1..]); - } else if (mem.eql(u8, arg, "--pkg-begin")) { - @panic("TODO --pkg-begin"); - } else if (mem.eql(u8, arg, "--pkg-end")) { - @panic("TODO --pkg-end"); - } else if (arg_i + 1 >= args.len) { - badArgs("expected another argument after {}", arg); - } else { - arg_i += 1; - if (mem.eql(u8, arg, "--output")) { - out_file = args[arg_i]; - } else if (mem.eql(u8, arg, "--output-h")) { - out_file_h = args[arg_i]; - } else if (mem.eql(u8, arg, "--color")) { - if (mem.eql(u8, args[arg_i], "auto")) { - color = ErrColor.Auto; - } else if (mem.eql(u8, args[arg_i], "on")) { - color = ErrColor.On; - } else if (mem.eql(u8, args[arg_i], "off")) { - color = ErrColor.Off; - } else { - badArgs("--color options are 'auto', 'on', or 'off'"); - } - } else if (mem.eql(u8, arg, "--emit")) { - if (mem.eql(u8, args[arg_i], "asm")) { - emit_file_type = Emit.Assembly; - } else if (mem.eql(u8, args[arg_i], "bin")) { - emit_file_type = Emit.Binary; - } else if (mem.eql(u8, args[arg_i], "llvm-ir")) { - emit_file_type = Emit.LlvmIr; - } else { - badArgs("--emit options are 'asm', 'bin', or 'llvm-ir'"); - } - } else if (mem.eql(u8, arg, "--name")) { - out_name_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--libc-lib-dir")) { - libc_lib_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--libc-static-lib-dir")) { - libc_static_lib_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--libc-include-dir")) { - libc_include_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--msvc-lib-dir")) { - msvc_lib_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--kernel32-lib-dir")) { - kernel32_lib_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--zig-install-prefix")) { - zig_install_prefix = args[arg_i]; - } else if (mem.eql(u8, arg, "--dynamic-linker")) { - dynamic_linker_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "-isystem")) { - try clang_argv.append("-isystem"); - try clang_argv.append(args[arg_i]); - } else if (mem.eql(u8, arg, "-dirafter")) { - try clang_argv.append("-dirafter"); - try clang_argv.append(args[arg_i]); - } else if (mem.eql(u8, arg, "-mllvm")) { - try clang_argv.append("-mllvm"); - try clang_argv.append(args[arg_i]); - - try llvm_argv.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--library-path") or mem.eql(u8, arg, "-L")) { - try lib_dirs.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--library")) { - try link_libs.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--object")) { - try objects.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--assembly")) { - try asm_files.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--cache-dir")) { - cache_dir_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--target-arch")) { - target_arch = args[arg_i]; - } else if (mem.eql(u8, arg, "--target-os")) { - target_os = args[arg_i]; - } else if (mem.eql(u8, arg, "--target-environ")) { - target_environ = args[arg_i]; - } else if (mem.eql(u8, arg, "-mmacosx-version-min")) { - mmacosx_version_min = args[arg_i]; - } else if (mem.eql(u8, arg, "-mios-version-min")) { - mios_version_min = args[arg_i]; - } else if (mem.eql(u8, arg, "-framework")) { - try frameworks.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--linker-script")) { - linker_script_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "-rpath")) { - try rpath_list.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--test-filter")) { - try test_filters.append(args[arg_i]); - } else if (mem.eql(u8, arg, "--test-name-prefix")) { - test_name_prefix_arg = args[arg_i]; - } else if (mem.eql(u8, arg, "--ver-major")) { - ver_major = try std.fmt.parseUnsigned(u32, args[arg_i], 10); - } else if (mem.eql(u8, arg, "--ver-minor")) { - ver_minor = try std.fmt.parseUnsigned(u32, args[arg_i], 10); - } else if (mem.eql(u8, arg, "--ver-patch")) { - ver_patch = try std.fmt.parseUnsigned(u32, args[arg_i], 10); - } else if (mem.eql(u8, arg, "--test-cmd")) { - @panic("TODO --test-cmd"); - } else { - badArgs("invalid argument: {}", arg); - } - } - } else if (cmd == Cmd.None) { - if (mem.eql(u8, arg, "build-obj")) { - cmd = Cmd.Build; - build_kind = Module.Kind.Obj; - } else if (mem.eql(u8, arg, "build-exe")) { - cmd = Cmd.Build; - build_kind = Module.Kind.Exe; - } else if (mem.eql(u8, arg, "build-lib")) { - cmd = Cmd.Build; - build_kind = Module.Kind.Lib; - } else if (mem.eql(u8, arg, "version")) { - cmd = Cmd.Version; - } else if (mem.eql(u8, arg, "zen")) { - cmd = Cmd.Zen; - } else if (mem.eql(u8, arg, "translate-c")) { - cmd = Cmd.TranslateC; - } else if (mem.eql(u8, arg, "test")) { - cmd = Cmd.Test; - build_kind = Module.Kind.Exe; - } else { - badArgs("unrecognized command: {}", arg); - } - } else switch (cmd) { - Cmd.Build, Cmd.TranslateC, Cmd.Test => { - if (in_file_arg == null) { - in_file_arg = arg; - } else { - badArgs("unexpected extra parameter: {}", arg); - } - }, - Cmd.Version, Cmd.Zen, Cmd.Targets => { - badArgs("unexpected extra parameter: {}", arg); - }, - Cmd.None => unreachable, + for (commands) |command| { + if (mem.eql(u8, command.name, args[1])) { + try command.exec(allocator, args[2..]); + return; } } - target.initializeAll(); - - // TODO -// ZigTarget alloc_target; -// ZigTarget *target; -// if (!target_arch && !target_os && !target_environ) { -// target = nullptr; -// } else { -// target = &alloc_target; -// get_unknown_target(target); -// if (target_arch) { -// if (parse_target_arch(target_arch, &target->arch)) { -// fprintf(stderr, "invalid --target-arch argument\n"); -// return usage(arg0); -// } -// } -// if (target_os) { -// if (parse_target_os(target_os, &target->os)) { -// fprintf(stderr, "invalid --target-os argument\n"); -// return usage(arg0); -// } -// } -// if (target_environ) { -// if (parse_target_environ(target_environ, &target->env_type)) { -// fprintf(stderr, "invalid --target-environ argument\n"); -// return usage(arg0); -// } -// } -// } - - switch (cmd) { - Cmd.None => badArgs("expected command"), - Cmd.Zen => return printZen(), - Cmd.Build, Cmd.Test, Cmd.TranslateC => { - if (cmd == Cmd.Build and in_file_arg == null and objects.len == 0 and asm_files.len == 0) { - badArgs("expected source file argument or at least one --object or --assembly argument"); - } else if ((cmd == Cmd.TranslateC or cmd == Cmd.Test) and in_file_arg == null) { - badArgs("expected source file argument"); - } else if (cmd == Cmd.Build and build_kind == Module.Kind.Obj and objects.len != 0) { - badArgs("When building an object file, --object arguments are invalid"); - } - - const root_name = switch (cmd) { - Cmd.Build, Cmd.TranslateC => x: { - if (out_name_arg) |out_name| { - break :x out_name; - } else if (in_file_arg) |in_file_path| { - const basename = os.path.basename(in_file_path); - var it = mem.split(basename, "."); - break :x it.next() ?? badArgs("file name cannot be empty"); - } else { - badArgs("--name [name] not provided and unable to infer"); - } - }, - Cmd.Test => "test", - else => unreachable, - }; - - const zig_root_source_file = if (cmd == Cmd.TranslateC) null else in_file_arg; - - const chosen_cache_dir = cache_dir_arg ?? default_zig_cache_name; - const full_cache_dir = try os.path.resolve(allocator, ".", chosen_cache_dir); - defer allocator.free(full_cache_dir); - - const zig_lib_dir = try resolveZigLibDir(allocator, zig_install_prefix); - errdefer allocator.free(zig_lib_dir); - - const module = try Module.create(allocator, root_name, zig_root_source_file, - Target.Native, build_kind, build_mode, zig_lib_dir, full_cache_dir); - defer module.destroy(); - - module.version_major = ver_major; - module.version_minor = ver_minor; - module.version_patch = ver_patch; - - module.is_test = cmd == Cmd.Test; - if (linker_script_arg) |linker_script| { - module.linker_script = linker_script; - } - module.each_lib_rpath = each_lib_rpath; - module.clang_argv = clang_argv.toSliceConst(); - module.llvm_argv = llvm_argv.toSliceConst(); - module.strip = strip; - module.is_static = is_static; - - if (libc_lib_dir_arg) |libc_lib_dir| { - module.libc_lib_dir = libc_lib_dir; - } - if (libc_static_lib_dir_arg) |libc_static_lib_dir| { - module.libc_static_lib_dir = libc_static_lib_dir; - } - if (libc_include_dir_arg) |libc_include_dir| { - module.libc_include_dir = libc_include_dir; - } - if (msvc_lib_dir_arg) |msvc_lib_dir| { - module.msvc_lib_dir = msvc_lib_dir; - } - if (kernel32_lib_dir_arg) |kernel32_lib_dir| { - module.kernel32_lib_dir = kernel32_lib_dir; - } - if (dynamic_linker_arg) |dynamic_linker| { - module.dynamic_linker = dynamic_linker; - } - module.verbose_tokenize = verbose_tokenize; - module.verbose_ast_tree = verbose_ast_tree; - module.verbose_ast_fmt = verbose_ast_fmt; - module.verbose_link = verbose_link; - module.verbose_ir = verbose_ir; - module.verbose_llvm_ir = verbose_llvm_ir; - module.verbose_cimport = verbose_cimport; - - module.err_color = color; - - module.lib_dirs = lib_dirs.toSliceConst(); - module.darwin_frameworks = frameworks.toSliceConst(); - module.rpath_list = rpath_list.toSliceConst(); - - for (link_libs.toSliceConst()) |name| { - _ = try module.addLinkLib(name, true); - } - - module.windows_subsystem_windows = mwindows; - module.windows_subsystem_console = mconsole; - module.linker_rdynamic = rdynamic; - - if (mmacosx_version_min != null and mios_version_min != null) { - badArgs("-mmacosx-version-min and -mios-version-min options not allowed together"); - } - - if (mmacosx_version_min) |ver| { - module.darwin_version_min = Module.DarwinVersionMin { .MacOS = ver }; - } else if (mios_version_min) |ver| { - module.darwin_version_min = Module.DarwinVersionMin { .Ios = ver }; - } - - module.test_filters = test_filters.toSliceConst(); - module.test_name_prefix = test_name_prefix_arg; - module.out_h_path = out_file_h; - - // TODO - //add_package(g, cur_pkg, g->root_package); - - switch (cmd) { - Cmd.Build => { - module.emit_file_type = emit_file_type; - - module.link_objects = objects.toSliceConst(); - module.assembly_files = asm_files.toSliceConst(); - - try module.build(); - try module.link(out_file); - }, - Cmd.TranslateC => @panic("TODO translate-c"), - Cmd.Test => @panic("TODO test cmd"), - else => unreachable, - } - }, - Cmd.Version => { - var stdout_file = try io.getStdErr(); - try stdout_file.write(std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); - try stdout_file.write("\n"); - }, - Cmd.Targets => @panic("TODO zig targets"), - } + try stderr.print("unknown command: {}\n\n", args[1]); + try stderr.write(usage); } -fn printUsage(stream: var) !void { - try stream.write( - \\Usage: zig [command] [options] - \\ - \\Commands: - \\ build build project from build.zig - \\ build-exe [source] create executable from source or object files - \\ build-lib [source] create library from source or object files - \\ build-obj [source] create object from source or assembly - \\ fmt [file] parse file and render in canonical zig format - \\ translate-c [source] convert c code to zig code - \\ targets list available compilation targets - \\ test [source] create and run a test build - \\ version print version number and exit - \\ zen print zen of zig and exit - \\Compile Options: - \\ --assembly [source] add assembly file to build - \\ --cache-dir [path] override the cache directory - \\ --color [auto|off|on] enable or disable colored error messages - \\ --emit [filetype] emit a specific file format as compilation output - \\ --enable-timing-info print timing diagnostics - \\ --libc-include-dir [path] directory where libc stdlib.h resides - \\ --name [name] override output name - \\ --output [file] override destination path - \\ --output-h [file] override generated header file path - \\ --pkg-begin [name] [path] make package available to import and push current pkg - \\ --pkg-end pop current pkg - \\ --release-fast build with optimizations on and safety off - \\ --release-safe build with optimizations on and safety on - \\ --static output will be statically linked - \\ --strip exclude debug symbols - \\ --target-arch [name] specify target architecture - \\ --target-environ [name] specify target environment - \\ --target-os [name] specify target operating system - \\ --verbose-tokenize enable compiler debug info: tokenization - \\ --verbose-ast-tree enable compiler debug info: parsing into an AST (treeview) - \\ --verbose-ast-fmt enable compiler debug info: parsing into an AST (render source) - \\ --verbose-cimport enable compiler debug info: C imports - \\ --verbose-ir enable compiler debug info: Zig IR - \\ --verbose-llvm-ir enable compiler debug info: LLVM IR - \\ --verbose-link enable compiler debug info: linking - \\ --zig-install-prefix [path] override directory where zig thinks it is installed - \\ -dirafter [dir] same as -isystem but do it last - \\ -isystem [dir] add additional search path for other .h files - \\ -mllvm [arg] additional arguments to forward to LLVM's option processing - \\Link Options: - \\ --ar-path [path] set the path to ar - \\ --dynamic-linker [path] set the path to ld.so - \\ --each-lib-rpath add rpath for each used dynamic library - \\ --libc-lib-dir [path] directory where libc crt1.o resides - \\ --libc-static-lib-dir [path] directory where libc crtbegin.o resides - \\ --msvc-lib-dir [path] (windows) directory where vcruntime.lib resides - \\ --kernel32-lib-dir [path] (windows) directory where kernel32.lib resides - \\ --library [lib] link against lib - \\ --library-path [dir] add a directory to the library search path - \\ --linker-script [path] use a custom linker script - \\ --object [obj] add object file to build - \\ -L[dir] alias for --library-path - \\ -rdynamic add all symbols to the dynamic symbol table - \\ -rpath [path] add directory to the runtime library search path - \\ -mconsole (windows) --subsystem console to the linker - \\ -mwindows (windows) --subsystem windows to the linker - \\ -framework [name] (darwin) link against framework - \\ -mios-version-min [ver] (darwin) set iOS deployment target - \\ -mmacosx-version-min [ver] (darwin) set Mac OS X deployment target - \\ --ver-major [ver] dynamic library semver major version - \\ --ver-minor [ver] dynamic library semver minor version - \\ --ver-patch [ver] dynamic library semver patch version - \\Test Options: - \\ --test-filter [text] skip tests that do not match filter - \\ --test-name-prefix [text] add prefix to all tests - \\ --test-cmd [arg] specify test execution command one arg at a time - \\ --test-cmd-bin appends test binary path to test cmd args - \\ - ); -} +// cmd:build /////////////////////////////////////////////////////////////////////////////////////// -fn printZen() !void { - var stdout_file = try io.getStdErr(); - try stdout_file.write( - \\ - \\ * Communicate intent precisely. - \\ * Edge cases matter. - \\ * Favor reading code over writing code. - \\ * Only one obvious way to do things. - \\ * Runtime crashes are better than bugs. - \\ * Compile errors are better than runtime crashes. - \\ * Incremental improvements. - \\ * Avoid local maximums. - \\ * Reduce the amount one must remember. - \\ * Minimize energy spent on coding style. - \\ * Together we serve end users. - \\ - \\ - ); -} +const usage_build = + \\usage: zig build + \\ + \\General Options: + \\ --help Print this help and exit + \\ --init Generate a build.zig template + \\ --build-file [file] Override path to build.zig + \\ --cache-dir [path] Override path to cache directory + \\ --verbose Print commands before executing them + \\ --prefix [path] Override default install prefix + \\ + \\Project-Specific Options: + \\ + \\ Project-specific options become available when the build file is found. + \\ + \\Advanced Options: + \\ --build-file [file] Override path to build.zig + \\ --cache-dir [path] Override path to cache directory + \\ --verbose-tokenize Enable compiler debug output for tokenization + \\ --verbose-ast Enable compiler debug output for parsing into an AST + \\ --verbose-link Enable compiler debug output for linking + \\ --verbose-ir Enable compiler debug output for Zig IR + \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR + \\ --verbose-cimport Enable compiler debug output for C imports + \\ + \\ + ; -fn buildMain(allocator: &mem.Allocator, argv: []const []const u8) !void { - var build_file: [] const u8 = "build.zig"; - var cache_dir: ?[] const u8 = null; - var zig_install_prefix: ?[] const u8 = null; - var asked_for_help = false; - var asked_for_init = false; +const args_build_spec = []Flag { + Flag.Bool("--help"), + Flag.Bool("--init"), + Flag.Arg1("--build-file"), + Flag.Arg1("--cache-dir"), + Flag.Bool("--verbose"), + Flag.Arg1("--prefix"), - var args = ArrayList([] const u8).init(allocator); - defer args.deinit(); + Flag.Arg1("--build-file"), + Flag.Arg1("--cache-dir"), + Flag.Bool("--verbose-tokenize"), + Flag.Bool("--verbose-ast"), + Flag.Bool("--verbose-link"), + Flag.Bool("--verbose-ir"), + Flag.Bool("--verbose-llvm-ir"), + Flag.Bool("--verbose-cimport"), +}; - var zig_exe_path = try os.selfExePath(allocator); - defer allocator.free(zig_exe_path); +const missing_build_file = + \\No 'build.zig' file found. + \\ + \\Initialize a 'build.zig' template file with `zig build --init`, + \\or build an executable directly with `zig build-exe $FILENAME.zig`. + \\ + \\See: `zig build --help` or `zig help` for more options. + \\ + ; - try args.append(""); // Placeholder for zig-cache/build - try args.append(""); // Placeholder for zig_exe_path - try args.append(""); // Placeholder for build_file_dirname - try args.append(""); // Placeholder for full_cache_dir +fn cmdBuild(allocator: &Allocator, args: []const []const u8) !void { + var flags = try Args.parse(allocator, args_build_spec, args); + defer flags.deinit(); - var i: usize = 0; - while (i < argv.len) : (i += 1) { - var arg = argv[i]; - if (mem.eql(u8, arg, "--help")) { - asked_for_help = true; - try args.append(argv[i]); - } else if (mem.eql(u8, arg, "--init")) { - asked_for_init = true; - try args.append(argv[i]); - } else if (i + 1 < argv.len and mem.eql(u8, arg, "--build-file")) { - build_file = argv[i + 1]; - i += 1; - } else if (i + 1 < argv.len and mem.eql(u8, arg, "--cache-dir")) { - cache_dir = argv[i + 1]; - i += 1; - } else if (i + 1 < argv.len and mem.eql(u8, arg, "--zig-install-prefix")) { - try args.append(arg); - i += 1; - zig_install_prefix = argv[i]; - try args.append(argv[i]); - } else { - try args.append(arg); - } + if (flags.present("help")) { + try stderr.write(usage_build); + os.exit(0); } - const zig_lib_dir = try resolveZigLibDir(allocator, zig_install_prefix); + const zig_lib_dir = try introspect.resolveZigLibDir(allocator); defer allocator.free(zig_lib_dir); const zig_std_dir = try os.path.join(allocator, zig_lib_dir, "std"); @@ -619,113 +171,502 @@ fn buildMain(allocator: &mem.Allocator, argv: []const []const u8) !void { const build_runner_path = try os.path.join(allocator, special_dir, "build_runner.zig"); defer allocator.free(build_runner_path); - // g = codegen_create(build_runner_path, ...) - // codegen_set_out_name(g, "build") - + const build_file = flags.single("build-file") ?? "build.zig"; const build_file_abs = try os.path.resolve(allocator, ".", build_file); defer allocator.free(build_file_abs); + const build_file_exists = os.File.access(allocator, build_file_abs, os.default_file_mode) catch false; + + if (flags.present("init")) { + if (build_file_exists) { + try stderr.print("build.zig already exists\n"); + os.exit(1); + } + + // need a new scope for proper defer scope finalization on exit + { + const build_template_path = try os.path.join(allocator, special_dir, "build_file_template.zig"); + defer allocator.free(build_template_path); + + try os.copyFile(allocator, build_template_path, build_file_abs); + try stderr.print("wrote build.zig template\n"); + } + + os.exit(0); + } + + if (!build_file_exists) { + try stderr.write(missing_build_file); + os.exit(1); + } + + // TODO: Invoke build.zig entrypoint directly? + var zig_exe_path = try os.selfExePath(allocator); + defer allocator.free(zig_exe_path); + + var build_args = ArrayList([]const u8).init(allocator); + defer build_args.deinit(); + const build_file_basename = os.path.basename(build_file_abs); const build_file_dirname = os.path.dirname(build_file_abs); var full_cache_dir: []u8 = undefined; - if (cache_dir == null) { - full_cache_dir = try os.path.join(allocator, build_file_dirname, "zig-cache"); + if (flags.single("cache-dir")) |cache_dir| { + full_cache_dir = try os.path.resolve(allocator, ".", cache_dir, full_cache_dir); } else { - full_cache_dir = try os.path.resolve(allocator, ".", ??cache_dir, full_cache_dir); + full_cache_dir = try os.path.join(allocator, build_file_dirname, "zig-cache"); } defer allocator.free(full_cache_dir); const path_to_build_exe = try os.path.join(allocator, full_cache_dir, "build"); defer allocator.free(path_to_build_exe); - // codegen_set_cache_dir(g, full_cache_dir) - args.items[0] = path_to_build_exe; - args.items[1] = zig_exe_path; - args.items[2] = build_file_dirname; - args.items[3] = full_cache_dir; + try build_args.append(path_to_build_exe); + try build_args.append(zig_exe_path); + try build_args.append(build_file_dirname); + try build_args.append(full_cache_dir); - var build_file_exists: bool = undefined; - if (os.File.openRead(allocator, build_file_abs)) |*file| { - file.close(); - build_file_exists = true; - } else |_| { - build_file_exists = false; - } - - if (!build_file_exists and asked_for_help) { - // TODO(bnoordhuis) Print help message from std/special/build_runner.zig - return; - } - - if (!build_file_exists and asked_for_init) { - const build_template_path = try os.path.join(allocator, special_dir, "build_file_template.zig"); - defer allocator.free(build_template_path); - - var srcfile = try os.File.openRead(allocator, build_template_path); - defer srcfile.close(); - - var dstfile = try os.File.openWrite(allocator, build_file_abs); - defer dstfile.close(); - - while (true) { - var buffer: [4096]u8 = undefined; - const n = try srcfile.read(buffer[0..]); - if (n == 0) break; - try dstfile.write(buffer[0..n]); - } - - return; - } - - if (!build_file_exists) { - warn( - \\No 'build.zig' file found. - \\Initialize a 'build.zig' template file with `zig build --init`, - \\or build an executable directly with `zig build-exe $FILENAME.zig`. - \\See: `zig build --help` or `zig help` for more options. - \\ - ); - os.exit(1); - } - - // codegen_build(g) - // codegen_link(g, path_to_build_exe) - // codegen_destroy(g) - - var proc = try os.ChildProcess.init(args.toSliceConst(), allocator); + var proc = try os.ChildProcess.init(build_args.toSliceConst(), allocator); defer proc.deinit(); var term = try proc.spawnAndWait(); switch (term) { os.ChildProcess.Term.Exited => |status| { if (status != 0) { - warn("{} exited with status {}\n", args.at(0), status); + try stderr.print("{} exited with status {}\n", build_args.at(0), status); os.exit(1); } }, os.ChildProcess.Term.Signal => |signal| { - warn("{} killed by signal {}\n", args.at(0), signal); + try stderr.print("{} killed by signal {}\n", build_args.at(0), signal); os.exit(1); }, os.ChildProcess.Term.Stopped => |signal| { - warn("{} stopped by signal {}\n", args.at(0), signal); + try stderr.print("{} stopped by signal {}\n", build_args.at(0), signal); os.exit(1); }, os.ChildProcess.Term.Unknown => |status| { - warn("{} encountered unknown failure {}\n", args.at(0), status); + try stderr.print("{} encountered unknown failure {}\n", build_args.at(0), status); os.exit(1); }, } } -fn fmtMain(allocator: &mem.Allocator, file_paths: []const []const u8) !void { - for (file_paths) |file_path| { +// cmd:build-exe /////////////////////////////////////////////////////////////////////////////////// + +const usage_build_generic = + \\usage: zig build-exe [file] + \\ zig build-lib [file] + \\ zig build-obj [file] + \\ + \\General Options: + \\ --help Print this help and exit + \\ --color [auto|off|on] Enable or disable colored error messages + \\ + \\Compile Options: + \\ --assembly [source] Add assembly file to build + \\ --cache-dir [path] Override the cache directory + \\ --emit [filetype] Emit a specific file format as compilation output + \\ --enable-timing-info Print timing diagnostics + \\ --libc-include-dir [path] Directory where libc stdlib.h resides + \\ --name [name] Override output name + \\ --output [file] Override destination path + \\ --output-h [file] Override generated header file path + \\ --pkg-begin [name] [path] Make package available to import and push current pkg + \\ --pkg-end Pop current pkg + \\ --release-fast Build with optimizations on and safety off + \\ --release-safe Build with optimizations on and safety on + \\ --static Output will be statically linked + \\ --strip Exclude debug symbols + \\ --target-arch [name] Specify target architecture + \\ --target-environ [name] Specify target environment + \\ --target-os [name] Specify target operating system + \\ --verbose-tokenize Turn on compiler debug output for tokenization + \\ --verbose-ast-tree Turn on compiler debug output for parsing into an AST (tree view) + \\ --verbose-ast-fmt Turn on compiler debug output for parsing into an AST (render source) + \\ --verbose-link Turn on compiler debug output for linking + \\ --verbose-ir Turn on compiler debug output for Zig IR + \\ --verbose-llvm-ir Turn on compiler debug output for LLVM IR + \\ --verbose-cimport Turn on compiler debug output for C imports + \\ -dirafter [dir] Same as -isystem but do it last + \\ -isystem [dir] Add additional search path for other .h files + \\ -mllvm [arg] Additional arguments to forward to LLVM's option processing + \\ + \\Link Options: + \\ --ar-path [path] Set the path to ar + \\ --dynamic-linker [path] Set the path to ld.so + \\ --each-lib-rpath Add rpath for each used dynamic library + \\ --libc-lib-dir [path] Directory where libc crt1.o resides + \\ --libc-static-lib-dir [path] Directory where libc crtbegin.o resides + \\ --msvc-lib-dir [path] (windows) directory where vcruntime.lib resides + \\ --kernel32-lib-dir [path] (windows) directory where kernel32.lib resides + \\ --library [lib] Link against lib + \\ --forbid-library [lib] Make it an error to link against lib + \\ --library-path [dir] Add a directory to the library search path + \\ --linker-script [path] Use a custom linker script + \\ --object [obj] Add object file to build + \\ -rdynamic Add all symbols to the dynamic symbol table + \\ -rpath [path] Add directory to the runtime library search path + \\ -mconsole (windows) --subsystem console to the linker + \\ -mwindows (windows) --subsystem windows to the linker + \\ -framework [name] (darwin) link against framework + \\ -mios-version-min [ver] (darwin) set iOS deployment target + \\ -mmacosx-version-min [ver] (darwin) set Mac OS X deployment target + \\ --ver-major [ver] Dynamic library semver major version + \\ --ver-minor [ver] Dynamic library semver minor version + \\ --ver-patch [ver] Dynamic library semver patch version + \\ + \\ + ; + +const args_build_generic = []Flag { + Flag.Bool("--help"), + Flag.Option("--color", []const []const u8 { "auto", "off", "on" }), + + Flag.ArgMergeN("--assembly", 1), + Flag.Arg1("--cache-dir"), + Flag.Option("--emit", []const []const u8 { "asm", "bin", "llvm-ir" }), + Flag.Bool("--enable-timing-info"), + Flag.Arg1("--libc-include-dir"), + Flag.Arg1("--name"), + Flag.Arg1("--output"), + Flag.Arg1("--output-h"), + // NOTE: Parsed manually after initial check + Flag.ArgN("--pkg-begin", 2), + Flag.Bool("--pkg-end"), + Flag.Bool("--release-fast"), + Flag.Bool("--release-safe"), + Flag.Bool("--static"), + Flag.Bool("--strip"), + Flag.Arg1("--target-arch"), + Flag.Arg1("--target-environ"), + Flag.Arg1("--target-os"), + Flag.Bool("--verbose-tokenize"), + Flag.Bool("--verbose-ast-tree"), + Flag.Bool("--verbose-ast-fmt"), + Flag.Bool("--verbose-link"), + Flag.Bool("--verbose-ir"), + Flag.Bool("--verbose-llvm-ir"), + Flag.Bool("--verbose-cimport"), + Flag.Arg1("-dirafter"), + Flag.ArgMergeN("-isystem", 1), + Flag.Arg1("-mllvm"), + + Flag.Arg1("--ar-path"), + Flag.Arg1("--dynamic-linker"), + Flag.Bool("--each-lib-rpath"), + Flag.Arg1("--libc-lib-dir"), + Flag.Arg1("--libc-static-lib-dir"), + Flag.Arg1("--msvc-lib-dir"), + Flag.Arg1("--kernel32-lib-dir"), + Flag.ArgMergeN("--library", 1), + Flag.ArgMergeN("--forbid-library", 1), + Flag.ArgMergeN("--library-path", 1), + Flag.Arg1("--linker-script"), + Flag.ArgMergeN("--object", 1), + // NOTE: Removed -L since it would need to be special-cased and we have an alias in library-path + Flag.Bool("-rdynamic"), + Flag.Arg1("-rpath"), + Flag.Bool("-mconsole"), + Flag.Bool("-mwindows"), + Flag.ArgMergeN("-framework", 1), + Flag.Arg1("-mios-version-min"), + Flag.Arg1("-mmacosx-version-min"), + Flag.Arg1("--ver-major"), + Flag.Arg1("--ver-minor"), + Flag.Arg1("--ver-patch"), +}; + +fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Module.Kind) !void { + var flags = try Args.parse(allocator, args_build_generic, args); + defer flags.deinit(); + + if (flags.present("help")) { + try stderr.write(usage_build_generic); + os.exit(0); + } + + var build_mode = builtin.Mode.Debug; + if (flags.present("release-fast")) { + build_mode = builtin.Mode.ReleaseFast; + } else if (flags.present("release-safe")) { + build_mode = builtin.Mode.ReleaseSafe; + } + + var color = Module.ErrColor.Auto; + if (flags.single("color")) |color_flag| { + if (mem.eql(u8, color_flag, "auto")) { + color = Module.ErrColor.Auto; + } else if (mem.eql(u8, color_flag, "on")) { + color = Module.ErrColor.On; + } else if (mem.eql(u8, color_flag, "off")) { + color = Module.ErrColor.Off; + } else { + unreachable; + } + } + + var emit_type = Module.Emit.Binary; + if (flags.single("emit")) |emit_flag| { + if (mem.eql(u8, emit_flag, "asm")) { + emit_type = Module.Emit.Assembly; + } else if (mem.eql(u8, emit_flag, "bin")) { + emit_type = Module.Emit.Binary; + } else if (mem.eql(u8, emit_flag, "llvm-ir")) { + emit_type = Module.Emit.LlvmIr; + } else { + unreachable; + } + } + + var cur_pkg = try Module.CliPkg.init(allocator, "", "", null); // TODO: Need a path, name? + defer cur_pkg.deinit(); + + var i: usize = 0; + while (i < args.len) : (i += 1) { + const arg_name = args[i]; + if (mem.eql(u8, "--pkg-begin", arg_name)) { + // following two arguments guaranteed to exist due to arg parsing + i += 1; + const new_pkg_name = args[i]; + i += 1; + const new_pkg_path = args[i]; + + var new_cur_pkg = try Module.CliPkg.init(allocator, new_pkg_name, new_pkg_path, cur_pkg); + try cur_pkg.children.append(new_cur_pkg); + cur_pkg = new_cur_pkg; + } else if (mem.eql(u8, "--pkg-end", arg_name)) { + if (cur_pkg.parent == null) { + try stderr.print("encountered --pkg-end with no matching --pkg-begin\n"); + os.exit(1); + } + cur_pkg = ??cur_pkg.parent; + } + } + + if (cur_pkg.parent != null) { + try stderr.print("unmatched --pkg-begin\n"); + os.exit(1); + } + + var in_file: ?[]const u8 = undefined; + switch (flags.positionals.len) { + 0 => { + try stderr.write("--name [name] not provided and unable to infer\n"); + os.exit(1); + }, + 1 => { + in_file = flags.positionals.at(0); + }, + else => { + try stderr.write("only one zig input file is accepted during build\n"); + os.exit(1); + }, + } + + const basename = os.path.basename(??in_file); + var it = mem.split(basename, "."); + const root_name = it.next() ?? { + try stderr.write("file name cannot be empty\n"); + os.exit(1); + }; + + const asm_a= flags.many("assembly"); + const obj_a = flags.many("object"); + if (in_file == null and (obj_a == null or (??obj_a).len == 0) and (asm_a == null or (??asm_a).len == 0)) { + try stderr.write("Expected source file argument or at least one --object or --assembly argument\n"); + os.exit(1); + } + + if (out_type == Module.Kind.Obj and (obj_a != null and (??obj_a).len != 0)) { + try stderr.write("When building an object file, --object arguments are invalid\n"); + os.exit(1); + } + + const zig_root_source_file = in_file; + + const full_cache_dir = os.path.resolve(allocator, ".", flags.single("cache-dir") ?? "zig-cache"[0..]) catch { + os.exit(1); + }; + defer allocator.free(full_cache_dir); + + const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1); + defer allocator.free(zig_lib_dir); + + var module = + try Module.create( + allocator, + root_name, + zig_root_source_file, + Target.Native, + out_type, + build_mode, + zig_lib_dir, + full_cache_dir + ); + defer module.destroy(); + + module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") ?? "0", 10); + module.version_minor = try std.fmt.parseUnsigned(u32, flags.single("ver-minor") ?? "0", 10); + module.version_patch = try std.fmt.parseUnsigned(u32, flags.single("ver-patch") ?? "0", 10); + + module.is_test = false; + + if (flags.single("linker-script")) |linker_script| { + module.linker_script = linker_script; + } + + module.each_lib_rpath = flags.present("each-lib-rpath"); + + var clang_argv_buf = ArrayList([]const u8).init(allocator); + defer clang_argv_buf.deinit(); + if (flags.many("mllvm")) |mllvm_flags| { + for (mllvm_flags) |mllvm| { + try clang_argv_buf.append("-mllvm"); + try clang_argv_buf.append(mllvm); + } + + module.llvm_argv = mllvm_flags; + module.clang_argv = clang_argv_buf.toSliceConst(); + } + + module.strip = flags.present("strip"); + module.is_static = flags.present("static"); + + if (flags.single("libc-lib-dir")) |libc_lib_dir| { + module.libc_lib_dir = libc_lib_dir; + } + if (flags.single("libc-static-lib-dir")) |libc_static_lib_dir| { + module.libc_static_lib_dir = libc_static_lib_dir; + } + if (flags.single("libc-include-dir")) |libc_include_dir| { + module.libc_include_dir = libc_include_dir; + } + if (flags.single("msvc-lib-dir")) |msvc_lib_dir| { + module.msvc_lib_dir = msvc_lib_dir; + } + if (flags.single("kernel32-lib-dir")) |kernel32_lib_dir| { + module.kernel32_lib_dir = kernel32_lib_dir; + } + if (flags.single("dynamic-linker")) |dynamic_linker| { + module.dynamic_linker = dynamic_linker; + } + + module.verbose_tokenize = flags.present("verbose-tokenize"); + module.verbose_ast_tree = flags.present("verbose-ast-tree"); + module.verbose_ast_fmt = flags.present("verbose-ast-fmt"); + module.verbose_link = flags.present("verbose-link"); + module.verbose_ir = flags.present("verbose-ir"); + module.verbose_llvm_ir = flags.present("verbose-llvm-ir"); + module.verbose_cimport = flags.present("verbose-cimport"); + + module.err_color = color; + + if (flags.many("library-path")) |lib_dirs| { + module.lib_dirs = lib_dirs; + } + + if (flags.many("framework")) |frameworks| { + module.darwin_frameworks = frameworks; + } + + if (flags.many("rpath")) |rpath_list| { + module.rpath_list = rpath_list; + } + + if (flags.single("output-h")) |output_h| { + module.out_h_path = output_h; + } + + module.windows_subsystem_windows = flags.present("mwindows"); + module.windows_subsystem_console = flags.present("mconsole"); + module.linker_rdynamic = flags.present("rdynamic"); + + if (flags.single("mmacosx-version-min") != null and flags.single("mios-version-min") != null) { + try stderr.write("-mmacosx-version-min and -mios-version-min options not allowed together\n"); + os.exit(1); + } + + if (flags.single("mmacosx-version-min")) |ver| { + module.darwin_version_min = Module.DarwinVersionMin { .MacOS = ver }; + } + if (flags.single("mios-version-min")) |ver| { + module.darwin_version_min = Module.DarwinVersionMin { .Ios = ver }; + } + + module.emit_file_type = emit_type; + if (flags.many("object")) |objects| { + module.link_objects = objects; + } + if (flags.many("assembly")) |assembly_files| { + module.assembly_files = assembly_files; + } + + try module.build(); + try module.link(flags.single("out-file") ?? null); + + if (flags.present("print-timing-info")) { + // codegen_print_timing_info(g, stderr); + } + + try stderr.print("building {}: {}\n", @tagName(out_type), in_file); +} + +fn cmdBuildExe(allocator: &Allocator, args: []const []const u8) !void { + try buildOutputType(allocator, args, Module.Kind.Exe); +} + +// cmd:build-lib /////////////////////////////////////////////////////////////////////////////////// + +fn cmdBuildLib(allocator: &Allocator, args: []const []const u8) !void { + try buildOutputType(allocator, args, Module.Kind.Lib); +} + +// cmd:build-obj /////////////////////////////////////////////////////////////////////////////////// + +fn cmdBuildObj(allocator: &Allocator, args: []const []const u8) !void { + try buildOutputType(allocator, args, Module.Kind.Obj); +} + +// cmd:fmt ///////////////////////////////////////////////////////////////////////////////////////// + +const usage_fmt = + \\usage: zig fmt [file]... + \\ + \\ Formats the input files and modifies them in-place. + \\ + \\Options: + \\ --help Print this help and exit + \\ --keep-backups Retain backup entries for every file + \\ + \\ + ; + +const args_fmt_spec = []Flag { + Flag.Bool("--help"), + Flag.Bool("--keep-backups"), +}; + +fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { + var flags = try Args.parse(allocator, args_fmt_spec, args); + defer flags.deinit(); + + if (flags.present("help")) { + try stderr.write(usage_fmt); + os.exit(0); + } + + if (flags.positionals.len == 0) { + try stderr.write("expected at least one source file argument\n"); + os.exit(1); + } + + for (flags.positionals.toSliceConst()) |file_path| { var file = try os.File.openRead(allocator, file_path); defer file.close(); const source_code = io.readFileAlloc(allocator, file_path) catch |err| { - warn("unable to open '{}': {}", file_path, err); + try stderr.print("unable to open '{}': {}", file_path, err); continue; }; defer allocator.free(source_code); @@ -734,72 +675,312 @@ fn fmtMain(allocator: &mem.Allocator, file_paths: []const []const u8) !void { var parser = std.zig.Parser.init(&tokenizer, allocator, file_path); defer parser.deinit(); - var tree = try parser.parse(); - defer tree.deinit(); - - const baf = try io.BufferedAtomicFile.create(allocator, file_path); - defer baf.destroy(); - - try parser.renderSource(baf.stream(), tree.root_node); - try baf.finish(); - } -} - -/// Caller must free result -fn resolveZigLibDir(allocator: &mem.Allocator, zig_install_prefix_arg: ?[]const u8) ![]u8 { - if (zig_install_prefix_arg) |zig_install_prefix| { - return testZigInstallPrefix(allocator, zig_install_prefix) catch |err| { - warn("No Zig installation found at prefix {}: {}\n", zig_install_prefix_arg, @errorName(err)); - return error.ZigInstallationNotFound; - }; - } else { - return findZigLibDir(allocator) catch |err| { - warn("Unable to find zig lib directory: {}.\nReinstall Zig or use --zig-install-prefix.\n", - @errorName(err)); - return error.ZigLibDirNotFound; - }; - } -} - -/// Caller must free result -fn testZigInstallPrefix(allocator: &mem.Allocator, test_path: []const u8) ![]u8 { - const test_zig_dir = try os.path.join(allocator, test_path, "lib", "zig"); - errdefer allocator.free(test_zig_dir); - - const test_index_file = try os.path.join(allocator, test_zig_dir, "std", "index.zig"); - defer allocator.free(test_index_file); - - var file = try os.File.openRead(allocator, test_index_file); - file.close(); - - return test_zig_dir; -} - -/// Caller must free result -fn findZigLibDir(allocator: &mem.Allocator) ![]u8 { - const self_exe_path = try os.selfExeDirPath(allocator); - defer allocator.free(self_exe_path); - - var cur_path: []const u8 = self_exe_path; - while (true) { - const test_dir = os.path.dirname(cur_path); - - if (mem.eql(u8, test_dir, cur_path)) { - break; - } - - return testZigInstallPrefix(allocator, test_dir) catch |err| { - cur_path = test_dir; + var tree = parser.parse() catch |err| { + try stderr.print("error parsing file '{}': {}\n", file_path, err); continue; }; + defer tree.deinit(); + + var original_file_backup = try Buffer.init(allocator, file_path); + defer original_file_backup.deinit(); + try original_file_backup.append(".backup"); + + try os.rename(allocator, file_path, original_file_backup.toSliceConst()); + + try stderr.print("{}\n", file_path); + + // TODO: BufferedAtomicFile has some access problems. + var out_file = try os.File.openWrite(allocator, file_path); + defer out_file.close(); + + var out_file_stream = io.FileOutStream.init(&out_file); + try parser.renderSource(out_file_stream.stream, tree.root_node); + + if (!flags.present("keep-backups")) { + try os.deleteFile(allocator, original_file_backup.toSliceConst()); + } + } +} + +// cmd:targets ///////////////////////////////////////////////////////////////////////////////////// + +fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void { + try stdout.write("Architectures:\n"); + { + comptime var i: usize = 0; + inline while (i < @memberCount(builtin.Arch)) : (i += 1) { + comptime const arch_tag = @memberName(builtin.Arch, i); + // NOTE: Cannot use empty string, see #918. + comptime const native_str = + if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n"; + + try stdout.print(" {}{}", arch_tag, native_str); + } + } + try stdout.write("\n"); + + try stdout.write("Operating Systems:\n"); + { + comptime var i: usize = 0; + inline while (i < @memberCount(builtin.Os)) : (i += 1) { + comptime const os_tag = @memberName(builtin.Os, i); + // NOTE: Cannot use empty string, see #918. + comptime const native_str = + if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n"; + + try stdout.print(" {}{}", os_tag, native_str); + } + } + try stdout.write("\n"); + + try stdout.write("Environments:\n"); + { + comptime var i: usize = 0; + inline while (i < @memberCount(builtin.Environ)) : (i += 1) { + comptime const environ_tag = @memberName(builtin.Environ, i); + // NOTE: Cannot use empty string, see #918. + comptime const native_str = + if (comptime mem.eql(u8, environ_tag, @tagName(builtin.environ))) " (native)\n" else "\n"; + + try stdout.print(" {}{}", environ_tag, native_str); + } + } +} + +// cmd:version ///////////////////////////////////////////////////////////////////////////////////// + +fn cmdVersion(allocator: &Allocator, args: []const []const u8) !void { + try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING)); +} + +// cmd:test //////////////////////////////////////////////////////////////////////////////////////// + +const usage_test = + \\usage: zig test [file]... + \\ + \\Options: + \\ --help Print this help and exit + \\ + \\ + ; + +const args_test_spec = []Flag { + Flag.Bool("--help"), +}; + + +fn cmdTest(allocator: &Allocator, args: []const []const u8) !void { + var flags = try Args.parse(allocator, args_build_spec, args); + defer flags.deinit(); + + if (flags.present("help")) { + try stderr.write(usage_test); + os.exit(0); } - // TODO look in hard coded installation path from configuration - //if (ZIG_INSTALL_PREFIX != nullptr) { - // if (test_zig_install_prefix(buf_create_from_str(ZIG_INSTALL_PREFIX), out_path)) { - // return 0; - // } - //} + if (flags.positionals.len != 1) { + try stderr.write("expected exactly one zig source file\n"); + os.exit(1); + } - return error.FileNotFound; + // compile the test program into the cache and run + + // NOTE: May be overlap with buildOutput, take the shared part out. + try stderr.print("testing file {}\n", flags.positionals.at(0)); +} + +// cmd:run ///////////////////////////////////////////////////////////////////////////////////////// + +// Run should be simple and not expose the full set of arguments provided by build-exe. If specific +// build requirements are need, the user should `build-exe` then `run` manually. +const usage_run = + \\usage: zig run [file] -- + \\ + \\Options: + \\ --help Print this help and exit + \\ + \\ + ; + +const args_run_spec = []Flag { + Flag.Bool("--help"), +}; + + +fn cmdRun(allocator: &Allocator, args: []const []const u8) !void { + var compile_args = args; + var runtime_args: []const []const u8 = []const []const u8 {}; + + for (args) |argv, i| { + if (mem.eql(u8, argv, "--")) { + compile_args = args[0..i]; + runtime_args = args[i+1..]; + break; + } + } + var flags = try Args.parse(allocator, args_run_spec, compile_args); + defer flags.deinit(); + + if (flags.present("help")) { + try stderr.write(usage_run); + os.exit(0); + } + + if (flags.positionals.len != 1) { + try stderr.write("expected exactly one zig source file\n"); + os.exit(1); + } + + try stderr.print("runtime args:\n"); + for (runtime_args) |cargs| { + try stderr.print("{}\n", cargs); + } +} + +// cmd:translate-c ///////////////////////////////////////////////////////////////////////////////// + +const usage_translate_c = + \\usage: zig translate-c [file] + \\ + \\Options: + \\ --help Print this help and exit + \\ --enable-timing-info Print timing diagnostics + \\ --output [path] Output file to write generated zig file (default: stdout) + \\ + \\ + ; + +const args_translate_c_spec = []Flag { + Flag.Bool("--help"), + Flag.Bool("--enable-timing-info"), + Flag.Arg1("--libc-include-dir"), + Flag.Arg1("--output"), +}; + +fn cmdTranslateC(allocator: &Allocator, args: []const []const u8) !void { + var flags = try Args.parse(allocator, args_translate_c_spec, args); + defer flags.deinit(); + + if (flags.present("help")) { + try stderr.write(usage_translate_c); + os.exit(0); + } + + if (flags.positionals.len != 1) { + try stderr.write("expected exactly one c source file\n"); + os.exit(1); + } + + // set up codegen + + const zig_root_source_file = null; + + // NOTE: translate-c shouldn't require setting up the full codegen instance as it does in + // the C++ compiler. + + // codegen_create(g); + // codegen_set_out_name(g, null); + // codegen_translate_c(g, flags.positional.at(0)) + + var output_stream = stdout; + if (flags.single("output")) |output_file| { + var file = try os.File.openWrite(allocator, output_file); + defer file.close(); + + var file_stream = io.FileOutStream.init(&file); + // TODO: Not being set correctly, still stdout + output_stream = &file_stream.stream; + } + + // ast_render(g, output_stream, g->root_import->root, 4); + try output_stream.write("pub const example = 10;\n"); + + if (flags.present("enable-timing-info")) { + // codegen_print_timing_info(g, stdout); + try stderr.write("printing timing info for translate-c\n"); + } +} + +// cmd:help //////////////////////////////////////////////////////////////////////////////////////// + +fn cmdHelp(allocator: &Allocator, args: []const []const u8) !void { + try stderr.write(usage); +} + +// cmd:zen ///////////////////////////////////////////////////////////////////////////////////////// + +const info_zen = + \\ + \\ * Communicate intent precisely. + \\ * Edge cases matter. + \\ * Favor reading code over writing code. + \\ * Only one obvious way to do things. + \\ * Runtime crashes are better than bugs. + \\ * Compile errors are better than runtime crashes. + \\ * Incremental improvements. + \\ * Avoid local maximums. + \\ * Reduce the amount one must remember. + \\ * Minimize energy spent on coding style. + \\ * Together we serve end users. + \\ + \\ + ; + +fn cmdZen(allocator: &Allocator, args: []const []const u8) !void { + try stdout.write(info_zen); +} + +// cmd:internal //////////////////////////////////////////////////////////////////////////////////// + +const usage_internal = + \\usage: zig internal [subcommand] + \\ + \\Sub-Commands: + \\ build-info Print static compiler build-info + \\ + \\ + ; + +fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void { + if (args.len == 0) { + try stderr.write(usage_internal); + os.exit(1); + } + + const sub_commands = []Command { + Command { .name = "build-info", .exec = cmdInternalBuildInfo }, + }; + + for (sub_commands) |sub_command| { + if (mem.eql(u8, sub_command.name, args[0])) { + try sub_command.exec(allocator, args[1..]); + return; + } + } + + try stderr.print("unknown sub command: {}\n\n", args[0]); + try stderr.write(usage_internal); +} + +fn cmdInternalBuildInfo(allocator: &Allocator, args: []const []const u8) !void { + try stdout.print( + \\ZIG_CMAKE_BINARY_DIR {} + \\ZIG_CXX_COMPILER {} + \\ZIG_LLVM_CONFIG_EXE {} + \\ZIG_LLD_INCLUDE_PATH {} + \\ZIG_LLD_LIBRARIES {} + \\ZIG_STD_FILES {} + \\ZIG_C_HEADER_FILES {} + \\ZIG_DIA_GUIDS_LIB {} + \\ + , + std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR), + std.cstr.toSliceConst(c.ZIG_CXX_COMPILER), + std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE), + std.cstr.toSliceConst(c.ZIG_LLD_INCLUDE_PATH), + std.cstr.toSliceConst(c.ZIG_LLD_LIBRARIES), + std.cstr.toSliceConst(c.ZIG_STD_FILES), + std.cstr.toSliceConst(c.ZIG_C_HEADER_FILES), + std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB), + ); } diff --git a/src-self-hosted/module.zig b/src-self-hosted/module.zig index 464737bbbb..eec30749e2 100644 --- a/src-self-hosted/module.zig +++ b/src-self-hosted/module.zig @@ -109,6 +109,29 @@ pub const Module = struct { LlvmIr, }; + pub const CliPkg = struct { + name: []const u8, + path: []const u8, + children: ArrayList(&CliPkg), + parent: ?&CliPkg, + + pub fn init(allocator: &mem.Allocator, name: []const u8, path: []const u8, parent: ?&CliPkg) !&CliPkg { + var pkg = try allocator.create(CliPkg); + pkg.name = name; + pkg.path = path; + pkg.children = ArrayList(&CliPkg).init(allocator); + pkg.parent = parent; + return pkg; + } + + pub fn deinit(self: &CliPkg) void { + for (self.children.toSliceConst()) |child| { + child.deinit(); + } + self.children.deinit(); + } + }; + pub fn create(allocator: &mem.Allocator, name: []const u8, root_src_path: ?[]const u8, target: &const Target, kind: Kind, build_mode: builtin.Mode, zig_lib_dir: []const u8, cache_dir: []const u8) !&Module { diff --git a/src/all_types.hpp b/src/all_types.hpp index ab23dc8e88..eae722e8c5 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -867,6 +867,7 @@ struct AstNodeAwaitExpr { }; struct AstNodeSuspend { + Buf *name; AstNode *block; AstNode *promise_symbol; }; @@ -1291,6 +1292,7 @@ enum BuiltinFnId { BuiltinFnIdMemberCount, BuiltinFnIdMemberType, BuiltinFnIdMemberName, + BuiltinFnIdField, BuiltinFnIdTypeof, BuiltinFnIdAddWithOverflow, BuiltinFnIdSubWithOverflow, @@ -1310,13 +1312,15 @@ enum BuiltinFnId { BuiltinFnIdReturnAddress, BuiltinFnIdFrameAddress, BuiltinFnIdEmbedFile, - BuiltinFnIdCmpExchange, + BuiltinFnIdCmpxchgWeak, + BuiltinFnIdCmpxchgStrong, BuiltinFnIdFence, BuiltinFnIdDivExact, BuiltinFnIdDivTrunc, BuiltinFnIdDivFloor, BuiltinFnIdRem, BuiltinFnIdMod, + BuiltinFnIdSqrt, BuiltinFnIdTruncate, BuiltinFnIdIntType, BuiltinFnIdSetCold, @@ -1346,6 +1350,7 @@ enum BuiltinFnId { BuiltinFnIdExport, BuiltinFnIdErrorReturnTrace, BuiltinFnIdAtomicRmw, + BuiltinFnIdAtomicLoad, }; struct BuiltinFnEntry { @@ -1413,6 +1418,7 @@ enum ZigLLVMFnId { ZigLLVMFnIdOverflowArithmetic, ZigLLVMFnIdFloor, ZigLLVMFnIdCeil, + ZigLLVMFnIdSqrt, }; enum AddSubMul { @@ -1433,7 +1439,7 @@ struct ZigLLVMFnKey { } clz; struct { uint32_t bit_count; - } floor_ceil; + } floating; struct { AddSubMul add_sub_mul; uint32_t bit_count; @@ -1454,6 +1460,7 @@ enum BuildMode { BuildModeDebug, BuildModeFastRelease, BuildModeSafeRelease, + BuildModeSmallRelease, }; enum EmitFileType { @@ -1698,6 +1705,8 @@ struct CodeGen { ZigList error_di_types; ZigList forbidden_libs; + + bool no_rosegment_workaround; }; enum VarLinkage { @@ -1748,6 +1757,7 @@ enum ScopeId { ScopeIdVarDecl, ScopeIdCImport, ScopeIdLoop, + ScopeIdSuspend, ScopeIdFnDef, ScopeIdCompTime, ScopeIdCoroPrelude, @@ -1843,6 +1853,17 @@ struct ScopeLoop { ZigList *incoming_blocks; }; +// This scope is created for a suspend block in order to have labeled +// suspend for breaking out of a suspend and for detecting if a suspend +// block is inside a suspend block. +struct ScopeSuspend { + Scope base; + + Buf *name; + IrBasicBlock *resume_block; + bool reported_err; +}; + // This scope is created for a comptime expression. // NodeTypeCompTime, NodeTypeSwitchExpr struct ScopeCompTime { @@ -2039,12 +2060,14 @@ enum IrInstructionId { IrInstructionIdCoroPromise, IrInstructionIdCoroAllocHelper, IrInstructionIdAtomicRmw, + IrInstructionIdAtomicLoad, IrInstructionIdPromiseResultType, IrInstructionIdAwaitBookkeeping, IrInstructionIdSaveErrRetAddr, IrInstructionIdAddImplicitReturnType, IrInstructionIdMergeErrRetTraces, IrInstructionIdMarkErrRetTracePtr, + IrInstructionIdSqrt, }; struct IrInstruction { @@ -2201,7 +2224,8 @@ struct IrInstructionFieldPtr { IrInstruction base; IrInstruction *container_ptr; - Buf *field_name; + Buf *field_name_buffer; + IrInstruction *field_name_expr; bool is_const; }; @@ -2520,6 +2544,7 @@ struct IrInstructionEmbedFile { struct IrInstructionCmpxchg { IrInstruction base; + IrInstruction *type_value; IrInstruction *ptr; IrInstruction *cmp_value; IrInstruction *new_value; @@ -2527,8 +2552,13 @@ struct IrInstructionCmpxchg { IrInstruction *failure_order_value; // if this instruction gets to runtime then we know these values: + TypeTableEntry *type; AtomicOrder success_order; AtomicOrder failure_order; + + bool is_weak; + + LLVMValueRef tmp_ptr; }; struct IrInstructionFence { @@ -2998,6 +3028,15 @@ struct IrInstructionAtomicRmw { AtomicOrder resolved_ordering; }; +struct IrInstructionAtomicLoad { + IrInstruction base; + + IrInstruction *operand_type; + IrInstruction *ptr; + IrInstruction *ordering; + AtomicOrder resolved_ordering; +}; + struct IrInstructionPromiseResultType { IrInstruction base; @@ -3034,6 +3073,13 @@ struct IrInstructionMarkErrRetTracePtr { IrInstruction *err_ret_trace_ptr; }; +struct IrInstructionSqrt { + IrInstruction base; + + IrInstruction *type; + IrInstruction *op; +}; + static const size_t slice_ptr_index = 0; static const size_t slice_len_index = 1; diff --git a/src/analyze.cpp b/src/analyze.cpp index c73e6b39e3..a598d7676e 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -156,6 +156,14 @@ ScopeLoop *create_loop_scope(AstNode *node, Scope *parent) { return scope; } +ScopeSuspend *create_suspend_scope(AstNode *node, Scope *parent) { + assert(node->type == NodeTypeSuspend); + ScopeSuspend *scope = allocate(1); + init_scope(&scope->base, ScopeIdSuspend, node, parent); + scope->name = node->data.suspend.name; + return scope; +} + ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry) { ScopeFnDef *scope = allocate(1); init_scope(&scope->base, ScopeIdFnDef, node, parent); @@ -3616,6 +3624,7 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) { case ScopeIdVarDecl: case ScopeIdCImport: case ScopeIdLoop: + case ScopeIdSuspend: case ScopeIdCompTime: case ScopeIdCoroPrelude: scope = scope->parent; @@ -4297,7 +4306,8 @@ bool handle_is_ptr(TypeTableEntry *type_entry) { static ZigWindowsSDK *get_windows_sdk(CodeGen *g) { if (g->win_sdk == nullptr) { if (os_find_windows_sdk(&g->win_sdk)) { - zig_panic("Unable to determine Windows SDK path."); + fprintf(stderr, "unable to determine windows sdk path\n"); + exit(1); } } assert(g->win_sdk != nullptr); @@ -4399,7 +4409,8 @@ void find_libc_include_path(CodeGen *g) { ZigWindowsSDK *sdk = get_windows_sdk(g); g->libc_include_dir = buf_alloc(); if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) { - zig_panic("Unable to determine libc include path."); + fprintf(stderr, "Unable to determine libc include path. --libc-include-dir"); + exit(1); } } else if (g->zig_target.os == OsLinux) { g->libc_include_dir = get_linux_libc_include_path(); @@ -4421,24 +4432,33 @@ void find_libc_lib_path(CodeGen *g) { if (g->zig_target.os == OsWindows) { ZigWindowsSDK *sdk = get_windows_sdk(g); - Buf* vc_lib_dir = buf_alloc(); - if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) { - zig_panic("Unable to determine vcruntime path."); + if (g->msvc_lib_dir == nullptr) { + Buf* vc_lib_dir = buf_alloc(); + if (os_get_win32_vcruntime_path(vc_lib_dir, g->zig_target.arch.arch)) { + fprintf(stderr, "Unable to determine vcruntime path. --msvc-lib-dir"); + exit(1); + } + g->msvc_lib_dir = vc_lib_dir; } - Buf* ucrt_lib_path = buf_alloc(); - if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) { - zig_panic("Unable to determine ucrt path."); + if (g->libc_lib_dir == nullptr) { + Buf* ucrt_lib_path = buf_alloc(); + if (os_get_win32_ucrt_lib_path(sdk, ucrt_lib_path, g->zig_target.arch.arch)) { + fprintf(stderr, "Unable to determine ucrt path. --libc-lib-dir"); + exit(1); + } + g->libc_lib_dir = ucrt_lib_path; } - Buf* kern_lib_path = buf_alloc(); - if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) { - zig_panic("Unable to determine kernel32 path."); + if (g->kernel32_lib_dir == nullptr) { + Buf* kern_lib_path = buf_alloc(); + if (os_get_win32_kern32_path(sdk, kern_lib_path, g->zig_target.arch.arch)) { + fprintf(stderr, "Unable to determine kernel32 path. --kernel32-lib-dir"); + exit(1); + } + g->kernel32_lib_dir = kern_lib_path; } - g->msvc_lib_dir = vc_lib_dir; - g->libc_lib_dir = ucrt_lib_path; - g->kernel32_lib_dir = kern_lib_path; } else if (g->zig_target.os == OsLinux) { g->libc_lib_dir = get_linux_libc_lib_path("crt1.o"); } else { @@ -5801,9 +5821,11 @@ uint32_t zig_llvm_fn_key_hash(ZigLLVMFnKey x) { case ZigLLVMFnIdClz: return (uint32_t)(x.data.clz.bit_count) * (uint32_t)2428952817; case ZigLLVMFnIdFloor: - return (uint32_t)(x.data.floor_ceil.bit_count) * (uint32_t)1899859168; + return (uint32_t)(x.data.floating.bit_count) * (uint32_t)1899859168; case ZigLLVMFnIdCeil: - return (uint32_t)(x.data.floor_ceil.bit_count) * (uint32_t)1953839089; + return (uint32_t)(x.data.floating.bit_count) * (uint32_t)1953839089; + case ZigLLVMFnIdSqrt: + return (uint32_t)(x.data.floating.bit_count) * (uint32_t)2225366385; case ZigLLVMFnIdOverflowArithmetic: return ((uint32_t)(x.data.overflow_arithmetic.bit_count) * 87135777) + ((uint32_t)(x.data.overflow_arithmetic.add_sub_mul) * 31640542) + @@ -5822,7 +5844,8 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) { return a.data.clz.bit_count == b.data.clz.bit_count; case ZigLLVMFnIdFloor: case ZigLLVMFnIdCeil: - return a.data.floor_ceil.bit_count == b.data.floor_ceil.bit_count; + case ZigLLVMFnIdSqrt: + return a.data.floating.bit_count == b.data.floating.bit_count; case ZigLLVMFnIdOverflowArithmetic: return (a.data.overflow_arithmetic.bit_count == b.data.overflow_arithmetic.bit_count) && (a.data.overflow_arithmetic.add_sub_mul == b.data.overflow_arithmetic.add_sub_mul) && diff --git a/src/analyze.hpp b/src/analyze.hpp index aa4557666b..aca78f4e25 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -104,6 +104,7 @@ ScopeDeferExpr *create_defer_expr_scope(AstNode *node, Scope *parent); Scope *create_var_scope(AstNode *node, Scope *parent, VariableTableEntry *var); ScopeCImport *create_cimport_scope(AstNode *node, Scope *parent); ScopeLoop *create_loop_scope(AstNode *node, Scope *parent); +ScopeSuspend *create_suspend_scope(AstNode *node, Scope *parent); ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry); ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import); Scope *create_comptime_scope(AstNode *node, Scope *parent); diff --git a/src/bigfloat.cpp b/src/bigfloat.cpp index 2cab9658e8..dcb6db61db 100644 --- a/src/bigfloat.cpp +++ b/src/bigfloat.cpp @@ -181,3 +181,7 @@ bool bigfloat_has_fraction(const BigFloat *bigfloat) { f128M_roundToInt(&bigfloat->value, softfloat_round_minMag, false, &floored); return !f128M_eq(&floored, &bigfloat->value); } + +void bigfloat_sqrt(BigFloat *dest, const BigFloat *op) { + f128M_sqrt(&op->value, &dest->value); +} diff --git a/src/bigfloat.hpp b/src/bigfloat.hpp index 894b252c3a..e212c30c87 100644 --- a/src/bigfloat.hpp +++ b/src/bigfloat.hpp @@ -42,6 +42,7 @@ void bigfloat_div_trunc(BigFloat *dest, const BigFloat *op1, const BigFloat *op2 void bigfloat_div_floor(BigFloat *dest, const BigFloat *op1, const BigFloat *op2); void bigfloat_rem(BigFloat *dest, const BigFloat *op1, const BigFloat *op2); void bigfloat_mod(BigFloat *dest, const BigFloat *op1, const BigFloat *op2); +void bigfloat_sqrt(BigFloat *dest, const BigFloat *op); void bigfloat_append_buf(Buf *buf, const BigFloat *op); Cmp bigfloat_cmp(const BigFloat *op1, const BigFloat *op2); diff --git a/src/bigint.cpp b/src/bigint.cpp index 85e5dad4ad..2a688debd5 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -86,6 +86,11 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count) size_t digits_to_copy = bit_count / 64; size_t leftover_bits = bit_count % 64; dest->digit_count = digits_to_copy + ((leftover_bits == 0) ? 0 : 1); + if (dest->digit_count == 1 && leftover_bits == 0) { + dest->data.digit = op_digits[0]; + if (dest->data.digit == 0) dest->digit_count = 0; + return; + } dest->data.digits = allocate_nonzero(dest->digit_count); for (size_t i = 0; i < digits_to_copy; i += 1) { uint64_t digit = (i < op->digit_count) ? op_digits[i] : 0; diff --git a/src/codegen.cpp b/src/codegen.cpp index 9da3129aa5..3dd6cff021 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -471,7 +471,7 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { fn_table_entry->llvm_value, buf_ptr(&fn_export->name)); } } - fn_table_entry->llvm_name = LLVMGetValueName(fn_table_entry->llvm_value); + fn_table_entry->llvm_name = strdup(LLVMGetValueName(fn_table_entry->llvm_value)); switch (fn_table_entry->fn_inline) { case FnInlineAlways: @@ -516,7 +516,9 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { } if (fn_table_entry->body_node != nullptr) { - bool want_fn_safety = g->build_mode != BuildModeFastRelease && !fn_table_entry->def_scope->safety_off; + bool want_fn_safety = g->build_mode != BuildModeFastRelease && + g->build_mode != BuildModeSmallRelease && + !fn_table_entry->def_scope->safety_off; if (want_fn_safety) { if (g->libc_link_lib != nullptr) { addLLVMFnAttr(fn_table_entry->llvm_value, "sspstrong"); @@ -656,6 +658,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) { } case ScopeIdDeferExpr: case ScopeIdLoop: + case ScopeIdSuspend: case ScopeIdCompTime: case ScopeIdCoroPrelude: return get_di_scope(g, scope->parent); @@ -721,12 +724,12 @@ static LLVMValueRef get_int_overflow_fn(CodeGen *g, TypeTableEntry *type_entry, return fn_val; } -static LLVMValueRef get_floor_ceil_fn(CodeGen *g, TypeTableEntry *type_entry, ZigLLVMFnId fn_id) { +static LLVMValueRef get_float_fn(CodeGen *g, TypeTableEntry *type_entry, ZigLLVMFnId fn_id) { assert(type_entry->id == TypeTableEntryIdFloat); ZigLLVMFnKey key = {}; key.id = fn_id; - key.data.floor_ceil.bit_count = (uint32_t)type_entry->data.floating.bit_count; + key.data.floating.bit_count = (uint32_t)type_entry->data.floating.bit_count; auto existing_entry = g->llvm_fn_table.maybe_get(key); if (existing_entry) @@ -737,6 +740,8 @@ static LLVMValueRef get_floor_ceil_fn(CodeGen *g, TypeTableEntry *type_entry, Zi name = "floor"; } else if (fn_id == ZigLLVMFnIdCeil) { name = "ceil"; + } else if (fn_id == ZigLLVMFnIdSqrt) { + name = "sqrt"; } else { zig_unreachable(); } @@ -819,7 +824,7 @@ static bool ir_want_fast_math(CodeGen *g, IrInstruction *instruction) { } static bool ir_want_runtime_safety(CodeGen *g, IrInstruction *instruction) { - if (g->build_mode == BuildModeFastRelease) + if (g->build_mode == BuildModeFastRelease || g->build_mode == BuildModeSmallRelease) return false; // TODO memoize @@ -1867,7 +1872,7 @@ static LLVMValueRef gen_floor(CodeGen *g, LLVMValueRef val, TypeTableEntry *type if (type_entry->id == TypeTableEntryIdInt) return val; - LLVMValueRef floor_fn = get_floor_ceil_fn(g, type_entry, ZigLLVMFnIdFloor); + LLVMValueRef floor_fn = get_float_fn(g, type_entry, ZigLLVMFnIdFloor); return LLVMBuildCall(g->builder, floor_fn, &val, 1, ""); } @@ -1875,7 +1880,7 @@ static LLVMValueRef gen_ceil(CodeGen *g, LLVMValueRef val, TypeTableEntry *type_ if (type_entry->id == TypeTableEntryIdInt) return val; - LLVMValueRef ceil_fn = get_floor_ceil_fn(g, type_entry, ZigLLVMFnIdCeil); + LLVMValueRef ceil_fn = get_float_fn(g, type_entry, ZigLLVMFnIdCeil); return LLVMBuildCall(g->builder, ceil_fn, &val, 1, ""); } @@ -3186,10 +3191,12 @@ static LLVMValueRef get_int_builtin_fn(CodeGen *g, TypeTableEntry *int_type, Bui fn_name = "cttz"; key.id = ZigLLVMFnIdCtz; key.data.ctz.bit_count = (uint32_t)int_type->data.integral.bit_count; - } else { + } else if (fn_id == BuiltinFnIdClz) { fn_name = "ctlz"; key.id = ZigLLVMFnIdClz; key.data.clz.bit_count = (uint32_t)int_type->data.integral.bit_count; + } else { + zig_unreachable(); } auto existing_entry = g->llvm_fn_table.maybe_get(key); @@ -3491,9 +3498,30 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn LLVMAtomicOrdering failure_order = to_LLVMAtomicOrdering(instruction->failure_order); LLVMValueRef result_val = ZigLLVMBuildCmpXchg(g->builder, ptr_val, cmp_val, new_val, - success_order, failure_order); + success_order, failure_order, instruction->is_weak); - return LLVMBuildExtractValue(g->builder, result_val, 1, ""); + TypeTableEntry *maybe_type = instruction->base.value.type; + assert(maybe_type->id == TypeTableEntryIdMaybe); + TypeTableEntry *child_type = maybe_type->data.maybe.child_type; + + if (type_is_codegen_pointer(child_type)) { + LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, ""); + LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, ""); + return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(child_type->type_ref), payload_val, ""); + } + + assert(instruction->tmp_ptr != nullptr); + assert(type_has_bits(instruction->type)); + + LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, ""); + LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_child_index, ""); + gen_assign_raw(g, val_ptr, get_pointer_to_type(g, instruction->type, false), payload_val); + + LLVMValueRef success_bit = LLVMBuildExtractValue(g->builder, result_val, 1, ""); + LLVMValueRef nonnull_bit = LLVMBuildNot(g->builder, success_bit, ""); + LLVMValueRef maybe_ptr = LLVMBuildStructGEP(g->builder, instruction->tmp_ptr, maybe_null_index, ""); + gen_store_untyped(g, nonnull_bit, maybe_ptr, 0, false); + return instruction->tmp_ptr; } static LLVMValueRef ir_render_fence(CodeGen *g, IrExecutable *executable, IrInstructionFence *instruction) { @@ -4296,6 +4324,16 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable, return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, ""); } +static LLVMValueRef ir_render_atomic_load(CodeGen *g, IrExecutable *executable, + IrInstructionAtomicLoad *instruction) +{ + LLVMAtomicOrdering ordering = to_LLVMAtomicOrdering(instruction->resolved_ordering); + LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr); + LLVMValueRef load_inst = gen_load(g, ptr, instruction->ptr->value.type, ""); + LLVMSetOrdering(load_inst, ordering); + return load_inst; +} + static LLVMValueRef ir_render_merge_err_ret_traces(CodeGen *g, IrExecutable *executable, IrInstructionMergeErrRetTraces *instruction) { @@ -4317,6 +4355,13 @@ static LLVMValueRef ir_render_mark_err_ret_trace_ptr(CodeGen *g, IrExecutable *e return nullptr; } +static LLVMValueRef ir_render_sqrt(CodeGen *g, IrExecutable *executable, IrInstructionSqrt *instruction) { + LLVMValueRef op = ir_llvm_value(g, instruction->op); + assert(instruction->base.value.type->id == TypeTableEntryIdFloat); + LLVMValueRef fn_val = get_float_fn(g, instruction->base.value.type, ZigLLVMFnIdSqrt); + return LLVMBuildCall(g->builder, fn_val, &op, 1, ""); +} + static void set_debug_location(CodeGen *g, IrInstruction *instruction) { AstNode *source_node = instruction->source_node; Scope *scope = instruction->scope; @@ -4532,12 +4577,16 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction); case IrInstructionIdAtomicRmw: return ir_render_atomic_rmw(g, executable, (IrInstructionAtomicRmw *)instruction); + case IrInstructionIdAtomicLoad: + return ir_render_atomic_load(g, executable, (IrInstructionAtomicLoad *)instruction); case IrInstructionIdSaveErrRetAddr: return ir_render_save_err_ret_addr(g, executable, (IrInstructionSaveErrRetAddr *)instruction); case IrInstructionIdMergeErrRetTraces: return ir_render_merge_err_ret_traces(g, executable, (IrInstructionMergeErrRetTraces *)instruction); case IrInstructionIdMarkErrRetTracePtr: return ir_render_mark_err_ret_trace_ptr(g, executable, (IrInstructionMarkErrRetTracePtr *)instruction); + case IrInstructionIdSqrt: + return ir_render_sqrt(g, executable, (IrInstructionSqrt *)instruction); } zig_unreachable(); } @@ -5476,6 +5525,9 @@ static void do_code_gen(CodeGen *g) { } else if (instruction->id == IrInstructionIdErrWrapCode) { IrInstructionErrWrapCode *err_wrap_code_instruction = (IrInstructionErrWrapCode *)instruction; slot = &err_wrap_code_instruction->tmp_ptr; + } else if (instruction->id == IrInstructionIdCmpxchg) { + IrInstructionCmpxchg *cmpxchg_instruction = (IrInstructionCmpxchg *)instruction; + slot = &cmpxchg_instruction->tmp_ptr; } else { zig_unreachable(); } @@ -5637,10 +5689,12 @@ static void do_code_gen(CodeGen *g) { os_path_join(g->cache_dir, o_basename, output_path); ensure_cache_dir(g); + bool is_small = g->build_mode == BuildModeSmallRelease; + switch (g->emit_file_type) { case EmitFileTypeBinary: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), - ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug)) + ZigLLVM_EmitBinary, &err_msg, g->build_mode == BuildModeDebug, is_small)) { zig_panic("unable to write object file %s: %s", buf_ptr(output_path), err_msg); } @@ -5650,7 +5704,7 @@ static void do_code_gen(CodeGen *g) { case EmitFileTypeAssembly: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), - ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug)) + ZigLLVM_EmitAssembly, &err_msg, g->build_mode == BuildModeDebug, is_small)) { zig_panic("unable to write assembly file %s: %s", buf_ptr(output_path), err_msg); } @@ -5659,7 +5713,7 @@ static void do_code_gen(CodeGen *g) { case EmitFileTypeLLVMIr: if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path), - ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug)) + ZigLLVM_EmitLLVMIr, &err_msg, g->build_mode == BuildModeDebug, is_small)) { zig_panic("unable to write llvm-ir file %s: %s", buf_ptr(output_path), err_msg); } @@ -5985,6 +6039,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdMemberCount, "memberCount", 1); create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2); create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2); + create_builtin_fn(g, BuiltinFnIdField, "field", 2); create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4); create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4); @@ -6001,7 +6056,8 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdTypeName, "typeName", 1); create_builtin_fn(g, BuiltinFnIdCanImplicitCast, "canImplicitCast", 2); create_builtin_fn(g, BuiltinFnIdEmbedFile, "embedFile", 1); - create_builtin_fn(g, BuiltinFnIdCmpExchange, "cmpxchg", 5); + create_builtin_fn(g, BuiltinFnIdCmpxchgWeak, "cmpxchgWeak", 6); + create_builtin_fn(g, BuiltinFnIdCmpxchgStrong, "cmpxchgStrong", 6); create_builtin_fn(g, BuiltinFnIdFence, "fence", 1); create_builtin_fn(g, BuiltinFnIdTruncate, "truncate", 2); create_builtin_fn(g, BuiltinFnIdCompileErr, "compileError", 1); @@ -6024,6 +6080,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdDivFloor, "divFloor", 2); create_builtin_fn(g, BuiltinFnIdRem, "rem", 2); create_builtin_fn(g, BuiltinFnIdMod, "mod", 2); + create_builtin_fn(g, BuiltinFnIdSqrt, "sqrt", 2); create_builtin_fn(g, BuiltinFnIdInlineCall, "inlineCall", SIZE_MAX); create_builtin_fn(g, BuiltinFnIdNoInlineCall, "noInlineCall", SIZE_MAX); create_builtin_fn(g, BuiltinFnIdTypeId, "typeId", 1); @@ -6037,6 +6094,7 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdExport, "export", 3); create_builtin_fn(g, BuiltinFnIdErrorReturnTrace, "errorReturnTrace", 0); create_builtin_fn(g, BuiltinFnIdAtomicRmw, "atomicRmw", 5); + create_builtin_fn(g, BuiltinFnIdAtomicLoad, "atomicLoad", 3); } static const char *bool_to_str(bool b) { @@ -6048,6 +6106,7 @@ static const char *build_mode_to_str(BuildMode build_mode) { case BuildModeDebug: return "Mode.Debug"; case BuildModeSafeRelease: return "Mode.ReleaseSafe"; case BuildModeFastRelease: return "Mode.ReleaseFast"; + case BuildModeSmallRelease: return "Mode.ReleaseSmall"; } zig_unreachable(); } @@ -6188,6 +6247,7 @@ static void define_builtin_compile_vars(CodeGen *g) { " Debug,\n" " ReleaseSafe,\n" " ReleaseFast,\n" + " ReleaseSmall,\n" "};\n\n"); } { @@ -6238,7 +6298,8 @@ static void define_builtin_compile_vars(CodeGen *g) { int err; Buf *abs_full_path = buf_alloc(); if ((err = os_path_real(builtin_zig_path, abs_full_path))) { - zig_panic("unable to open '%s': %s", buf_ptr(builtin_zig_path), err_str(err)); + fprintf(stderr, "unable to open '%s': %s", buf_ptr(builtin_zig_path), err_str(err)); + exit(1); } assert(g->root_package); @@ -6358,7 +6419,7 @@ static void init(CodeGen *g) { } } - g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease; + g->have_err_ret_tracing = g->build_mode != BuildModeFastRelease && g->build_mode != BuildModeSmallRelease; define_builtin_fns(g); define_builtin_compile_vars(g); @@ -6492,12 +6553,14 @@ static void gen_root_source(CodeGen *g) { Buf *abs_full_path = buf_alloc(); int err; if ((err = os_path_real(rel_full_path, abs_full_path))) { - zig_panic("unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err)); + fprintf(stderr, "unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err)); + exit(1); } Buf *source_code = buf_alloc(); if ((err = os_fetch_file_path(rel_full_path, source_code, true))) { - zig_panic("unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err)); + fprintf(stderr, "unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err)); + exit(1); } g->root_import = add_source_file(g, g->root_package, abs_full_path, source_code); @@ -7050,4 +7113,3 @@ PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, } return pkg; } - diff --git a/src/ir.cpp b/src/ir.cpp index 7d8088d5ed..86c77758b2 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -110,6 +110,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type); static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, VariableTableEntry *var, bool is_const_ptr, bool is_volatile_ptr); +static TypeTableEntry *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op); +static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval); ConstExprValue *const_ptr_pointee(CodeGen *g, ConstExprValue *const_val) { assert(const_val->type->id == TypeTableEntryIdPointer); @@ -709,6 +711,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAtomicRmw *) { return IrInstructionIdAtomicRmw; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionAtomicLoad *) { + return IrInstructionIdAtomicLoad; +} + static constexpr IrInstructionId ir_instruction_id(IrInstructionPromiseResultType *) { return IrInstructionIdPromiseResultType; } @@ -733,6 +739,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionMarkErrRetTraceP return IrInstructionIdMarkErrRetTracePtr; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionSqrt *) { + return IrInstructionIdSqrt; +} + template static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate(1); @@ -1024,12 +1034,27 @@ static IrInstruction *ir_build_elem_ptr_from(IrBuilder *irb, IrInstruction *old_ return new_instruction; } +static IrInstruction *ir_build_field_ptr_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *container_ptr, IrInstruction *field_name_expr) +{ + IrInstructionFieldPtr *instruction = ir_build_instruction(irb, scope, source_node); + instruction->container_ptr = container_ptr; + instruction->field_name_buffer = nullptr; + instruction->field_name_expr = field_name_expr; + + ir_ref_instruction(container_ptr, irb->current_basic_block); + ir_ref_instruction(field_name_expr, irb->current_basic_block); + + return &instruction->base; +} + static IrInstruction *ir_build_field_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *container_ptr, Buf *field_name) { IrInstructionFieldPtr *instruction = ir_build_instruction(irb, scope, source_node); instruction->container_ptr = container_ptr; - instruction->field_name = field_name; + instruction->field_name_buffer = field_name; + instruction->field_name_expr = nullptr; ir_ref_instruction(container_ptr, irb->current_basic_block); @@ -1824,38 +1849,34 @@ static IrInstruction *ir_build_embed_file(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } -static IrInstruction *ir_build_cmpxchg(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *ptr, - IrInstruction *cmp_value, IrInstruction *new_value, IrInstruction *success_order_value, IrInstruction *failure_order_value, - AtomicOrder success_order, AtomicOrder failure_order) +static IrInstruction *ir_build_cmpxchg(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value, + IrInstruction *ptr, IrInstruction *cmp_value, IrInstruction *new_value, + IrInstruction *success_order_value, IrInstruction *failure_order_value, + bool is_weak, + TypeTableEntry *type, AtomicOrder success_order, AtomicOrder failure_order) { IrInstructionCmpxchg *instruction = ir_build_instruction(irb, scope, source_node); + instruction->type_value = type_value; instruction->ptr = ptr; instruction->cmp_value = cmp_value; instruction->new_value = new_value; instruction->success_order_value = success_order_value; instruction->failure_order_value = failure_order_value; + instruction->is_weak = is_weak; + instruction->type = type; instruction->success_order = success_order; instruction->failure_order = failure_order; + if (type_value != nullptr) ir_ref_instruction(type_value, irb->current_basic_block); ir_ref_instruction(ptr, irb->current_basic_block); ir_ref_instruction(cmp_value, irb->current_basic_block); ir_ref_instruction(new_value, irb->current_basic_block); - ir_ref_instruction(success_order_value, irb->current_basic_block); - ir_ref_instruction(failure_order_value, irb->current_basic_block); + if (type_value != nullptr) ir_ref_instruction(success_order_value, irb->current_basic_block); + if (type_value != nullptr) ir_ref_instruction(failure_order_value, irb->current_basic_block); return &instruction->base; } -static IrInstruction *ir_build_cmpxchg_from(IrBuilder *irb, IrInstruction *old_instruction, IrInstruction *ptr, - IrInstruction *cmp_value, IrInstruction *new_value, IrInstruction *success_order_value, IrInstruction *failure_order_value, - AtomicOrder success_order, AtomicOrder failure_order) -{ - IrInstruction *new_instruction = ir_build_cmpxchg(irb, old_instruction->scope, old_instruction->source_node, - ptr, cmp_value, new_value, success_order_value, failure_order_value, success_order, failure_order); - ir_link_new_instruction(new_instruction, old_instruction); - return new_instruction; -} - static IrInstruction *ir_build_fence(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *order_value, AtomicOrder order) { IrInstructionFence *instruction = ir_build_instruction(irb, scope, source_node); instruction->order_value = order_value; @@ -2669,6 +2690,23 @@ static IrInstruction *ir_build_atomic_rmw(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } +static IrInstruction *ir_build_atomic_load(IrBuilder *irb, Scope *scope, AstNode *source_node, + IrInstruction *operand_type, IrInstruction *ptr, + IrInstruction *ordering, AtomicOrder resolved_ordering) +{ + IrInstructionAtomicLoad *instruction = ir_build_instruction(irb, scope, source_node); + instruction->operand_type = operand_type; + instruction->ptr = ptr; + instruction->ordering = ordering; + instruction->resolved_ordering = resolved_ordering; + + if (operand_type != nullptr) ir_ref_instruction(operand_type, irb->current_basic_block); + ir_ref_instruction(ptr, irb->current_basic_block); + if (ordering != nullptr) ir_ref_instruction(ordering, irb->current_basic_block); + + return &instruction->base; +} + static IrInstruction *ir_build_promise_result_type(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *promise_type) { @@ -2731,6 +2769,17 @@ static IrInstruction *ir_build_mark_err_ret_trace_ptr(IrBuilder *irb, Scope *sco return &instruction->base; } +static IrInstruction *ir_build_sqrt(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type, IrInstruction *op) { + IrInstructionSqrt *instruction = ir_build_instruction(irb, scope, source_node); + instruction->type = type; + instruction->op = op; + + if (type != nullptr) ir_ref_instruction(type, irb->current_basic_block); + ir_ref_instruction(op, irb->current_basic_block); + + return &instruction->base; +} + static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) { results[ReturnKindUnconditional] = 0; results[ReturnKindError] = 0; @@ -2796,6 +2845,18 @@ static void ir_set_cursor_at_end_and_append_block(IrBuilder *irb, IrBasicBlock * ir_set_cursor_at_end(irb, basic_block); } +static ScopeSuspend *get_scope_suspend(Scope *scope) { + while (scope) { + if (scope->id == ScopeIdSuspend) + return (ScopeSuspend *)scope; + if (scope->id == ScopeIdFnDef) + return nullptr; + + scope = scope->parent; + } + return nullptr; +} + static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) { while (scope) { if (scope->id == ScopeIdDeferExpr) @@ -3086,6 +3147,9 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode if (block_node->data.block.name == nullptr || incoming_blocks.length == 0) { return noreturn_return_value; } + + ir_set_cursor_at_end_and_append_block(irb, scope_block->end_block); + return ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length, incoming_blocks.items, incoming_values.items); } else { incoming_blocks.append(irb->current_basic_block); incoming_values.append(ir_mark_gen(ir_build_const_void(irb, parent_scope, block_node))); @@ -3479,7 +3543,7 @@ static IrInstruction *ir_gen_array_access(IrBuilder *irb, Scope *scope, AstNode return ir_build_load_ptr(irb, scope, node, ptr_instruction); } -static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) { +static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode *node) { assert(node->type == NodeTypeFieldAccessExpr); AstNode *container_ref_node = node->data.field_access_expr.struct_expr; @@ -3489,11 +3553,7 @@ static IrInstruction *ir_gen_field_access(IrBuilder *irb, Scope *scope, AstNode if (container_ref_instruction == irb->codegen->invalid_instruction) return container_ref_instruction; - IrInstruction *ptr_instruction = ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name); - if (lval.is_ptr) - return ptr_instruction; - - return ir_build_load_ptr(irb, scope, node, ptr_instruction); + return ir_build_field_ptr(irb, scope, node, container_ref_instruction, field_name); } static IrInstruction *ir_gen_overflow_op(IrBuilder *irb, Scope *scope, AstNode *node, IrOverflowOp op) { @@ -3524,7 +3584,7 @@ static IrInstruction *ir_gen_overflow_op(IrBuilder *irb, Scope *scope, AstNode * return ir_build_overflow_op(irb, scope, node, op, type_value, op1, op2, result_ptr, nullptr); } -static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNode *node) { +static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) { assert(node->type == NodeTypeFnCallExpr); AstNode *fn_ref_expr = node->data.fn_call_expr.fn_ref_expr; @@ -3556,7 +3616,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo IrInstruction *arg = ir_gen_node(irb, arg_node, scope); if (arg == irb->codegen->invalid_instruction) return arg; - return ir_build_typeof(irb, scope, node, arg); + + IrInstruction *type_of = ir_build_typeof(irb, scope, node, arg); + return ir_lval_wrap(irb, scope, type_of, lval); } case BuiltinFnIdSetCold: { @@ -3565,7 +3627,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_set_cold(irb, scope, node, arg0_value); + IrInstruction *set_cold = ir_build_set_cold(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, set_cold, lval); } case BuiltinFnIdSetRuntimeSafety: { @@ -3574,7 +3637,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_set_runtime_safety(irb, scope, node, arg0_value); + IrInstruction *set_safety = ir_build_set_runtime_safety(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, set_safety, lval); } case BuiltinFnIdSetFloatMode: { @@ -3588,7 +3652,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_set_float_mode(irb, scope, node, arg0_value, arg1_value); + IrInstruction *set_float_mode = ir_build_set_float_mode(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, set_float_mode, lval); } case BuiltinFnIdSizeof: { @@ -3597,7 +3662,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_size_of(irb, scope, node, arg0_value); + IrInstruction *size_of = ir_build_size_of(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, size_of, lval); } case BuiltinFnIdCtz: { @@ -3606,7 +3672,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_ctz(irb, scope, node, arg0_value); + IrInstruction *ctz = ir_build_ctz(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, ctz, lval); } case BuiltinFnIdClz: { @@ -3615,7 +3682,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_clz(irb, scope, node, arg0_value); + IrInstruction *clz = ir_build_clz(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, clz, lval); } case BuiltinFnIdImport: { @@ -3624,11 +3692,13 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_import(irb, scope, node, arg0_value); + IrInstruction *import = ir_build_import(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, import, lval); } case BuiltinFnIdCImport: { - return ir_build_c_import(irb, scope, node); + IrInstruction *c_import = ir_build_c_import(irb, scope, node); + return ir_lval_wrap(irb, scope, c_import, lval); } case BuiltinFnIdCInclude: { @@ -3642,7 +3712,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return irb->codegen->invalid_instruction; } - return ir_build_c_include(irb, scope, node, arg0_value); + IrInstruction *c_include = ir_build_c_include(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, c_include, lval); } case BuiltinFnIdCDefine: { @@ -3661,7 +3732,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return irb->codegen->invalid_instruction; } - return ir_build_c_define(irb, scope, node, arg0_value, arg1_value); + IrInstruction *c_define = ir_build_c_define(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, c_define, lval); } case BuiltinFnIdCUndef: { @@ -3675,7 +3747,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return irb->codegen->invalid_instruction; } - return ir_build_c_undef(irb, scope, node, arg0_value); + IrInstruction *c_undef = ir_build_c_undef(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, c_undef, lval); } case BuiltinFnIdMaxValue: { @@ -3684,7 +3757,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_max_value(irb, scope, node, arg0_value); + IrInstruction *max_value = ir_build_max_value(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, max_value, lval); } case BuiltinFnIdMinValue: { @@ -3693,7 +3767,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_min_value(irb, scope, node, arg0_value); + IrInstruction *min_value = ir_build_min_value(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, min_value, lval); } case BuiltinFnIdCompileErr: { @@ -3702,7 +3777,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_compile_err(irb, scope, node, arg0_value); + IrInstruction *compile_err = ir_build_compile_err(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, compile_err, lval); } case BuiltinFnIdCompileLog: { @@ -3715,7 +3791,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return irb->codegen->invalid_instruction; } - return ir_build_compile_log(irb, scope, node, actual_param_count, args); + IrInstruction *compile_log = ir_build_compile_log(irb, scope, node, actual_param_count, args); + return ir_lval_wrap(irb, scope, compile_log, lval); } case BuiltinFnIdErrName: { @@ -3724,7 +3801,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_err_name(irb, scope, node, arg0_value); + IrInstruction *err_name = ir_build_err_name(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, err_name, lval); } case BuiltinFnIdEmbedFile: { @@ -3733,9 +3811,11 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_embed_file(irb, scope, node, arg0_value); + IrInstruction *embed_file = ir_build_embed_file(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, embed_file, lval); } - case BuiltinFnIdCmpExchange: + case BuiltinFnIdCmpxchgWeak: + case BuiltinFnIdCmpxchgStrong: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); @@ -3762,9 +3842,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg4_value == irb->codegen->invalid_instruction) return arg4_value; - return ir_build_cmpxchg(irb, scope, node, arg0_value, arg1_value, - arg2_value, arg3_value, arg4_value, - AtomicOrderUnordered, AtomicOrderUnordered); + AstNode *arg5_node = node->data.fn_call_expr.params.at(5); + IrInstruction *arg5_value = ir_gen_node(irb, arg5_node, scope); + if (arg5_value == irb->codegen->invalid_instruction) + return arg5_value; + + IrInstruction *cmpxchg = ir_build_cmpxchg(irb, scope, node, arg0_value, arg1_value, + arg2_value, arg3_value, arg4_value, arg5_value, (builtin_fn->id == BuiltinFnIdCmpxchgWeak), + nullptr, AtomicOrderUnordered, AtomicOrderUnordered); + return ir_lval_wrap(irb, scope, cmpxchg, lval); } case BuiltinFnIdFence: { @@ -3773,7 +3859,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_fence(irb, scope, node, arg0_value, AtomicOrderUnordered); + IrInstruction *fence = ir_build_fence(irb, scope, node, arg0_value, AtomicOrderUnordered); + return ir_lval_wrap(irb, scope, fence, lval); } case BuiltinFnIdDivExact: { @@ -3787,7 +3874,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpDivExact, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivExact, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdDivTrunc: { @@ -3801,7 +3889,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpDivTrunc, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivTrunc, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdDivFloor: { @@ -3815,7 +3904,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpDivFloor, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpDivFloor, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdRem: { @@ -3829,7 +3919,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpRemRem, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpRemRem, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdMod: { @@ -3843,7 +3934,23 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpRemMod, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpRemMod, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); + } + case BuiltinFnIdSqrt: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + IrInstruction *ir_sqrt = ir_build_sqrt(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, ir_sqrt, lval); } case BuiltinFnIdTruncate: { @@ -3857,7 +3964,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_truncate(irb, scope, node, arg0_value, arg1_value); + IrInstruction *truncate = ir_build_truncate(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, truncate, lval); } case BuiltinFnIdIntType: { @@ -3871,7 +3979,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_int_type(irb, scope, node, arg0_value, arg1_value); + IrInstruction *int_type = ir_build_int_type(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, int_type, lval); } case BuiltinFnIdMemcpy: { @@ -3890,7 +3999,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg2_value == irb->codegen->invalid_instruction) return arg2_value; - return ir_build_memcpy(irb, scope, node, arg0_value, arg1_value, arg2_value); + IrInstruction *ir_memcpy = ir_build_memcpy(irb, scope, node, arg0_value, arg1_value, arg2_value); + return ir_lval_wrap(irb, scope, ir_memcpy, lval); } case BuiltinFnIdMemset: { @@ -3909,7 +4019,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg2_value == irb->codegen->invalid_instruction) return arg2_value; - return ir_build_memset(irb, scope, node, arg0_value, arg1_value, arg2_value); + IrInstruction *ir_memset = ir_build_memset(irb, scope, node, arg0_value, arg1_value, arg2_value); + return ir_lval_wrap(irb, scope, ir_memset, lval); } case BuiltinFnIdMemberCount: { @@ -3918,7 +4029,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_member_count(irb, scope, node, arg0_value); + IrInstruction *member_count = ir_build_member_count(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, member_count, lval); } case BuiltinFnIdMemberType: { @@ -3933,7 +4045,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return arg1_value; - return ir_build_member_type(irb, scope, node, arg0_value, arg1_value); + IrInstruction *member_type = ir_build_member_type(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, member_type, lval); } case BuiltinFnIdMemberName: { @@ -3948,14 +4061,34 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return arg1_value; - return ir_build_member_name(irb, scope, node, arg0_value, arg1_value); + IrInstruction *member_name = ir_build_member_name(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, member_name, lval); + } + case BuiltinFnIdField: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node_extra(irb, arg0_node, scope, LVAL_PTR); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + IrInstruction *ptr_instruction = ir_build_field_ptr_instruction(irb, scope, node, arg0_value, arg1_value); + + if (lval.is_ptr) + return ptr_instruction; + + return ir_build_load_ptr(irb, scope, node, ptr_instruction); } case BuiltinFnIdBreakpoint: - return ir_build_breakpoint(irb, scope, node); + return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval); case BuiltinFnIdReturnAddress: - return ir_build_return_address(irb, scope, node); + return ir_lval_wrap(irb, scope, ir_build_return_address(irb, scope, node), lval); case BuiltinFnIdFrameAddress: - return ir_build_frame_address(irb, scope, node); + return ir_lval_wrap(irb, scope, ir_build_frame_address(irb, scope, node), lval); case BuiltinFnIdAlignOf: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); @@ -3963,16 +4096,17 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_align_of(irb, scope, node, arg0_value); + IrInstruction *align_of = ir_build_align_of(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, align_of, lval); } case BuiltinFnIdAddWithOverflow: - return ir_gen_overflow_op(irb, scope, node, IrOverflowOpAdd); + return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpAdd), lval); case BuiltinFnIdSubWithOverflow: - return ir_gen_overflow_op(irb, scope, node, IrOverflowOpSub); + return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpSub), lval); case BuiltinFnIdMulWithOverflow: - return ir_gen_overflow_op(irb, scope, node, IrOverflowOpMul); + return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpMul), lval); case BuiltinFnIdShlWithOverflow: - return ir_gen_overflow_op(irb, scope, node, IrOverflowOpShl); + return ir_lval_wrap(irb, scope, ir_gen_overflow_op(irb, scope, node, IrOverflowOpShl), lval); case BuiltinFnIdTypeName: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); @@ -3980,7 +4114,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_type_name(irb, scope, node, arg0_value); + IrInstruction *type_name = ir_build_type_name(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, type_name, lval); } case BuiltinFnIdCanImplicitCast: { @@ -3994,7 +4129,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_can_implicit_cast(irb, scope, node, arg0_value, arg1_value); + IrInstruction *can_implicit_cast = ir_build_can_implicit_cast(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, can_implicit_cast, lval); } case BuiltinFnIdPanic: { @@ -4003,7 +4139,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_panic(irb, scope, node, arg0_value); + IrInstruction *panic = ir_build_panic(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, panic, lval); } case BuiltinFnIdPtrCast: { @@ -4017,7 +4154,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_ptr_cast(irb, scope, node, arg0_value, arg1_value); + IrInstruction *ptr_cast = ir_build_ptr_cast(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, ptr_cast, lval); } case BuiltinFnIdBitCast: { @@ -4031,7 +4169,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bit_cast(irb, scope, node, arg0_value, arg1_value); + IrInstruction *bit_cast = ir_build_bit_cast(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, bit_cast, lval); } case BuiltinFnIdIntToPtr: { @@ -4045,7 +4184,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_int_to_ptr(irb, scope, node, arg0_value, arg1_value); + IrInstruction *int_to_ptr = ir_build_int_to_ptr(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, int_to_ptr, lval); } case BuiltinFnIdPtrToInt: { @@ -4054,7 +4194,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_ptr_to_int(irb, scope, node, arg0_value); + IrInstruction *ptr_to_int = ir_build_ptr_to_int(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, ptr_to_int, lval); } case BuiltinFnIdTagName: { @@ -4064,7 +4205,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return arg0_value; IrInstruction *actual_tag = ir_build_union_tag(irb, scope, node, arg0_value); - return ir_build_tag_name(irb, scope, node, actual_tag); + IrInstruction *tag_name = ir_build_tag_name(irb, scope, node, actual_tag); + return ir_lval_wrap(irb, scope, tag_name, lval); } case BuiltinFnIdTagType: { @@ -4073,7 +4215,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_tag_type(irb, scope, node, arg0_value); + IrInstruction *tag_type = ir_build_tag_type(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, tag_type, lval); } case BuiltinFnIdFieldParentPtr: { @@ -4092,7 +4235,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg2_value == irb->codegen->invalid_instruction) return arg2_value; - return ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr); + IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr); + return ir_lval_wrap(irb, scope, field_parent_ptr, lval); } case BuiltinFnIdOffsetOf: { @@ -4106,7 +4250,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_offset_of(irb, scope, node, arg0_value, arg1_value); + IrInstruction *offset_of = ir_build_offset_of(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, offset_of, lval); } case BuiltinFnIdInlineCall: case BuiltinFnIdNoInlineCall: @@ -4132,7 +4277,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo } FnInline fn_inline = (builtin_fn->id == BuiltinFnIdInlineCall) ? FnInlineAlways : FnInlineNever; - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr); + IrInstruction *call = ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, fn_inline, false, nullptr); + return ir_lval_wrap(irb, scope, call, lval); } case BuiltinFnIdTypeId: { @@ -4141,7 +4287,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_type_id(irb, scope, node, arg0_value); + IrInstruction *type_id = ir_build_type_id(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, type_id, lval); } case BuiltinFnIdShlExact: { @@ -4155,7 +4302,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpBitShiftLeftExact, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpBitShiftLeftExact, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdShrExact: { @@ -4169,7 +4317,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_bin_op(irb, scope, node, IrBinOpBitShiftRightExact, arg0_value, arg1_value, true); + IrInstruction *bin_op = ir_build_bin_op(irb, scope, node, IrBinOpBitShiftRightExact, arg0_value, arg1_value, true); + return ir_lval_wrap(irb, scope, bin_op, lval); } case BuiltinFnIdSetEvalBranchQuota: { @@ -4178,7 +4327,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_set_eval_branch_quota(irb, scope, node, arg0_value); + IrInstruction *set_eval_branch_quota = ir_build_set_eval_branch_quota(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, set_eval_branch_quota, lval); } case BuiltinFnIdAlignCast: { @@ -4192,10 +4342,14 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_align_cast(irb, scope, node, arg0_value, arg1_value); + IrInstruction *align_cast = ir_build_align_cast(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, align_cast, lval); } case BuiltinFnIdOpaqueType: - return ir_build_opaque_type(irb, scope, node); + { + IrInstruction *opaque_type = ir_build_opaque_type(irb, scope, node); + return ir_lval_wrap(irb, scope, opaque_type, lval); + } case BuiltinFnIdSetAlignStack: { AstNode *arg0_node = node->data.fn_call_expr.params.at(0); @@ -4203,7 +4357,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg0_value == irb->codegen->invalid_instruction) return arg0_value; - return ir_build_set_align_stack(irb, scope, node, arg0_value); + IrInstruction *set_align_stack = ir_build_set_align_stack(irb, scope, node, arg0_value); + return ir_lval_wrap(irb, scope, set_align_stack, lval); } case BuiltinFnIdArgType: { @@ -4217,7 +4372,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg1_value == irb->codegen->invalid_instruction) return arg1_value; - return ir_build_arg_type(irb, scope, node, arg0_value, arg1_value); + IrInstruction *arg_type = ir_build_arg_type(irb, scope, node, arg0_value, arg1_value); + return ir_lval_wrap(irb, scope, arg_type, lval); } case BuiltinFnIdExport: { @@ -4236,11 +4392,13 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo if (arg2_value == irb->codegen->invalid_instruction) return arg2_value; - return ir_build_export(irb, scope, node, arg0_value, arg1_value, arg2_value); + IrInstruction *ir_export = ir_build_export(irb, scope, node, arg0_value, arg1_value, arg2_value); + return ir_lval_wrap(irb, scope, ir_export, lval); } case BuiltinFnIdErrorReturnTrace: { - return ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::Null); + IrInstruction *error_return_trace = ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::Null); + return ir_lval_wrap(irb, scope, error_return_trace, lval); } case BuiltinFnIdAtomicRmw: { @@ -4274,15 +4432,36 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo // these 2 values don't mean anything since we passed non-null values for other args AtomicRmwOp_xchg, AtomicOrderMonotonic); } + case BuiltinFnIdAtomicLoad: + { + AstNode *arg0_node = node->data.fn_call_expr.params.at(0); + IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope); + if (arg0_value == irb->codegen->invalid_instruction) + return arg0_value; + + AstNode *arg1_node = node->data.fn_call_expr.params.at(1); + IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); + if (arg1_value == irb->codegen->invalid_instruction) + return arg1_value; + + AstNode *arg2_node = node->data.fn_call_expr.params.at(2); + IrInstruction *arg2_value = ir_gen_node(irb, arg2_node, scope); + if (arg2_value == irb->codegen->invalid_instruction) + return arg2_value; + + return ir_build_atomic_load(irb, scope, node, arg0_value, arg1_value, arg2_value, + // this value does not mean anything since we passed non-null values for other arg + AtomicOrderMonotonic); + } } zig_unreachable(); } -static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node) { +static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) { assert(node->type == NodeTypeFnCallExpr); if (node->data.fn_call_expr.is_builtin) - return ir_gen_builtin_fn_call(irb, scope, node); + return ir_gen_builtin_fn_call(irb, scope, node, lval); AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr; IrInstruction *fn_ref = ir_gen_node(irb, fn_ref_node, scope); @@ -4308,7 +4487,8 @@ static IrInstruction *ir_gen_fn_call(IrBuilder *irb, Scope *scope, AstNode *node } } - return ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator); + IrInstruction *fn_call = ir_build_call(irb, scope, node, nullptr, fn_ref, arg_count, args, false, FnInlineAuto, is_async, async_allocator); + return ir_lval_wrap(irb, scope, fn_call, lval); } static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode *node) { @@ -5591,6 +5771,15 @@ static IrInstruction *ir_gen_return_from_block(IrBuilder *irb, Scope *break_scop return ir_build_br(irb, break_scope, node, dest_block, is_comptime); } +static IrInstruction *ir_gen_break_from_suspend(IrBuilder *irb, Scope *break_scope, AstNode *node, ScopeSuspend *suspend_scope) { + IrInstruction *is_comptime = ir_build_const_bool(irb, break_scope, node, false); + + IrBasicBlock *dest_block = suspend_scope->resume_block; + ir_gen_defers_for_block(irb, break_scope, dest_block->scope, false); + + return ir_build_br(irb, break_scope, node, dest_block, is_comptime); +} + static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode *node) { assert(node->type == NodeTypeBreak); @@ -5630,6 +5819,13 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode * assert(this_block_scope->end_block != nullptr); return ir_gen_return_from_block(irb, break_scope, node, this_block_scope); } + } else if (search_scope->id == ScopeIdSuspend) { + ScopeSuspend *this_suspend_scope = (ScopeSuspend *)search_scope; + if (node->data.break_expr.name != nullptr && + (this_suspend_scope->name != nullptr && buf_eql_buf(node->data.break_expr.name, this_suspend_scope->name))) + { + return ir_gen_break_from_suspend(irb, break_scope, node, this_suspend_scope); + } } search_scope = search_scope->parent; } @@ -6216,14 +6412,26 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod ScopeDeferExpr *scope_defer_expr = get_scope_defer_expr(parent_scope); if (scope_defer_expr) { if (!scope_defer_expr->reported_err) { - add_node_error(irb->codegen, node, buf_sprintf("cannot suspend inside defer expression")); + ErrorMsg *msg = add_node_error(irb->codegen, node, buf_sprintf("cannot suspend inside defer expression")); + add_error_note(irb->codegen, msg, scope_defer_expr->base.source_node, buf_sprintf("defer here")); scope_defer_expr->reported_err = true; } return irb->codegen->invalid_instruction; } + ScopeSuspend *existing_suspend_scope = get_scope_suspend(parent_scope); + if (existing_suspend_scope) { + if (!existing_suspend_scope->reported_err) { + ErrorMsg *msg = add_node_error(irb->codegen, node, buf_sprintf("cannot suspend inside suspend block")); + add_error_note(irb->codegen, msg, existing_suspend_scope->base.source_node, buf_sprintf("other suspend block here")); + existing_suspend_scope->reported_err = true; + } + return irb->codegen->invalid_instruction; + } Scope *outer_scope = irb->exec->begin_scope; + IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); + IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); IrInstruction *suspend_code; IrInstruction *const_bool_false = ir_build_const_bool(irb, parent_scope, node, false); @@ -6242,28 +6450,28 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod } else { child_scope = parent_scope; } + ScopeSuspend *suspend_scope = create_suspend_scope(node, child_scope); + suspend_scope->resume_block = resume_block; + child_scope = &suspend_scope->base; IrInstruction *save_token = ir_build_coro_save(irb, child_scope, node, irb->exec->coro_handle); ir_gen_node(irb, node->data.suspend.block, child_scope); - suspend_code = ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false); + suspend_code = ir_mark_gen(ir_build_coro_suspend(irb, parent_scope, node, save_token, const_bool_false)); } - IrBasicBlock *cleanup_block = ir_create_basic_block(irb, parent_scope, "SuspendCleanup"); - IrBasicBlock *resume_block = ir_create_basic_block(irb, parent_scope, "SuspendResume"); - IrInstructionSwitchBrCase *cases = allocate(2); - cases[0].value = ir_build_const_u8(irb, parent_scope, node, 0); + cases[0].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 0)); cases[0].block = resume_block; - cases[1].value = ir_build_const_u8(irb, parent_scope, node, 1); + cases[1].value = ir_mark_gen(ir_build_const_u8(irb, parent_scope, node, 1)); cases[1].block = cleanup_block; - ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, - 2, cases, const_bool_false); + ir_mark_gen(ir_build_switch_br(irb, parent_scope, node, suspend_code, irb->exec->coro_suspend_block, + 2, cases, const_bool_false)); ir_set_cursor_at_end_and_append_block(irb, cleanup_block); ir_gen_defers_for_block(irb, parent_scope, outer_scope, true); ir_mark_gen(ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false)); ir_set_cursor_at_end_and_append_block(irb, resume_block); - return ir_build_const_void(irb, parent_scope, node); + return ir_mark_gen(ir_build_const_void(irb, parent_scope, node)); } static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scope, @@ -6296,7 +6504,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop case NodeTypeSymbol: return ir_gen_symbol(irb, scope, node, lval); case NodeTypeFnCallExpr: - return ir_lval_wrap(irb, scope, ir_gen_fn_call(irb, scope, node), lval); + return ir_gen_fn_call(irb, scope, node, lval); case NodeTypeIfBoolExpr: return ir_lval_wrap(irb, scope, ir_gen_if_bool_expr(irb, scope, node), lval); case NodeTypePrefixOpExpr: @@ -6316,7 +6524,15 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop case NodeTypeReturnExpr: return ir_gen_return(irb, scope, node, lval); case NodeTypeFieldAccessExpr: - return ir_gen_field_access(irb, scope, node, lval); + { + IrInstruction *ptr_instruction = ir_gen_field_access(irb, scope, node); + if (ptr_instruction == irb->codegen->invalid_instruction) + return ptr_instruction; + if (lval.is_ptr) + return ptr_instruction; + + return ir_build_load_ptr(irb, scope, node, ptr_instruction); + } case NodeTypeThisLiteral: return ir_lval_wrap(irb, scope, ir_gen_this_literal(irb, scope, node), lval); case NodeTypeBoolLiteral: @@ -11395,7 +11611,19 @@ static TypeTableEntry *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstruc } break; case VarClassRequiredAny: - // OK + if (casted_init_value->value.special == ConstValSpecialStatic && + casted_init_value->value.type->id == TypeTableEntryIdFn && + casted_init_value->value.data.x_ptr.data.fn.fn_entry->fn_inline == FnInlineAlways) + { + var_class_requires_const = true; + if (!var->src_is_const && !is_comptime_var) { + ErrorMsg *msg = ir_add_error_node(ira, source_node, + buf_sprintf("functions marked inline must be stored in const or comptime var")); + AstNode *proto_node = casted_init_value->value.data.x_ptr.data.fn.fn_entry->proto_node; + add_error_note(ira->codegen, msg, proto_node, buf_sprintf("declared here")); + result_type = ira->codegen->builtin_types.entry_invalid; + } + } break; } } @@ -13332,7 +13560,15 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru zig_unreachable(); } - Buf *field_name = field_ptr_instruction->field_name; + Buf *field_name = field_ptr_instruction->field_name_buffer; + if (!field_name) { + IrInstruction *field_name_expr = field_ptr_instruction->field_name_expr->other; + field_name = ir_resolve_str(ira, field_name_expr); + if (!field_name) + return ira->codegen->builtin_types.entry_invalid; + } + + AstNode *source_node = field_ptr_instruction->base.source_node; if (type_is_invalid(container_type)) { @@ -14549,7 +14785,10 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira, return out_val->type; } - assert(target_value_ptr->value.type->id == TypeTableEntryIdPointer); + if (target_value_ptr->value.type->id != TypeTableEntryIdPointer) { + ir_add_error(ira, target_value_ptr, buf_sprintf("invalid deref on switch target")); + return ira->codegen->builtin_types.entry_invalid; + } TypeTableEntry *target_type = target_value_ptr->value.type->data.pointer.child_type; ConstExprValue *pointee_val = nullptr; @@ -15325,6 +15564,8 @@ static TypeTableEntry *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira, } ensure_complete_type(ira->codegen, container_type); + if (type_is_invalid(container_type)) + return ira->codegen->builtin_types.entry_invalid; TypeStructField *field = find_struct_type_field(container_type, field_name); if (field == nullptr) { @@ -15647,10 +15888,20 @@ static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstr } static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstructionCmpxchg *instruction) { + TypeTableEntry *operand_type = ir_resolve_atomic_operand_type(ira, instruction->type_value->other); + if (type_is_invalid(operand_type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *ptr = instruction->ptr->other; if (type_is_invalid(ptr->value.type)) return ira->codegen->builtin_types.entry_invalid; + // TODO let this be volatile + TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, operand_type, false); + IrInstruction *casted_ptr = ir_implicit_cast(ira, ptr, ptr_type); + if (type_is_invalid(casted_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *cmp_value = instruction->cmp_value->other; if (type_is_invalid(cmp_value->value.type)) return ira->codegen->builtin_types.entry_invalid; @@ -15675,28 +15926,11 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct if (!ir_resolve_atomic_order(ira, failure_order_value, &failure_order)) return ira->codegen->builtin_types.entry_invalid; - if (ptr->value.type->id != TypeTableEntryIdPointer) { - ir_add_error(ira, instruction->ptr, - buf_sprintf("expected pointer argument, found '%s'", buf_ptr(&ptr->value.type->name))); - return ira->codegen->builtin_types.entry_invalid; - } - - TypeTableEntry *child_type = ptr->value.type->data.pointer.child_type; - - uint32_t align_bytes = ptr->value.type->data.pointer.alignment; - uint64_t size_bytes = type_size(ira->codegen, child_type); - if (align_bytes < size_bytes) { - ir_add_error(ira, instruction->ptr, - buf_sprintf("expected pointer alignment of at least %" ZIG_PRI_u64 ", found %" PRIu32, - size_bytes, align_bytes)); - return ira->codegen->builtin_types.entry_invalid; - } - - IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, child_type); + IrInstruction *casted_cmp_value = ir_implicit_cast(ira, cmp_value, operand_type); if (type_is_invalid(casted_cmp_value->value.type)) return ira->codegen->builtin_types.entry_invalid; - IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, child_type); + IrInstruction *casted_new_value = ir_implicit_cast(ira, new_value, operand_type); if (type_is_invalid(casted_new_value->value.type)) return ira->codegen->builtin_types.entry_invalid; @@ -15721,9 +15955,17 @@ static TypeTableEntry *ir_analyze_instruction_cmpxchg(IrAnalyze *ira, IrInstruct return ira->codegen->builtin_types.entry_invalid; } - ir_build_cmpxchg_from(&ira->new_irb, &instruction->base, ptr, casted_cmp_value, casted_new_value, - success_order_value, failure_order_value, success_order, failure_order); - return ira->codegen->builtin_types.entry_bool; + if (instr_is_comptime(casted_ptr) && instr_is_comptime(casted_cmp_value) && instr_is_comptime(casted_new_value)) { + zig_panic("TODO compile-time execution of cmpxchg"); + } + + IrInstruction *result = ir_build_cmpxchg(&ira->new_irb, instruction->base.scope, instruction->base.source_node, + nullptr, casted_ptr, casted_cmp_value, casted_new_value, nullptr, nullptr, instruction->is_weak, + operand_type, success_order, failure_order); + result->value.type = get_maybe_type(ira->codegen, operand_type); + ir_link_new_instruction(result, &instruction->base); + ir_add_alloca(ira, result, result->value.type); + return result->value.type; } static TypeTableEntry *ir_analyze_instruction_fence(IrAnalyze *ira, IrInstructionFence *instruction) { @@ -17857,39 +18099,48 @@ static TypeTableEntry *ir_analyze_instruction_coro_alloc_helper(IrAnalyze *ira, return result->value.type; } -static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstructionAtomicRmw *instruction) { - TypeTableEntry *operand_type = ir_resolve_type(ira, instruction->operand_type->other); - if (type_is_invalid(operand_type)) { +static TypeTableEntry *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op) { + TypeTableEntry *operand_type = ir_resolve_type(ira, op); + if (type_is_invalid(operand_type)) return ira->codegen->builtin_types.entry_invalid; - } + if (operand_type->id == TypeTableEntryIdInt) { if (operand_type->data.integral.bit_count < 8) { - ir_add_error(ira, &instruction->base, + ir_add_error(ira, op, buf_sprintf("expected integer type 8 bits or larger, found %" PRIu32 "-bit integer type", operand_type->data.integral.bit_count)); return ira->codegen->builtin_types.entry_invalid; } if (operand_type->data.integral.bit_count > ira->codegen->pointer_size_bytes * 8) { - ir_add_error(ira, &instruction->base, + ir_add_error(ira, op, buf_sprintf("expected integer type pointer size or smaller, found %" PRIu32 "-bit integer type", operand_type->data.integral.bit_count)); return ira->codegen->builtin_types.entry_invalid; } if (!is_power_of_2(operand_type->data.integral.bit_count)) { - ir_add_error(ira, &instruction->base, + ir_add_error(ira, op, buf_sprintf("%" PRIu32 "-bit integer type is not a power of 2", operand_type->data.integral.bit_count)); return ira->codegen->builtin_types.entry_invalid; } } else if (get_codegen_ptr_type(operand_type) == nullptr) { - ir_add_error(ira, &instruction->base, + ir_add_error(ira, op, buf_sprintf("expected integer or pointer type, found '%s'", buf_ptr(&operand_type->name))); return ira->codegen->builtin_types.entry_invalid; } + return operand_type; +} + +static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstructionAtomicRmw *instruction) { + TypeTableEntry *operand_type = ir_resolve_atomic_operand_type(ira, instruction->operand_type->other); + if (type_is_invalid(operand_type)) + return ira->codegen->builtin_types.entry_invalid; + IrInstruction *ptr_inst = instruction->ptr->other; if (type_is_invalid(ptr_inst->value.type)) return ira->codegen->builtin_types.entry_invalid; + // TODO let this be volatile TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, operand_type, false); IrInstruction *casted_ptr = ir_implicit_cast(ira, ptr_inst, ptr_type); if (type_is_invalid(casted_ptr->value.type)) @@ -17933,6 +18184,49 @@ static TypeTableEntry *ir_analyze_instruction_atomic_rmw(IrAnalyze *ira, IrInstr return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_atomic_load(IrAnalyze *ira, IrInstructionAtomicLoad *instruction) { + TypeTableEntry *operand_type = ir_resolve_atomic_operand_type(ira, instruction->operand_type->other); + if (type_is_invalid(operand_type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *ptr_inst = instruction->ptr->other; + if (type_is_invalid(ptr_inst->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, operand_type, true); + IrInstruction *casted_ptr = ir_implicit_cast(ira, ptr_inst, ptr_type); + if (type_is_invalid(casted_ptr->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + AtomicOrder ordering; + if (instruction->ordering == nullptr) { + ordering = instruction->resolved_ordering; + } else { + if (!ir_resolve_atomic_order(ira, instruction->ordering->other, &ordering)) + return ira->codegen->builtin_types.entry_invalid; + } + + if (ordering == AtomicOrderRelease || ordering == AtomicOrderAcqRel) { + assert(instruction->ordering != nullptr); + ir_add_error(ira, instruction->ordering, + buf_sprintf("@atomicLoad atomic ordering must not be Release or AcqRel")); + return ira->codegen->builtin_types.entry_invalid; + } + + if (instr_is_comptime(casted_ptr)) { + IrInstruction *result = ir_get_deref(ira, &instruction->base, casted_ptr); + ir_link_new_instruction(result, &instruction->base); + assert(result->value.type != nullptr); + return result->value.type; + } + + IrInstruction *result = ir_build_atomic_load(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, nullptr, casted_ptr, nullptr, ordering); + ir_link_new_instruction(result, &instruction->base); + result->value.type = operand_type; + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_promise_result_type(IrAnalyze *ira, IrInstructionPromiseResultType *instruction) { TypeTableEntry *promise_type = ir_resolve_type(ira, instruction->promise_type->other); if (type_is_invalid(promise_type)) @@ -18019,6 +18313,68 @@ static TypeTableEntry *ir_analyze_instruction_mark_err_ret_trace_ptr(IrAnalyze * return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_sqrt(IrAnalyze *ira, IrInstructionSqrt *instruction) { + TypeTableEntry *float_type = ir_resolve_type(ira, instruction->type->other); + if (type_is_invalid(float_type)) + return ira->codegen->builtin_types.entry_invalid; + + IrInstruction *op = instruction->op->other; + if (type_is_invalid(op->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + bool ok_type = float_type->id == TypeTableEntryIdNumLitFloat || float_type->id == TypeTableEntryIdFloat; + if (!ok_type) { + ir_add_error(ira, instruction->type, buf_sprintf("@sqrt does not support type '%s'", buf_ptr(&float_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + + IrInstruction *casted_op = ir_implicit_cast(ira, op, float_type); + if (type_is_invalid(casted_op->value.type)) + return ira->codegen->builtin_types.entry_invalid; + + if (instr_is_comptime(casted_op)) { + ConstExprValue *val = ir_resolve_const(ira, casted_op, UndefBad); + if (!val) + return ira->codegen->builtin_types.entry_invalid; + + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); + + if (float_type->id == TypeTableEntryIdNumLitFloat) { + bigfloat_sqrt(&out_val->data.x_bigfloat, &val->data.x_bigfloat); + } else if (float_type->id == TypeTableEntryIdFloat) { + switch (float_type->data.floating.bit_count) { + case 32: + out_val->data.x_f32 = sqrtf(val->data.x_f32); + break; + case 64: + out_val->data.x_f64 = sqrt(val->data.x_f64); + break; + case 128: + f128M_sqrt(&val->data.x_f128, &out_val->data.x_f128); + break; + default: + zig_unreachable(); + } + } else { + zig_unreachable(); + } + + return float_type; + } + + assert(float_type->id == TypeTableEntryIdFloat); + if (float_type->data.floating.bit_count != 32 && float_type->data.floating.bit_count != 64) { + ir_add_error(ira, instruction->type, buf_sprintf("compiler TODO: add implementation of sqrt for '%s'", buf_ptr(&float_type->name))); + return ira->codegen->builtin_types.entry_invalid; + } + + IrInstruction *result = ir_build_sqrt(&ira->new_irb, instruction->base.scope, + instruction->base.source_node, nullptr, casted_op); + ir_link_new_instruction(result, &instruction->base); + result->value.type = float_type; + return result->value.type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -18254,6 +18610,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_coro_alloc_helper(ira, (IrInstructionCoroAllocHelper *)instruction); case IrInstructionIdAtomicRmw: return ir_analyze_instruction_atomic_rmw(ira, (IrInstructionAtomicRmw *)instruction); + case IrInstructionIdAtomicLoad: + return ir_analyze_instruction_atomic_load(ira, (IrInstructionAtomicLoad *)instruction); case IrInstructionIdPromiseResultType: return ir_analyze_instruction_promise_result_type(ira, (IrInstructionPromiseResultType *)instruction); case IrInstructionIdAwaitBookkeeping: @@ -18266,6 +18624,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_merge_err_ret_traces(ira, (IrInstructionMergeErrRetTraces *)instruction); case IrInstructionIdMarkErrRetTracePtr: return ir_analyze_instruction_mark_err_ret_trace_ptr(ira, (IrInstructionMarkErrRetTracePtr *)instruction); + case IrInstructionIdSqrt: + return ir_analyze_instruction_sqrt(ira, (IrInstructionSqrt *)instruction); } zig_unreachable(); } @@ -18395,6 +18755,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdAddImplicitReturnType: case IrInstructionIdMergeErrRetTraces: case IrInstructionIdMarkErrRetTracePtr: + case IrInstructionIdAtomicRmw: return true; case IrInstructionIdPhi: @@ -18475,9 +18836,10 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdCoroSize: case IrInstructionIdCoroSuspend: case IrInstructionIdCoroFree: - case IrInstructionIdAtomicRmw: case IrInstructionIdCoroPromise: case IrInstructionIdPromiseResultType: + case IrInstructionIdSqrt: + case IrInstructionIdAtomicLoad: return false; case IrInstructionIdAsm: diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 99f79ff75e..a77ae244d4 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -358,9 +358,18 @@ static void ir_print_ptr_type_child(IrPrint *irp, IrInstructionPtrTypeChild *ins } static void ir_print_field_ptr(IrPrint *irp, IrInstructionFieldPtr *instruction) { - fprintf(irp->f, "fieldptr "); - ir_print_other_instruction(irp, instruction->container_ptr); - fprintf(irp->f, ".%s", buf_ptr(instruction->field_name)); + if (instruction->field_name_buffer) { + fprintf(irp->f, "fieldptr "); + ir_print_other_instruction(irp, instruction->container_ptr); + fprintf(irp->f, ".%s", buf_ptr(instruction->field_name_buffer)); + } else { + assert(instruction->field_name_expr); + fprintf(irp->f, "@field("); + ir_print_other_instruction(irp, instruction->container_ptr); + fprintf(irp->f, ", "); + ir_print_other_instruction(irp, instruction->field_name_expr); + fprintf(irp->f, ")"); + } } static void ir_print_struct_field_ptr(IrPrint *irp, IrInstructionStructFieldPtr *instruction) { @@ -1172,6 +1181,24 @@ static void ir_print_atomic_rmw(IrPrint *irp, IrInstructionAtomicRmw *instructio fprintf(irp->f, ")"); } +static void ir_print_atomic_load(IrPrint *irp, IrInstructionAtomicLoad *instruction) { + fprintf(irp->f, "@atomicLoad("); + if (instruction->operand_type != nullptr) { + ir_print_other_instruction(irp, instruction->operand_type); + } else { + fprintf(irp->f, "[TODO print]"); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->ptr); + fprintf(irp->f, ","); + if (instruction->ordering != nullptr) { + ir_print_other_instruction(irp, instruction->ordering); + } else { + fprintf(irp->f, "[TODO print]"); + } + fprintf(irp->f, ")"); +} + static void ir_print_await_bookkeeping(IrPrint *irp, IrInstructionAwaitBookkeeping *instruction) { fprintf(irp->f, "@awaitBookkeeping("); ir_print_other_instruction(irp, instruction->promise_result_type); @@ -1204,6 +1231,18 @@ static void ir_print_mark_err_ret_trace_ptr(IrPrint *irp, IrInstructionMarkErrRe fprintf(irp->f, ")"); } +static void ir_print_sqrt(IrPrint *irp, IrInstructionSqrt *instruction) { + fprintf(irp->f, "@sqrt("); + if (instruction->type != nullptr) { + ir_print_other_instruction(irp, instruction->type); + } else { + fprintf(irp->f, "null"); + } + fprintf(irp->f, ","); + ir_print_other_instruction(irp, instruction->op); + fprintf(irp->f, ")"); +} + static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { ir_print_prefix(irp, instruction); switch (instruction->id) { @@ -1590,6 +1629,12 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) { case IrInstructionIdMarkErrRetTracePtr: ir_print_mark_err_ret_trace_ptr(irp, (IrInstructionMarkErrRetTracePtr *)instruction); break; + case IrInstructionIdSqrt: + ir_print_sqrt(irp, (IrInstructionSqrt *)instruction); + break; + case IrInstructionIdAtomicLoad: + ir_print_atomic_load(irp, (IrInstructionAtomicLoad *)instruction); + break; } fprintf(irp->f, "\n"); } diff --git a/src/link.cpp b/src/link.cpp index 3c6e27e331..d454d77aae 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -217,6 +217,9 @@ static void construct_linker_job_elf(LinkJob *lj) { lj->args.append(g->linker_script); } + if (g->no_rosegment_workaround) { + lj->args.append("--no-rosegment"); + } lj->args.append("--gc-sections"); lj->args.append("-m"); diff --git a/src/main.cpp b/src/main.cpp index 63b077e833..9c36f9b091 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -43,6 +43,7 @@ static int usage(const char *arg0) { " --pkg-end pop current pkg\n" " --release-fast build with optimizations on and safety off\n" " --release-safe build with optimizations on and safety on\n" + " --release-small build with size optimizations on and safety off\n" " --static output will be statically linked\n" " --strip exclude debug symbols\n" " --target-arch [name] specify target architecture\n" @@ -54,7 +55,6 @@ static int usage(const char *arg0) { " --verbose-ir turn on compiler debug output for Zig IR\n" " --verbose-llvm-ir turn on compiler debug output for LLVM IR\n" " --verbose-cimport turn on compiler debug output for C imports\n" - " --zig-install-prefix [path] override directory where zig thinks it is installed\n" " -dirafter [dir] same as -isystem but do it last\n" " -isystem [dir] add additional search path for other .h files\n" " -mllvm [arg] additional arguments to forward to LLVM's option processing\n" @@ -74,6 +74,7 @@ static int usage(const char *arg0) { " -L[dir] alias for --library-path\n" " -rdynamic add all symbols to the dynamic symbol table\n" " -rpath [path] add directory to the runtime library search path\n" + " --no-rosegment compromise security to workaround valgrind bug\n" " -mconsole (windows) --subsystem console to the linker\n" " -mwindows (windows) --subsystem windows to the linker\n" " -framework [name] (darwin) link against framework\n" @@ -177,6 +178,7 @@ static int find_zig_lib_dir(Buf *out_path) { int err; Buf self_exe_path = BUF_INIT; + buf_resize(&self_exe_path, 0); if (!(err = os_self_exe_path(&self_exe_path))) { Buf *cur_path = &self_exe_path; @@ -199,23 +201,14 @@ static int find_zig_lib_dir(Buf *out_path) { return ErrorFileNotFound; } -static Buf *resolve_zig_lib_dir(const char *zig_install_prefix_arg) { +static Buf *resolve_zig_lib_dir(void) { int err; Buf *result = buf_alloc(); - if (zig_install_prefix_arg == nullptr) { - if ((err = find_zig_lib_dir(result))) { - fprintf(stderr, "Unable to find zig lib directory. Reinstall Zig or use --zig-install-prefix.\n"); - exit(EXIT_FAILURE); - } - return result; + if ((err = find_zig_lib_dir(result))) { + fprintf(stderr, "Unable to find zig lib directory\n"); + exit(EXIT_FAILURE); } - Buf *zig_lib_dir_buf = buf_create_from_str(zig_install_prefix_arg); - if (test_zig_install_prefix(zig_lib_dir_buf, result)) { - return result; - } - - fprintf(stderr, "No Zig installation found at prefix: %s\n", zig_install_prefix_arg); - exit(EXIT_FAILURE); + return result; } enum Cmd { @@ -299,7 +292,6 @@ int main(int argc, char **argv) { const char *libc_include_dir = nullptr; const char *msvc_lib_dir = nullptr; const char *kernel32_lib_dir = nullptr; - const char *zig_install_prefix = nullptr; const char *dynamic_linker = nullptr; ZigList clang_argv = {0}; ZigList llvm_argv = {0}; @@ -333,6 +325,7 @@ int main(int argc, char **argv) { ZigList test_exec_args = {0}; int comptime_args_end = 0; int runtime_args_start = argc; + bool no_rosegment_workaround = false; if (argc >= 2 && strcmp(argv[1], "build") == 0) { const char *zig_exe_path = arg0; @@ -359,17 +352,12 @@ int main(int argc, char **argv) { } else if (i + 1 < argc && strcmp(argv[i], "--cache-dir") == 0) { cache_dir = argv[i + 1]; i += 1; - } else if (i + 1 < argc && strcmp(argv[i], "--zig-install-prefix") == 0) { - args.append(argv[i]); - i += 1; - zig_install_prefix = argv[i]; - args.append(zig_install_prefix); } else { args.append(argv[i]); } } - Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix); + Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); Buf *zig_std_dir = buf_alloc(); os_path_join(zig_lib_dir_buf, buf_create_from_str("std"), zig_std_dir); @@ -497,6 +485,8 @@ int main(int argc, char **argv) { build_mode = BuildModeFastRelease; } else if (strcmp(arg, "--release-safe") == 0) { build_mode = BuildModeSafeRelease; + } else if (strcmp(arg, "--release-small") == 0) { + build_mode = BuildModeSmallRelease; } else if (strcmp(arg, "--strip") == 0) { strip = true; } else if (strcmp(arg, "--static") == 0) { @@ -519,6 +509,8 @@ int main(int argc, char **argv) { mconsole = true; } else if (strcmp(arg, "-rdynamic") == 0) { rdynamic = true; + } else if (strcmp(arg, "--no-rosegment") == 0) { + no_rosegment_workaround = true; } else if (strcmp(arg, "--each-lib-rpath") == 0) { each_lib_rpath = true; } else if (strcmp(arg, "--enable-timing-info") == 0) { @@ -590,8 +582,6 @@ int main(int argc, char **argv) { msvc_lib_dir = argv[i]; } else if (strcmp(arg, "--kernel32-lib-dir") == 0) { kernel32_lib_dir = argv[i]; - } else if (strcmp(arg, "--zig-install-prefix") == 0) { - zig_install_prefix = argv[i]; } else if (strcmp(arg, "--dynamic-linker") == 0) { dynamic_linker = argv[i]; } else if (strcmp(arg, "-isystem") == 0) { @@ -803,7 +793,7 @@ int main(int argc, char **argv) { full_cache_dir); } - Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix); + Buf *zig_lib_dir_buf = resolve_zig_lib_dir(); CodeGen *g = codegen_create(zig_root_source_file, target, out_type, build_mode, zig_lib_dir_buf); codegen_set_out_name(g, buf_out_name); @@ -858,6 +848,7 @@ int main(int argc, char **argv) { codegen_set_windows_subsystem(g, mwindows, mconsole); codegen_set_rdynamic(g, rdynamic); + g->no_rosegment_workaround = no_rosegment_workaround; if (mmacosx_version_min && mios_version_min) { fprintf(stderr, "-mmacosx-version-min and -mios-version-min options not allowed together\n"); return EXIT_FAILURE; diff --git a/src/os.cpp b/src/os.cpp index e0491b21de..97462bd658 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1007,6 +1007,7 @@ int os_self_exe_path(Buf *out_path) { buf_resize(out_path, buf_len(out_path) * 2); continue; } + buf_resize(out_path, amt); return 0; } #endif diff --git a/src/parser.cpp b/src/parser.cpp index 2bd94033cc..4b70e904b8 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -648,12 +648,30 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc, size_t *token_index, bool m } /* -SuspendExpression(body) = "suspend" "|" Symbol "|" body +SuspendExpression(body) = option(Symbol ":") "suspend" option(("|" Symbol "|" body)) */ static AstNode *ast_parse_suspend_block(ParseContext *pc, size_t *token_index, bool mandatory) { size_t orig_token_index = *token_index; - Token *suspend_token = &pc->tokens->at(*token_index); + Token *name_token = nullptr; + Token *token = &pc->tokens->at(*token_index); + if (token->id == TokenIdSymbol) { + *token_index += 1; + Token *colon_token = &pc->tokens->at(*token_index); + if (colon_token->id == TokenIdColon) { + *token_index += 1; + name_token = token; + token = &pc->tokens->at(*token_index); + } else if (mandatory) { + ast_expect_token(pc, colon_token, TokenIdColon); + zig_unreachable(); + } else { + *token_index = orig_token_index; + return nullptr; + } + } + + Token *suspend_token = token; if (suspend_token->id == TokenIdKeywordSuspend) { *token_index += 1; } else if (mandatory) { @@ -675,6 +693,9 @@ static AstNode *ast_parse_suspend_block(ParseContext *pc, size_t *token_index, b } AstNode *node = ast_create_node(pc, NodeTypeSuspend, suspend_token); + if (name_token != nullptr) { + node->data.suspend.name = token_buf(name_token); + } node->data.suspend.promise_symbol = ast_parse_symbol(pc, token_index); ast_eat_token(pc, token_index, TokenIdBinOr); node->data.suspend.block = ast_parse_block(pc, token_index, true); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index ba915bf064..22fd409859 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -82,7 +82,7 @@ static const bool assertions_on = false; #endif bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, - const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug) + const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug, bool is_small) { std::error_code EC; raw_fd_ostream dest(filename, EC, sys::fs::F_None); @@ -101,7 +101,7 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM return true; } PMBuilder->OptLevel = target_machine->getOptLevel(); - PMBuilder->SizeLevel = 0; + PMBuilder->SizeLevel = is_small ? 2 : 0; PMBuilder->DisableTailCalls = is_debug; PMBuilder->DisableUnitAtATime = is_debug; @@ -780,10 +780,12 @@ static AtomicOrdering mapFromLLVMOrdering(LLVMAtomicOrdering Ordering) { LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef new_val, LLVMAtomicOrdering success_ordering, - LLVMAtomicOrdering failure_ordering) + LLVMAtomicOrdering failure_ordering, bool is_weak) { - return wrap(unwrap(builder)->CreateAtomicCmpXchg(unwrap(ptr), unwrap(cmp), unwrap(new_val), - mapFromLLVMOrdering(success_ordering), mapFromLLVMOrdering(failure_ordering))); + AtomicCmpXchgInst *inst = unwrap(builder)->CreateAtomicCmpXchg(unwrap(ptr), unwrap(cmp), + unwrap(new_val), mapFromLLVMOrdering(success_ordering), mapFromLLVMOrdering(failure_ordering)); + inst->setWeak(is_weak); + return wrap(inst); } LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS, diff --git a/src/zig_llvm.h b/src/zig_llvm.h index a63524281a..e26a081761 100644 --- a/src/zig_llvm.h +++ b/src/zig_llvm.h @@ -52,7 +52,7 @@ enum ZigLLVM_EmitOutputType { }; ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref, - const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug); + const char *filename, enum ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug, bool is_small); ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref); @@ -72,7 +72,7 @@ ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildMemSet(LLVMBuilderRef B, LLVMValueRef Ptr, ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp, LLVMValueRef new_val, LLVMAtomicOrdering success_ordering, - LLVMAtomicOrdering failure_ordering); + LLVMAtomicOrdering failure_ordering, bool is_weak); ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS, const char *name); diff --git a/std/build.zig b/std/build.zig index a4d745e450..a312b28a6f 100644 --- a/std/build.zig +++ b/std/build.zig @@ -426,15 +426,18 @@ pub const Builder = struct { const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false; const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false; + const release_small = self.option(bool, "release-small", "size optimizations on and safety off") ?? false; - const mode = if (release_safe and !release_fast) + const mode = if (release_safe and !release_fast and !release_small) builtin.Mode.ReleaseSafe - else if (release_fast and !release_safe) + else if (release_fast and !release_safe and !release_small) builtin.Mode.ReleaseFast - else if (!release_fast and !release_safe) + else if (release_small and !release_fast and !release_safe) + builtin.Mode.ReleaseSmall + else if (!release_fast and !release_safe and !release_small) builtin.Mode.Debug else x: { - warn("Both -Drelease-safe and -Drelease-fast specified"); + warn("Multiple release modes (of -Drelease-safe, -Drelease-fast and -Drelease-small)"); self.markInvalidUserInput(); break :x builtin.Mode.Debug; }; @@ -1229,6 +1232,7 @@ pub const LibExeObjStep = struct { builtin.Mode.Debug => {}, builtin.Mode.ReleaseSafe => zig_args.append("--release-safe") catch unreachable, builtin.Mode.ReleaseFast => zig_args.append("--release-fast") catch unreachable, + builtin.Mode.ReleaseSmall => zig_args.append("--release-small") catch unreachable, } zig_args.append("--cache-dir") catch unreachable; @@ -1369,7 +1373,7 @@ pub const LibExeObjStep = struct { args.append("ssp-buffer-size=4") catch unreachable; } }, - builtin.Mode.ReleaseFast => { + builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => { args.append("-O2") catch unreachable; args.append("-fno-stack-protector") catch unreachable; }, @@ -1706,6 +1710,7 @@ pub const TestStep = struct { builtin.Mode.Debug => {}, builtin.Mode.ReleaseSafe => try zig_args.append("--release-safe"), builtin.Mode.ReleaseFast => try zig_args.append("--release-fast"), + builtin.Mode.ReleaseSmall => try zig_args.append("--release-small"), } switch (self.target) { diff --git a/std/c/darwin.zig b/std/c/darwin.zig index feb689cdc5..b958055ae8 100644 --- a/std/c/darwin.zig +++ b/std/c/darwin.zig @@ -3,10 +3,28 @@ pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) c_int; pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: &u8, buf_len: usize, basep: &i64) usize; +pub extern "c" fn mach_absolute_time() u64; +pub extern "c" fn mach_timebase_info(tinfo: ?&mach_timebase_info_data) void; + pub use @import("../os/darwin_errno.zig"); pub const _errno = __error; +pub const timeval = extern struct { + tv_sec: isize, + tv_usec: isize, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; + +pub const mach_timebase_info_data = struct { + numer: u32, + denom: u32, +}; + /// Renamed to Stat to not conflict with the stat function. pub const Stat = extern struct { dev: i32, diff --git a/std/c/index.zig b/std/c/index.zig index 369ea2b358..cff86f4041 100644 --- a/std/c/index.zig +++ b/std/c/index.zig @@ -28,6 +28,7 @@ pub extern "c" fn unlink(path: &const u8) c_int; pub extern "c" fn getcwd(buf: &u8, size: usize) ?&u8; pub extern "c" fn waitpid(pid: c_int, stat_loc: &c_int, options: c_int) c_int; pub extern "c" fn fork() c_int; +pub extern "c" fn access(path: &const u8, mode: c_uint) c_int; pub extern "c" fn pipe(fds: &c_int) c_int; pub extern "c" fn mkdir(path: &const u8, mode: c_uint) c_int; pub extern "c" fn symlink(existing: &const u8, new: &const u8) c_int; @@ -40,6 +41,7 @@ pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int; pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize; pub extern "c" fn realpath(noalias file_name: &const u8, noalias resolved_name: &u8) ?&u8; pub extern "c" fn sigprocmask(how: c_int, noalias set: &const sigset_t, noalias oset: ?&sigset_t) c_int; +pub extern "c" fn gettimeofday(tv: ?&timeval, tz: ?&timezone) c_int; pub extern "c" fn sigaction(sig: c_int, noalias act: &const Sigaction, noalias oact: ?&Sigaction) c_int; pub extern "c" fn nanosleep(rqtp: &const timespec, rmtp: ?×pec) c_int; pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int; diff --git a/std/crypto/throughput_test.zig b/std/crypto/throughput_test.zig index 60610411b5..0756f9a4eb 100644 --- a/std/crypto/throughput_test.zig +++ b/std/crypto/throughput_test.zig @@ -1,22 +1,17 @@ // Modify the HashFunction variable to the one wanted to test. // -// NOTE: The throughput measurement may be slightly lower than other measurements since we run -// through our block alignment functions as well. Be aware when comparing against other tests. -// // ``` -// zig build-exe --release-fast --library c throughput_test.zig +// zig build-exe --release-fast throughput_test.zig // ./throughput_test // ``` -const HashFunction = @import("md5.zig").Md5; -const BytesToHash = 1024 * Mb; const std = @import("std"); +const time = std.os.time; +const Timer = time.Timer; +const HashFunction = @import("md5.zig").Md5; -const c = @cImport({ - @cInclude("time.h"); -}); - -const Mb = 1024 * 1024; +const MiB = 1024 * 1024; +const BytesToHash = 1024 * MiB; pub fn main() !void { var stdout_file = try std.io.getStdOut(); @@ -29,15 +24,15 @@ pub fn main() !void { var h = HashFunction.init(); var offset: usize = 0; - const start = c.clock(); + var timer = try Timer.start(); + const start = timer.lap(); while (offset < BytesToHash) : (offset += block.len) { h.update(block[0..]); } - const end = c.clock(); + const end = timer.read(); - const elapsed_s = f64((end - start) * c.CLOCKS_PER_SEC) / 1000000; + const elapsed_s = f64(end - start) / time.ns_per_s; const throughput = u64(BytesToHash / elapsed_s); - try stdout.print("{}: ", @typeName(HashFunction)); - try stdout.print("{} Mb/s\n", throughput); + try stdout.print("{}: {} MiB/s\n", @typeName(HashFunction), throughput / (1 * MiB)); } diff --git a/std/debug/index.zig b/std/debug/index.zig index a573dc5549..9057f157de 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -38,7 +38,7 @@ pub fn getSelfDebugInfo() !&ElfStackTrace { if (self_debug_info) |info| { return info; } else { - const info = try openSelfDebugInfo(global_allocator); + const info = try openSelfDebugInfo(getDebugInfoAllocator()); self_debug_info = info; return info; } @@ -51,7 +51,7 @@ pub fn dumpCurrentStackTrace(start_addr: ?usize) void { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; - writeCurrentStackTrace(stderr, global_allocator, debug_info, stderr_file.isTty(), start_addr) catch |err| { + writeCurrentStackTrace(stderr, getDebugInfoAllocator(), debug_info, stderr_file.isTty(), start_addr) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; @@ -64,7 +64,7 @@ pub fn dumpStackTrace(stack_trace: &const builtin.StackTrace) void { stderr.print("Unable to dump stack trace: Unable to open debug info: {}\n", @errorName(err)) catch return; return; }; - writeStackTrace(stack_trace, stderr, global_allocator, debug_info, stderr_file.isTty()) catch |err| { + writeStackTrace(stack_trace, stderr, getDebugInfoAllocator(), debug_info, stderr_file.isTty()) catch |err| { stderr.print("Unable to dump stack trace: {}\n", @errorName(err)) catch return; return; }; @@ -592,8 +592,8 @@ fn getString(st: &ElfStackTrace, offset: u64) ![]u8 { } fn readAllocBytes(allocator: &mem.Allocator, in_stream: var, size: usize) ![]u8 { - const buf = try global_allocator.alloc(u8, size); - errdefer global_allocator.free(buf); + const buf = try allocator.alloc(u8, size); + errdefer allocator.free(buf); if ((try in_stream.read(buf)) < size) return error.EndOfFile; return buf; } @@ -1126,6 +1126,21 @@ fn readILeb128(in_stream: var) !i64 { } } +/// This should only be used in temporary test programs. pub const global_allocator = &global_fixed_allocator.allocator; var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator_mem[0..]); var global_allocator_mem: [100 * 1024]u8 = undefined; + + +// TODO make thread safe +var debug_info_allocator: ?&mem.Allocator = null; +var debug_info_direct_allocator: std.heap.DirectAllocator = undefined; +var debug_info_arena_allocator: std.heap.ArenaAllocator = undefined; +fn getDebugInfoAllocator() &mem.Allocator { + if (debug_info_allocator) |a| return a; + + debug_info_direct_allocator = std.heap.DirectAllocator.init(); + debug_info_arena_allocator = std.heap.ArenaAllocator.init(&debug_info_direct_allocator.allocator); + debug_info_allocator = &debug_info_arena_allocator.allocator; + return &debug_info_arena_allocator.allocator; +} diff --git a/std/elf.zig b/std/elf.zig index 7e20fa000f..1764829bc8 100644 --- a/std/elf.zig +++ b/std/elf.zig @@ -7,6 +7,246 @@ const mem = std.mem; const debug = std.debug; const InStream = std.stream.InStream; +pub const AT_NULL = 0; +pub const AT_IGNORE = 1; +pub const AT_EXECFD = 2; +pub const AT_PHDR = 3; +pub const AT_PHENT = 4; +pub const AT_PHNUM = 5; +pub const AT_PAGESZ = 6; +pub const AT_BASE = 7; +pub const AT_FLAGS = 8; +pub const AT_ENTRY = 9; +pub const AT_NOTELF = 10; +pub const AT_UID = 11; +pub const AT_EUID = 12; +pub const AT_GID = 13; +pub const AT_EGID = 14; +pub const AT_CLKTCK = 17; +pub const AT_PLATFORM = 15; +pub const AT_HWCAP = 16; +pub const AT_FPUCW = 18; +pub const AT_DCACHEBSIZE = 19; +pub const AT_ICACHEBSIZE = 20; +pub const AT_UCACHEBSIZE = 21; +pub const AT_IGNOREPPC = 22; +pub const AT_SECURE = 23; +pub const AT_BASE_PLATFORM = 24; +pub const AT_RANDOM = 25; +pub const AT_HWCAP2 = 26; +pub const AT_EXECFN = 31; +pub const AT_SYSINFO = 32; +pub const AT_SYSINFO_EHDR = 33; +pub const AT_L1I_CACHESHAPE = 34; +pub const AT_L1D_CACHESHAPE = 35; +pub const AT_L2_CACHESHAPE = 36; +pub const AT_L3_CACHESHAPE = 37; +pub const AT_L1I_CACHESIZE = 40; +pub const AT_L1I_CACHEGEOMETRY = 41; +pub const AT_L1D_CACHESIZE = 42; +pub const AT_L1D_CACHEGEOMETRY = 43; +pub const AT_L2_CACHESIZE = 44; +pub const AT_L2_CACHEGEOMETRY = 45; +pub const AT_L3_CACHESIZE = 46; +pub const AT_L3_CACHEGEOMETRY = 47; + +pub const DT_NULL = 0; +pub const DT_NEEDED = 1; +pub const DT_PLTRELSZ = 2; +pub const DT_PLTGOT = 3; +pub const DT_HASH = 4; +pub const DT_STRTAB = 5; +pub const DT_SYMTAB = 6; +pub const DT_RELA = 7; +pub const DT_RELASZ = 8; +pub const DT_RELAENT = 9; +pub const DT_STRSZ = 10; +pub const DT_SYMENT = 11; +pub const DT_INIT = 12; +pub const DT_FINI = 13; +pub const DT_SONAME = 14; +pub const DT_RPATH = 15; +pub const DT_SYMBOLIC = 16; +pub const DT_REL = 17; +pub const DT_RELSZ = 18; +pub const DT_RELENT = 19; +pub const DT_PLTREL = 20; +pub const DT_DEBUG = 21; +pub const DT_TEXTREL = 22; +pub const DT_JMPREL = 23; +pub const DT_BIND_NOW = 24; +pub const DT_INIT_ARRAY = 25; +pub const DT_FINI_ARRAY = 26; +pub const DT_INIT_ARRAYSZ = 27; +pub const DT_FINI_ARRAYSZ = 28; +pub const DT_RUNPATH = 29; +pub const DT_FLAGS = 30; +pub const DT_ENCODING = 32; +pub const DT_PREINIT_ARRAY = 32; +pub const DT_PREINIT_ARRAYSZ = 33; +pub const DT_SYMTAB_SHNDX = 34; +pub const DT_NUM = 35; +pub const DT_LOOS = 0x6000000d; +pub const DT_HIOS = 0x6ffff000; +pub const DT_LOPROC = 0x70000000; +pub const DT_HIPROC = 0x7fffffff; +pub const DT_PROCNUM = DT_MIPS_NUM; + +pub const DT_VALRNGLO = 0x6ffffd00; +pub const DT_GNU_PRELINKED = 0x6ffffdf5; +pub const DT_GNU_CONFLICTSZ = 0x6ffffdf6; +pub const DT_GNU_LIBLISTSZ = 0x6ffffdf7; +pub const DT_CHECKSUM = 0x6ffffdf8; +pub const DT_PLTPADSZ = 0x6ffffdf9; +pub const DT_MOVEENT = 0x6ffffdfa; +pub const DT_MOVESZ = 0x6ffffdfb; +pub const DT_FEATURE_1 = 0x6ffffdfc; +pub const DT_POSFLAG_1 = 0x6ffffdfd; + +pub const DT_SYMINSZ = 0x6ffffdfe; +pub const DT_SYMINENT = 0x6ffffdff; +pub const DT_VALRNGHI = 0x6ffffdff; +pub const DT_VALNUM = 12; + +pub const DT_ADDRRNGLO = 0x6ffffe00; +pub const DT_GNU_HASH = 0x6ffffef5; +pub const DT_TLSDESC_PLT = 0x6ffffef6; +pub const DT_TLSDESC_GOT = 0x6ffffef7; +pub const DT_GNU_CONFLICT = 0x6ffffef8; +pub const DT_GNU_LIBLIST = 0x6ffffef9; +pub const DT_CONFIG = 0x6ffffefa; +pub const DT_DEPAUDIT = 0x6ffffefb; +pub const DT_AUDIT = 0x6ffffefc; +pub const DT_PLTPAD = 0x6ffffefd; +pub const DT_MOVETAB = 0x6ffffefe; +pub const DT_SYMINFO = 0x6ffffeff; +pub const DT_ADDRRNGHI = 0x6ffffeff; +pub const DT_ADDRNUM = 11; + + +pub const DT_VERSYM = 0x6ffffff0; + +pub const DT_RELACOUNT = 0x6ffffff9; +pub const DT_RELCOUNT = 0x6ffffffa; + + +pub const DT_FLAGS_1 = 0x6ffffffb; +pub const DT_VERDEF = 0x6ffffffc; + +pub const DT_VERDEFNUM = 0x6ffffffd; +pub const DT_VERNEED = 0x6ffffffe; + +pub const DT_VERNEEDNUM = 0x6fffffff; +pub const DT_VERSIONTAGNUM = 16; + + + +pub const DT_AUXILIARY = 0x7ffffffd; +pub const DT_FILTER = 0x7fffffff; +pub const DT_EXTRANUM = 3; + + +pub const DT_SPARC_REGISTER = 0x70000001; +pub const DT_SPARC_NUM = 2; + +pub const DT_MIPS_RLD_VERSION = 0x70000001; +pub const DT_MIPS_TIME_STAMP = 0x70000002; +pub const DT_MIPS_ICHECKSUM = 0x70000003; +pub const DT_MIPS_IVERSION = 0x70000004; +pub const DT_MIPS_FLAGS = 0x70000005; +pub const DT_MIPS_BASE_ADDRESS = 0x70000006; +pub const DT_MIPS_MSYM = 0x70000007; +pub const DT_MIPS_CONFLICT = 0x70000008; +pub const DT_MIPS_LIBLIST = 0x70000009; +pub const DT_MIPS_LOCAL_GOTNO = 0x7000000a; +pub const DT_MIPS_CONFLICTNO = 0x7000000b; +pub const DT_MIPS_LIBLISTNO = 0x70000010; +pub const DT_MIPS_SYMTABNO = 0x70000011; +pub const DT_MIPS_UNREFEXTNO = 0x70000012; +pub const DT_MIPS_GOTSYM = 0x70000013; +pub const DT_MIPS_HIPAGENO = 0x70000014; +pub const DT_MIPS_RLD_MAP = 0x70000016; +pub const DT_MIPS_DELTA_CLASS = 0x70000017; +pub const DT_MIPS_DELTA_CLASS_NO = 0x70000018; + +pub const DT_MIPS_DELTA_INSTANCE = 0x70000019; +pub const DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a; + +pub const DT_MIPS_DELTA_RELOC = 0x7000001b; +pub const DT_MIPS_DELTA_RELOC_NO = 0x7000001c; + +pub const DT_MIPS_DELTA_SYM = 0x7000001d; + +pub const DT_MIPS_DELTA_SYM_NO = 0x7000001e; + +pub const DT_MIPS_DELTA_CLASSSYM = 0x70000020; + +pub const DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021; + +pub const DT_MIPS_CXX_FLAGS = 0x70000022; +pub const DT_MIPS_PIXIE_INIT = 0x70000023; +pub const DT_MIPS_SYMBOL_LIB = 0x70000024; +pub const DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025; +pub const DT_MIPS_LOCAL_GOTIDX = 0x70000026; +pub const DT_MIPS_HIDDEN_GOTIDX = 0x70000027; +pub const DT_MIPS_PROTECTED_GOTIDX = 0x70000028; +pub const DT_MIPS_OPTIONS = 0x70000029; +pub const DT_MIPS_INTERFACE = 0x7000002a; +pub const DT_MIPS_DYNSTR_ALIGN = 0x7000002b; +pub const DT_MIPS_INTERFACE_SIZE = 0x7000002c; +pub const DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d; + +pub const DT_MIPS_PERF_SUFFIX = 0x7000002e; + +pub const DT_MIPS_COMPACT_SIZE = 0x7000002f; +pub const DT_MIPS_GP_VALUE = 0x70000030; +pub const DT_MIPS_AUX_DYNAMIC = 0x70000031; + +pub const DT_MIPS_PLTGOT = 0x70000032; + +pub const DT_MIPS_RWPLT = 0x70000034; +pub const DT_MIPS_RLD_MAP_REL = 0x70000035; +pub const DT_MIPS_NUM = 0x36; + +pub const DT_ALPHA_PLTRO = (DT_LOPROC + 0); +pub const DT_ALPHA_NUM = 1; + +pub const DT_PPC_GOT = (DT_LOPROC + 0); +pub const DT_PPC_OPT = (DT_LOPROC + 1); +pub const DT_PPC_NUM = 2; + +pub const DT_PPC64_GLINK = (DT_LOPROC + 0); +pub const DT_PPC64_OPD = (DT_LOPROC + 1); +pub const DT_PPC64_OPDSZ = (DT_LOPROC + 2); +pub const DT_PPC64_OPT = (DT_LOPROC + 3); +pub const DT_PPC64_NUM = 4; + +pub const DT_IA_64_PLT_RESERVE = (DT_LOPROC + 0); +pub const DT_IA_64_NUM = 1; + +pub const DT_NIOS2_GP = 0x70000002; + +pub const PT_NULL = 0; +pub const PT_LOAD = 1; +pub const PT_DYNAMIC = 2; +pub const PT_INTERP = 3; +pub const PT_NOTE = 4; +pub const PT_SHLIB = 5; +pub const PT_PHDR = 6; +pub const PT_TLS = 7; +pub const PT_NUM = 8; +pub const PT_LOOS = 0x60000000; +pub const PT_GNU_EH_FRAME = 0x6474e550; +pub const PT_GNU_STACK = 0x6474e551; +pub const PT_GNU_RELRO = 0x6474e552; +pub const PT_LOSUNW = 0x6ffffffa; +pub const PT_SUNWBSS = 0x6ffffffa; +pub const PT_SUNWSTACK = 0x6ffffffb; +pub const PT_HISUNW = 0x6fffffff; +pub const PT_HIOS = 0x6fffffff; +pub const PT_LOPROC = 0x70000000; +pub const PT_HIPROC = 0x7fffffff; + pub const SHT_NULL = 0; pub const SHT_PROGBITS = 1; pub const SHT_SYMTAB = 2; @@ -31,6 +271,45 @@ pub const SHT_HIPROC = 0x7fffffff; pub const SHT_LOUSER = 0x80000000; pub const SHT_HIUSER = 0xffffffff; +pub const STB_LOCAL = 0; +pub const STB_GLOBAL = 1; +pub const STB_WEAK = 2; +pub const STB_NUM = 3; +pub const STB_LOOS = 10; +pub const STB_GNU_UNIQUE = 10; +pub const STB_HIOS = 12; +pub const STB_LOPROC = 13; +pub const STB_HIPROC = 15; + +pub const STB_MIPS_SPLIT_COMMON = 13; + +pub const STT_NOTYPE = 0; +pub const STT_OBJECT = 1; +pub const STT_FUNC = 2; +pub const STT_SECTION = 3; +pub const STT_FILE = 4; +pub const STT_COMMON = 5; +pub const STT_TLS = 6; +pub const STT_NUM = 7; +pub const STT_LOOS = 10; +pub const STT_GNU_IFUNC = 10; +pub const STT_HIOS = 12; +pub const STT_LOPROC = 13; +pub const STT_HIPROC = 15; + +pub const STT_SPARC_REGISTER = 13; + +pub const STT_PARISC_MILLICODE = 13; + +pub const STT_HP_OPAQUE = (STT_LOOS + 0x1); +pub const STT_HP_STUB = (STT_LOOS + 0x2); + +pub const STT_ARM_TFUNC = STT_LOPROC; +pub const STT_ARM_16BIT = STT_HIPROC; + +pub const VER_FLG_BASE = 0x1; +pub const VER_FLG_WEAK = 0x2; + pub const FileType = enum { Relocatable, Executable, @@ -266,3 +545,335 @@ pub const Elf = struct { try elf.in_file.seekTo(elf_section.offset); } }; + +pub const EI_NIDENT = 16; +pub const Elf32_Half = u16; +pub const Elf64_Half = u16; +pub const Elf32_Word = u32; +pub const Elf32_Sword = i32; +pub const Elf64_Word = u32; +pub const Elf64_Sword = i32; +pub const Elf32_Xword = u64; +pub const Elf32_Sxword = i64; +pub const Elf64_Xword = u64; +pub const Elf64_Sxword = i64; +pub const Elf32_Addr = u32; +pub const Elf64_Addr = u64; +pub const Elf32_Off = u32; +pub const Elf64_Off = u64; +pub const Elf32_Section = u16; +pub const Elf64_Section = u16; +pub const Elf32_Versym = Elf32_Half; +pub const Elf64_Versym = Elf64_Half; +pub const Elf32_Ehdr = extern struct { + e_ident: [EI_NIDENT]u8, + e_type: Elf32_Half, + e_machine: Elf32_Half, + e_version: Elf32_Word, + e_entry: Elf32_Addr, + e_phoff: Elf32_Off, + e_shoff: Elf32_Off, + e_flags: Elf32_Word, + e_ehsize: Elf32_Half, + e_phentsize: Elf32_Half, + e_phnum: Elf32_Half, + e_shentsize: Elf32_Half, + e_shnum: Elf32_Half, + e_shstrndx: Elf32_Half, +}; +pub const Elf64_Ehdr = extern struct { + e_ident: [EI_NIDENT]u8, + e_type: Elf64_Half, + e_machine: Elf64_Half, + e_version: Elf64_Word, + e_entry: Elf64_Addr, + e_phoff: Elf64_Off, + e_shoff: Elf64_Off, + e_flags: Elf64_Word, + e_ehsize: Elf64_Half, + e_phentsize: Elf64_Half, + e_phnum: Elf64_Half, + e_shentsize: Elf64_Half, + e_shnum: Elf64_Half, + e_shstrndx: Elf64_Half, +}; +pub const Elf32_Shdr = extern struct { + sh_name: Elf32_Word, + sh_type: Elf32_Word, + sh_flags: Elf32_Word, + sh_addr: Elf32_Addr, + sh_offset: Elf32_Off, + sh_size: Elf32_Word, + sh_link: Elf32_Word, + sh_info: Elf32_Word, + sh_addralign: Elf32_Word, + sh_entsize: Elf32_Word, +}; +pub const Elf64_Shdr = extern struct { + sh_name: Elf64_Word, + sh_type: Elf64_Word, + sh_flags: Elf64_Xword, + sh_addr: Elf64_Addr, + sh_offset: Elf64_Off, + sh_size: Elf64_Xword, + sh_link: Elf64_Word, + sh_info: Elf64_Word, + sh_addralign: Elf64_Xword, + sh_entsize: Elf64_Xword, +}; +pub const Elf32_Chdr = extern struct { + ch_type: Elf32_Word, + ch_size: Elf32_Word, + ch_addralign: Elf32_Word, +}; +pub const Elf64_Chdr = extern struct { + ch_type: Elf64_Word, + ch_reserved: Elf64_Word, + ch_size: Elf64_Xword, + ch_addralign: Elf64_Xword, +}; +pub const Elf32_Sym = extern struct { + st_name: Elf32_Word, + st_value: Elf32_Addr, + st_size: Elf32_Word, + st_info: u8, + st_other: u8, + st_shndx: Elf32_Section, +}; +pub const Elf64_Sym = extern struct { + st_name: Elf64_Word, + st_info: u8, + st_other: u8, + st_shndx: Elf64_Section, + st_value: Elf64_Addr, + st_size: Elf64_Xword, +}; +pub const Elf32_Syminfo = extern struct { + si_boundto: Elf32_Half, + si_flags: Elf32_Half, +}; +pub const Elf64_Syminfo = extern struct { + si_boundto: Elf64_Half, + si_flags: Elf64_Half, +}; +pub const Elf32_Rel = extern struct { + r_offset: Elf32_Addr, + r_info: Elf32_Word, +}; +pub const Elf64_Rel = extern struct { + r_offset: Elf64_Addr, + r_info: Elf64_Xword, +}; +pub const Elf32_Rela = extern struct { + r_offset: Elf32_Addr, + r_info: Elf32_Word, + r_addend: Elf32_Sword, +}; +pub const Elf64_Rela = extern struct { + r_offset: Elf64_Addr, + r_info: Elf64_Xword, + r_addend: Elf64_Sxword, +}; +pub const Elf32_Phdr = extern struct { + p_type: Elf32_Word, + p_offset: Elf32_Off, + p_vaddr: Elf32_Addr, + p_paddr: Elf32_Addr, + p_filesz: Elf32_Word, + p_memsz: Elf32_Word, + p_flags: Elf32_Word, + p_align: Elf32_Word, +}; +pub const Elf64_Phdr = extern struct { + p_type: Elf64_Word, + p_flags: Elf64_Word, + p_offset: Elf64_Off, + p_vaddr: Elf64_Addr, + p_paddr: Elf64_Addr, + p_filesz: Elf64_Xword, + p_memsz: Elf64_Xword, + p_align: Elf64_Xword, +}; +pub const Elf32_Dyn = extern struct { + d_tag: Elf32_Sword, + d_un: extern union { + d_val: Elf32_Word, + d_ptr: Elf32_Addr, + }, +}; +pub const Elf64_Dyn = extern struct { + d_tag: Elf64_Sxword, + d_un: extern union { + d_val: Elf64_Xword, + d_ptr: Elf64_Addr, + }, +}; +pub const Elf32_Verdef = extern struct { + vd_version: Elf32_Half, + vd_flags: Elf32_Half, + vd_ndx: Elf32_Half, + vd_cnt: Elf32_Half, + vd_hash: Elf32_Word, + vd_aux: Elf32_Word, + vd_next: Elf32_Word, +}; +pub const Elf64_Verdef = extern struct { + vd_version: Elf64_Half, + vd_flags: Elf64_Half, + vd_ndx: Elf64_Half, + vd_cnt: Elf64_Half, + vd_hash: Elf64_Word, + vd_aux: Elf64_Word, + vd_next: Elf64_Word, +}; +pub const Elf32_Verdaux = extern struct { + vda_name: Elf32_Word, + vda_next: Elf32_Word, +}; +pub const Elf64_Verdaux = extern struct { + vda_name: Elf64_Word, + vda_next: Elf64_Word, +}; +pub const Elf32_Verneed = extern struct { + vn_version: Elf32_Half, + vn_cnt: Elf32_Half, + vn_file: Elf32_Word, + vn_aux: Elf32_Word, + vn_next: Elf32_Word, +}; +pub const Elf64_Verneed = extern struct { + vn_version: Elf64_Half, + vn_cnt: Elf64_Half, + vn_file: Elf64_Word, + vn_aux: Elf64_Word, + vn_next: Elf64_Word, +}; +pub const Elf32_Vernaux = extern struct { + vna_hash: Elf32_Word, + vna_flags: Elf32_Half, + vna_other: Elf32_Half, + vna_name: Elf32_Word, + vna_next: Elf32_Word, +}; +pub const Elf64_Vernaux = extern struct { + vna_hash: Elf64_Word, + vna_flags: Elf64_Half, + vna_other: Elf64_Half, + vna_name: Elf64_Word, + vna_next: Elf64_Word, +}; +pub const Elf32_auxv_t = extern struct { + a_type: u32, + a_un: extern union { + a_val: u32, + }, +}; +pub const Elf64_auxv_t = extern struct { + a_type: u64, + a_un: extern union { + a_val: u64, + }, +}; +pub const Elf32_Nhdr = extern struct { + n_namesz: Elf32_Word, + n_descsz: Elf32_Word, + n_type: Elf32_Word, +}; +pub const Elf64_Nhdr = extern struct { + n_namesz: Elf64_Word, + n_descsz: Elf64_Word, + n_type: Elf64_Word, +}; +pub const Elf32_Move = extern struct { + m_value: Elf32_Xword, + m_info: Elf32_Word, + m_poffset: Elf32_Word, + m_repeat: Elf32_Half, + m_stride: Elf32_Half, +}; +pub const Elf64_Move = extern struct { + m_value: Elf64_Xword, + m_info: Elf64_Xword, + m_poffset: Elf64_Xword, + m_repeat: Elf64_Half, + m_stride: Elf64_Half, +}; +pub const Elf32_gptab = extern union { + gt_header: extern struct { + gt_current_g_value: Elf32_Word, + gt_unused: Elf32_Word, + }, + gt_entry: extern struct { + gt_g_value: Elf32_Word, + gt_bytes: Elf32_Word, + }, +}; +pub const Elf32_RegInfo = extern struct { + ri_gprmask: Elf32_Word, + ri_cprmask: [4]Elf32_Word, + ri_gp_value: Elf32_Sword, +}; +pub const Elf_Options = extern struct { + kind: u8, + size: u8, + @"section": Elf32_Section, + info: Elf32_Word, +}; +pub const Elf_Options_Hw = extern struct { + hwp_flags1: Elf32_Word, + hwp_flags2: Elf32_Word, +}; +pub const Elf32_Lib = extern struct { + l_name: Elf32_Word, + l_time_stamp: Elf32_Word, + l_checksum: Elf32_Word, + l_version: Elf32_Word, + l_flags: Elf32_Word, +}; +pub const Elf64_Lib = extern struct { + l_name: Elf64_Word, + l_time_stamp: Elf64_Word, + l_checksum: Elf64_Word, + l_version: Elf64_Word, + l_flags: Elf64_Word, +}; +pub const Elf32_Conflict = Elf32_Addr; +pub const Elf_MIPS_ABIFlags_v0 = extern struct { + version: Elf32_Half, + isa_level: u8, + isa_rev: u8, + gpr_size: u8, + cpr1_size: u8, + cpr2_size: u8, + fp_abi: u8, + isa_ext: Elf32_Word, + ases: Elf32_Word, + flags1: Elf32_Word, + flags2: Elf32_Word, +}; + +pub const Ehdr = switch(@sizeOf(usize)) { + 4 => Elf32_Ehdr, + 8 => Elf64_Ehdr, + else => @compileError("expected pointer size of 32 or 64"), +}; +pub const Phdr = switch(@sizeOf(usize)) { + 4 => Elf32_Phdr, + 8 => Elf64_Phdr, + else => @compileError("expected pointer size of 32 or 64"), +}; +pub const Sym = switch(@sizeOf(usize)) { + 4 => Elf32_Sym, + 8 => Elf64_Sym, + else => @compileError("expected pointer size of 32 or 64"), +}; +pub const Verdef = switch(@sizeOf(usize)) { + 4 => Elf32_Verdef, + 8 => Elf64_Verdef, + else => @compileError("expected pointer size of 32 or 64"), +}; +pub const Verdaux = switch(@sizeOf(usize)) { + 4 => Elf32_Verdaux, + 8 => Elf64_Verdaux, + else => @compileError("expected pointer size of 32 or 64"), +}; diff --git a/std/fmt/index.zig b/std/fmt/index.zig index cfdd70e95b..7bb9829117 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -8,25 +8,25 @@ const errol3 = @import("errol/index.zig").errol3; const max_int_digits = 65; -const State = enum { // TODO put inside format function and make sure the name and debug info is correct - Start, - OpenBrace, - CloseBrace, - Integer, - IntegerWidth, - Float, - FloatWidth, - Character, - Buf, - BufWidth, -}; - /// Renders fmt string with args, calling output with slices of bytes. /// If `output` returns an error, the error is returned from `format` and /// `output` is not called again. pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8) Errors!void, comptime fmt: []const u8, args: ...) Errors!void { + const State = enum { + Start, + OpenBrace, + CloseBrace, + Integer, + IntegerWidth, + Float, + FloatWidth, + Character, + Buf, + BufWidth, + }; + comptime var start_index = 0; comptime var state = State.Start; comptime var next_arg = 0; @@ -86,7 +86,8 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context), }, 's' => { state = State.Buf; - },'.' => { + }, + '.' => { state = State.Float; }, else => @compileError("Unknown format character: " ++ []u8{c}), diff --git a/std/heap.zig b/std/heap.zig index ca6736af1e..b3a1e6bf27 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -79,19 +79,38 @@ pub const DirectAllocator = struct { switch (builtin.os) { Os.linux, Os.macosx, Os.ios => { - assert(alignment <= os.page_size); const p = os.posix; - const addr = p.mmap(null, n, p.PROT_READ|p.PROT_WRITE, - p.MAP_PRIVATE|p.MAP_ANONYMOUS, -1, 0); - if (addr == p.MAP_FAILED) { - return error.OutOfMemory; - } - return @intToPtr(&u8, addr)[0..n]; + const alloc_size = if(alignment <= os.page_size) n else n + alignment; + const addr = p.mmap(null, alloc_size, p.PROT_READ|p.PROT_WRITE, + p.MAP_PRIVATE|p.MAP_ANONYMOUS, -1, 0); + if(addr == p.MAP_FAILED) return error.OutOfMemory; + + if(alloc_size == n) return @intToPtr(&u8, addr)[0..n]; + + var aligned_addr = addr & ~usize(alignment - 1); + aligned_addr += alignment; + + //We can unmap the unused portions of our mmap, but we must only + // pass munmap bytes that exist outside our allocated pages or it + // will happily eat us too + + //Since alignment > page_size, we are by definition on a page boundry + const unused_start = addr; + const unused_len = aligned_addr - 1 - unused_start; + + var err = p.munmap(@intToPtr(&u8, unused_start), unused_len); + debug.assert(p.getErrno(err) == 0); + + //It is impossible that there is an unoccupied page at the top of our + // mmap. + + return @intToPtr(&u8, aligned_addr)[0..n]; }, Os.windows => { const amt = n + alignment + @sizeOf(usize); const heap_handle = self.heap_handle ?? blk: { - const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) ?? return error.OutOfMemory; + const hh = os.windows.HeapCreate(os.windows.HEAP_NO_SERIALIZE, amt, 0) + ?? return error.OutOfMemory; self.heap_handle = hh; break :blk hh; }; @@ -322,6 +341,7 @@ test "DirectAllocator" { const allocator = &direct_allocator.allocator; try testAllocator(allocator); + try testAllocatorLargeAlignment(allocator); } test "ArenaAllocator" { @@ -332,6 +352,7 @@ test "ArenaAllocator" { defer arena_allocator.deinit(); try testAllocator(&arena_allocator.allocator); + try testAllocatorLargeAlignment(&arena_allocator.allocator); } var test_fixed_buffer_allocator_memory: [30000 * @sizeOf(usize)]u8 = undefined; @@ -339,6 +360,7 @@ test "FixedBufferAllocator" { var fixed_buffer_allocator = FixedBufferAllocator.init(test_fixed_buffer_allocator_memory[0..]); try testAllocator(&fixed_buffer_allocator.allocator); + try testAllocatorLargeAlignment(&fixed_buffer_allocator.allocator); } fn testAllocator(allocator: &mem.Allocator) !void { @@ -360,3 +382,32 @@ fn testAllocator(allocator: &mem.Allocator) !void { allocator.free(slice); } + +fn testAllocatorLargeAlignment(allocator: &mem.Allocator) mem.Allocator.Error!void { + //Maybe a platform's page_size is actually the same as or + // very near usize? + if(os.page_size << 2 > @maxValue(usize)) return; + + const USizeShift = @IntType(false, std.math.log2(usize.bit_count)); + const large_align = u29(os.page_size << 2); + + var align_mask: usize = undefined; + _ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(large_align)), &align_mask); + + var slice = try allocator.allocFn(allocator, 500, large_align); + debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + + slice = try allocator.reallocFn(allocator, slice, 100, large_align); + debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + + slice = try allocator.reallocFn(allocator, slice, 5000, large_align); + debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + + slice = try allocator.reallocFn(allocator, slice, 10, large_align); + debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + + slice = try allocator.reallocFn(allocator, slice, 20000, large_align); + debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + + allocator.free(slice); +} diff --git a/std/math/complex/abs.zig b/std/math/complex/abs.zig new file mode 100644 index 0000000000..4cd095c46b --- /dev/null +++ b/std/math/complex/abs.zig @@ -0,0 +1,18 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn abs(z: var) @typeOf(z.re) { + const T = @typeOf(z.re); + return math.hypot(T, z.re, z.im); +} + +const epsilon = 0.0001; + +test "complex.cabs" { + const a = Complex(f32).new(5, 3); + const c = abs(a); + debug.assert(math.approxEq(f32, c, 5.83095, epsilon)); +} diff --git a/std/math/complex/acos.zig b/std/math/complex/acos.zig new file mode 100644 index 0000000000..a5760b4ace --- /dev/null +++ b/std/math/complex/acos.zig @@ -0,0 +1,21 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn acos(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const q = cmath.asin(z); + return Complex(T).new(T(math.pi) / 2 - q.re, -q.im); +} + +const epsilon = 0.0001; + +test "complex.cacos" { + const a = Complex(f32).new(5, 3); + const c = acos(a); + + debug.assert(math.approxEq(f32, c.re, 0.546975, epsilon)); + debug.assert(math.approxEq(f32, c.im, -2.452914, epsilon)); +} diff --git a/std/math/complex/acosh.zig b/std/math/complex/acosh.zig new file mode 100644 index 0000000000..8dd91b2836 --- /dev/null +++ b/std/math/complex/acosh.zig @@ -0,0 +1,21 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn acosh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const q = cmath.acos(z); + return Complex(T).new(-q.im, q.re); +} + +const epsilon = 0.0001; + +test "complex.cacosh" { + const a = Complex(f32).new(5, 3); + const c = acosh(a); + + debug.assert(math.approxEq(f32, c.re, 2.452914, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.546975, epsilon)); +} diff --git a/std/math/complex/arg.zig b/std/math/complex/arg.zig new file mode 100644 index 0000000000..f24512ac73 --- /dev/null +++ b/std/math/complex/arg.zig @@ -0,0 +1,18 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn arg(z: var) @typeOf(z.re) { + const T = @typeOf(z.re); + return math.atan2(T, z.im, z.re); +} + +const epsilon = 0.0001; + +test "complex.carg" { + const a = Complex(f32).new(5, 3); + const c = arg(a); + debug.assert(math.approxEq(f32, c, 0.540420, epsilon)); +} diff --git a/std/math/complex/asin.zig b/std/math/complex/asin.zig new file mode 100644 index 0000000000..584a3a1a9b --- /dev/null +++ b/std/math/complex/asin.zig @@ -0,0 +1,27 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn asin(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const x = z.re; + const y = z.im; + + const p = Complex(T).new(1.0 - (x - y) * (x + y), -2.0 * x * y); + const q = Complex(T).new(-y, x); + const r = cmath.log(q.add(cmath.sqrt(p))); + + return Complex(T).new(r.im, -r.re); +} + +const epsilon = 0.0001; + +test "complex.casin" { + const a = Complex(f32).new(5, 3); + const c = asin(a); + + debug.assert(math.approxEq(f32, c.re, 1.023822, epsilon)); + debug.assert(math.approxEq(f32, c.im, 2.452914, epsilon)); +} diff --git a/std/math/complex/asinh.zig b/std/math/complex/asinh.zig new file mode 100644 index 0000000000..0c4dc2b6e4 --- /dev/null +++ b/std/math/complex/asinh.zig @@ -0,0 +1,22 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn asinh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const q = Complex(T).new(-z.im, z.re); + const r = cmath.asin(q); + return Complex(T).new(r.im, -r.re); +} + +const epsilon = 0.0001; + +test "complex.casinh" { + const a = Complex(f32).new(5, 3); + const c = asinh(a); + + debug.assert(math.approxEq(f32, c.re, 2.459831, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.533999, epsilon)); +} diff --git a/std/math/complex/atan.zig b/std/math/complex/atan.zig new file mode 100644 index 0000000000..b7bbf930eb --- /dev/null +++ b/std/math/complex/atan.zig @@ -0,0 +1,130 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn atan(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + return switch (T) { + f32 => atan32(z), + f64 => atan64(z), + else => @compileError("atan not implemented for " ++ @typeName(z)), + }; +} + +fn redupif32(x: f32) f32 { + const DP1 = 3.140625; + const DP2 = 9.67502593994140625e-4; + const DP3 = 1.509957990978376432e-7; + + var t = x / math.pi; + if (t >= 0.0) { + t += 0.5; + } else { + t -= 0.5; + } + + const u = f32(i32(t)); + return ((x - u * DP1) - u * DP2) - t * DP3; +} + +fn atan32(z: &const Complex(f32)) Complex(f32) { + const maxnum = 1.0e38; + + const x = z.re; + const y = z.im; + + if ((x == 0.0) and (y > 1.0)) { + // overflow + return Complex(f32).new(maxnum, maxnum); + } + + const x2 = x * x; + var a = 1.0 - x2 - (y * y); + if (a == 0.0) { + // overflow + return Complex(f32).new(maxnum, maxnum); + } + + var t = 0.5 * math.atan2(f32, 2.0 * x, a); + var w = redupif32(t); + + t = y - 1.0; + a = x2 + t * t; + if (a == 0.0) { + // overflow + return Complex(f32).new(maxnum, maxnum); + } + + t = y + 1.0; + a = (x2 + (t * t)) / a; + return Complex(f32).new(w, 0.25 * math.ln(a)); +} + +fn redupif64(x: f64) f64 { + const DP1 = 3.14159265160560607910; + const DP2 = 1.98418714791870343106e-9; + const DP3 = 1.14423774522196636802e-17; + + var t = x / math.pi; + if (t >= 0.0) { + t += 0.5; + } else { + t -= 0.5; + } + + const u = f64(i64(t)); + return ((x - u * DP1) - u * DP2) - t * DP3; +} + +fn atan64(z: &const Complex(f64)) Complex(f64) { + const maxnum = 1.0e308; + + const x = z.re; + const y = z.im; + + if ((x == 0.0) and (y > 1.0)) { + // overflow + return Complex(f64).new(maxnum, maxnum); + } + + const x2 = x * x; + var a = 1.0 - x2 - (y * y); + if (a == 0.0) { + // overflow + return Complex(f64).new(maxnum, maxnum); + } + + var t = 0.5 * math.atan2(f64, 2.0 * x, a); + var w = redupif64(t); + + t = y - 1.0; + a = x2 + t * t; + if (a == 0.0) { + // overflow + return Complex(f64).new(maxnum, maxnum); + } + + t = y + 1.0; + a = (x2 + (t * t)) / a; + return Complex(f64).new(w, 0.25 * math.ln(a)); +} + +const epsilon = 0.0001; + +test "complex.catan32" { + const a = Complex(f32).new(5, 3); + const c = atan(a); + + debug.assert(math.approxEq(f32, c.re, 1.423679, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.086569, epsilon)); +} + +test "complex.catan64" { + const a = Complex(f64).new(5, 3); + const c = atan(a); + + debug.assert(math.approxEq(f64, c.re, 1.423679, epsilon)); + debug.assert(math.approxEq(f64, c.im, 0.086569, epsilon)); +} diff --git a/std/math/complex/atanh.zig b/std/math/complex/atanh.zig new file mode 100644 index 0000000000..f70c741765 --- /dev/null +++ b/std/math/complex/atanh.zig @@ -0,0 +1,22 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn atanh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const q = Complex(T).new(-z.im, z.re); + const r = cmath.atan(q); + return Complex(T).new(r.im, -r.re); +} + +const epsilon = 0.0001; + +test "complex.catanh" { + const a = Complex(f32).new(5, 3); + const c = atanh(a); + + debug.assert(math.approxEq(f32, c.re, 0.146947, epsilon)); + debug.assert(math.approxEq(f32, c.im, 1.480870, epsilon)); +} diff --git a/std/math/complex/conj.zig b/std/math/complex/conj.zig new file mode 100644 index 0000000000..ad3e8b5036 --- /dev/null +++ b/std/math/complex/conj.zig @@ -0,0 +1,17 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn conj(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + return Complex(T).new(z.re, -z.im); +} + +test "complex.conj" { + const a = Complex(f32).new(5, 3); + const c = a.conjugate(); + + debug.assert(c.re == 5 and c.im == -3); +} diff --git a/std/math/complex/cos.zig b/std/math/complex/cos.zig new file mode 100644 index 0000000000..96e4ffcdb0 --- /dev/null +++ b/std/math/complex/cos.zig @@ -0,0 +1,21 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn cos(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const p = Complex(T).new(-z.im, z.re); + return cmath.cosh(p); +} + +const epsilon = 0.0001; + +test "complex.ccos" { + const a = Complex(f32).new(5, 3); + const c = cos(a); + + debug.assert(math.approxEq(f32, c.re, 2.855815, epsilon)); + debug.assert(math.approxEq(f32, c.im, 9.606383, epsilon)); +} diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig new file mode 100644 index 0000000000..96eac68556 --- /dev/null +++ b/std/math/complex/cosh.zig @@ -0,0 +1,165 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +const ldexp_cexp = @import("ldexp.zig").ldexp_cexp; + +pub fn cosh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + return switch (T) { + f32 => cosh32(z), + f64 => cosh64(z), + else => @compileError("cosh not implemented for " ++ @typeName(z)), + }; +} + +fn cosh32(z: &const Complex(f32)) Complex(f32) { + const x = z.re; + const y = z.im; + + const hx = @bitCast(u32, x); + const ix = hx & 0x7fffffff; + + const hy = @bitCast(u32, y); + const iy = hy & 0x7fffffff; + + if (ix < 0x7f800000 and iy < 0x7f800000) { + if (iy == 0) { + return Complex(f32).new(math.cosh(x), y); + } + // small x: normal case + if (ix < 0x41100000) { + return Complex(f32).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); + } + + // |x|>= 9, so cosh(x) ~= exp(|x|) + if (ix < 0x42b17218) { + // x < 88.7: exp(|x|) won't overflow + const h = math.exp(math.fabs(x)) * 0.5; + return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); + } + // x < 192.7: scale to avoid overflow + else if (ix < 0x4340b1e7) { + const v = Complex(f32).new(math.fabs(x), y); + const r = ldexp_cexp(v, -1); + return Complex(f32).new(x, y * math.copysign(f32, 1, x)); + } + // x >= 192.7: result always overflows + else { + const h = 0x1p127 * x; + return Complex(f32).new(h * h * math.cos(y), h * math.sin(y)); + } + } + + if (ix == 0 and iy >= 0x7f800000) { + return Complex(f32).new(y - y, math.copysign(f32, 0, x * (y - y))); + } + + if (iy == 0 and ix >= 0x7f800000) { + if (hx & 0x7fffff == 0) { + return Complex(f32).new(x * x, math.copysign(f32, 0, x) * y); + } + return Complex(f32).new(x, math.copysign(f32, 0, (x + x) * y)); + } + + if (ix < 0x7f800000 and iy >= 0x7f800000) { + return Complex(f32).new(y - y, x * (y - y)); + } + + if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { + if (iy >= 0x7f800000) { + return Complex(f32).new(x * x, x * (y - y)); + } + return Complex(f32).new((x * x) * math.cos(y), x * math.sin(y)); + } + + return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); +} + +fn cosh64(z: &const Complex(f64)) Complex(f64) { + const x = z.re; + const y = z.im; + + const fx = @bitCast(u64, x); + const hx = u32(fx >> 32); + const lx = @truncate(u32, fx); + const ix = hx & 0x7fffffff; + + const fy = @bitCast(u64, y); + const hy = u32(fy >> 32); + const ly = @truncate(u32, fy); + const iy = hy & 0x7fffffff; + + // nearly non-exceptional case where x, y are finite + if (ix < 0x7ff00000 and iy < 0x7ff00000) { + if (iy | ly == 0) { + return Complex(f64).new(math.cosh(x), x * y); + } + // small x: normal case + if (ix < 0x40360000) { + return Complex(f64).new(math.cosh(x) * math.cos(y), math.sinh(x) * math.sin(y)); + } + + // |x|>= 22, so cosh(x) ~= exp(|x|) + if (ix < 0x40862e42) { + // x < 710: exp(|x|) won't overflow + const h = math.exp(math.fabs(x)) * 0.5; + return Complex(f64).new(h * math.cos(y), math.copysign(f64, h, x) * math.sin(y)); + } + // x < 1455: scale to avoid overflow + else if (ix < 0x4096bbaa) { + const v = Complex(f64).new(math.fabs(x), y); + const r = ldexp_cexp(v, -1); + return Complex(f64).new(x, y * math.copysign(f64, 1, x)); + } + // x >= 1455: result always overflows + else { + const h = 0x1p1023; + return Complex(f64).new(h * h * math.cos(y), h * math.sin(y)); + } + } + + if (ix | lx == 0 and iy >= 0x7ff00000) { + return Complex(f64).new(y - y, math.copysign(f64, 0, x * (y - y))); + } + + if (iy | ly == 0 and ix >= 0x7ff00000) { + if ((hx & 0xfffff) | lx == 0) { + return Complex(f64).new(x * x, math.copysign(f64, 0, x) * y); + } + return Complex(f64).new(x * x, math.copysign(f64, 0, (x + x) * y)); + } + + if (ix < 0x7ff00000 and iy >= 0x7ff00000) { + return Complex(f64).new(y - y, x * (y - y)); + } + + if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { + if (iy >= 0x7ff00000) { + return Complex(f64).new(x * x, x * (y - y)); + } + return Complex(f64).new(x * x * math.cos(y), x * math.sin(y)); + } + + return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); +} + +const epsilon = 0.0001; + +test "complex.ccosh32" { + const a = Complex(f32).new(5, 3); + const c = cosh(a); + + debug.assert(math.approxEq(f32, c.re, -73.467300, epsilon)); + debug.assert(math.approxEq(f32, c.im, 10.471557, epsilon)); +} + +test "complex.ccosh64" { + const a = Complex(f64).new(5, 3); + const c = cosh(a); + + debug.assert(math.approxEq(f64, c.re, -73.467300, epsilon)); + debug.assert(math.approxEq(f64, c.im, 10.471557, epsilon)); +} diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig new file mode 100644 index 0000000000..03f7f9e41b --- /dev/null +++ b/std/math/complex/exp.zig @@ -0,0 +1,140 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +const ldexp_cexp = @import("ldexp.zig").ldexp_cexp; + +pub fn exp(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + + return switch (T) { + f32 => exp32(z), + f64 => exp64(z), + else => @compileError("exp not implemented for " ++ @typeName(z)), + }; +} + +fn exp32(z: &const Complex(f32)) Complex(f32) { + @setFloatMode(this, @import("builtin").FloatMode.Strict); + + const exp_overflow = 0x42b17218; // max_exp * ln2 ~= 88.72283955 + const cexp_overflow = 0x43400074; // (max_exp - min_denom_exp) * ln2 + + const x = z.re; + const y = z.im; + + const hy = @bitCast(u32, y) & 0x7fffffff; + // cexp(x + i0) = exp(x) + i0 + if (hy == 0) { + return Complex(f32).new(math.exp(x), y); + } + + const hx = @bitCast(u32, x); + // cexp(0 + iy) = cos(y) + isin(y) + if ((hx & 0x7fffffff) == 0) { + return Complex(f32).new(math.cos(y), math.sin(y)); + } + + if (hy >= 0x7f800000) { + // cexp(finite|nan +- i inf|nan) = nan + i nan + if ((hx & 0x7fffffff) != 0x7f800000) { + return Complex(f32).new(y - y, y - y); + } + // cexp(-inf +- i inf|nan) = 0 + i0 + else if (hx & 0x80000000 != 0) { + return Complex(f32).new(0, 0); + } + // cexp(+inf +- i inf|nan) = inf + i nan + else { + return Complex(f32).new(x, y - y); + } + } + + // 88.7 <= x <= 192 so must scale + if (hx >= exp_overflow and hx <= cexp_overflow) { + return ldexp_cexp(z, 0); + } + // - x < exp_overflow => exp(x) won't overflow (common) + // - x > cexp_overflow, so exp(x) * s overflows for s > 0 + // - x = +-inf + // - x = nan + else { + const exp_x = math.exp(x); + return Complex(f32).new(exp_x * math.cos(y), exp_x * math.sin(y)); + } +} + +fn exp64(z: &const Complex(f64)) Complex(f64) { + const exp_overflow = 0x40862e42; // high bits of max_exp * ln2 ~= 710 + const cexp_overflow = 0x4096b8e4; // (max_exp - min_denorm_exp) * ln2 + + const x = z.re; + const y = z.im; + + const fy = @bitCast(u64, y); + const hy = u32(fy >> 32) & 0x7fffffff; + const ly = @truncate(u32, fy); + + // cexp(x + i0) = exp(x) + i0 + if (hy | ly == 0) { + return Complex(f64).new(math.exp(x), y); + } + + const fx = @bitCast(u64, x); + const hx = u32(fx >> 32); + const lx = @truncate(u32, fx); + + // cexp(0 + iy) = cos(y) + isin(y) + if ((hx & 0x7fffffff) | lx == 0) { + return Complex(f64).new(math.cos(y), math.sin(y)); + } + + if (hy >= 0x7ff00000) { + // cexp(finite|nan +- i inf|nan) = nan + i nan + if (lx != 0 or (hx & 0x7fffffff) != 0x7ff00000) { + return Complex(f64).new(y - y, y - y); + } + // cexp(-inf +- i inf|nan) = 0 + i0 + else if (hx & 0x80000000 != 0) { + return Complex(f64).new(0, 0); + } + // cexp(+inf +- i inf|nan) = inf + i nan + else { + return Complex(f64).new(x, y - y); + } + } + + // 709.7 <= x <= 1454.3 so must scale + if (hx >= exp_overflow and hx <= cexp_overflow) { + const r = ldexp_cexp(z, 0); + return *r; + } + // - x < exp_overflow => exp(x) won't overflow (common) + // - x > cexp_overflow, so exp(x) * s overflows for s > 0 + // - x = +-inf + // - x = nan + else { + const exp_x = math.exp(x); + return Complex(f64).new(exp_x * math.cos(y), exp_x * math.sin(y)); + } +} + +const epsilon = 0.0001; + +test "complex.cexp32" { + const a = Complex(f32).new(5, 3); + const c = exp(a); + + debug.assert(math.approxEq(f32, c.re, -146.927917, epsilon)); + debug.assert(math.approxEq(f32, c.im, 20.944065, epsilon)); +} + +test "complex.cexp64" { + const a = Complex(f32).new(5, 3); + const c = exp(a); + + debug.assert(math.approxEq(f64, c.re, -146.927917, epsilon)); + debug.assert(math.approxEq(f64, c.im, 20.944065, epsilon)); +} diff --git a/std/math/complex/index.zig b/std/math/complex/index.zig new file mode 100644 index 0000000000..a4d493307e --- /dev/null +++ b/std/math/complex/index.zig @@ -0,0 +1,171 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; + +pub const abs = @import("abs.zig").abs; +pub const acosh = @import("acosh.zig").acosh; +pub const acos = @import("acos.zig").acos; +pub const arg = @import("arg.zig").arg; +pub const asinh = @import("asinh.zig").asinh; +pub const asin = @import("asin.zig").asin; +pub const atanh = @import("atanh.zig").atanh; +pub const atan = @import("atan.zig").atan; +pub const conj = @import("conj.zig").conj; +pub const cosh = @import("cosh.zig").cosh; +pub const cos = @import("cos.zig").cos; +pub const exp = @import("exp.zig").exp; +pub const log = @import("log.zig").log; +pub const pow = @import("pow.zig").pow; +pub const proj = @import("proj.zig").proj; +pub const sinh = @import("sinh.zig").sinh; +pub const sin = @import("sin.zig").sin; +pub const sqrt = @import("sqrt.zig").sqrt; +pub const tanh = @import("tanh.zig").tanh; +pub const tan = @import("tan.zig").tan; + +pub fn Complex(comptime T: type) type { + return struct { + const Self = this; + + re: T, + im: T, + + pub fn new(re: T, im: T) Self { + return Self { + .re = re, + .im = im, + }; + } + + pub fn add(self: &const Self, other: &const Self) Self { + return Self { + .re = self.re + other.re, + .im = self.im + other.im, + }; + } + + pub fn sub(self: &const Self, other: &const Self) Self { + return Self { + .re = self.re - other.re, + .im = self.im - other.im, + }; + } + + pub fn mul(self: &const Self, other: &const Self) Self { + return Self { + .re = self.re * other.re - self.im * other.im, + .im = self.im * other.re + self.re * other.im, + }; + } + + pub fn div(self: &const Self, other: &const Self) Self { + const re_num = self.re * other.re + self.im * other.im; + const im_num = self.im * other.re - self.re * other.im; + const den = other.re * other.re + other.im * other.im; + + return Self { + .re = re_num / den, + .im = im_num / den, + }; + } + + pub fn conjugate(self: &const Self) Self { + return Self { + .re = self.re, + .im = -self.im, + }; + } + + pub fn reciprocal(self: &const Self) Self { + const m = self.re * self.re + self.im * self.im; + return Self { + .re = self.re / m, + .im = -self.im / m, + }; + } + + pub fn magnitude(self: &const Self) T { + return math.sqrt(self.re * self.re + self.im * self.im); + } + }; +} + +const epsilon = 0.0001; + +test "complex.add" { + const a = Complex(f32).new(5, 3); + const b = Complex(f32).new(2, 7); + const c = a.add(b); + + debug.assert(c.re == 7 and c.im == 10); +} + +test "complex.sub" { + const a = Complex(f32).new(5, 3); + const b = Complex(f32).new(2, 7); + const c = a.sub(b); + + debug.assert(c.re == 3 and c.im == -4); +} + +test "complex.mul" { + const a = Complex(f32).new(5, 3); + const b = Complex(f32).new(2, 7); + const c = a.mul(b); + + debug.assert(c.re == -11 and c.im == 41); +} + +test "complex.div" { + const a = Complex(f32).new(5, 3); + const b = Complex(f32).new(2, 7); + const c = a.div(b); + + debug.assert(math.approxEq(f32, c.re, f32(31)/53, epsilon) and + math.approxEq(f32, c.im, f32(-29)/53, epsilon)); +} + +test "complex.conjugate" { + const a = Complex(f32).new(5, 3); + const c = a.conjugate(); + + debug.assert(c.re == 5 and c.im == -3); +} + +test "complex.reciprocal" { + const a = Complex(f32).new(5, 3); + const c = a.reciprocal(); + + debug.assert(math.approxEq(f32, c.re, f32(5)/34, epsilon) and + math.approxEq(f32, c.im, f32(-3)/34, epsilon)); +} + +test "complex.magnitude" { + const a = Complex(f32).new(5, 3); + const c = a.magnitude(); + + debug.assert(math.approxEq(f32, c, 5.83095, epsilon)); +} + +test "complex.cmath" { + _ = @import("abs.zig"); + _ = @import("acosh.zig"); + _ = @import("acos.zig"); + _ = @import("arg.zig"); + _ = @import("asinh.zig"); + _ = @import("asin.zig"); + _ = @import("atanh.zig"); + _ = @import("atan.zig"); + _ = @import("conj.zig"); + _ = @import("cosh.zig"); + _ = @import("cos.zig"); + _ = @import("exp.zig"); + _ = @import("log.zig"); + _ = @import("pow.zig"); + _ = @import("proj.zig"); + _ = @import("sinh.zig"); + _ = @import("sin.zig"); + _ = @import("sqrt.zig"); + _ = @import("tanh.zig"); + _ = @import("tan.zig"); +} diff --git a/std/math/complex/ldexp.zig b/std/math/complex/ldexp.zig new file mode 100644 index 0000000000..4fb5a6815f --- /dev/null +++ b/std/math/complex/ldexp.zig @@ -0,0 +1,75 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn ldexp_cexp(z: var, expt: i32) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + + return switch (T) { + f32 => ldexp_cexp32(z, expt), + f64 => ldexp_cexp64(z, expt), + else => unreachable, + }; +} + +fn frexp_exp32(x: f32, expt: &i32) f32 { + const k = 235; // reduction constant + const kln2 = 162.88958740; // k * ln2 + + const exp_x = math.exp(x - kln2); + const hx = @bitCast(u32, exp_x); + *expt = i32(hx >> 23) - (0x7f + 127) + k; + return @bitCast(f32, (hx & 0x7fffff) | ((0x7f + 127) << 23)); +} + +fn ldexp_cexp32(z: &const Complex(f32), expt: i32) Complex(f32) { + var ex_expt: i32 = undefined; + const exp_x = frexp_exp32(z.re, &ex_expt); + const exptf = expt + ex_expt; + + const half_expt1 = @divTrunc(exptf, 2); + const scale1 = @bitCast(f32, (0x7f + half_expt1) << 23); + + const half_expt2 = exptf - half_expt1; + const scale2 = @bitCast(f32, (0x7f + half_expt2) << 23); + + return Complex(f32).new( + math.cos(z.im) * exp_x * scale1 * scale2, + math.sin(z.im) * exp_x * scale1 * scale2, + ); +} + +fn frexp_exp64(x: f64, expt: &i32) f64 { + const k = 1799; // reduction constant + const kln2 = 1246.97177782734161156; // k * ln2 + + const exp_x = math.exp(x - kln2); + + const fx = @bitCast(u64, x); + const hx = u32(fx >> 32); + const lx = @truncate(u32, fx); + + *expt = i32(hx >> 20) - (0x3ff + 1023) + k; + + const high_word = (hx & 0xfffff) | ((0x3ff + 1023) << 20); + return @bitCast(f64, (u64(high_word) << 32) | lx); +} + +fn ldexp_cexp64(z: &const Complex(f64), expt: i32) Complex(f64) { + var ex_expt: i32 = undefined; + const exp_x = frexp_exp64(z.re, &ex_expt); + const exptf = i64(expt + ex_expt); + + const half_expt1 = @divTrunc(exptf, 2); + const scale1 = @bitCast(f64, (0x3ff + half_expt1) << 20); + + const half_expt2 = exptf - half_expt1; + const scale2 = @bitCast(f64, (0x3ff + half_expt2) << 20); + + return Complex(f64).new( + math.cos(z.im) * exp_x * scale1 * scale2, + math.sin(z.im) * exp_x * scale1 * scale2, + ); +} diff --git a/std/math/complex/log.zig b/std/math/complex/log.zig new file mode 100644 index 0000000000..a4a1d1664f --- /dev/null +++ b/std/math/complex/log.zig @@ -0,0 +1,23 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn log(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const r = cmath.abs(z); + const phi = cmath.arg(z); + + return Complex(T).new(math.ln(r), phi); +} + +const epsilon = 0.0001; + +test "complex.clog" { + const a = Complex(f32).new(5, 3); + const c = log(a); + + debug.assert(math.approxEq(f32, c.re, 1.763180, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.540419, epsilon)); +} diff --git a/std/math/complex/pow.zig b/std/math/complex/pow.zig new file mode 100644 index 0000000000..bef9fde542 --- /dev/null +++ b/std/math/complex/pow.zig @@ -0,0 +1,22 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn pow(comptime T: type, z: &const T, c: &const T) T { + const p = cmath.log(z); + const q = c.mul(p); + return cmath.exp(q); +} + +const epsilon = 0.0001; + +test "complex.cpow" { + const a = Complex(f32).new(5, 3); + const b = Complex(f32).new(2.3, -1.3); + const c = pow(Complex(f32), a, b); + + debug.assert(math.approxEq(f32, c.re, 58.049110, epsilon)); + debug.assert(math.approxEq(f32, c.im, -101.003433, epsilon)); +} diff --git a/std/math/complex/proj.zig b/std/math/complex/proj.zig new file mode 100644 index 0000000000..b6c4cc046e --- /dev/null +++ b/std/math/complex/proj.zig @@ -0,0 +1,24 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn proj(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + + if (math.isInf(z.re) or math.isInf(z.im)) { + return Complex(T).new(math.inf(T), math.copysign(T, 0, z.re)); + } + + return Complex(T).new(z.re, z.im); +} + +const epsilon = 0.0001; + +test "complex.cproj" { + const a = Complex(f32).new(5, 3); + const c = proj(a); + + debug.assert(c.re == 5 and c.im == 3); +} diff --git a/std/math/complex/sin.zig b/std/math/complex/sin.zig new file mode 100644 index 0000000000..d32b771d3b --- /dev/null +++ b/std/math/complex/sin.zig @@ -0,0 +1,22 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn sin(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const p = Complex(T).new(-z.im, z.re); + const q = cmath.sinh(p); + return Complex(T).new(q.im, -q.re); +} + +const epsilon = 0.0001; + +test "complex.csin" { + const a = Complex(f32).new(5, 3); + const c = sin(a); + + debug.assert(math.approxEq(f32, c.re, -9.654126, epsilon)); + debug.assert(math.approxEq(f32, c.im, 2.841692, epsilon)); +} diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig new file mode 100644 index 0000000000..09a62ca058 --- /dev/null +++ b/std/math/complex/sinh.zig @@ -0,0 +1,164 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +const ldexp_cexp = @import("ldexp.zig").ldexp_cexp; + +pub fn sinh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + return switch (T) { + f32 => sinh32(z), + f64 => sinh64(z), + else => @compileError("tan not implemented for " ++ @typeName(z)), + }; +} + +fn sinh32(z: &const Complex(f32)) Complex(f32) { + const x = z.re; + const y = z.im; + + const hx = @bitCast(u32, x); + const ix = hx & 0x7fffffff; + + const hy = @bitCast(u32, y); + const iy = hy & 0x7fffffff; + + if (ix < 0x7f800000 and iy < 0x7f800000) { + if (iy == 0) { + return Complex(f32).new(math.sinh(x), y); + } + // small x: normal case + if (ix < 0x41100000) { + return Complex(f32).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); + } + + // |x|>= 9, so cosh(x) ~= exp(|x|) + if (ix < 0x42b17218) { + // x < 88.7: exp(|x|) won't overflow + const h = math.exp(math.fabs(x)) * 0.5; + return Complex(f32).new(math.copysign(f32, h, x) * math.cos(y), h * math.sin(y)); + } + // x < 192.7: scale to avoid overflow + else if (ix < 0x4340b1e7) { + const v = Complex(f32).new(math.fabs(x), y); + const r = ldexp_cexp(v, -1); + return Complex(f32).new(x * math.copysign(f32, 1, x), y); + } + // x >= 192.7: result always overflows + else { + const h = 0x1p127 * x; + return Complex(f32).new(h * math.cos(y), h * h * math.sin(y)); + } + } + + if (ix == 0 and iy >= 0x7f800000) { + return Complex(f32).new(math.copysign(f32, 0, x * (y - y)), y - y); + } + + if (iy == 0 and ix >= 0x7f800000) { + if (hx & 0x7fffff == 0) { + return Complex(f32).new(x, y); + } + return Complex(f32).new(x, math.copysign(f32, 0, y)); + } + + if (ix < 0x7f800000 and iy >= 0x7f800000) { + return Complex(f32).new(y - y, x * (y - y)); + } + + if (ix >= 0x7f800000 and (hx & 0x7fffff) == 0) { + if (iy >= 0x7f800000) { + return Complex(f32).new(x * x, x * (y - y)); + } + return Complex(f32).new(x * math.cos(y), math.inf_f32 * math.sin(y)); + } + + return Complex(f32).new((x * x) * (y - y), (x + x) * (y - y)); +} + +fn sinh64(z: &const Complex(f64)) Complex(f64) { + const x = z.re; + const y = z.im; + + const fx = @bitCast(u64, x); + const hx = u32(fx >> 32); + const lx = @truncate(u32, fx); + const ix = hx & 0x7fffffff; + + const fy = @bitCast(u64, y); + const hy = u32(fy >> 32); + const ly = @truncate(u32, fy); + const iy = hy & 0x7fffffff; + + if (ix < 0x7ff00000 and iy < 0x7ff00000) { + if (iy | ly == 0) { + return Complex(f64).new(math.sinh(x), y); + } + // small x: normal case + if (ix < 0x40360000) { + return Complex(f64).new(math.sinh(x) * math.cos(y), math.cosh(x) * math.sin(y)); + } + + // |x|>= 22, so cosh(x) ~= exp(|x|) + if (ix < 0x40862e42) { + // x < 710: exp(|x|) won't overflow + const h = math.exp(math.fabs(x)) * 0.5; + return Complex(f64).new(math.copysign(f64, h, x) * math.cos(y), h * math.sin(y)); + } + // x < 1455: scale to avoid overflow + else if (ix < 0x4096bbaa) { + const v = Complex(f64).new(math.fabs(x), y); + const r = ldexp_cexp(v, -1); + return Complex(f64).new(x * math.copysign(f64, 1, x), y); + } + // x >= 1455: result always overflows + else { + const h = 0x1p1023 * x; + return Complex(f64).new(h * math.cos(y), h * h * math.sin(y)); + } + } + + if (ix | lx == 0 and iy >= 0x7ff00000) { + return Complex(f64).new(math.copysign(f64, 0, x * (y - y)), y - y); + } + + if (iy | ly == 0 and ix >= 0x7ff00000) { + if ((hx & 0xfffff) | lx == 0) { + return Complex(f64).new(x, y); + } + return Complex(f64).new(x, math.copysign(f64, 0, y)); + } + + if (ix < 0x7ff00000 and iy >= 0x7ff00000) { + return Complex(f64).new(y - y, x * (y - y)); + } + + if (ix >= 0x7ff00000 and (hx & 0xfffff) | lx == 0) { + if (iy >= 0x7ff00000) { + return Complex(f64).new(x * x, x * (y - y)); + } + return Complex(f64).new(x * math.cos(y), math.inf_f64 * math.sin(y)); + } + + return Complex(f64).new((x * x) * (y - y), (x + x) * (y - y)); +} + +const epsilon = 0.0001; + +test "complex.csinh32" { + const a = Complex(f32).new(5, 3); + const c = sinh(a); + + debug.assert(math.approxEq(f32, c.re, -73.460617, epsilon)); + debug.assert(math.approxEq(f32, c.im, 10.472508, epsilon)); +} + +test "complex.csinh64" { + const a = Complex(f64).new(5, 3); + const c = sinh(a); + + debug.assert(math.approxEq(f64, c.re, -73.460617, epsilon)); + debug.assert(math.approxEq(f64, c.im, 10.472508, epsilon)); +} diff --git a/std/math/complex/sqrt.zig b/std/math/complex/sqrt.zig new file mode 100644 index 0000000000..afda69f7c9 --- /dev/null +++ b/std/math/complex/sqrt.zig @@ -0,0 +1,133 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +// TODO when #733 is solved this can be @typeOf(z) instead of Complex(@typeOf(z.re)) +pub fn sqrt(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + + return switch (T) { + f32 => sqrt32(z), + f64 => sqrt64(z), + else => @compileError("sqrt not implemented for " ++ @typeName(z)), + }; +} + +fn sqrt32(z: &const Complex(f32)) Complex(f32) { + const x = z.re; + const y = z.im; + + if (x == 0 and y == 0) { + return Complex(f32).new(0, y); + } + if (math.isInf(y)) { + return Complex(f32).new(math.inf(f32), y); + } + if (math.isNan(x)) { + // raise invalid if y is not nan + const t = (y - y) / (y - y); + return Complex(f32).new(x, t); + } + if (math.isInf(x)) { + // sqrt(inf + i nan) = inf + nan i + // sqrt(inf + iy) = inf + i0 + // sqrt(-inf + i nan) = nan +- inf i + // sqrt(-inf + iy) = 0 + inf i + if (math.signbit(x)) { + return Complex(f32).new(math.fabs(x - y), math.copysign(f32, x, y)); + } else { + return Complex(f32).new(x, math.copysign(f32, y - y, y)); + } + } + + // y = nan special case is handled fine below + + // double-precision avoids overflow with correct rounding. + const dx = f64(x); + const dy = f64(y); + + if (dx >= 0) { + const t = math.sqrt((dx + math.hypot(f64, dx, dy)) * 0.5); + return Complex(f32).new(f32(t), f32(dy / (2.0 * t))); + } else { + const t = math.sqrt((-dx + math.hypot(f64, dx, dy)) * 0.5); + return Complex(f32).new(f32(math.fabs(y) / (2.0 * t)), f32(math.copysign(f64, t, y))); + } +} + +fn sqrt64(z: &const Complex(f64)) Complex(f64) { + // may encounter overflow for im,re >= DBL_MAX / (1 + sqrt(2)) + const threshold = 0x1.a827999fcef32p+1022; + + var x = z.re; + var y = z.im; + + if (x == 0 and y == 0) { + return Complex(f64).new(0, y); + } + if (math.isInf(y)) { + return Complex(f64).new(math.inf(f64), y); + } + if (math.isNan(x)) { + // raise invalid if y is not nan + const t = (y - y) / (y - y); + return Complex(f64).new(x, t); + } + if (math.isInf(x)) { + // sqrt(inf + i nan) = inf + nan i + // sqrt(inf + iy) = inf + i0 + // sqrt(-inf + i nan) = nan +- inf i + // sqrt(-inf + iy) = 0 + inf i + if (math.signbit(x)) { + return Complex(f64).new(math.fabs(x - y), math.copysign(f64, x, y)); + } else { + return Complex(f64).new(x, math.copysign(f64, y - y, y)); + } + } + + // y = nan special case is handled fine below + + // scale to avoid overflow + var scale = false; + if (math.fabs(x) >= threshold or math.fabs(y) >= threshold) { + x *= 0.25; + y *= 0.25; + scale = true; + } + + var result: Complex(f64) = undefined; + if (x >= 0) { + const t = math.sqrt((x + math.hypot(f64, x, y)) * 0.5); + result = Complex(f64).new(t, y / (2.0 * t)); + } else { + const t = math.sqrt((-x + math.hypot(f64, x, y)) * 0.5); + result = Complex(f64).new(math.fabs(y) / (2.0 * t), math.copysign(f64, t, y)); + } + + if (scale) { + result.re *= 2; + result.im *= 2; + } + + return result; +} + +const epsilon = 0.0001; + +test "complex.csqrt32" { + const a = Complex(f32).new(5, 3); + const c = sqrt(a); + + debug.assert(math.approxEq(f32, c.re, 2.327117, epsilon)); + debug.assert(math.approxEq(f32, c.im, 0.644574, epsilon)); +} + +test "complex.csqrt64" { + const a = Complex(f64).new(5, 3); + const c = sqrt(a); + + debug.assert(math.approxEq(f64, c.re, 2.3271175190399496, epsilon)); + debug.assert(math.approxEq(f64, c.im, 0.6445742373246469, epsilon)); +} diff --git a/std/math/complex/tan.zig b/std/math/complex/tan.zig new file mode 100644 index 0000000000..4ea5182fa7 --- /dev/null +++ b/std/math/complex/tan.zig @@ -0,0 +1,22 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn tan(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + const q = Complex(T).new(-z.im, z.re); + const r = cmath.tanh(q); + return Complex(T).new(r.im, -r.re); +} + +const epsilon = 0.0001; + +test "complex.ctan" { + const a = Complex(f32).new(5, 3); + const c = tan(a); + + debug.assert(math.approxEq(f32, c.re, -0.002708233, epsilon)); + debug.assert(math.approxEq(f32, c.im, 1.004165, epsilon)); +} diff --git a/std/math/complex/tanh.zig b/std/math/complex/tanh.zig new file mode 100644 index 0000000000..6af62f48ae --- /dev/null +++ b/std/math/complex/tanh.zig @@ -0,0 +1,111 @@ +const std = @import("../../index.zig"); +const debug = std.debug; +const math = std.math; +const cmath = math.complex; +const Complex = cmath.Complex; + +pub fn tanh(z: var) Complex(@typeOf(z.re)) { + const T = @typeOf(z.re); + return switch (T) { + f32 => tanh32(z), + f64 => tanh64(z), + else => @compileError("tan not implemented for " ++ @typeName(z)), + }; +} + +fn tanh32(z: &const Complex(f32)) Complex(f32) { + const x = z.re; + const y = z.im; + + const hx = @bitCast(u32, x); + const ix = hx & 0x7fffffff; + + if (ix >= 0x7f800000) { + if (ix & 0x7fffff != 0) { + const r = if (y == 0) y else x * y; + return Complex(f32).new(x, r); + } + const xx = @bitCast(f32, hx - 0x40000000); + const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); + return Complex(f32).new(xx, math.copysign(f32, 0, r)); + } + + if (!math.isFinite(y)) { + const r = if (ix != 0) y - y else x; + return Complex(f32).new(r, y - y); + } + + // x >= 11 + if (ix >= 0x41300000) { + const exp_mx = math.exp(-math.fabs(x)); + return Complex(f32).new(math.copysign(f32, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); + } + + // Kahan's algorithm + const t = math.tan(y); + const beta = 1.0 + t * t; + const s = math.sinh(x); + const rho = math.sqrt(1 + s * s); + const den = 1 + beta * s * s; + + return Complex(f32).new((beta * rho * s) / den, t / den); +} + +fn tanh64(z: &const Complex(f64)) Complex(f64) { + const x = z.re; + const y = z.im; + + const fx = @bitCast(u64, x); + const hx = u32(fx >> 32); + const lx = @truncate(u32, fx); + const ix = hx & 0x7fffffff; + + if (ix >= 0x7ff00000) { + if ((ix & 0x7fffff) | lx != 0) { + const r = if (y == 0) y else x * y; + return Complex(f64).new(x, r); + } + + const xx = @bitCast(f64, (u64(hx - 0x40000000) << 32) | lx); + const r = if (math.isInf(y)) y else math.sin(y) * math.cos(y); + return Complex(f64).new(xx, math.copysign(f64, 0, r)); + } + + if (!math.isFinite(y)) { + const r = if (ix != 0) y - y else x; + return Complex(f64).new(r, y - y); + } + + // x >= 22 + if (ix >= 0x40360000) { + const exp_mx = math.exp(-math.fabs(x)); + return Complex(f64).new(math.copysign(f64, 1, x), 4 * math.sin(y) * math.cos(y) * exp_mx * exp_mx); + } + + // Kahan's algorithm + const t = math.tan(y); + const beta = 1.0 + t * t; + const s = math.sinh(x); + const rho = math.sqrt(1 + s * s); + const den = 1 + beta * s * s; + + return Complex(f64).new((beta * rho * s) / den, t / den); +} + +const epsilon = 0.0001; + +test "complex.ctanh32" { + const a = Complex(f32).new(5, 3); + const c = tanh(a); + + debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon)); + debug.assert(math.approxEq(f32, c.im, -0.000025, epsilon)); +} + +test "complex.ctanh64" { + const a = Complex(f64).new(5, 3); + const c = tanh(a); + + debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon)); + debug.assert(math.approxEq(f64, c.im, -0.000025, epsilon)); +} diff --git a/std/math/exp.zig b/std/math/exp.zig index 4032930a43..21aa558c57 100644 --- a/std/math/exp.zig +++ b/std/math/exp.zig @@ -6,6 +6,7 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; +const builtin = @import("builtin"); pub fn exp(x: var) @typeOf(x) { const T = @typeOf(x); @@ -17,6 +18,8 @@ pub fn exp(x: var) @typeOf(x) { } fn exp32(x_: f32) f32 { + @setFloatMode(this, builtin.FloatMode.Strict); + const half = []f32 { 0.5, -0.5 }; const ln2hi = 6.9314575195e-1; const ln2lo = 1.4286067653e-6; @@ -94,6 +97,8 @@ fn exp32(x_: f32) f32 { } fn exp64(x_: f64) f64 { + @setFloatMode(this, builtin.FloatMode.Strict); + const half = []const f64 { 0.5, -0.5 }; const ln2hi: f64 = 6.93147180369123816490e-01; const ln2lo: f64 = 1.90821492927058770002e-10; diff --git a/std/math/index.zig b/std/math/index.zig index 477dafcbcc..83ba055329 100644 --- a/std/math/index.zig +++ b/std/math/index.zig @@ -129,6 +129,9 @@ pub const cos = @import("cos.zig").cos; pub const sin = @import("sin.zig").sin; pub const tan = @import("tan.zig").tan; +pub const complex = @import("complex/index.zig"); +pub const Complex = complex.Complex; + test "math" { _ = @import("nan.zig"); _ = @import("isnan.zig"); @@ -172,6 +175,8 @@ test "math" { _ = @import("sin.zig"); _ = @import("cos.zig"); _ = @import("tan.zig"); + + _ = @import("complex/index.zig"); } diff --git a/std/math/ln.zig b/std/math/ln.zig index c349ed7c6f..d09494b998 100644 --- a/std/math/ln.zig +++ b/std/math/ln.zig @@ -89,6 +89,8 @@ pub fn ln_32(x_: f32) f32 { } pub fn ln_64(x_: f64) f64 { + @setFloatMode(this, @import("builtin").FloatMode.Strict); + const ln2_hi: f64 = 6.93147180369123816490e-01; const ln2_lo: f64 = 1.90821492927058770002e-10; const Lg1: f64 = 6.666666666666735130e-01; diff --git a/std/math/sqrt.zig b/std/math/sqrt.zig index 690f8b6901..982bd28b72 100644 --- a/std/math/sqrt.zig +++ b/std/math/sqrt.zig @@ -14,26 +14,8 @@ const TypeId = builtin.TypeId; pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typeOf(x).bit_count / 2) else @typeOf(x)) { const T = @typeOf(x); switch (@typeId(T)) { - TypeId.FloatLiteral => { - return T(sqrt64(x)); - }, - TypeId.Float => { - switch (T) { - f32 => { - switch (builtin.arch) { - builtin.Arch.x86_64 => return @import("x86_64/sqrt.zig").sqrt32(x), - else => return sqrt32(x), - } - }, - f64 => { - switch (builtin.arch) { - builtin.Arch.x86_64 => return @import("x86_64/sqrt.zig").sqrt64(x), - else => return sqrt64(x), - } - }, - else => @compileError("sqrt not implemented for " ++ @typeName(T)), - } - }, + TypeId.FloatLiteral => return T(@sqrt(f64, x)), // TODO upgrade to f128 + TypeId.Float => return @sqrt(T, x), TypeId.IntLiteral => comptime { if (x > @maxValue(u128)) { @compileError("sqrt not implemented for comptime_int greater than 128 bits"); @@ -43,269 +25,58 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ } return T(sqrt_int(u128, x)); }, - TypeId.Int => { - return sqrt_int(T, x); - }, + TypeId.Int => return sqrt_int(T, x), else => @compileError("sqrt not implemented for " ++ @typeName(T)), } } -fn sqrt32(x: f32) f32 { - const tiny: f32 = 1.0e-30; - const sign: i32 = @bitCast(i32, u32(0x80000000)); - var ix: i32 = @bitCast(i32, x); - - if ((ix & 0x7F800000) == 0x7F800000) { - return x * x + x; // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = snan - } - - // zero - if (ix <= 0) { - if (ix & ~sign == 0) { - return x; // sqrt (+-0) = +-0 - } - if (ix < 0) { - return math.snan(f32); - } - } - - // normalize - var m = ix >> 23; - if (m == 0) { - // subnormal - var i: i32 = 0; - while (ix & 0x00800000 == 0) : (i += 1) { - ix <<= 1; - } - m -= i - 1; - } - - m -= 127; // unbias exponent - ix = (ix & 0x007FFFFF) | 0x00800000; - - if (m & 1 != 0) { // odd m, double x to even - ix += ix; - } - - m >>= 1; // m = [m / 2] - - // sqrt(x) bit by bit - ix += ix; - var q: i32 = 0; // q = sqrt(x) - var s: i32 = 0; - var r: i32 = 0x01000000; // r = moving bit right -> left - - while (r != 0) { - const t = s + r; - if (t <= ix) { - s = t + r; - ix -= t; - q += r; - } - ix += ix; - r >>= 1; - } - - // floating add to find rounding direction - if (ix != 0) { - var z = 1.0 - tiny; // inexact - if (z >= 1.0) { - z = 1.0 + tiny; - if (z > 1.0) { - q += 2; - } else { - if (q & 1 != 0) { - q += 1; - } - } - } - } - - ix = (q >> 1) + 0x3f000000; - ix += m << 23; - return @bitCast(f32, ix); -} - -// NOTE: The original code is full of implicit signed -> unsigned assumptions and u32 wraparound -// behaviour. Most intermediate i32 values are changed to u32 where appropriate but there are -// potentially some edge cases remaining that are not handled in the same way. -fn sqrt64(x: f64) f64 { - const tiny: f64 = 1.0e-300; - const sign: u32 = 0x80000000; - const u = @bitCast(u64, x); - - var ix0 = u32(u >> 32); - var ix1 = u32(u & 0xFFFFFFFF); - - // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan - if (ix0 & 0x7FF00000 == 0x7FF00000) { - return x * x + x; - } - - // sqrt(+-0) = +-0 - if (x == 0.0) { - return x; - } - // sqrt(-ve) = snan - if (ix0 & sign != 0) { - return math.snan(f64); - } - - // normalize x - var m = i32(ix0 >> 20); - if (m == 0) { - // subnormal - while (ix0 == 0) { - m -= 21; - ix0 |= ix1 >> 11; - ix1 <<= 21; - } - - // subnormal - var i: u32 = 0; - while (ix0 & 0x00100000 == 0) : (i += 1) { - ix0 <<= 1; - } - m -= i32(i) - 1; - ix0 |= ix1 >> u5(32 - i); - ix1 <<= u5(i); - } - - // unbias exponent - m -= 1023; - ix0 = (ix0 & 0x000FFFFF) | 0x00100000; - if (m & 1 != 0) { - ix0 += ix0 + (ix1 >> 31); - ix1 = ix1 +% ix1; - } - m >>= 1; - - // sqrt(x) bit by bit - ix0 += ix0 + (ix1 >> 31); - ix1 = ix1 +% ix1; - - var q: u32 = 0; - var q1: u32 = 0; - var s0: u32 = 0; - var s1: u32 = 0; - var r: u32 = 0x00200000; - var t: u32 = undefined; - var t1: u32 = undefined; - - while (r != 0) { - t = s0 +% r; - if (t <= ix0) { - s0 = t + r; - ix0 -= t; - q += r; - } - ix0 = ix0 +% ix0 +% (ix1 >> 31); - ix1 = ix1 +% ix1; - r >>= 1; - } - - r = sign; - while (r != 0) { - t = s1 +% r; - t = s0; - if (t < ix0 or (t == ix0 and t1 <= ix1)) { - s1 = t1 +% r; - if (t1 & sign == sign and s1 & sign == 0) { - s0 += 1; - } - ix0 -= t; - if (ix1 < t1) { - ix0 -= 1; - } - ix1 = ix1 -% t1; - q1 += r; - } - ix0 = ix0 +% ix0 +% (ix1 >> 31); - ix1 = ix1 +% ix1; - r >>= 1; - } - - // rounding direction - if (ix0 | ix1 != 0) { - var z = 1.0 - tiny; // raise inexact - if (z >= 1.0) { - z = 1.0 + tiny; - if (q1 == 0xFFFFFFFF) { - q1 = 0; - q += 1; - } else if (z > 1.0) { - if (q1 == 0xFFFFFFFE) { - q += 1; - } - q1 += 2; - } else { - q1 += q1 & 1; - } - } - } - - ix0 = (q >> 1) + 0x3FE00000; - ix1 = q1 >> 1; - if (q & 1 != 0) { - ix1 |= 0x80000000; - } - - // NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same - // behaviour at least. - var iix0 = i32(ix0); - iix0 = iix0 +% (m << 20); - - const uz = (u64(iix0) << 32) | ix1; - return @bitCast(f64, uz); -} - test "math.sqrt" { - assert(sqrt(f32(0.0)) == sqrt32(0.0)); - assert(sqrt(f64(0.0)) == sqrt64(0.0)); + assert(sqrt(f32(0.0)) == @sqrt(f32, 0.0)); + assert(sqrt(f64(0.0)) == @sqrt(f64, 0.0)); } test "math.sqrt32" { const epsilon = 0.000001; - assert(sqrt32(0.0) == 0.0); - assert(math.approxEq(f32, sqrt32(2.0), 1.414214, epsilon)); - assert(math.approxEq(f32, sqrt32(3.6), 1.897367, epsilon)); - assert(sqrt32(4.0) == 2.0); - assert(math.approxEq(f32, sqrt32(7.539840), 2.745877, epsilon)); - assert(math.approxEq(f32, sqrt32(19.230934), 4.385309, epsilon)); - assert(sqrt32(64.0) == 8.0); - assert(math.approxEq(f32, sqrt32(64.1), 8.006248, epsilon)); - assert(math.approxEq(f32, sqrt32(8942.230469), 94.563370, epsilon)); + assert(@sqrt(f32, 0.0) == 0.0); + assert(math.approxEq(f32, @sqrt(f32, 2.0), 1.414214, epsilon)); + assert(math.approxEq(f32, @sqrt(f32, 3.6), 1.897367, epsilon)); + assert(@sqrt(f32, 4.0) == 2.0); + assert(math.approxEq(f32, @sqrt(f32, 7.539840), 2.745877, epsilon)); + assert(math.approxEq(f32, @sqrt(f32, 19.230934), 4.385309, epsilon)); + assert(@sqrt(f32, 64.0) == 8.0); + assert(math.approxEq(f32, @sqrt(f32, 64.1), 8.006248, epsilon)); + assert(math.approxEq(f32, @sqrt(f32, 8942.230469), 94.563370, epsilon)); } test "math.sqrt64" { const epsilon = 0.000001; - assert(sqrt64(0.0) == 0.0); - assert(math.approxEq(f64, sqrt64(2.0), 1.414214, epsilon)); - assert(math.approxEq(f64, sqrt64(3.6), 1.897367, epsilon)); - assert(sqrt64(4.0) == 2.0); - assert(math.approxEq(f64, sqrt64(7.539840), 2.745877, epsilon)); - assert(math.approxEq(f64, sqrt64(19.230934), 4.385309, epsilon)); - assert(sqrt64(64.0) == 8.0); - assert(math.approxEq(f64, sqrt64(64.1), 8.006248, epsilon)); - assert(math.approxEq(f64, sqrt64(8942.230469), 94.563367, epsilon)); + assert(@sqrt(f64, 0.0) == 0.0); + assert(math.approxEq(f64, @sqrt(f64, 2.0), 1.414214, epsilon)); + assert(math.approxEq(f64, @sqrt(f64, 3.6), 1.897367, epsilon)); + assert(@sqrt(f64, 4.0) == 2.0); + assert(math.approxEq(f64, @sqrt(f64, 7.539840), 2.745877, epsilon)); + assert(math.approxEq(f64, @sqrt(f64, 19.230934), 4.385309, epsilon)); + assert(@sqrt(f64, 64.0) == 8.0); + assert(math.approxEq(f64, @sqrt(f64, 64.1), 8.006248, epsilon)); + assert(math.approxEq(f64, @sqrt(f64, 8942.230469), 94.563367, epsilon)); } test "math.sqrt32.special" { - assert(math.isPositiveInf(sqrt32(math.inf(f32)))); - assert(sqrt32(0.0) == 0.0); - assert(sqrt32(-0.0) == -0.0); - assert(math.isNan(sqrt32(-1.0))); - assert(math.isNan(sqrt32(math.nan(f32)))); + assert(math.isPositiveInf(@sqrt(f32, math.inf(f32)))); + assert(@sqrt(f32, 0.0) == 0.0); + assert(@sqrt(f32, -0.0) == -0.0); + assert(math.isNan(@sqrt(f32, -1.0))); + assert(math.isNan(@sqrt(f32, math.nan(f32)))); } test "math.sqrt64.special" { - assert(math.isPositiveInf(sqrt64(math.inf(f64)))); - assert(sqrt64(0.0) == 0.0); - assert(sqrt64(-0.0) == -0.0); - assert(math.isNan(sqrt64(-1.0))); - assert(math.isNan(sqrt64(math.nan(f64)))); + assert(math.isPositiveInf(@sqrt(f64, math.inf(f64)))); + assert(@sqrt(f64, 0.0) == 0.0); + assert(@sqrt(f64, -0.0) == -0.0); + assert(math.isNan(@sqrt(f64, -1.0))); + assert(math.isNan(@sqrt(f64, math.nan(f64)))); } fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) { diff --git a/std/math/x86_64/sqrt.zig b/std/math/x86_64/sqrt.zig deleted file mode 100644 index ad9ce0c96c..0000000000 --- a/std/math/x86_64/sqrt.zig +++ /dev/null @@ -1,15 +0,0 @@ -pub fn sqrt32(x: f32) f32 { - return asm ( - \\sqrtss %%xmm0, %%xmm0 - : [ret] "={xmm0}" (-> f32) - : [x] "{xmm0}" (x) - ); -} - -pub fn sqrt64(x: f64) f64 { - return asm ( - \\sqrtsd %%xmm0, %%xmm0 - : [ret] "={xmm0}" (-> f64) - : [x] "{xmm0}" (x) - ); -} diff --git a/std/mem.zig b/std/mem.zig index 8a59d6251b..cc3161cddd 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -11,6 +11,8 @@ pub const Allocator = struct { /// Allocate byte_count bytes and return them in a slice, with the /// slice's pointer aligned at least to alignment bytes. /// The returned newly allocated memory is undefined. + /// `alignment` is guaranteed to be >= 1 + /// `alignment` is guaranteed to be a power of 2 allocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) Error![]u8, /// If `new_byte_count > old_mem.len`: @@ -18,10 +20,12 @@ pub const Allocator = struct { /// * alignment >= alignment of old_mem.ptr /// /// If `new_byte_count <= old_mem.len`: - /// * this function must return successfully. + /// * this function must return successfully. /// * alignment <= alignment of old_mem.ptr /// /// The returned newly allocated memory is undefined. + /// `alignment` is guaranteed to be >= 1 + /// `alignment` is guaranteed to be a power of 2 reallocFn: fn (self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8, /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn` @@ -170,6 +174,20 @@ pub fn dupe(allocator: &Allocator, comptime T: type, m: []const T) ![]T { return new_buf; } +/// Remove values from the beginning of a slice. +pub fn trimLeft(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { + var begin: usize = 0; + while (begin < slice.len and indexOfScalar(T, values_to_strip, slice[begin]) != null) : (begin += 1) {} + return slice[begin..]; +} + +/// Remove values from the end of a slice. +pub fn trimRight(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { + var end: usize = slice.len; + while (end > 0 and indexOfScalar(T, values_to_strip, slice[end - 1]) != null) : (end -= 1) {} + return slice[0..end]; +} + /// Remove values from the beginning and end of a slice. pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []const T { var begin: usize = 0; @@ -180,6 +198,8 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co } test "mem.trim" { + assert(eql(u8, trimLeft(u8, " foo\n ", " \n"), "foo\n ")); + assert(eql(u8, trimRight(u8, " foo\n ", " \n"), " foo")); assert(eql(u8, trim(u8, " foo\n ", " \n"), "foo")); assert(eql(u8, trim(u8, "foo", " \n"), "foo")); } @@ -189,6 +209,17 @@ pub fn indexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { return indexOfScalarPos(T, slice, 0, value); } +/// Linear search for the last index of a scalar value inside a slice. +pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize { + var i: usize = slice.len; + while (i != 0) { + i -= 1; + if (slice[i] == value) + return i; + } + return null; +} + pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize { var i: usize = start_index; while (i < slice.len) : (i += 1) { @@ -202,6 +233,18 @@ pub fn indexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize return indexOfAnyPos(T, slice, 0, values); } +pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?usize { + var i: usize = slice.len; + while (i != 0) { + i -= 1; + for (values) |value| { + if (slice[i] == value) + return i; + } + } + return null; +} + pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, values: []const T) ?usize { var i: usize = start_index; while (i < slice.len) : (i += 1) { @@ -217,6 +260,22 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize return indexOfPos(T, haystack, 0, needle); } +/// Find the index in a slice of a sub-slice, searching from the end backwards. +/// To start looking at a different index, slice the haystack first. +/// TODO is there even a better algorithm for this? +pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize { + if (needle.len > haystack.len) + return null; + + var i: usize = haystack.len - needle.len; + while (true) : (i -= 1) { + if (mem.eql(T, haystack[i..i+needle.len], needle)) + return i; + if (i == 0) + return null; + } +} + // TODO boyer-moore algorithm pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize { if (needle.len > haystack.len) @@ -233,9 +292,19 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee test "mem.indexOf" { assert(??indexOf(u8, "one two three four", "four") == 14); + assert(??lastIndexOf(u8, "one two three two four", "two") == 14); assert(indexOf(u8, "one two three four", "gour") == null); + assert(lastIndexOf(u8, "one two three four", "gour") == null); assert(??indexOf(u8, "foo", "foo") == 0); + assert(??lastIndexOf(u8, "foo", "foo") == 0); assert(indexOf(u8, "foo", "fool") == null); + assert(lastIndexOf(u8, "foo", "lfoo") == null); + assert(lastIndexOf(u8, "foo", "fool") == null); + + assert(??indexOf(u8, "foo foo", "foo") == 0); + assert(??lastIndexOf(u8, "foo foo", "foo") == 4); + assert(??lastIndexOfAny(u8, "boo, cat", "abo") == 6); + assert(??lastIndexOfScalar(u8, "boo", 'o') == 2); } /// Reads an integer from memory with size equal to bytes.len. @@ -355,9 +424,24 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool return if (needle.len > haystack.len) false else eql(T, haystack[0 .. needle.len], needle); } +test "mem.startsWith" { + assert(startsWith(u8, "Bob", "Bo")); + assert(!startsWith(u8, "Needle in haystack", "haystack")); +} + +pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool { + return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle); +} + + +test "mem.endsWith" { + assert(endsWith(u8, "Needle in haystack", "haystack")); + assert(!endsWith(u8, "Bob", "Bo")); +} + pub const SplitIterator = struct { buffer: []const u8, - split_bytes: []const u8, + split_bytes: []const u8, index: usize, pub fn next(self: &SplitIterator) ?[]const u8 { diff --git a/std/os/darwin.zig b/std/os/darwin.zig index 40da55315c..44418649ab 100644 --- a/std/os/darwin.zig +++ b/std/os/darwin.zig @@ -41,6 +41,11 @@ pub const SA_64REGSET = 0x0200; /// signal handler with SA_SIGINFO args with 64 pub const O_LARGEFILE = 0x0000; pub const O_PATH = 0x0000; +pub const F_OK = 0; +pub const X_OK = 1; +pub const W_OK = 2; +pub const R_OK = 4; + pub const O_RDONLY = 0x0000; /// open for reading only pub const O_WRONLY = 0x0001; /// open for writing only pub const O_RDWR = 0x0002; /// open for reading and writing @@ -209,6 +214,10 @@ pub fn fork() usize { return errnoWrap(c.fork()); } +pub fn access(path: &const u8, mode: u32) usize { + return errnoWrap(c.access(path, mode)); +} + pub fn pipe(fds: &[2]i32) usize { comptime assert(i32.bit_count == c_int.bit_count); return errnoWrap(c.pipe(@ptrCast(&c_int, fds))); @@ -251,6 +260,10 @@ pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) u return errnoWrap(c.readlink(path, buf_ptr, buf_len)); } +pub fn gettimeofday(tv: ?&timeval, tz: ?&timezone) usize { + return errnoWrap(c.gettimeofday(tv, tz)); +} + pub fn nanosleep(req: &const timespec, rem: ?×pec) usize { return errnoWrap(c.nanosleep(req, rem)); } @@ -321,3 +334,11 @@ pub fn sigaddset(set: &sigset_t, signo: u5) void { fn errnoWrap(value: isize) usize { return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value); } + + +pub const timezone = c.timezone; +pub const timeval = c.timeval; +pub const mach_timebase_info_data = c.mach_timebase_info_data; + +pub const mach_absolute_time = c.mach_absolute_time; +pub const mach_timebase_info = c.mach_timebase_info; \ No newline at end of file diff --git a/std/os/epoch.zig b/std/os/epoch.zig new file mode 100644 index 0000000000..e1256c1374 --- /dev/null +++ b/std/os/epoch.zig @@ -0,0 +1,26 @@ +/// Epoch reference times in terms of their difference from +/// posix epoch in seconds. +pub const posix = 0; //Jan 01, 1970 AD +pub const dos = 315532800; //Jan 01, 1980 AD +pub const ios = 978307200; //Jan 01, 2001 AD +pub const openvms = -3506716800; //Nov 17, 1858 AD +pub const zos = -2208988800; //Jan 01, 1900 AD +pub const windows = -11644473600; //Jan 01, 1601 AD +pub const amiga = 252460800; //Jan 01, 1978 AD +pub const pickos = -63244800; //Dec 31, 1967 AD +pub const gps = 315964800; //Jan 06, 1980 AD +pub const clr = -62135769600; //Jan 01, 0001 AD + +pub const unix = posix; +pub const android = posix; +pub const os2 = dos; +pub const bios = dos; +pub const vfat = dos; +pub const ntfs = windows; +pub const ntp = zos; +pub const jbase = pickos; +pub const aros = amiga; +pub const morphos = amiga; +pub const brew = gps; +pub const atsc = gps; +pub const go = clr; \ No newline at end of file diff --git a/std/os/file.zig b/std/os/file.zig index eed3a443b9..61fc2b1455 100644 --- a/std/os/file.zig +++ b/std/os/file.zig @@ -85,6 +85,47 @@ pub const File = struct { }; } + pub fn access(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) !bool { + const path_with_null = try std.cstr.addNullByte(allocator, path); + defer allocator.free(path_with_null); + + if (is_posix) { + // mode is ignored and is always F_OK for now + const result = posix.access(path_with_null.ptr, posix.F_OK); + const err = posix.getErrno(result); + if (err > 0) { + return switch (err) { + posix.EACCES => error.PermissionDenied, + posix.EROFS => error.PermissionDenied, + posix.ELOOP => error.PermissionDenied, + posix.ETXTBSY => error.PermissionDenied, + posix.ENOTDIR => error.NotFound, + posix.ENOENT => error.NotFound, + + posix.ENAMETOOLONG => error.NameTooLong, + posix.EINVAL => error.BadMode, + posix.EFAULT => error.BadPathName, + posix.EIO => error.Io, + posix.ENOMEM => error.SystemResources, + else => os.unexpectedErrorPosix(err), + }; + } + return true; + } else if (is_windows) { + if (os.windows.PathFileExists(path_with_null.ptr) == os.windows.TRUE) { + return true; + } + + const err = windows.GetLastError(); + return switch (err) { + windows.ERROR.FILE_NOT_FOUND => error.NotFound, + windows.ERROR.ACCESS_DENIED => error.PermissionDenied, + else => os.unexpectedErrorWindows(err), + }; + } else { + @compileError("TODO implement access for this OS"); + } + } /// Upon success, the stream is in an uninitialized state. To continue using it, /// you must use the open() function. @@ -245,7 +286,9 @@ pub const File = struct { }; } - return stat.mode; + // TODO: we should be able to cast u16 to ModeError!u32, making this + // explicit cast not necessary + return os.FileMode(stat.mode); } else if (is_windows) { return {}; } else { diff --git a/std/os/index.zig b/std/os/index.zig index b6caed6f53..0639490725 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -9,11 +9,10 @@ test "std.os" { _ = @import("darwin.zig"); _ = @import("darwin_errno.zig"); _ = @import("get_user_id.zig"); - _ = @import("linux/errno.zig"); _ = @import("linux/index.zig"); - _ = @import("linux/x86_64.zig"); _ = @import("path.zig"); _ = @import("test.zig"); + _ = @import("time.zig"); _ = @import("windows/index.zig"); } @@ -32,6 +31,7 @@ pub const net = @import("net.zig"); pub const ChildProcess = @import("child_process.zig").ChildProcess; pub const path = @import("path.zig"); pub const File = @import("file.zig").File; +pub const time = @import("time.zig"); pub const FileMode = switch (builtin.os) { Os.windows => void, @@ -478,6 +478,7 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError { }; } +pub var linux_aux_raw = []usize{0} ** 38; pub var posix_environ_raw: []&u8 = undefined; /// Caller must free result when done. @@ -1379,50 +1380,6 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 { } } -pub fn sleep(seconds: usize, nanoseconds: usize) void { - switch(builtin.os) { - Os.linux, Os.macosx, Os.ios => { - posixSleep(u63(seconds), u63(nanoseconds)); - }, - Os.windows => { - const milliseconds = seconds * 1000 + nanoseconds / 1000000; - windows.Sleep(windows.DWORD(milliseconds)); - }, - else => @compileError("Unsupported OS"), - } -} - -const u63 = @IntType(false, 63); -pub fn posixSleep(seconds: u63, nanoseconds: u63) void { - var req = posix.timespec { - .tv_sec = seconds, - .tv_nsec = nanoseconds, - }; - var rem: posix.timespec = undefined; - while (true) { - const ret_val = posix.nanosleep(&req, &rem); - const err = posix.getErrno(ret_val); - if (err == 0) return; - switch (err) { - posix.EFAULT => unreachable, - posix.EINVAL => { - // Sometimes Darwin returns EINVAL for no reason. - // We treat it as a spurious wakeup. - return; - }, - posix.EINTR => { - req = rem; - continue; - }, - else => return, - } - } -} - -test "os.sleep" { - sleep(0, 1); -} - pub fn posix_setuid(uid: u32) !void { const err = posix.getErrno(posix.setuid(uid)); if (err == 0) return; @@ -2384,3 +2341,132 @@ pub fn posixGetSockOptConnectError(sockfd: i32) PosixConnectError!void { posix.ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. } } + +pub const Thread = struct { + pid: i32, + allocator: ?&mem.Allocator, + stack: []u8, + + pub fn wait(self: &const Thread) void { + while (true) { + const pid_value = @atomicLoad(i32, &self.pid, builtin.AtomicOrder.SeqCst); + if (pid_value == 0) break; + const rc = linux.futex_wait(@ptrToInt(&self.pid), linux.FUTEX_WAIT, pid_value, null); + switch (linux.getErrno(rc)) { + 0 => continue, + posix.EINTR => continue, + posix.EAGAIN => continue, + else => unreachable, + } + } + if (self.allocator) |a| { + a.free(self.stack); + } + } +}; + +pub const SpawnThreadError = error { + /// A system-imposed limit on the number of threads was encountered. + /// There are a number of limits that may trigger this error: + /// * the RLIMIT_NPROC soft resource limit (set via setrlimit(2)), + /// which limits the number of processes and threads for a real + /// user ID, was reached; + /// * the kernel's system-wide limit on the number of processes and + /// threads, /proc/sys/kernel/threads-max, was reached (see + /// proc(5)); + /// * the maximum number of PIDs, /proc/sys/kernel/pid_max, was + /// reached (see proc(5)); or + /// * the PID limit (pids.max) imposed by the cgroup "process num‐ + /// ber" (PIDs) controller was reached. + ThreadQuotaExceeded, + + /// The kernel cannot allocate sufficient memory to allocate a task structure + /// for the child, or to copy those parts of the caller's context that need to + /// be copied. + SystemResources, + + Unexpected, +}; + +pub const SpawnThreadAllocatorError = SpawnThreadError || error{OutOfMemory}; + +/// caller must call wait on the returned thread +/// fn startFn(@typeOf(context)) T +/// where T is u8, noreturn, void, or !void +pub fn spawnThreadAllocator(allocator: &mem.Allocator, context: var, comptime startFn: var) SpawnThreadAllocatorError!&Thread { + // TODO compile-time call graph analysis to determine stack upper bound + // https://github.com/zig-lang/zig/issues/157 + const default_stack_size = 8 * 1024 * 1024; + const stack_bytes = try allocator.alloc(u8, default_stack_size); + const thread = try spawnThread(stack_bytes, context, startFn); + thread.allocator = allocator; + return thread; +} + +/// stack must be big enough to store one Thread and one @typeOf(context), each with default alignment, at the end +/// fn startFn(@typeOf(context)) T +/// where T is u8, noreturn, void, or !void +/// caller must call wait on the returned thread +pub fn spawnThread(stack: []u8, context: var, comptime startFn: var) SpawnThreadError!&Thread { + const Context = @typeOf(context); + comptime assert(@ArgType(@typeOf(startFn), 0) == Context); + + var stack_end: usize = @ptrToInt(stack.ptr) + stack.len; + var arg: usize = undefined; + if (@sizeOf(Context) != 0) { + stack_end -= @sizeOf(Context); + stack_end -= stack_end % @alignOf(Context); + assert(stack_end >= @ptrToInt(stack.ptr)); + const context_ptr = @alignCast(@alignOf(Context), @intToPtr(&Context, stack_end)); + *context_ptr = context; + arg = stack_end; + } + + stack_end -= @sizeOf(Thread); + stack_end -= stack_end % @alignOf(Thread); + assert(stack_end >= @ptrToInt(stack.ptr)); + const thread_ptr = @alignCast(@alignOf(Thread), @intToPtr(&Thread, stack_end)); + thread_ptr.stack = stack; + thread_ptr.allocator = null; + + const threadMain = struct { + extern fn threadMain(ctx_addr: usize) u8 { + if (@sizeOf(Context) == 0) { + return startFn({}); + } else { + return startFn(*@intToPtr(&const Context, ctx_addr)); + } + } + }.threadMain; + + const flags = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND + | posix.CLONE_THREAD | posix.CLONE_SYSVSEM // | posix.CLONE_SETTLS + | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | posix.CLONE_DETACHED; + const newtls: usize = 0; + const rc = posix.clone(threadMain, stack_end, flags, arg, &thread_ptr.pid, newtls, &thread_ptr.pid); + const err = posix.getErrno(rc); + switch (err) { + 0 => return thread_ptr, + posix.EAGAIN => return SpawnThreadError.ThreadQuotaExceeded, + posix.EINVAL => unreachable, + posix.ENOMEM => return SpawnThreadError.SystemResources, + posix.ENOSPC => unreachable, + posix.EPERM => unreachable, + posix.EUSERS => unreachable, + else => return unexpectedErrorPosix(err), + } +} + +pub fn posixWait(pid: i32) i32 { + var status: i32 = undefined; + while (true) { + const err = posix.getErrno(posix.waitpid(pid, &status, 0)); + switch (err) { + 0 => return status, + posix.EINTR => continue, + posix.ECHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error. + posix.EINVAL => unreachable, // The options argument was invalid + else => unreachable, + } + } +} diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig index aa2a6d85da..dcd9532d1d 100644 --- a/std/os/linux/index.zig +++ b/std/os/linux/index.zig @@ -1,6 +1,7 @@ const std = @import("../../index.zig"); const assert = std.debug.assert; const builtin = @import("builtin"); +const vdso = @import("vdso.zig"); pub use switch (builtin.arch) { builtin.Arch.x86_64 => @import("x86_64.zig"), builtin.Arch.i386 => @import("i386.zig"), @@ -14,6 +15,22 @@ pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; +pub const FUTEX_WAIT = 0; +pub const FUTEX_WAKE = 1; +pub const FUTEX_FD = 2; +pub const FUTEX_REQUEUE = 3; +pub const FUTEX_CMP_REQUEUE = 4; +pub const FUTEX_WAKE_OP = 5; +pub const FUTEX_LOCK_PI = 6; +pub const FUTEX_UNLOCK_PI = 7; +pub const FUTEX_TRYLOCK_PI = 8; +pub const FUTEX_WAIT_BITSET = 9; + +pub const FUTEX_PRIVATE_FLAG = 128; + +pub const FUTEX_CLOCK_REALTIME = 256; + + pub const PROT_NONE = 0; pub const PROT_READ = 1; pub const PROT_WRITE = 2; @@ -38,6 +55,11 @@ pub const MAP_STACK = 0x20000; pub const MAP_HUGETLB = 0x40000; pub const MAP_FILE = 0; +pub const F_OK = 0; +pub const X_OK = 1; +pub const W_OK = 2; +pub const R_OK = 4; + pub const WNOHANG = 1; pub const WUNTRACED = 2; pub const WSTOPPED = 2; @@ -647,6 +669,10 @@ pub fn fork() usize { return syscall0(SYS_fork); } +pub fn futex_wait(uaddr: usize, futex_op: u32, val: i32, timeout: ?×pec) usize { + return syscall4(SYS_futex, uaddr, futex_op, @bitCast(u32, val), @ptrToInt(timeout)); +} + pub fn getcwd(buf: &u8, size: usize) usize { return syscall2(SYS_getcwd, @ptrToInt(buf), size); } @@ -705,6 +731,10 @@ pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) usize { return syscall4(SYS_pread, usize(fd), @ptrToInt(buf), count, offset); } +pub fn access(path: &const u8, mode: u32) usize { + return syscall2(SYS_access, @ptrToInt(path), mode); +} + pub fn pipe(fd: &[2]i32) usize { return pipe2(fd, 0); } @@ -737,6 +767,16 @@ pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) usize { return syscall4(SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); } +/// See also `clone` (from the arch-specific include) +pub fn clone5(flags: usize, child_stack_ptr: usize, parent_tid: &i32, child_tid: &i32, newtls: usize) usize { + return syscall5(SYS_clone, flags, child_stack_ptr, @ptrToInt(parent_tid), @ptrToInt(child_tid), newtls); +} + +/// See also `clone` (from the arch-specific include) +pub fn clone2(flags: usize, child_stack_ptr: usize) usize { + return syscall2(SYS_clone, flags, child_stack_ptr); +} + pub fn close(fd: i32) usize { return syscall1(SYS_close, usize(fd)); } @@ -766,6 +806,45 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) usize { return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); } +pub fn clock_gettime(clk_id: i32, tp: ×pec) usize { + if (VDSO_CGT_SYM.len != 0) { + const f = @atomicLoad(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, builtin.AtomicOrder.Unordered); + if (@ptrToInt(f) != 0) { + const rc = f(clk_id, tp); + switch (rc) { + 0, @bitCast(usize, isize(-EINVAL)) => return rc, + else => {}, + } + } + } + return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} +var vdso_clock_gettime = init_vdso_clock_gettime; +extern fn init_vdso_clock_gettime(clk: i32, ts: ×pec) usize { + const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM); + var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr); + _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f, + builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic); + if (@ptrToInt(f) == 0) return @bitCast(usize, isize(-ENOSYS)); + return f(clk, ts); +} + +pub fn clock_getres(clk_id: i32, tp: ×pec) usize { + return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn clock_settime(clk_id: i32, tp: &const timespec) usize { + return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); +} + +pub fn gettimeofday(tv: &timeval, tz: &timezone) usize { + return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + +pub fn settimeofday(tv: &const timeval, tz: &const timezone) usize { + return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz)); +} + pub fn nanosleep(req: &const timespec, rem: ?×pec) usize { return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); } @@ -1250,9 +1329,7 @@ pub fn capset(hdrp: &cap_user_header_t, datap: &const cap_user_data_t) usize { return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap)); } -test "import linux test" { - // TODO lazy analysis should prevent this test from being compiled on windows, but - // it is still compiled on windows +test "import" { if (builtin.os == builtin.Os.linux) { _ = @import("test.zig"); } diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig index e427fd5d59..18a6e5f19f 100644 --- a/std/os/linux/test.zig +++ b/std/os/linux/test.zig @@ -1,4 +1,5 @@ const std = @import("../../index.zig"); +const builtin = @import("builtin"); const linux = std.os.linux; const assert = std.debug.assert; diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig new file mode 100644 index 0000000000..f4fb513af9 --- /dev/null +++ b/std/os/linux/vdso.zig @@ -0,0 +1,89 @@ +const std = @import("../../index.zig"); +const elf = std.elf; +const linux = std.os.linux; +const cstr = std.cstr; +const mem = std.mem; + +pub fn lookup(vername: []const u8, name: []const u8) usize { + const vdso_addr = std.os.linux_aux_raw[std.elf.AT_SYSINFO_EHDR]; + if (vdso_addr == 0) return 0; + + const eh = @intToPtr(&elf.Ehdr, vdso_addr); + var ph_addr: usize = vdso_addr + eh.e_phoff; + const ph = @intToPtr(&elf.Phdr, ph_addr); + + var maybe_dynv: ?&usize = null; + var base: usize = @maxValue(usize); + { + var i: usize = 0; + while (i < eh.e_phnum) : ({i += 1; ph_addr += eh.e_phentsize;}) { + const this_ph = @intToPtr(&elf.Phdr, ph_addr); + switch (this_ph.p_type) { + elf.PT_LOAD => base = vdso_addr + this_ph.p_offset - this_ph.p_vaddr, + elf.PT_DYNAMIC => maybe_dynv = @intToPtr(&usize, vdso_addr + this_ph.p_offset), + else => {}, + } + } + } + const dynv = maybe_dynv ?? return 0; + if (base == @maxValue(usize)) return 0; + + var maybe_strings: ?&u8 = null; + var maybe_syms: ?&elf.Sym = null; + var maybe_hashtab: ?&linux.Elf_Symndx = null; + var maybe_versym: ?&u16 = null; + var maybe_verdef: ?&elf.Verdef = null; + + { + var i: usize = 0; + while (dynv[i] != 0) : (i += 2) { + const p = base + dynv[i + 1]; + switch (dynv[i]) { + elf.DT_STRTAB => maybe_strings = @intToPtr(&u8, p), + elf.DT_SYMTAB => maybe_syms = @intToPtr(&elf.Sym, p), + elf.DT_HASH => maybe_hashtab = @intToPtr(&linux.Elf_Symndx, p), + elf.DT_VERSYM => maybe_versym = @intToPtr(&u16, p), + elf.DT_VERDEF => maybe_verdef = @intToPtr(&elf.Verdef, p), + else => {}, + } + } + } + + const strings = maybe_strings ?? return 0; + const syms = maybe_syms ?? return 0; + const hashtab = maybe_hashtab ?? return 0; + if (maybe_verdef == null) maybe_versym = null; + + + const OK_TYPES = (1<>4) & OK_BINDS)) continue; + if (0==syms[i].st_shndx) continue; + if (!mem.eql(u8, name, cstr.toSliceConst(&strings[syms[i].st_name]))) continue; + if (maybe_versym) |versym| { + if (!checkver(??maybe_verdef, versym[i], vername, strings)) + continue; + } + return base + syms[i].st_value; + } + + return 0; +} + +fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &u8) bool { + var def = def_arg; + const vsym = @bitCast(u32, vsym_arg) & 0x7fff; + while (true) { + if (0==(def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym) + break; + if (def.vd_next == 0) + return false; + def = @intToPtr(&elf.Verdef, @ptrToInt(def) + def.vd_next); + } + const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def ) + def.vd_aux); + return mem.eql(u8, vername, cstr.toSliceConst(&strings[aux.vda_name])); +} diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig index cfb2231df9..544b2365ce 100644 --- a/std/os/linux/x86_64.zig +++ b/std/os/linux/x86_64.zig @@ -371,6 +371,13 @@ pub const F_GETOWN_EX = 16; pub const F_GETOWNER_UIDS = 17; + +pub const VDSO_USEFUL = true; +pub const VDSO_CGT_SYM = "__vdso_clock_gettime"; +pub const VDSO_CGT_VER = "LINUX_2.6"; +pub const VDSO_GETCPU_SYM = "__vdso_getcpu"; +pub const VDSO_GETCPU_VER = "LINUX_2.6"; + pub fn syscall0(number: usize) usize { return asm volatile ("syscall" : [ret] "={rax}" (-> usize) @@ -443,6 +450,9 @@ pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz : "rcx", "r11"); } +/// This matches the libc clone function. +pub extern fn clone(func: extern fn(arg: usize) u8, stack: usize, flags: usize, arg: usize, ptid: &i32, tls: usize, ctid: &i32) usize; + pub nakedcc fn restore_rt() void { return asm volatile ("syscall" : @@ -489,6 +499,16 @@ pub const timespec = extern struct { tv_nsec: isize, }; +pub const timeval = extern struct { + tv_sec: isize, + tv_usec: isize, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; + pub const dirent = extern struct { d_ino: usize, d_off: usize, @@ -496,3 +516,4 @@ pub const dirent = extern struct { d_name: u8, // field address is the address of first byte of name }; +pub const Elf_Symndx = u32; diff --git a/std/os/test.zig b/std/os/test.zig index 9c718d5b6b..41afee004a 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -6,6 +6,8 @@ const io = std.io; const a = std.debug.global_allocator; const builtin = @import("builtin"); +const AtomicRmwOp = builtin.AtomicRmwOp; +const AtomicOrder = builtin.AtomicOrder; test "makePath, put some files in it, deleteTree" { if (builtin.os == builtin.Os.windows) { @@ -23,3 +25,57 @@ test "makePath, put some files in it, deleteTree" { assert(err == error.PathNotFound); } } + +test "access file" { + if (builtin.os == builtin.Os.windows) { + return; + } + + try os.makePath(a, "os_test_tmp"); + if (os.File.access(a, "os_test_tmp/file.txt", os.default_file_mode)) |ok| { + unreachable; + } else |err| { + assert(err == error.NotFound); + } + + try io.writeFile(a, "os_test_tmp/file.txt", ""); + assert((try os.File.access(a, "os_test_tmp/file.txt", os.default_file_mode)) == true); + try os.deleteTree(a, "os_test_tmp"); +} + +test "spawn threads" { + if (builtin.os != builtin.Os.linux) { + // TODO implement threads on macos and windows + return; + } + + var direct_allocator = std.heap.DirectAllocator.init(); + defer direct_allocator.deinit(); + + var shared_ctx: i32 = 1; + + const thread1 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, {}, start1); + const thread4 = try std.os.spawnThreadAllocator(&direct_allocator.allocator, &shared_ctx, start2); + + var stack1: [1024]u8 = undefined; + var stack2: [1024]u8 = undefined; + + const thread2 = try std.os.spawnThread(stack1[0..], &shared_ctx, start2); + const thread3 = try std.os.spawnThread(stack2[0..], &shared_ctx, start2); + + thread1.wait(); + thread2.wait(); + thread3.wait(); + thread4.wait(); + + assert(shared_ctx == 4); +} + +fn start1(ctx: void) u8 { + return 0; +} + +fn start2(ctx: &i32) u8 { + _ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.SeqCst); + return 0; +} diff --git a/std/os/time.zig b/std/os/time.zig new file mode 100644 index 0000000000..e9fbf9798c --- /dev/null +++ b/std/os/time.zig @@ -0,0 +1,288 @@ +const std = @import("../index.zig"); +const builtin = @import("builtin"); +const Os = builtin.Os; +const debug = std.debug; + +const windows = std.os.windows; +const linux = std.os.linux; +const darwin = std.os.darwin; +const posix = std.os.posix; + +pub const epoch = @import("epoch.zig"); + +/// Sleep for the specified duration +pub fn sleep(seconds: usize, nanoseconds: usize) void { + switch (builtin.os) { + Os.linux, Os.macosx, Os.ios => { + posixSleep(u63(seconds), u63(nanoseconds)); + }, + Os.windows => { + const ns_per_ms = ns_per_s / ms_per_s; + const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms; + windows.Sleep(windows.DWORD(milliseconds)); + }, + else => @compileError("Unsupported OS"), + } +} + +const u63 = @IntType(false, 63); +pub fn posixSleep(seconds: u63, nanoseconds: u63) void { + var req = posix.timespec { + .tv_sec = seconds, + .tv_nsec = nanoseconds, + }; + var rem: posix.timespec = undefined; + while (true) { + const ret_val = posix.nanosleep(&req, &rem); + const err = posix.getErrno(ret_val); + if (err == 0) return; + switch (err) { + posix.EFAULT => unreachable, + posix.EINVAL => { + // Sometimes Darwin returns EINVAL for no reason. + // We treat it as a spurious wakeup. + return; + }, + posix.EINTR => { + req = rem; + continue; + }, + else => return, + } + } +} + +/// Get the posix timestamp, UTC, in seconds +pub fn timestamp() u64 { + return @divFloor(milliTimestamp(), ms_per_s); +} + +/// Get the posix timestamp, UTC, in milliseconds +pub const milliTimestamp = switch (builtin.os) { + Os.windows => milliTimestampWindows, + Os.linux => milliTimestampPosix, + Os.macosx, Os.ios => milliTimestampDarwin, + else => @compileError("Unsupported OS"), +}; + +fn milliTimestampWindows() u64 { + //FileTime has a granularity of 100 nanoseconds + // and uses the NTFS/Windows epoch + var ft: i64 = undefined; + windows.GetSystemTimeAsFileTime(&ft); + const hns_per_ms = (ns_per_s / 100) / ms_per_s; + const epoch_adj = epoch.windows * ms_per_s; + return u64(@divFloor(ft, hns_per_ms) + epoch_adj); +} + +fn milliTimestampDarwin() u64 { + //Sources suggest MacOS 10.12 has support for + // posix clock_gettime. + var tv: darwin.timeval = undefined; + var err = darwin.gettimeofday(&tv, null); + debug.assert(err == 0); + const sec_ms = u64(tv.tv_sec) * ms_per_s; + const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s); + return u64(sec_ms) + u64(usec_ms); +} + +fn milliTimestampPosix() u64 { + //From what I can tell there's no reason clock_gettime + // should ever fail for us with CLOCK_REALTIME, + // seccomp aside. + var ts: posix.timespec = undefined; + const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts); + debug.assert(err == 0); + const sec_ms = u64(ts.tv_sec) * ms_per_s; + const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s); + return sec_ms + nsec_ms; +} + +/// Divisions of a second +pub const ns_per_s = 1000000000; +pub const us_per_s = 1000000; +pub const ms_per_s = 1000; +pub const cs_per_s = 100; + +/// Common time divisions +pub const s_per_min = 60; +pub const s_per_hour = s_per_min * 60; +pub const s_per_day = s_per_hour * 24; +pub const s_per_week = s_per_day * 7; + + +/// A monotonic high-performance timer. +/// Timer.start() must be called to initialize the struct, which captures +/// the counter frequency on windows and darwin, records the resolution, +/// and gives the user an oportunity to check for the existnece of +/// monotonic clocks without forcing them to check for error on each read. +/// .resolution is in nanoseconds on all platforms but .start_time's meaning +/// depends on the OS. On Windows and Darwin it is a hardware counter +/// value that requires calculation to convert to a meaninful unit. +pub const Timer = struct { + + //if we used resolution's value when performing the + // performance counter calc on windows/darwin, it would + // be less precise + frequency: switch (builtin.os) { + Os.windows => u64, + Os.macosx, Os.ios => darwin.mach_timebase_info_data, + else => void, + }, + resolution: u64, + start_time: u64, + + + //At some point we may change our minds on RAW, but for now we're + // sticking with posix standard MONOTONIC. For more information, see: + // https://github.com/zig-lang/zig/pull/933 + // + //const monotonic_clock_id = switch(builtin.os) { + // Os.linux => linux.CLOCK_MONOTONIC_RAW, + // else => posix.CLOCK_MONOTONIC, + //}; + const monotonic_clock_id = posix.CLOCK_MONOTONIC; + + + /// Initialize the timer structure. + //This gives us an oportunity to grab the counter frequency in windows. + //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000. + //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not + // supported, or if the timespec pointer is out of bounds, which should be + // impossible here barring cosmic rays or other such occurances of + // incredibly bad luck. + //On Darwin: This cannot fail, as far as I am able to tell. + const TimerError = error{TimerUnsupported, Unexpected}; + pub fn start() TimerError!Timer { + var self: Timer = undefined; + + switch (builtin.os) { + Os.windows => { + var freq: i64 = undefined; + var err = windows.QueryPerformanceFrequency(&freq); + if (err == windows.FALSE) return error.TimerUnsupported; + self.frequency = u64(freq); + self.resolution = @divFloor(ns_per_s, self.frequency); + + var start_time: i64 = undefined; + err = windows.QueryPerformanceCounter(&start_time); + debug.assert(err != windows.FALSE); + self.start_time = u64(start_time); + }, + Os.linux => { + //On Linux, seccomp can do arbitrary things to our ability to call + // syscalls, including return any errno value it wants and + // inconsistently throwing errors. Since we can't account for + // abuses of seccomp in a reasonable way, we'll assume that if + // seccomp is going to block us it will at least do so consistently + var ts: posix.timespec = undefined; + var result = posix.clock_getres(monotonic_clock_id, &ts); + var errno = posix.getErrno(result); + switch (errno) { + 0 => {}, + posix.EINVAL => return error.TimerUnsupported, + else => return std.os.unexpectedErrorPosix(errno), + } + self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec); + + result = posix.clock_gettime(monotonic_clock_id, &ts); + errno = posix.getErrno(result); + if (errno != 0) return std.os.unexpectedErrorPosix(errno); + self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec); + }, + Os.macosx, Os.ios => { + darwin.mach_timebase_info(&self.frequency); + self.resolution = @divFloor(self.frequency.numer, self.frequency.denom); + self.start_time = darwin.mach_absolute_time(); + }, + else => @compileError("Unsupported OS"), + } + return self; + } + + /// Reads the timer value since start or the last reset in nanoseconds + pub fn read(self: &Timer) u64 { + var clock = clockNative() - self.start_time; + return switch (builtin.os) { + Os.windows => @divFloor(clock * ns_per_s, self.frequency), + Os.linux => clock, + Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom), + else => @compileError("Unsupported OS"), + }; + } + + /// Resets the timer value to 0/now. + pub fn reset(self: &Timer) void + { + self.start_time = clockNative(); + } + + /// Returns the current value of the timer in nanoseconds, then resets it + pub fn lap(self: &Timer) u64 { + var now = clockNative(); + var lap_time = self.read(); + self.start_time = now; + return lap_time; + } + + + const clockNative = switch (builtin.os) { + Os.windows => clockWindows, + Os.linux => clockLinux, + Os.macosx, Os.ios => clockDarwin, + else => @compileError("Unsupported OS"), + }; + + fn clockWindows() u64 { + var result: i64 = undefined; + var err = windows.QueryPerformanceCounter(&result); + debug.assert(err != windows.FALSE); + return u64(result); + } + + fn clockDarwin() u64 { + return darwin.mach_absolute_time(); + } + + fn clockLinux() u64 { + var ts: posix.timespec = undefined; + var result = posix.clock_gettime(monotonic_clock_id, &ts); + debug.assert(posix.getErrno(result) == 0); + return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec); + } +}; + + + + + +test "os.time.sleep" { + sleep(0, 1); +} + +test "os.time.timestamp" { + const ns_per_ms = (ns_per_s / ms_per_s); + const margin = 50; + + const time_0 = milliTimestamp(); + sleep(0, ns_per_ms); + const time_1 = milliTimestamp(); + const interval = time_1 - time_0; + debug.assert(interval > 0 and interval < margin); +} + +test "os.time.Timer" { + const ns_per_ms = (ns_per_s / ms_per_s); + const margin = ns_per_ms * 50; + + var timer = try Timer.start(); + sleep(0, 10 * ns_per_ms); + const time_0 = timer.read(); + debug.assert(time_0 > 0 and time_0 < margin); + + const time_1 = timer.lap(); + debug.assert(time_1 > time_0); + + timer.reset(); + debug.assert(timer.read() < time_1); +} diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig index 2709cf2a78..d6ef7205e8 100644 --- a/std/os/windows/index.zig +++ b/std/os/windows/index.zig @@ -61,6 +61,8 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(hFile: HANDLE, lpsz pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE; +pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(?&FILETIME) void; + pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE; pub extern "kernel32" stdcallcc fn HeapDestroy(hHeap: HANDLE) BOOL; pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: &c_void, dwBytes: SIZE_T) ?&c_void; @@ -77,6 +79,12 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem pub extern "kernel32" stdcallcc fn MoveFileExA(lpExistingFileName: LPCSTR, lpNewFileName: LPCSTR, dwFlags: DWORD) BOOL; + +pub extern "kernel32" stdcallcc fn QueryPerformanceCounter(lpPerformanceCount: &LARGE_INTEGER) BOOL; + +pub extern "kernel32" stdcallcc fn QueryPerformanceFrequency(lpFrequency: &LARGE_INTEGER) BOOL; + +pub extern "kernel32" stdcallcc fn PathFileExists(pszPath: ?LPCTSTR) BOOL; pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: &c_void, in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD, @@ -137,6 +145,7 @@ pub const UNICODE = false; pub const WCHAR = u16; pub const WORD = u16; pub const LARGE_INTEGER = i64; +pub const FILETIME = i64; pub const TRUE = 1; pub const FALSE = 0; @@ -308,3 +317,7 @@ pub const FILE_END = 2; pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000; pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004; pub const HEAP_NO_SERIALIZE = 0x00000001; + +test "import" { + _ = @import("util.zig"); +} diff --git a/std/rand/index.zig b/std/rand/index.zig index 6a746fce92..bd6209009e 100644 --- a/std/rand/index.zig +++ b/std/rand/index.zig @@ -19,6 +19,7 @@ const builtin = @import("builtin"); const assert = std.debug.assert; const mem = std.mem; const math = std.math; +const ziggurat = @import("ziggurat.zig"); // When you need fast unbiased random numbers pub const DefaultPrng = Xoroshiro128; @@ -109,15 +110,28 @@ pub const Random = struct { } } - /// Return a floating point value normally distributed in the range [0, 1]. + /// Return a floating point value normally distributed with mean = 0, stddev = 1. + /// + /// To use different parameters, use: floatNorm(...) * desiredStddev + desiredMean. pub fn floatNorm(r: &Random, comptime T: type) T { - // TODO(tiehuis): See https://www.doornik.com/research/ziggurat.pdf - @compileError("floatNorm is unimplemented"); + const value = ziggurat.next_f64(r, ziggurat.NormDist); + switch (T) { + f32 => return f32(value), + f64 => return value, + else => @compileError("unknown floating point type"), + } } - /// Return a exponentially distributed float between (0, @maxValue(f64)) + /// Return an exponentially distributed float with a rate parameter of 1. + /// + /// To use a different rate parameter, use: floatExp(...) / desiredRate. pub fn floatExp(r: &Random, comptime T: type) T { - @compileError("floatExp is unimplemented"); + const value = ziggurat.next_f64(r, ziggurat.ExpDist); + switch (T) { + f32 => return f32(value), + f64 => return value, + else => @compileError("unknown floating point type"), + } } /// Shuffle a slice into a random order. diff --git a/std/rand/ziggurat.zig b/std/rand/ziggurat.zig new file mode 100644 index 0000000000..7790b71d26 --- /dev/null +++ b/std/rand/ziggurat.zig @@ -0,0 +1,146 @@ +// Implements ZIGNOR [1]. +// +// [1]: Jurgen A. Doornik (2005). [*An Improved Ziggurat Method to Generate Normal Random Samples*] +// (https://www.doornik.com/research/ziggurat.pdf). Nuffield College, Oxford. +// +// rust/rand used as a reference; +// +// NOTE: This seems interesting but reference code is a bit hard to grok: +// https://sbarral.github.io/etf. + +const std = @import("../index.zig"); +const math = std.math; +const Random = std.rand.Random; + +pub fn next_f64(random: &Random, comptime tables: &const ZigTable) f64 { + while (true) { + // We manually construct a float from parts as we can avoid an extra random lookup here by + // using the unused exponent for the lookup table entry. + const bits = random.scalar(u64); + const i = usize(bits & 0xff); + + const u = blk: { + if (tables.is_symmetric) { + // Generate a value in the range [2, 4) and scale into [-1, 1) + const repr = ((0x3ff + 1) << 52) | (bits >> 12); + break :blk @bitCast(f64, repr) - 3.0; + } else { + // Generate a value in the range [1, 2) and scale into (0, 1) + const repr = (0x3ff << 52) | (bits >> 12); + break :blk @bitCast(f64, repr) - (1.0 - math.f64_epsilon / 2.0); + } + }; + + const x = u * tables.x[i]; + const test_x = if (tables.is_symmetric) math.fabs(x) else x; + + // equivalent to |u| < tables.x[i+1] / tables.x[i] (or u < tables.x[i+1] / tables.x[i]) + if (test_x < tables.x[i + 1]) { + return x; + } + + if (i == 0) { + return tables.zero_case(random, u); + } + + // equivalent to f1 + DRanU() * (f0 - f1) < 1 + if (tables.f[i + 1] + (tables.f[i] - tables.f[i + 1]) * random.float(f64) < tables.pdf(x)) { + return x; + } + } +} + +pub const ZigTable = struct { + r: f64, + x: [257]f64, + f: [257]f64, + + // probability density function used as a fallback + pdf: fn(f64) f64, + // whether the distribution is symmetric + is_symmetric: bool, + // fallback calculation in the case we are in the 0 block + zero_case: fn(&Random, f64) f64, +}; + +// zigNorInit +fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64, + comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable { + var tables: ZigTable = undefined; + + tables.is_symmetric = is_symmetric; + tables.r = r; + tables.pdf = f; + tables.zero_case = zero_case; + + tables.x[0] = v / f(r); + tables.x[1] = r; + + for (tables.x[2..256]) |*entry, i| { + const last = tables.x[2 + i - 1]; + *entry = f_inv(v / last + f(last)); + } + tables.x[256] = 0; + + for (tables.f[0..]) |*entry, i| { + *entry = f(tables.x[i]); + } + + return tables; +} + +// N(0, 1) +pub const NormDist = blk: { + @setEvalBranchQuota(30000); + break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case); +}; + +const norm_r = 3.6541528853610088; +const norm_v = 0.00492867323399; + +fn norm_f(x: f64) f64 { return math.exp(-x * x / 2.0); } +fn norm_f_inv(y: f64) f64 { return math.sqrt(-2.0 * math.ln(y)); } +fn norm_zero_case(random: &Random, u: f64) f64 { + var x: f64 = 1; + var y: f64 = 0; + + while (-2.0 * y < x * x) { + x = math.ln(random.float(f64)) / norm_r; + y = math.ln(random.float(f64)); + } + + if (u < 0) { + return x - norm_r; + } else { + return norm_r - x; + } +} + +test "ziggurant normal dist sanity" { + var prng = std.rand.DefaultPrng.init(0); + var i: usize = 0; + while (i < 1000) : (i += 1) { + _ = prng.random.floatNorm(f64); + } +} + +// Exp(1) +pub const ExpDist = blk: { + @setEvalBranchQuota(30000); + break :blk ZigTableGen(false, exp_r, exp_v, exp_f, exp_f_inv, exp_zero_case); +}; + +const exp_r = 7.69711747013104972; +const exp_v = 0.0039496598225815571993; + +fn exp_f(x: f64) f64 { return math.exp(-x); } +fn exp_f_inv(y: f64) f64 { return -math.ln(y); } +fn exp_zero_case(random: &Random, _: f64) f64 { return exp_r - math.ln(random.float(f64)); } + +test "ziggurant exp dist sanity" { + var prng = std.rand.DefaultPrng.init(0); + var i: usize = 0; + while (i < 1000) : (i += 1) { + _ = prng.random.floatExp(f64); + } +} diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index d2c22c13e1..1dc7e24869 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -48,22 +48,33 @@ extern fn WinMainCRTStartup() noreturn { fn posixCallMainAndExit() noreturn { const argc = *argc_ptr; const argv = @ptrCast(&&u8, &argc_ptr[1]); - const envp = @ptrCast(&?&u8, &argv[argc + 1]); + const envp_nullable = @ptrCast(&?&u8, &argv[argc + 1]); + var envp_count: usize = 0; + while (envp_nullable[envp_count]) |_| : (envp_count += 1) {} + const envp = @ptrCast(&&u8, envp_nullable)[0..envp_count]; + if (builtin.os == builtin.Os.linux) { + const auxv = &@ptrCast(&usize, envp.ptr)[envp_count + 1]; + var i: usize = 0; + while (auxv[i] != 0) : (i += 2) { + if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i+1]; + } + std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size); + } + std.os.posix.exit(callMainWithArgs(argc, argv, envp)); } -fn callMainWithArgs(argc: usize, argv: &&u8, envp: &?&u8) u8 { +fn callMainWithArgs(argc: usize, argv: &&u8, envp: []&u8) u8 { std.os.ArgIteratorPosix.raw = argv[0..argc]; - - var env_count: usize = 0; - while (envp[env_count] != null) : (env_count += 1) {} - std.os.posix_environ_raw = @ptrCast(&&u8, envp)[0..env_count]; - + std.os.posix_environ_raw = envp; return callMain(); } extern fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) i32 { - return callMainWithArgs(usize(c_argc), c_argv, c_envp); + var env_count: usize = 0; + while (c_envp[env_count] != null) : (env_count += 1) {} + const envp = @ptrCast(&&u8, c_envp)[0..env_count]; + return callMainWithArgs(usize(c_argc), c_argv, envp); } fn callMain() u8 { diff --git a/std/special/builtin.zig b/std/special/builtin.zig index 9de0aa7679..a5126bc4f3 100644 --- a/std/special/builtin.zig +++ b/std/special/builtin.zig @@ -54,14 +54,51 @@ export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 { } comptime { - if (builtin.mode != builtin.Mode.ReleaseFast and builtin.os != builtin.Os.windows) { + if (builtin.mode != builtin.Mode.ReleaseFast and + builtin.mode != builtin.Mode.ReleaseSmall and + builtin.os != builtin.Os.windows) { @export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong); } + if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) { + @export("clone", clone, builtin.GlobalLinkage.Strong); + } } extern fn __stack_chk_fail() noreturn { @panic("stack smashing detected"); } +// TODO we should be able to put this directly in std/linux/x86_64.zig but +// it causes a segfault in release mode. this is a workaround of calling it +// across .o file boundaries. fix comptime @ptrCast of nakedcc functions. +nakedcc fn clone() void { + asm volatile ( + \\ xor %%eax,%%eax + \\ mov $56,%%al + \\ mov %%rdi,%%r11 + \\ mov %%rdx,%%rdi + \\ mov %%r8,%%rdx + \\ mov %%r9,%%r8 + \\ mov 8(%%rsp),%%r10 + \\ mov %%r11,%%r9 + \\ and $-16,%%rsi + \\ sub $8,%%rsi + \\ mov %%rcx,(%%rsi) + \\ syscall + \\ test %%eax,%%eax + \\ jnz 1f + \\ xor %%ebp,%%ebp + \\ pop %%rdi + \\ call *%%r9 + \\ mov %%eax,%%edi + \\ xor %%eax,%%eax + \\ mov $60,%%al + \\ syscall + \\ hlt + \\1: ret + \\ + ); +} + const math = @import("../math/index.zig"); export fn fmodf(x: f32, y: f32) f32 { return generic_fmod(f32, x, y); } @@ -159,3 +196,212 @@ fn isNan(comptime T: type, bits: T) bool { unreachable; } } + +// NOTE: The original code is full of implicit signed -> unsigned assumptions and u32 wraparound +// behaviour. Most intermediate i32 values are changed to u32 where appropriate but there are +// potentially some edge cases remaining that are not handled in the same way. +export fn sqrt(x: f64) f64 { + const tiny: f64 = 1.0e-300; + const sign: u32 = 0x80000000; + const u = @bitCast(u64, x); + + var ix0 = u32(u >> 32); + var ix1 = u32(u & 0xFFFFFFFF); + + // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = nan + if (ix0 & 0x7FF00000 == 0x7FF00000) { + return x * x + x; + } + + // sqrt(+-0) = +-0 + if (x == 0.0) { + return x; + } + // sqrt(-ve) = snan + if (ix0 & sign != 0) { + return math.snan(f64); + } + + // normalize x + var m = i32(ix0 >> 20); + if (m == 0) { + // subnormal + while (ix0 == 0) { + m -= 21; + ix0 |= ix1 >> 11; + ix1 <<= 21; + } + + // subnormal + var i: u32 = 0; + while (ix0 & 0x00100000 == 0) : (i += 1) { + ix0 <<= 1; + } + m -= i32(i) - 1; + ix0 |= ix1 >> u5(32 - i); + ix1 <<= u5(i); + } + + // unbias exponent + m -= 1023; + ix0 = (ix0 & 0x000FFFFF) | 0x00100000; + if (m & 1 != 0) { + ix0 += ix0 + (ix1 >> 31); + ix1 = ix1 +% ix1; + } + m >>= 1; + + // sqrt(x) bit by bit + ix0 += ix0 + (ix1 >> 31); + ix1 = ix1 +% ix1; + + var q: u32 = 0; + var q1: u32 = 0; + var s0: u32 = 0; + var s1: u32 = 0; + var r: u32 = 0x00200000; + var t: u32 = undefined; + var t1: u32 = undefined; + + while (r != 0) { + t = s0 +% r; + if (t <= ix0) { + s0 = t + r; + ix0 -= t; + q += r; + } + ix0 = ix0 +% ix0 +% (ix1 >> 31); + ix1 = ix1 +% ix1; + r >>= 1; + } + + r = sign; + while (r != 0) { + t = s1 +% r; + t = s0; + if (t < ix0 or (t == ix0 and t1 <= ix1)) { + s1 = t1 +% r; + if (t1 & sign == sign and s1 & sign == 0) { + s0 += 1; + } + ix0 -= t; + if (ix1 < t1) { + ix0 -= 1; + } + ix1 = ix1 -% t1; + q1 += r; + } + ix0 = ix0 +% ix0 +% (ix1 >> 31); + ix1 = ix1 +% ix1; + r >>= 1; + } + + // rounding direction + if (ix0 | ix1 != 0) { + var z = 1.0 - tiny; // raise inexact + if (z >= 1.0) { + z = 1.0 + tiny; + if (q1 == 0xFFFFFFFF) { + q1 = 0; + q += 1; + } else if (z > 1.0) { + if (q1 == 0xFFFFFFFE) { + q += 1; + } + q1 += 2; + } else { + q1 += q1 & 1; + } + } + } + + ix0 = (q >> 1) + 0x3FE00000; + ix1 = q1 >> 1; + if (q & 1 != 0) { + ix1 |= 0x80000000; + } + + // NOTE: musl here appears to rely on signed twos-complement wraparound. +% has the same + // behaviour at least. + var iix0 = i32(ix0); + iix0 = iix0 +% (m << 20); + + const uz = (u64(iix0) << 32) | ix1; + return @bitCast(f64, uz); +} + +export fn sqrtf(x: f32) f32 { + const tiny: f32 = 1.0e-30; + const sign: i32 = @bitCast(i32, u32(0x80000000)); + var ix: i32 = @bitCast(i32, x); + + if ((ix & 0x7F800000) == 0x7F800000) { + return x * x + x; // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = snan + } + + // zero + if (ix <= 0) { + if (ix & ~sign == 0) { + return x; // sqrt (+-0) = +-0 + } + if (ix < 0) { + return math.snan(f32); + } + } + + // normalize + var m = ix >> 23; + if (m == 0) { + // subnormal + var i: i32 = 0; + while (ix & 0x00800000 == 0) : (i += 1) { + ix <<= 1; + } + m -= i - 1; + } + + m -= 127; // unbias exponent + ix = (ix & 0x007FFFFF) | 0x00800000; + + if (m & 1 != 0) { // odd m, double x to even + ix += ix; + } + + m >>= 1; // m = [m / 2] + + // sqrt(x) bit by bit + ix += ix; + var q: i32 = 0; // q = sqrt(x) + var s: i32 = 0; + var r: i32 = 0x01000000; // r = moving bit right -> left + + while (r != 0) { + const t = s + r; + if (t <= ix) { + s = t + r; + ix -= t; + q += r; + } + ix += ix; + r >>= 1; + } + + // floating add to find rounding direction + if (ix != 0) { + var z = 1.0 - tiny; // inexact + if (z >= 1.0) { + z = 1.0 + tiny; + if (z > 1.0) { + q += 2; + } else { + if (q & 1 != 0) { + q += 1; + } + } + } + } + + ix = (q >> 1) + 0x3f000000; + ix += m << 23; + return @bitCast(f32, ix); +} diff --git a/std/zig/ast.zig b/std/zig/ast.zig index 230fe26568..76977a979a 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -6,7 +6,6 @@ const mem = std.mem; pub const Node = struct { id: Id, - comment: ?&NodeLineComment, pub const Id = enum { // Top level @@ -74,1734 +73,1686 @@ pub const Node = struct { FieldInitializer, }; - const IdTypePair = struct { - id: Id, - Type: type, - }; - - // TODO: When @field exists, we could generate this by iterating over all members of `Id`, - // and making an array of `IdTypePair { .id = @field(Id, @memberName(Id, i)), .Type = @field(ast, "Node" ++ @memberName(Id, i)) }` - const idTypeTable = []IdTypePair { - IdTypePair { .id = Id.Root, .Type = NodeRoot }, - IdTypePair { .id = Id.Use, .Type = NodeUse }, - IdTypePair { .id = Id.TestDecl, .Type = NodeTestDecl }, - - IdTypePair { .id = Id.VarDecl, .Type = NodeVarDecl }, - IdTypePair { .id = Id.Defer, .Type = NodeDefer }, - - IdTypePair { .id = Id.InfixOp, .Type = NodeInfixOp }, - IdTypePair { .id = Id.PrefixOp, .Type = NodePrefixOp }, - IdTypePair { .id = Id.SuffixOp, .Type = NodeSuffixOp }, - - IdTypePair { .id = Id.Switch, .Type = NodeSwitch }, - IdTypePair { .id = Id.While, .Type = NodeWhile }, - IdTypePair { .id = Id.For, .Type = NodeFor }, - IdTypePair { .id = Id.If, .Type = NodeIf }, - IdTypePair { .id = Id.ControlFlowExpression, .Type = NodeControlFlowExpression }, - IdTypePair { .id = Id.Suspend, .Type = NodeSuspend }, - - IdTypePair { .id = Id.VarType, .Type = NodeVarType }, - IdTypePair { .id = Id.ErrorType, .Type = NodeErrorType }, - IdTypePair { .id = Id.FnProto, .Type = NodeFnProto }, - - IdTypePair { .id = Id.IntegerLiteral, .Type = NodeIntegerLiteral }, - IdTypePair { .id = Id.FloatLiteral, .Type = NodeFloatLiteral }, - IdTypePair { .id = Id.StringLiteral, .Type = NodeStringLiteral }, - IdTypePair { .id = Id.MultilineStringLiteral, .Type = NodeMultilineStringLiteral }, - IdTypePair { .id = Id.CharLiteral, .Type = NodeCharLiteral }, - IdTypePair { .id = Id.BoolLiteral, .Type = NodeBoolLiteral }, - IdTypePair { .id = Id.NullLiteral, .Type = NodeNullLiteral }, - IdTypePair { .id = Id.UndefinedLiteral, .Type = NodeUndefinedLiteral }, - IdTypePair { .id = Id.ThisLiteral, .Type = NodeThisLiteral }, - IdTypePair { .id = Id.Unreachable, .Type = NodeUnreachable }, - IdTypePair { .id = Id.Identifier, .Type = NodeIdentifier }, - IdTypePair { .id = Id.GroupedExpression, .Type = NodeGroupedExpression }, - IdTypePair { .id = Id.BuiltinCall, .Type = NodeBuiltinCall }, - IdTypePair { .id = Id.ErrorSetDecl, .Type = NodeErrorSetDecl }, - IdTypePair { .id = Id.ContainerDecl, .Type = NodeContainerDecl }, - IdTypePair { .id = Id.Asm, .Type = NodeAsm }, - IdTypePair { .id = Id.Comptime, .Type = NodeComptime }, - IdTypePair { .id = Id.Block, .Type = NodeBlock }, - - IdTypePair { .id = Id.LineComment, .Type = NodeLineComment }, - IdTypePair { .id = Id.SwitchCase, .Type = NodeSwitchCase }, - IdTypePair { .id = Id.SwitchElse, .Type = NodeSwitchElse }, - IdTypePair { .id = Id.Else, .Type = NodeElse }, - IdTypePair { .id = Id.Payload, .Type = NodePayload }, - IdTypePair { .id = Id.PointerPayload, .Type = NodePointerPayload }, - IdTypePair { .id = Id.PointerIndexPayload, .Type = NodePointerIndexPayload }, - IdTypePair { .id = Id.StructField, .Type = NodeStructField }, - IdTypePair { .id = Id.UnionTag, .Type = NodeUnionTag }, - IdTypePair { .id = Id.EnumTag, .Type = NodeEnumTag }, - IdTypePair { .id = Id.AsmInput, .Type = NodeAsmInput }, - IdTypePair { .id = Id.AsmOutput, .Type = NodeAsmOutput }, - IdTypePair { .id = Id.AsyncAttribute, .Type = NodeAsyncAttribute }, - IdTypePair { .id = Id.ParamDecl, .Type = NodeParamDecl }, - IdTypePair { .id = Id.FieldInitializer, .Type = NodeFieldInitializer }, - }; - - pub fn IdToType(comptime id: Id) type { - inline for (idTypeTable) |id_type_pair| { - if (id == id_type_pair.id) - return id_type_pair.Type; - } - - unreachable; - } - - pub fn typeToId(comptime T: type) Id { - inline for (idTypeTable) |id_type_pair| { - if (T == id_type_pair.Type) - return id_type_pair.id; - } - - unreachable; - } - pub fn iterate(base: &Node, index: usize) ?&Node { - inline for (idTypeTable) |id_type_pair| { - if (base.id == id_type_pair.id) - return @fieldParentPtr(id_type_pair.Type, "base", base).iterate(index); + comptime var i = 0; + inline while (i < @memberCount(Id)) : (i += 1) { + if (base.id == @field(Id, @memberName(Id, i))) { + const T = @field(Node, @memberName(Id, i)); + return @fieldParentPtr(T, "base", base).iterate(index); + } } - unreachable; } pub fn firstToken(base: &Node) Token { - inline for (idTypeTable) |id_type_pair| { - if (base.id == id_type_pair.id) - return @fieldParentPtr(id_type_pair.Type, "base", base).firstToken(); + comptime var i = 0; + inline while (i < @memberCount(Id)) : (i += 1) { + if (base.id == @field(Id, @memberName(Id, i))) { + const T = @field(Node, @memberName(Id, i)); + return @fieldParentPtr(T, "base", base).firstToken(); + } } - unreachable; } pub fn lastToken(base: &Node) Token { - inline for (idTypeTable) |id_type_pair| { - if (base.id == id_type_pair.id) - return @fieldParentPtr(id_type_pair.Type, "base", base).lastToken(); - } - - unreachable; - } -}; - -pub const NodeRoot = struct { - base: Node, - decls: ArrayList(&Node), - eof_token: Token, - - pub fn iterate(self: &NodeRoot, index: usize) ?&Node { - if (index < self.decls.len) { - return self.decls.items[self.decls.len - index - 1]; - } - return null; - } - - pub fn firstToken(self: &NodeRoot) Token { - return if (self.decls.len == 0) self.eof_token else self.decls.at(0).firstToken(); - } - - pub fn lastToken(self: &NodeRoot) Token { - return if (self.decls.len == 0) self.eof_token else self.decls.at(self.decls.len - 1).lastToken(); - } -}; - -pub const NodeVarDecl = struct { - base: Node, - visib_token: ?Token, - name_token: Token, - eq_token: Token, - mut_token: Token, - comptime_token: ?Token, - extern_export_token: ?Token, - lib_name: ?&Node, - type_node: ?&Node, - align_node: ?&Node, - init_node: ?&Node, - semicolon_token: Token, - - pub fn iterate(self: &NodeVarDecl, index: usize) ?&Node { - var i = index; - - if (self.type_node) |type_node| { - if (i < 1) return type_node; - i -= 1; - } - - if (self.align_node) |align_node| { - if (i < 1) return align_node; - i -= 1; - } - - if (self.init_node) |init_node| { - if (i < 1) return init_node; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeVarDecl) Token { - if (self.visib_token) |visib_token| return visib_token; - if (self.comptime_token) |comptime_token| return comptime_token; - if (self.extern_export_token) |extern_export_token| return extern_export_token; - assert(self.lib_name == null); - return self.mut_token; - } - - pub fn lastToken(self: &NodeVarDecl) Token { - return self.semicolon_token; - } -}; - -pub const NodeUse = struct { - base: Node, - visib_token: ?Token, - expr: &Node, - semicolon_token: Token, - - pub fn iterate(self: &NodeUse, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeUse) Token { - if (self.visib_token) |visib_token| return visib_token; - return self.expr.firstToken(); - } - - pub fn lastToken(self: &NodeUse) Token { - return self.semicolon_token; - } -}; - -pub const NodeErrorSetDecl = struct { - base: Node, - error_token: Token, - decls: ArrayList(&NodeIdentifier), - rbrace_token: Token, - - pub fn iterate(self: &NodeErrorSetDecl, index: usize) ?&Node { - var i = index; - - if (i < self.decls.len) return &self.decls.at(i).base; - i -= self.decls.len; - - return null; - } - - pub fn firstToken(self: &NodeErrorSetDecl) Token { - return self.error_token; - } - - pub fn lastToken(self: &NodeErrorSetDecl) Token { - return self.rbrace_token; - } -}; - -pub const NodeContainerDecl = struct { - base: Node, - ltoken: Token, - layout: Layout, - kind: Kind, - init_arg_expr: InitArg, - fields_and_decls: ArrayList(&Node), - rbrace_token: Token, - - const Layout = enum { - Auto, - Extern, - Packed, - }; - - const Kind = enum { - Struct, - Enum, - Union, - }; - - const InitArg = union(enum) { - None, - Enum, - Type: &Node, - }; - - pub fn iterate(self: &NodeContainerDecl, index: usize) ?&Node { - var i = index; - - switch (self.init_arg_expr) { - InitArg.Type => |t| { - if (i < 1) return t; - i -= 1; - }, - InitArg.None, - InitArg.Enum => { } - } - - if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i); - i -= self.fields_and_decls.len; - - return null; - } - - pub fn firstToken(self: &NodeContainerDecl) Token { - return self.ltoken; - } - - pub fn lastToken(self: &NodeContainerDecl) Token { - return self.rbrace_token; - } -}; - -pub const NodeStructField = struct { - base: Node, - visib_token: ?Token, - name_token: Token, - type_expr: &Node, - - pub fn iterate(self: &NodeStructField, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.type_expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeStructField) Token { - if (self.visib_token) |visib_token| return visib_token; - return self.name_token; - } - - pub fn lastToken(self: &NodeStructField) Token { - return self.type_expr.lastToken(); - } -}; - -pub const NodeUnionTag = struct { - base: Node, - name_token: Token, - type_expr: ?&Node, - - pub fn iterate(self: &NodeUnionTag, index: usize) ?&Node { - var i = index; - - if (self.type_expr) |type_expr| { - if (i < 1) return type_expr; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeUnionTag) Token { - return self.name_token; - } - - pub fn lastToken(self: &NodeUnionTag) Token { - if (self.type_expr) |type_expr| { - return type_expr.lastToken(); - } - - return self.name_token; - } -}; - -pub const NodeEnumTag = struct { - base: Node, - name_token: Token, - value: ?&Node, - - pub fn iterate(self: &NodeEnumTag, index: usize) ?&Node { - var i = index; - - if (self.value) |value| { - if (i < 1) return value; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeEnumTag) Token { - return self.name_token; - } - - pub fn lastToken(self: &NodeEnumTag) Token { - if (self.value) |value| { - return value.lastToken(); - } - - return self.name_token; - } -}; - -pub const NodeIdentifier = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeIdentifier, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeIdentifier) Token { - return self.token; - } - - pub fn lastToken(self: &NodeIdentifier) Token { - return self.token; - } -}; - -pub const NodeAsyncAttribute = struct { - base: Node, - async_token: Token, - allocator_type: ?&Node, - rangle_bracket: ?Token, - - pub fn iterate(self: &NodeAsyncAttribute, index: usize) ?&Node { - var i = index; - - if (self.allocator_type) |allocator_type| { - if (i < 1) return allocator_type; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeAsyncAttribute) Token { - return self.async_token; - } - - pub fn lastToken(self: &NodeAsyncAttribute) Token { - if (self.rangle_bracket) |rangle_bracket| { - return rangle_bracket; - } - - return self.async_token; - } -}; - -pub const NodeFnProto = struct { - base: Node, - visib_token: ?Token, - fn_token: Token, - name_token: ?Token, - params: ArrayList(&Node), - return_type: ReturnType, - var_args_token: ?Token, - extern_export_inline_token: ?Token, - cc_token: ?Token, - async_attr: ?&NodeAsyncAttribute, - body_node: ?&Node, - lib_name: ?&Node, // populated if this is an extern declaration - align_expr: ?&Node, // populated if align(A) is present - - pub const ReturnType = union(enum) { - Explicit: &Node, - InferErrorSet: &Node, - }; - - pub fn iterate(self: &NodeFnProto, index: usize) ?&Node { - var i = index; - - if (self.body_node) |body_node| { - if (i < 1) return body_node; - i -= 1; - } - - switch (self.return_type) { - // TODO allow this and next prong to share bodies since the types are the same - ReturnType.Explicit => |node| { - if (i < 1) return node; - i -= 1; - }, - ReturnType.InferErrorSet => |node| { - if (i < 1) return node; - i -= 1; - }, - } - - if (self.align_expr) |align_expr| { - if (i < 1) return align_expr; - i -= 1; - } - - if (i < self.params.len) return self.params.items[self.params.len - i - 1]; - i -= self.params.len; - - if (self.lib_name) |lib_name| { - if (i < 1) return lib_name; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeFnProto) Token { - if (self.visib_token) |visib_token| return visib_token; - if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token; - assert(self.lib_name == null); - if (self.cc_token) |cc_token| return cc_token; - return self.fn_token; - } - - pub fn lastToken(self: &NodeFnProto) Token { - if (self.body_node) |body_node| return body_node.lastToken(); - switch (self.return_type) { - // TODO allow this and next prong to share bodies since the types are the same - ReturnType.Explicit => |node| return node.lastToken(), - ReturnType.InferErrorSet => |node| return node.lastToken(), - } - } -}; - -pub const NodeParamDecl = struct { - base: Node, - comptime_token: ?Token, - noalias_token: ?Token, - name_token: ?Token, - type_node: &Node, - var_args_token: ?Token, - - pub fn iterate(self: &NodeParamDecl, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.type_node; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeParamDecl) Token { - if (self.comptime_token) |comptime_token| return comptime_token; - if (self.noalias_token) |noalias_token| return noalias_token; - if (self.name_token) |name_token| return name_token; - return self.type_node.firstToken(); - } - - pub fn lastToken(self: &NodeParamDecl) Token { - if (self.var_args_token) |var_args_token| return var_args_token; - return self.type_node.lastToken(); - } -}; - -pub const NodeBlock = struct { - base: Node, - label: ?Token, - lbrace: Token, - statements: ArrayList(&Node), - rbrace: Token, - - pub fn iterate(self: &NodeBlock, index: usize) ?&Node { - var i = index; - - if (i < self.statements.len) return self.statements.items[i]; - i -= self.statements.len; - - return null; - } - - pub fn firstToken(self: &NodeBlock) Token { - if (self.label) |label| { - return label; - } - - return self.lbrace; - } - - pub fn lastToken(self: &NodeBlock) Token { - return self.rbrace; - } -}; - -pub const NodeDefer = struct { - base: Node, - defer_token: Token, - kind: Kind, - expr: &Node, - - const Kind = enum { - Error, - Unconditional, - }; - - pub fn iterate(self: &NodeDefer, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeDefer) Token { - return self.defer_token; - } - - pub fn lastToken(self: &NodeDefer) Token { - return self.expr.lastToken(); - } -}; - -pub const NodeComptime = struct { - base: Node, - comptime_token: Token, - expr: &Node, - - pub fn iterate(self: &NodeComptime, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeComptime) Token { - return self.comptime_token; - } - - pub fn lastToken(self: &NodeComptime) Token { - return self.expr.lastToken(); - } -}; - -pub const NodePayload = struct { - base: Node, - lpipe: Token, - error_symbol: &NodeIdentifier, - rpipe: Token, - - pub fn iterate(self: &NodePayload, index: usize) ?&Node { - var i = index; - - if (i < 1) return &self.error_symbol.base; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodePayload) Token { - return self.lpipe; - } - - pub fn lastToken(self: &NodePayload) Token { - return self.rpipe; - } -}; - -pub const NodePointerPayload = struct { - base: Node, - lpipe: Token, - is_ptr: bool, - value_symbol: &NodeIdentifier, - rpipe: Token, - - pub fn iterate(self: &NodePointerPayload, index: usize) ?&Node { - var i = index; - - if (i < 1) return &self.value_symbol.base; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodePointerPayload) Token { - return self.lpipe; - } - - pub fn lastToken(self: &NodePointerPayload) Token { - return self.rpipe; - } -}; - -pub const NodePointerIndexPayload = struct { - base: Node, - lpipe: Token, - is_ptr: bool, - value_symbol: &NodeIdentifier, - index_symbol: ?&NodeIdentifier, - rpipe: Token, - - pub fn iterate(self: &NodePointerIndexPayload, index: usize) ?&Node { - var i = index; - - if (i < 1) return &self.value_symbol.base; - i -= 1; - - if (self.index_symbol) |index_symbol| { - if (i < 1) return &index_symbol.base; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodePointerIndexPayload) Token { - return self.lpipe; - } - - pub fn lastToken(self: &NodePointerIndexPayload) Token { - return self.rpipe; - } -}; - -pub const NodeElse = struct { - base: Node, - else_token: Token, - payload: ?&NodePayload, - body: &Node, - - pub fn iterate(self: &NodeElse, index: usize) ?&Node { - var i = index; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (i < 1) return self.body; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeElse) Token { - return self.else_token; - } - - pub fn lastToken(self: &NodeElse) Token { - return self.body.lastToken(); - } -}; - -pub const NodeSwitch = struct { - base: Node, - switch_token: Token, - expr: &Node, - cases: ArrayList(&NodeSwitchCase), - rbrace: Token, - - pub fn iterate(self: &NodeSwitch, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - if (i < self.cases.len) return &self.cases.at(i).base; - i -= self.cases.len; - - return null; - } - - pub fn firstToken(self: &NodeSwitch) Token { - return self.switch_token; - } - - pub fn lastToken(self: &NodeSwitch) Token { - return self.rbrace; - } -}; - -pub const NodeSwitchCase = struct { - base: Node, - items: ArrayList(&Node), - payload: ?&NodePointerPayload, - expr: &Node, - - pub fn iterate(self: &NodeSwitchCase, index: usize) ?&Node { - var i = index; - - if (i < self.items.len) return self.items.at(i); - i -= self.items.len; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeSwitchCase) Token { - return self.items.at(0).firstToken(); - } - - pub fn lastToken(self: &NodeSwitchCase) Token { - return self.expr.lastToken(); - } -}; - -pub const NodeSwitchElse = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeSwitchElse, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeSwitchElse) Token { - return self.token; - } - - pub fn lastToken(self: &NodeSwitchElse) Token { - return self.token; - } -}; - -pub const NodeWhile = struct { - base: Node, - label: ?Token, - inline_token: ?Token, - while_token: Token, - condition: &Node, - payload: ?&NodePointerPayload, - continue_expr: ?&Node, - body: &Node, - @"else": ?&NodeElse, - - pub fn iterate(self: &NodeWhile, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.condition; - i -= 1; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (self.continue_expr) |continue_expr| { - if (i < 1) return continue_expr; - i -= 1; - } - - if (i < 1) return self.body; - i -= 1; - - if (self.@"else") |@"else"| { - if (i < 1) return &@"else".base; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeWhile) Token { - if (self.label) |label| { - return label; - } - - if (self.inline_token) |inline_token| { - return inline_token; - } - - return self.while_token; - } - - pub fn lastToken(self: &NodeWhile) Token { - if (self.@"else") |@"else"| { - return @"else".body.lastToken(); - } - - return self.body.lastToken(); - } -}; - -pub const NodeFor = struct { - base: Node, - label: ?Token, - inline_token: ?Token, - for_token: Token, - array_expr: &Node, - payload: ?&NodePointerIndexPayload, - body: &Node, - @"else": ?&NodeElse, - - pub fn iterate(self: &NodeFor, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.array_expr; - i -= 1; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (i < 1) return self.body; - i -= 1; - - if (self.@"else") |@"else"| { - if (i < 1) return &@"else".base; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeFor) Token { - if (self.label) |label| { - return label; - } - - if (self.inline_token) |inline_token| { - return inline_token; - } - - return self.for_token; - } - - pub fn lastToken(self: &NodeFor) Token { - if (self.@"else") |@"else"| { - return @"else".body.lastToken(); - } - - return self.body.lastToken(); - } -}; - -pub const NodeIf = struct { - base: Node, - if_token: Token, - condition: &Node, - payload: ?&NodePointerPayload, - body: &Node, - @"else": ?&NodeElse, - - pub fn iterate(self: &NodeIf, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.condition; - i -= 1; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (i < 1) return self.body; - i -= 1; - - if (self.@"else") |@"else"| { - if (i < 1) return &@"else".base; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeIf) Token { - return self.if_token; - } - - pub fn lastToken(self: &NodeIf) Token { - if (self.@"else") |@"else"| { - return @"else".body.lastToken(); - } - - return self.body.lastToken(); - } -}; - -pub const NodeInfixOp = struct { - base: Node, - op_token: Token, - lhs: &Node, - op: InfixOp, - rhs: &Node, - - const InfixOp = union(enum) { - Add, - AddWrap, - ArrayCat, - ArrayMult, - Assign, - AssignBitAnd, - AssignBitOr, - AssignBitShiftLeft, - AssignBitShiftRight, - AssignBitXor, - AssignDiv, - AssignMinus, - AssignMinusWrap, - AssignMod, - AssignPlus, - AssignPlusWrap, - AssignTimes, - AssignTimesWarp, - BangEqual, - BitAnd, - BitOr, - BitShiftLeft, - BitShiftRight, - BitXor, - BoolAnd, - BoolOr, - Catch: ?&NodePayload, - Div, - EqualEqual, - ErrorUnion, - GreaterOrEqual, - GreaterThan, - LessOrEqual, - LessThan, - MergeErrorSets, - Mod, - Mult, - MultWrap, - Period, - Range, - Sub, - SubWrap, - UnwrapMaybe, - }; - - pub fn iterate(self: &NodeInfixOp, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.lhs; - i -= 1; - - switch (self.op) { - InfixOp.Catch => |maybe_payload| { - if (maybe_payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - }, - - InfixOp.Add, - InfixOp.AddWrap, - InfixOp.ArrayCat, - InfixOp.ArrayMult, - InfixOp.Assign, - InfixOp.AssignBitAnd, - InfixOp.AssignBitOr, - InfixOp.AssignBitShiftLeft, - InfixOp.AssignBitShiftRight, - InfixOp.AssignBitXor, - InfixOp.AssignDiv, - InfixOp.AssignMinus, - InfixOp.AssignMinusWrap, - InfixOp.AssignMod, - InfixOp.AssignPlus, - InfixOp.AssignPlusWrap, - InfixOp.AssignTimes, - InfixOp.AssignTimesWarp, - InfixOp.BangEqual, - InfixOp.BitAnd, - InfixOp.BitOr, - InfixOp.BitShiftLeft, - InfixOp.BitShiftRight, - InfixOp.BitXor, - InfixOp.BoolAnd, - InfixOp.BoolOr, - InfixOp.Div, - InfixOp.EqualEqual, - InfixOp.ErrorUnion, - InfixOp.GreaterOrEqual, - InfixOp.GreaterThan, - InfixOp.LessOrEqual, - InfixOp.LessThan, - InfixOp.MergeErrorSets, - InfixOp.Mod, - InfixOp.Mult, - InfixOp.MultWrap, - InfixOp.Period, - InfixOp.Range, - InfixOp.Sub, - InfixOp.SubWrap, - InfixOp.UnwrapMaybe => {}, - } - - if (i < 1) return self.rhs; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeInfixOp) Token { - return self.lhs.firstToken(); - } - - pub fn lastToken(self: &NodeInfixOp) Token { - return self.rhs.lastToken(); - } -}; - -pub const NodePrefixOp = struct { - base: Node, - op_token: Token, - op: PrefixOp, - rhs: &Node, - - const PrefixOp = union(enum) { - AddrOf: AddrOfInfo, - ArrayType: &Node, - Await, - BitNot, - BoolNot, - Cancel, - Deref, - MaybeType, - Negation, - NegationWrap, - Resume, - SliceType: AddrOfInfo, - Try, - UnwrapMaybe, - }; - - const AddrOfInfo = struct { - align_expr: ?&Node, - bit_offset_start_token: ?Token, - bit_offset_end_token: ?Token, - const_token: ?Token, - volatile_token: ?Token, - }; - - pub fn iterate(self: &NodePrefixOp, index: usize) ?&Node { - var i = index; - - switch (self.op) { - PrefixOp.SliceType => |addr_of_info| { - if (addr_of_info.align_expr) |align_expr| { - if (i < 1) return align_expr; - i -= 1; - } - }, - PrefixOp.AddrOf => |addr_of_info| { - if (addr_of_info.align_expr) |align_expr| { - if (i < 1) return align_expr; - i -= 1; - } - }, - PrefixOp.ArrayType => |size_expr| { - if (i < 1) return size_expr; - i -= 1; - }, - PrefixOp.Await, - PrefixOp.BitNot, - PrefixOp.BoolNot, - PrefixOp.Cancel, - PrefixOp.Deref, - PrefixOp.MaybeType, - PrefixOp.Negation, - PrefixOp.NegationWrap, - PrefixOp.Try, - PrefixOp.Resume, - PrefixOp.UnwrapMaybe => {}, - } - - if (i < 1) return self.rhs; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodePrefixOp) Token { - return self.op_token; - } - - pub fn lastToken(self: &NodePrefixOp) Token { - return self.rhs.lastToken(); - } -}; - -pub const NodeFieldInitializer = struct { - base: Node, - period_token: Token, - name_token: Token, - expr: &Node, - - pub fn iterate(self: &NodeFieldInitializer, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeFieldInitializer) Token { - return self.period_token; - } - - pub fn lastToken(self: &NodeFieldInitializer) Token { - return self.expr.lastToken(); - } -}; - -pub const NodeSuffixOp = struct { - base: Node, - lhs: &Node, - op: SuffixOp, - rtoken: Token, - - const SuffixOp = union(enum) { - Call: CallInfo, - ArrayAccess: &Node, - Slice: SliceRange, - ArrayInitializer: ArrayList(&Node), - StructInitializer: ArrayList(&NodeFieldInitializer), - }; - - const CallInfo = struct { - params: ArrayList(&Node), - async_attr: ?&NodeAsyncAttribute, - }; - - const SliceRange = struct { - start: &Node, - end: ?&Node, - }; - - pub fn iterate(self: &NodeSuffixOp, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.lhs; - i -= 1; - - switch (self.op) { - SuffixOp.Call => |call_info| { - if (i < call_info.params.len) return call_info.params.at(i); - i -= call_info.params.len; - }, - SuffixOp.ArrayAccess => |index_expr| { - if (i < 1) return index_expr; - i -= 1; - }, - SuffixOp.Slice => |range| { - if (i < 1) return range.start; - i -= 1; - - if (range.end) |end| { - if (i < 1) return end; - i -= 1; - } - }, - SuffixOp.ArrayInitializer => |exprs| { - if (i < exprs.len) return exprs.at(i); - i -= exprs.len; - }, - SuffixOp.StructInitializer => |fields| { - if (i < fields.len) return &fields.at(i).base; - i -= fields.len; - }, - } - - return null; - } - - pub fn firstToken(self: &NodeSuffixOp) Token { - return self.lhs.firstToken(); - } - - pub fn lastToken(self: &NodeSuffixOp) Token { - return self.rtoken; - } -}; - -pub const NodeGroupedExpression = struct { - base: Node, - lparen: Token, - expr: &Node, - rparen: Token, - - pub fn iterate(self: &NodeGroupedExpression, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeGroupedExpression) Token { - return self.lparen; - } - - pub fn lastToken(self: &NodeGroupedExpression) Token { - return self.rparen; - } -}; - -pub const NodeControlFlowExpression = struct { - base: Node, - ltoken: Token, - kind: Kind, - rhs: ?&Node, - - const Kind = union(enum) { - Break: ?Token, - Continue: ?Token, - Return, - }; - - pub fn iterate(self: &NodeControlFlowExpression, index: usize) ?&Node { - var i = index; - - if (self.rhs) |rhs| { - if (i < 1) return rhs; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeControlFlowExpression) Token { - return self.ltoken; - } - - pub fn lastToken(self: &NodeControlFlowExpression) Token { - if (self.rhs) |rhs| { - return rhs.lastToken(); - } - - switch (self.kind) { - Kind.Break => |maybe_blk_token| { - if (maybe_blk_token) |blk_token| { - return blk_token; - } - }, - Kind.Continue => |maybe_blk_token| { - if (maybe_blk_token) |blk_token| { - return blk_token; - } - }, - Kind.Return => return self.ltoken, - } - - return self.ltoken; - } -}; - -pub const NodeSuspend = struct { - base: Node, - suspend_token: Token, - payload: ?&NodePayload, - body: ?&Node, - - pub fn iterate(self: &NodeSuspend, index: usize) ?&Node { - var i = index; - - if (self.payload) |payload| { - if (i < 1) return &payload.base; - i -= 1; - } - - if (self.body) |body| { - if (i < 1) return body; - i -= 1; - } - - return null; - } - - pub fn firstToken(self: &NodeSuspend) Token { - return self.suspend_token; - } - - pub fn lastToken(self: &NodeSuspend) Token { - if (self.body) |body| { - return body.lastToken(); - } - - if (self.payload) |payload| { - return payload.lastToken(); - } - - return self.suspend_token; - } -}; - -pub const NodeIntegerLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeIntegerLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeIntegerLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeIntegerLiteral) Token { - return self.token; - } -}; - -pub const NodeFloatLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeFloatLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeFloatLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeFloatLiteral) Token { - return self.token; - } -}; - -pub const NodeBuiltinCall = struct { - base: Node, - builtin_token: Token, - params: ArrayList(&Node), - rparen_token: Token, - - pub fn iterate(self: &NodeBuiltinCall, index: usize) ?&Node { - var i = index; - - if (i < self.params.len) return self.params.at(i); - i -= self.params.len; - - return null; - } - - pub fn firstToken(self: &NodeBuiltinCall) Token { - return self.builtin_token; - } - - pub fn lastToken(self: &NodeBuiltinCall) Token { - return self.rparen_token; - } -}; - -pub const NodeStringLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeStringLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeStringLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeStringLiteral) Token { - return self.token; - } -}; - -pub const NodeMultilineStringLiteral = struct { - base: Node, - tokens: ArrayList(Token), - - pub fn iterate(self: &NodeMultilineStringLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeMultilineStringLiteral) Token { - return self.tokens.at(0); - } - - pub fn lastToken(self: &NodeMultilineStringLiteral) Token { - return self.tokens.at(self.tokens.len - 1); - } -}; - -pub const NodeCharLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeCharLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeCharLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeCharLiteral) Token { - return self.token; - } -}; - -pub const NodeBoolLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeBoolLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeBoolLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeBoolLiteral) Token { - return self.token; - } -}; - -pub const NodeNullLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeNullLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeNullLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeNullLiteral) Token { - return self.token; - } -}; - -pub const NodeUndefinedLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeUndefinedLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeUndefinedLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeUndefinedLiteral) Token { - return self.token; - } -}; - -pub const NodeThisLiteral = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeThisLiteral, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeThisLiteral) Token { - return self.token; - } - - pub fn lastToken(self: &NodeThisLiteral) Token { - return self.token; - } -}; - -pub const NodeAsmOutput = struct { - base: Node, - symbolic_name: &NodeIdentifier, - constraint: &Node, - kind: Kind, - - const Kind = union(enum) { - Variable: &NodeIdentifier, - Return: &Node - }; - - pub fn iterate(self: &NodeAsmOutput, index: usize) ?&Node { - var i = index; - - if (i < 1) return &self.symbolic_name.base; - i -= 1; - - if (i < 1) return self.constraint; - i -= 1; - - switch (self.kind) { - Kind.Variable => |variable_name| { - if (i < 1) return &variable_name.base; - i -= 1; - }, - Kind.Return => |return_type| { - if (i < 1) return return_type; - i -= 1; + comptime var i = 0; + inline while (i < @memberCount(Id)) : (i += 1) { + if (base.id == @field(Id, @memberName(Id, i))) { + const T = @field(Node, @memberName(Id, i)); + return @fieldParentPtr(T, "base", base).lastToken(); } } - - return null; + unreachable; } - pub fn firstToken(self: &NodeAsmOutput) Token { - return self.symbolic_name.firstToken(); + pub fn typeToId(comptime T: type) Id { + comptime var i = 0; + inline while (i < @memberCount(Id)) : (i += 1) { + if (T == @field(Node, @memberName(Id, i))) { + return @field(Id, @memberName(Id, i)); + } + } + unreachable; } - pub fn lastToken(self: &NodeAsmOutput) Token { - return switch (self.kind) { - Kind.Variable => |variable_name| variable_name.lastToken(), - Kind.Return => |return_type| return_type.lastToken(), + pub const Root = struct { + base: Node, + decls: ArrayList(&Node), + eof_token: Token, + + pub fn iterate(self: &Root, index: usize) ?&Node { + if (index < self.decls.len) { + return self.decls.items[self.decls.len - index - 1]; + } + return null; + } + + pub fn firstToken(self: &Root) Token { + return if (self.decls.len == 0) self.eof_token else self.decls.at(0).firstToken(); + } + + pub fn lastToken(self: &Root) Token { + return if (self.decls.len == 0) self.eof_token else self.decls.at(self.decls.len - 1).lastToken(); + } + }; + + pub const VarDecl = struct { + base: Node, + comments: ?&LineComment, + visib_token: ?Token, + name_token: Token, + eq_token: Token, + mut_token: Token, + comptime_token: ?Token, + extern_export_token: ?Token, + lib_name: ?&Node, + type_node: ?&Node, + align_node: ?&Node, + init_node: ?&Node, + semicolon_token: Token, + + pub fn iterate(self: &VarDecl, index: usize) ?&Node { + var i = index; + + if (self.type_node) |type_node| { + if (i < 1) return type_node; + i -= 1; + } + + if (self.align_node) |align_node| { + if (i < 1) return align_node; + i -= 1; + } + + if (self.init_node) |init_node| { + if (i < 1) return init_node; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &VarDecl) Token { + if (self.visib_token) |visib_token| return visib_token; + if (self.comptime_token) |comptime_token| return comptime_token; + if (self.extern_export_token) |extern_export_token| return extern_export_token; + assert(self.lib_name == null); + return self.mut_token; + } + + pub fn lastToken(self: &VarDecl) Token { + return self.semicolon_token; + } + }; + + pub const Use = struct { + base: Node, + visib_token: ?Token, + expr: &Node, + semicolon_token: Token, + + pub fn iterate(self: &Use, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &Use) Token { + if (self.visib_token) |visib_token| return visib_token; + return self.expr.firstToken(); + } + + pub fn lastToken(self: &Use) Token { + return self.semicolon_token; + } + }; + + pub const ErrorSetDecl = struct { + base: Node, + error_token: Token, + decls: ArrayList(&Node), + rbrace_token: Token, + + pub fn iterate(self: &ErrorSetDecl, index: usize) ?&Node { + var i = index; + + if (i < self.decls.len) return self.decls.at(i); + i -= self.decls.len; + + return null; + } + + pub fn firstToken(self: &ErrorSetDecl) Token { + return self.error_token; + } + + pub fn lastToken(self: &ErrorSetDecl) Token { + return self.rbrace_token; + } + }; + + pub const ContainerDecl = struct { + base: Node, + ltoken: Token, + layout: Layout, + kind: Kind, + init_arg_expr: InitArg, + fields_and_decls: ArrayList(&Node), + rbrace_token: Token, + + const Layout = enum { + Auto, + Extern, + Packed, }; - } + + const Kind = enum { + Struct, + Enum, + Union, + }; + + const InitArg = union(enum) { + None, + Enum, + Type: &Node, + }; + + pub fn iterate(self: &ContainerDecl, index: usize) ?&Node { + var i = index; + + switch (self.init_arg_expr) { + InitArg.Type => |t| { + if (i < 1) return t; + i -= 1; + }, + InitArg.None, + InitArg.Enum => { } + } + + if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i); + i -= self.fields_and_decls.len; + + return null; + } + + pub fn firstToken(self: &ContainerDecl) Token { + return self.ltoken; + } + + pub fn lastToken(self: &ContainerDecl) Token { + return self.rbrace_token; + } + }; + + pub const StructField = struct { + base: Node, + visib_token: ?Token, + name_token: Token, + type_expr: &Node, + + pub fn iterate(self: &StructField, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.type_expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &StructField) Token { + if (self.visib_token) |visib_token| return visib_token; + return self.name_token; + } + + pub fn lastToken(self: &StructField) Token { + return self.type_expr.lastToken(); + } + }; + + pub const UnionTag = struct { + base: Node, + name_token: Token, + type_expr: ?&Node, + + pub fn iterate(self: &UnionTag, index: usize) ?&Node { + var i = index; + + if (self.type_expr) |type_expr| { + if (i < 1) return type_expr; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &UnionTag) Token { + return self.name_token; + } + + pub fn lastToken(self: &UnionTag) Token { + if (self.type_expr) |type_expr| { + return type_expr.lastToken(); + } + + return self.name_token; + } + }; + + pub const EnumTag = struct { + base: Node, + name_token: Token, + value: ?&Node, + + pub fn iterate(self: &EnumTag, index: usize) ?&Node { + var i = index; + + if (self.value) |value| { + if (i < 1) return value; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &EnumTag) Token { + return self.name_token; + } + + pub fn lastToken(self: &EnumTag) Token { + if (self.value) |value| { + return value.lastToken(); + } + + return self.name_token; + } + }; + + pub const Identifier = struct { + base: Node, + token: Token, + + pub fn iterate(self: &Identifier, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &Identifier) Token { + return self.token; + } + + pub fn lastToken(self: &Identifier) Token { + return self.token; + } + }; + + pub const AsyncAttribute = struct { + base: Node, + async_token: Token, + allocator_type: ?&Node, + rangle_bracket: ?Token, + + pub fn iterate(self: &AsyncAttribute, index: usize) ?&Node { + var i = index; + + if (self.allocator_type) |allocator_type| { + if (i < 1) return allocator_type; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &AsyncAttribute) Token { + return self.async_token; + } + + pub fn lastToken(self: &AsyncAttribute) Token { + if (self.rangle_bracket) |rangle_bracket| { + return rangle_bracket; + } + + return self.async_token; + } + }; + + pub const FnProto = struct { + base: Node, + comments: ?&LineComment, + visib_token: ?Token, + fn_token: Token, + name_token: ?Token, + params: ArrayList(&Node), + return_type: ReturnType, + var_args_token: ?Token, + extern_export_inline_token: ?Token, + cc_token: ?Token, + async_attr: ?&AsyncAttribute, + body_node: ?&Node, + lib_name: ?&Node, // populated if this is an extern declaration + align_expr: ?&Node, // populated if align(A) is present + + pub const ReturnType = union(enum) { + Explicit: &Node, + InferErrorSet: &Node, + }; + + pub fn iterate(self: &FnProto, index: usize) ?&Node { + var i = index; + + if (self.body_node) |body_node| { + if (i < 1) return body_node; + i -= 1; + } + + switch (self.return_type) { + // TODO allow this and next prong to share bodies since the types are the same + ReturnType.Explicit => |node| { + if (i < 1) return node; + i -= 1; + }, + ReturnType.InferErrorSet => |node| { + if (i < 1) return node; + i -= 1; + }, + } + + if (self.align_expr) |align_expr| { + if (i < 1) return align_expr; + i -= 1; + } + + if (i < self.params.len) return self.params.items[self.params.len - i - 1]; + i -= self.params.len; + + if (self.lib_name) |lib_name| { + if (i < 1) return lib_name; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &FnProto) Token { + if (self.visib_token) |visib_token| return visib_token; + if (self.extern_export_inline_token) |extern_export_inline_token| return extern_export_inline_token; + assert(self.lib_name == null); + if (self.cc_token) |cc_token| return cc_token; + return self.fn_token; + } + + pub fn lastToken(self: &FnProto) Token { + if (self.body_node) |body_node| return body_node.lastToken(); + switch (self.return_type) { + // TODO allow this and next prong to share bodies since the types are the same + ReturnType.Explicit => |node| return node.lastToken(), + ReturnType.InferErrorSet => |node| return node.lastToken(), + } + } + }; + + pub const ParamDecl = struct { + base: Node, + comptime_token: ?Token, + noalias_token: ?Token, + name_token: ?Token, + type_node: &Node, + var_args_token: ?Token, + + pub fn iterate(self: &ParamDecl, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.type_node; + i -= 1; + + return null; + } + + pub fn firstToken(self: &ParamDecl) Token { + if (self.comptime_token) |comptime_token| return comptime_token; + if (self.noalias_token) |noalias_token| return noalias_token; + if (self.name_token) |name_token| return name_token; + return self.type_node.firstToken(); + } + + pub fn lastToken(self: &ParamDecl) Token { + if (self.var_args_token) |var_args_token| return var_args_token; + return self.type_node.lastToken(); + } + }; + + pub const Block = struct { + base: Node, + label: ?Token, + lbrace: Token, + statements: ArrayList(&Node), + rbrace: Token, + + pub fn iterate(self: &Block, index: usize) ?&Node { + var i = index; + + if (i < self.statements.len) return self.statements.items[i]; + i -= self.statements.len; + + return null; + } + + pub fn firstToken(self: &Block) Token { + if (self.label) |label| { + return label; + } + + return self.lbrace; + } + + pub fn lastToken(self: &Block) Token { + return self.rbrace; + } + }; + + pub const Defer = struct { + base: Node, + defer_token: Token, + kind: Kind, + expr: &Node, + + const Kind = enum { + Error, + Unconditional, + }; + + pub fn iterate(self: &Defer, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &Defer) Token { + return self.defer_token; + } + + pub fn lastToken(self: &Defer) Token { + return self.expr.lastToken(); + } + }; + + pub const Comptime = struct { + base: Node, + comptime_token: Token, + expr: &Node, + + pub fn iterate(self: &Comptime, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &Comptime) Token { + return self.comptime_token; + } + + pub fn lastToken(self: &Comptime) Token { + return self.expr.lastToken(); + } + }; + + pub const Payload = struct { + base: Node, + lpipe: Token, + error_symbol: &Node, + rpipe: Token, + + pub fn iterate(self: &Payload, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.error_symbol; + i -= 1; + + return null; + } + + pub fn firstToken(self: &Payload) Token { + return self.lpipe; + } + + pub fn lastToken(self: &Payload) Token { + return self.rpipe; + } + }; + + pub const PointerPayload = struct { + base: Node, + lpipe: Token, + ptr_token: ?Token, + value_symbol: &Node, + rpipe: Token, + + pub fn iterate(self: &PointerPayload, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.value_symbol; + i -= 1; + + return null; + } + + pub fn firstToken(self: &PointerPayload) Token { + return self.lpipe; + } + + pub fn lastToken(self: &PointerPayload) Token { + return self.rpipe; + } + }; + + pub const PointerIndexPayload = struct { + base: Node, + lpipe: Token, + ptr_token: ?Token, + value_symbol: &Node, + index_symbol: ?&Node, + rpipe: Token, + + pub fn iterate(self: &PointerIndexPayload, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.value_symbol; + i -= 1; + + if (self.index_symbol) |index_symbol| { + if (i < 1) return index_symbol; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &PointerIndexPayload) Token { + return self.lpipe; + } + + pub fn lastToken(self: &PointerIndexPayload) Token { + return self.rpipe; + } + }; + + pub const Else = struct { + base: Node, + else_token: Token, + payload: ?&Node, + body: &Node, + + pub fn iterate(self: &Else, index: usize) ?&Node { + var i = index; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (i < 1) return self.body; + i -= 1; + + return null; + } + + pub fn firstToken(self: &Else) Token { + return self.else_token; + } + + pub fn lastToken(self: &Else) Token { + return self.body.lastToken(); + } + }; + + pub const Switch = struct { + base: Node, + switch_token: Token, + expr: &Node, + cases: ArrayList(&SwitchCase), + rbrace: Token, + + pub fn iterate(self: &Switch, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + if (i < self.cases.len) return &self.cases.at(i).base; + i -= self.cases.len; + + return null; + } + + pub fn firstToken(self: &Switch) Token { + return self.switch_token; + } + + pub fn lastToken(self: &Switch) Token { + return self.rbrace; + } + }; + + pub const SwitchCase = struct { + base: Node, + items: ArrayList(&Node), + payload: ?&Node, + expr: &Node, + + pub fn iterate(self: &SwitchCase, index: usize) ?&Node { + var i = index; + + if (i < self.items.len) return self.items.at(i); + i -= self.items.len; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &SwitchCase) Token { + return self.items.at(0).firstToken(); + } + + pub fn lastToken(self: &SwitchCase) Token { + return self.expr.lastToken(); + } + }; + + pub const SwitchElse = struct { + base: Node, + token: Token, + + pub fn iterate(self: &SwitchElse, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &SwitchElse) Token { + return self.token; + } + + pub fn lastToken(self: &SwitchElse) Token { + return self.token; + } + }; + + pub const While = struct { + base: Node, + label: ?Token, + inline_token: ?Token, + while_token: Token, + condition: &Node, + payload: ?&Node, + continue_expr: ?&Node, + body: &Node, + @"else": ?&Else, + + pub fn iterate(self: &While, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.condition; + i -= 1; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (self.continue_expr) |continue_expr| { + if (i < 1) return continue_expr; + i -= 1; + } + + if (i < 1) return self.body; + i -= 1; + + if (self.@"else") |@"else"| { + if (i < 1) return &@"else".base; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &While) Token { + if (self.label) |label| { + return label; + } + + if (self.inline_token) |inline_token| { + return inline_token; + } + + return self.while_token; + } + + pub fn lastToken(self: &While) Token { + if (self.@"else") |@"else"| { + return @"else".body.lastToken(); + } + + return self.body.lastToken(); + } + }; + + pub const For = struct { + base: Node, + label: ?Token, + inline_token: ?Token, + for_token: Token, + array_expr: &Node, + payload: ?&Node, + body: &Node, + @"else": ?&Else, + + pub fn iterate(self: &For, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.array_expr; + i -= 1; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (i < 1) return self.body; + i -= 1; + + if (self.@"else") |@"else"| { + if (i < 1) return &@"else".base; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &For) Token { + if (self.label) |label| { + return label; + } + + if (self.inline_token) |inline_token| { + return inline_token; + } + + return self.for_token; + } + + pub fn lastToken(self: &For) Token { + if (self.@"else") |@"else"| { + return @"else".body.lastToken(); + } + + return self.body.lastToken(); + } + }; + + pub const If = struct { + base: Node, + if_token: Token, + condition: &Node, + payload: ?&Node, + body: &Node, + @"else": ?&Else, + + pub fn iterate(self: &If, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.condition; + i -= 1; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (i < 1) return self.body; + i -= 1; + + if (self.@"else") |@"else"| { + if (i < 1) return &@"else".base; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &If) Token { + return self.if_token; + } + + pub fn lastToken(self: &If) Token { + if (self.@"else") |@"else"| { + return @"else".body.lastToken(); + } + + return self.body.lastToken(); + } + }; + + pub const InfixOp = struct { + base: Node, + op_token: Token, + lhs: &Node, + op: Op, + rhs: &Node, + + pub const Op = union(enum) { + Add, + AddWrap, + ArrayCat, + ArrayMult, + Assign, + AssignBitAnd, + AssignBitOr, + AssignBitShiftLeft, + AssignBitShiftRight, + AssignBitXor, + AssignDiv, + AssignMinus, + AssignMinusWrap, + AssignMod, + AssignPlus, + AssignPlusWrap, + AssignTimes, + AssignTimesWarp, + BangEqual, + BitAnd, + BitOr, + BitShiftLeft, + BitShiftRight, + BitXor, + BoolAnd, + BoolOr, + Catch: ?&Node, + Div, + EqualEqual, + ErrorUnion, + GreaterOrEqual, + GreaterThan, + LessOrEqual, + LessThan, + MergeErrorSets, + Mod, + Mult, + MultWrap, + Period, + Range, + Sub, + SubWrap, + UnwrapMaybe, + }; + + pub fn iterate(self: &InfixOp, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.lhs; + i -= 1; + + switch (self.op) { + Op.Catch => |maybe_payload| { + if (maybe_payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + }, + + Op.Add, + Op.AddWrap, + Op.ArrayCat, + Op.ArrayMult, + Op.Assign, + Op.AssignBitAnd, + Op.AssignBitOr, + Op.AssignBitShiftLeft, + Op.AssignBitShiftRight, + Op.AssignBitXor, + Op.AssignDiv, + Op.AssignMinus, + Op.AssignMinusWrap, + Op.AssignMod, + Op.AssignPlus, + Op.AssignPlusWrap, + Op.AssignTimes, + Op.AssignTimesWarp, + Op.BangEqual, + Op.BitAnd, + Op.BitOr, + Op.BitShiftLeft, + Op.BitShiftRight, + Op.BitXor, + Op.BoolAnd, + Op.BoolOr, + Op.Div, + Op.EqualEqual, + Op.ErrorUnion, + Op.GreaterOrEqual, + Op.GreaterThan, + Op.LessOrEqual, + Op.LessThan, + Op.MergeErrorSets, + Op.Mod, + Op.Mult, + Op.MultWrap, + Op.Period, + Op.Range, + Op.Sub, + Op.SubWrap, + Op.UnwrapMaybe => {}, + } + + if (i < 1) return self.rhs; + i -= 1; + + return null; + } + + pub fn firstToken(self: &InfixOp) Token { + return self.lhs.firstToken(); + } + + pub fn lastToken(self: &InfixOp) Token { + return self.rhs.lastToken(); + } + }; + + pub const PrefixOp = struct { + base: Node, + op_token: Token, + op: Op, + rhs: &Node, + + const Op = union(enum) { + AddrOf: AddrOfInfo, + ArrayType: &Node, + Await, + BitNot, + BoolNot, + Cancel, + Deref, + MaybeType, + Negation, + NegationWrap, + Resume, + SliceType: AddrOfInfo, + Try, + UnwrapMaybe, + }; + + const AddrOfInfo = struct { + align_expr: ?&Node, + bit_offset_start_token: ?Token, + bit_offset_end_token: ?Token, + const_token: ?Token, + volatile_token: ?Token, + }; + + pub fn iterate(self: &PrefixOp, index: usize) ?&Node { + var i = index; + + switch (self.op) { + Op.SliceType => |addr_of_info| { + if (addr_of_info.align_expr) |align_expr| { + if (i < 1) return align_expr; + i -= 1; + } + }, + Op.AddrOf => |addr_of_info| { + if (addr_of_info.align_expr) |align_expr| { + if (i < 1) return align_expr; + i -= 1; + } + }, + Op.ArrayType => |size_expr| { + if (i < 1) return size_expr; + i -= 1; + }, + Op.Await, + Op.BitNot, + Op.BoolNot, + Op.Cancel, + Op.Deref, + Op.MaybeType, + Op.Negation, + Op.NegationWrap, + Op.Try, + Op.Resume, + Op.UnwrapMaybe => {}, + } + + if (i < 1) return self.rhs; + i -= 1; + + return null; + } + + pub fn firstToken(self: &PrefixOp) Token { + return self.op_token; + } + + pub fn lastToken(self: &PrefixOp) Token { + return self.rhs.lastToken(); + } + }; + + pub const FieldInitializer = struct { + base: Node, + period_token: Token, + name_token: Token, + expr: &Node, + + pub fn iterate(self: &FieldInitializer, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &FieldInitializer) Token { + return self.period_token; + } + + pub fn lastToken(self: &FieldInitializer) Token { + return self.expr.lastToken(); + } + }; + + pub const SuffixOp = struct { + base: Node, + lhs: &Node, + op: Op, + rtoken: Token, + + const Op = union(enum) { + Call: CallInfo, + ArrayAccess: &Node, + Slice: SliceRange, + ArrayInitializer: ArrayList(&Node), + StructInitializer: ArrayList(&FieldInitializer), + }; + + const CallInfo = struct { + params: ArrayList(&Node), + async_attr: ?&AsyncAttribute, + }; + + const SliceRange = struct { + start: &Node, + end: ?&Node, + }; + + pub fn iterate(self: &SuffixOp, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.lhs; + i -= 1; + + switch (self.op) { + Op.Call => |call_info| { + if (i < call_info.params.len) return call_info.params.at(i); + i -= call_info.params.len; + }, + Op.ArrayAccess => |index_expr| { + if (i < 1) return index_expr; + i -= 1; + }, + Op.Slice => |range| { + if (i < 1) return range.start; + i -= 1; + + if (range.end) |end| { + if (i < 1) return end; + i -= 1; + } + }, + Op.ArrayInitializer => |exprs| { + if (i < exprs.len) return exprs.at(i); + i -= exprs.len; + }, + Op.StructInitializer => |fields| { + if (i < fields.len) return &fields.at(i).base; + i -= fields.len; + }, + } + + return null; + } + + pub fn firstToken(self: &SuffixOp) Token { + return self.lhs.firstToken(); + } + + pub fn lastToken(self: &SuffixOp) Token { + return self.rtoken; + } + }; + + pub const GroupedExpression = struct { + base: Node, + lparen: Token, + expr: &Node, + rparen: Token, + + pub fn iterate(self: &GroupedExpression, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &GroupedExpression) Token { + return self.lparen; + } + + pub fn lastToken(self: &GroupedExpression) Token { + return self.rparen; + } + }; + + pub const ControlFlowExpression = struct { + base: Node, + ltoken: Token, + kind: Kind, + rhs: ?&Node, + + const Kind = union(enum) { + Break: ?&Node, + Continue: ?&Node, + Return, + }; + + pub fn iterate(self: &ControlFlowExpression, index: usize) ?&Node { + var i = index; + + switch (self.kind) { + Kind.Break => |maybe_label| { + if (maybe_label) |label| { + if (i < 1) return label; + i -= 1; + } + }, + Kind.Continue => |maybe_label| { + if (maybe_label) |label| { + if (i < 1) return label; + i -= 1; + } + }, + Kind.Return => {}, + } + + if (self.rhs) |rhs| { + if (i < 1) return rhs; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &ControlFlowExpression) Token { + return self.ltoken; + } + + pub fn lastToken(self: &ControlFlowExpression) Token { + if (self.rhs) |rhs| { + return rhs.lastToken(); + } + + switch (self.kind) { + Kind.Break => |maybe_label| { + if (maybe_label) |label| { + return label.lastToken(); + } + }, + Kind.Continue => |maybe_label| { + if (maybe_label) |label| { + return label.lastToken(); + } + }, + Kind.Return => return self.ltoken, + } + + return self.ltoken; + } + }; + + pub const Suspend = struct { + base: Node, + suspend_token: Token, + payload: ?&Node, + body: ?&Node, + + pub fn iterate(self: &Suspend, index: usize) ?&Node { + var i = index; + + if (self.payload) |payload| { + if (i < 1) return payload; + i -= 1; + } + + if (self.body) |body| { + if (i < 1) return body; + i -= 1; + } + + return null; + } + + pub fn firstToken(self: &Suspend) Token { + return self.suspend_token; + } + + pub fn lastToken(self: &Suspend) Token { + if (self.body) |body| { + return body.lastToken(); + } + + if (self.payload) |payload| { + return payload.lastToken(); + } + + return self.suspend_token; + } + }; + + pub const IntegerLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &IntegerLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &IntegerLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &IntegerLiteral) Token { + return self.token; + } + }; + + pub const FloatLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &FloatLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &FloatLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &FloatLiteral) Token { + return self.token; + } + }; + + pub const BuiltinCall = struct { + base: Node, + builtin_token: Token, + params: ArrayList(&Node), + rparen_token: Token, + + pub fn iterate(self: &BuiltinCall, index: usize) ?&Node { + var i = index; + + if (i < self.params.len) return self.params.at(i); + i -= self.params.len; + + return null; + } + + pub fn firstToken(self: &BuiltinCall) Token { + return self.builtin_token; + } + + pub fn lastToken(self: &BuiltinCall) Token { + return self.rparen_token; + } + }; + + pub const StringLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &StringLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &StringLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &StringLiteral) Token { + return self.token; + } + }; + + pub const MultilineStringLiteral = struct { + base: Node, + tokens: ArrayList(Token), + + pub fn iterate(self: &MultilineStringLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &MultilineStringLiteral) Token { + return self.tokens.at(0); + } + + pub fn lastToken(self: &MultilineStringLiteral) Token { + return self.tokens.at(self.tokens.len - 1); + } + }; + + pub const CharLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &CharLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &CharLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &CharLiteral) Token { + return self.token; + } + }; + + pub const BoolLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &BoolLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &BoolLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &BoolLiteral) Token { + return self.token; + } + }; + + pub const NullLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &NullLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &NullLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &NullLiteral) Token { + return self.token; + } + }; + + pub const UndefinedLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &UndefinedLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &UndefinedLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &UndefinedLiteral) Token { + return self.token; + } + }; + + pub const ThisLiteral = struct { + base: Node, + token: Token, + + pub fn iterate(self: &ThisLiteral, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &ThisLiteral) Token { + return self.token; + } + + pub fn lastToken(self: &ThisLiteral) Token { + return self.token; + } + }; + + pub const AsmOutput = struct { + base: Node, + symbolic_name: &Node, + constraint: &Node, + kind: Kind, + + const Kind = union(enum) { + Variable: &Identifier, + Return: &Node + }; + + pub fn iterate(self: &AsmOutput, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.symbolic_name; + i -= 1; + + if (i < 1) return self.constraint; + i -= 1; + + switch (self.kind) { + Kind.Variable => |variable_name| { + if (i < 1) return &variable_name.base; + i -= 1; + }, + Kind.Return => |return_type| { + if (i < 1) return return_type; + i -= 1; + } + } + + return null; + } + + pub fn firstToken(self: &AsmOutput) Token { + return self.symbolic_name.firstToken(); + } + + pub fn lastToken(self: &AsmOutput) Token { + return switch (self.kind) { + Kind.Variable => |variable_name| variable_name.lastToken(), + Kind.Return => |return_type| return_type.lastToken(), + }; + } + }; + + pub const AsmInput = struct { + base: Node, + symbolic_name: &Node, + constraint: &Node, + expr: &Node, + + pub fn iterate(self: &AsmInput, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.symbolic_name; + i -= 1; + + if (i < 1) return self.constraint; + i -= 1; + + if (i < 1) return self.expr; + i -= 1; + + return null; + } + + pub fn firstToken(self: &AsmInput) Token { + return self.symbolic_name.firstToken(); + } + + pub fn lastToken(self: &AsmInput) Token { + return self.expr.lastToken(); + } + }; + + pub const Asm = struct { + base: Node, + asm_token: Token, + volatile_token: ?Token, + template: &Node, + //tokens: ArrayList(AsmToken), + outputs: ArrayList(&AsmOutput), + inputs: ArrayList(&AsmInput), + cloppers: ArrayList(&Node), + rparen: Token, + + pub fn iterate(self: &Asm, index: usize) ?&Node { + var i = index; + + if (i < self.outputs.len) return &self.outputs.at(index).base; + i -= self.outputs.len; + + if (i < self.inputs.len) return &self.inputs.at(index).base; + i -= self.inputs.len; + + if (i < self.cloppers.len) return self.cloppers.at(index); + i -= self.cloppers.len; + + return null; + } + + pub fn firstToken(self: &Asm) Token { + return self.asm_token; + } + + pub fn lastToken(self: &Asm) Token { + return self.rparen; + } + }; + + pub const Unreachable = struct { + base: Node, + token: Token, + + pub fn iterate(self: &Unreachable, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &Unreachable) Token { + return self.token; + } + + pub fn lastToken(self: &Unreachable) Token { + return self.token; + } + }; + + pub const ErrorType = struct { + base: Node, + token: Token, + + pub fn iterate(self: &ErrorType, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &ErrorType) Token { + return self.token; + } + + pub fn lastToken(self: &ErrorType) Token { + return self.token; + } + }; + + pub const VarType = struct { + base: Node, + token: Token, + + pub fn iterate(self: &VarType, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &VarType) Token { + return self.token; + } + + pub fn lastToken(self: &VarType) Token { + return self.token; + } + }; + + pub const LineComment = struct { + base: Node, + lines: ArrayList(Token), + + pub fn iterate(self: &LineComment, index: usize) ?&Node { + return null; + } + + pub fn firstToken(self: &LineComment) Token { + return self.lines.at(0); + } + + pub fn lastToken(self: &LineComment) Token { + return self.lines.at(self.lines.len - 1); + } + }; + + pub const TestDecl = struct { + base: Node, + comments: ?&LineComment, + test_token: Token, + name: &Node, + body_node: &Node, + + pub fn iterate(self: &TestDecl, index: usize) ?&Node { + var i = index; + + if (i < 1) return self.body_node; + i -= 1; + + return null; + } + + pub fn firstToken(self: &TestDecl) Token { + return self.test_token; + } + + pub fn lastToken(self: &TestDecl) Token { + return self.body_node.lastToken(); + } + }; }; -pub const NodeAsmInput = struct { - base: Node, - symbolic_name: &NodeIdentifier, - constraint: &Node, - expr: &Node, - - pub fn iterate(self: &NodeAsmInput, index: usize) ?&Node { - var i = index; - - if (i < 1) return &self.symbolic_name.base; - i -= 1; - - if (i < 1) return self.constraint; - i -= 1; - - if (i < 1) return self.expr; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeAsmInput) Token { - return self.symbolic_name.firstToken(); - } - - pub fn lastToken(self: &NodeAsmInput) Token { - return self.expr.lastToken(); - } -}; - -pub const NodeAsm = struct { - base: Node, - asm_token: Token, - is_volatile: bool, - template: &Node, - //tokens: ArrayList(AsmToken), - outputs: ArrayList(&NodeAsmOutput), - inputs: ArrayList(&NodeAsmInput), - cloppers: ArrayList(&Node), - rparen: Token, - - pub fn iterate(self: &NodeAsm, index: usize) ?&Node { - var i = index; - - if (i < self.outputs.len) return &self.outputs.at(index).base; - i -= self.outputs.len; - - if (i < self.inputs.len) return &self.inputs.at(index).base; - i -= self.inputs.len; - - if (i < self.cloppers.len) return self.cloppers.at(index); - i -= self.cloppers.len; - - return null; - } - - pub fn firstToken(self: &NodeAsm) Token { - return self.asm_token; - } - - pub fn lastToken(self: &NodeAsm) Token { - return self.rparen; - } -}; - -pub const NodeUnreachable = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeUnreachable, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeUnreachable) Token { - return self.token; - } - - pub fn lastToken(self: &NodeUnreachable) Token { - return self.token; - } -}; - -pub const NodeErrorType = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeErrorType, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeErrorType) Token { - return self.token; - } - - pub fn lastToken(self: &NodeErrorType) Token { - return self.token; - } -}; - -pub const NodeVarType = struct { - base: Node, - token: Token, - - pub fn iterate(self: &NodeVarType, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeVarType) Token { - return self.token; - } - - pub fn lastToken(self: &NodeVarType) Token { - return self.token; - } -}; - -pub const NodeLineComment = struct { - base: Node, - lines: ArrayList(Token), - - pub fn iterate(self: &NodeLineComment, index: usize) ?&Node { - return null; - } - - pub fn firstToken(self: &NodeLineComment) Token { - return self.lines.at(0); - } - - pub fn lastToken(self: &NodeLineComment) Token { - return self.lines.at(self.lines.len - 1); - } -}; - -pub const NodeTestDecl = struct { - base: Node, - test_token: Token, - name: &Node, - body_node: &Node, - - pub fn iterate(self: &NodeTestDecl, index: usize) ?&Node { - var i = index; - - if (i < 1) return self.body_node; - i -= 1; - - return null; - } - - pub fn firstToken(self: &NodeTestDecl) Token { - return self.test_token; - } - - pub fn lastToken(self: &NodeTestDecl) Token { - return self.body_node.lastToken(); - } -}; diff --git a/std/zig/parser.zig b/std/zig/parser.zig index 88e6ece35b..7f45cce28b 100644 --- a/std/zig/parser.zig +++ b/std/zig/parser.zig @@ -18,10 +18,9 @@ pub const Parser = struct { put_back_tokens: [2]Token, put_back_count: usize, source_file_name: []const u8, - pending_line_comment_node: ?&ast.NodeLineComment, pub const Tree = struct { - root_node: &ast.NodeRoot, + root_node: &ast.Node.Root, arena_allocator: std.heap.ArenaAllocator, pub fn deinit(self: &Tree) void { @@ -44,7 +43,6 @@ pub const Parser = struct { .put_back_count = 0, .source_file_name = source_file_name, .utility_bytes = []align(utility_bytes_align) u8{}, - .pending_line_comment_node = null, }; } @@ -59,29 +57,31 @@ pub const Parser = struct { lib_name: ?&ast.Node, }; - const ContainerExternCtx = struct { - dest_ptr: DestPtr, - ltoken: Token, - layout: ast.NodeContainerDecl.Layout, + const VarDeclCtx = struct { + mut_token: Token, + visib_token: ?Token, + comptime_token: ?Token, + extern_export_token: ?Token, + lib_name: ?&ast.Node, + list: &ArrayList(&ast.Node), + comments: ?&ast.Node.LineComment, }; - const DestPtr = union(enum) { - Field: &&ast.Node, - NullableField: &?&ast.Node, + const TopLevelExternOrFieldCtx = struct { + visib_token: Token, + container_decl: &ast.Node.ContainerDecl, + }; - pub fn store(self: &const DestPtr, value: &ast.Node) void { - switch (*self) { - DestPtr.Field => |ptr| *ptr = value, - DestPtr.NullableField => |ptr| *ptr = value, - } - } + const ExternTypeCtx = struct { + opt_ctx: OptionalCtx, + extern_token: Token, + comments: ?&ast.Node.LineComment, + }; - pub fn get(self: &const DestPtr) &ast.Node { - switch (*self) { - DestPtr.Field => |ptr| return *ptr, - DestPtr.NullableField => |ptr| return ??*ptr, - } - } + const ContainerKindCtx = struct { + opt_ctx: OptionalCtx, + ltoken: Token, + layout: ast.Node.ContainerDecl.Layout, }; const ExpectTokenSave = struct { @@ -89,13 +89,9 @@ pub const Parser = struct { ptr: &Token, }; - const RevertState = struct { - parser: Parser, - tokenizer: Tokenizer, - - // We expect, that if something is optional, then there is a field, - // that needs to be set to null, when we revert. - ptr: &?&ast.Node, + const OptionalTokenSave = struct { + id: Token.Id, + ptr: &?Token, }; const ExprListCtx = struct { @@ -104,11 +100,6 @@ pub const Parser = struct { ptr: &Token, }; - const ElseCtx = struct { - payload: ?DestPtr, - body: DestPtr, - }; - fn ListSave(comptime T: type) type { return struct { list: &ArrayList(T), @@ -116,27 +107,79 @@ pub const Parser = struct { }; } + const MaybeLabeledExpressionCtx = struct { + label: Token, + opt_ctx: OptionalCtx, + }; + const LabelCtx = struct { label: ?Token, - dest_ptr: DestPtr, + opt_ctx: OptionalCtx, }; const InlineCtx = struct { label: ?Token, inline_token: ?Token, - dest_ptr: DestPtr, + opt_ctx: OptionalCtx, }; const LoopCtx = struct { label: ?Token, inline_token: ?Token, loop_token: Token, - dest_ptr: DestPtr, + opt_ctx: OptionalCtx, }; const AsyncEndCtx = struct { - dest_ptr: DestPtr, - attribute: &ast.NodeAsyncAttribute, + ctx: OptionalCtx, + attribute: &ast.Node.AsyncAttribute, + }; + + const ErrorTypeOrSetDeclCtx = struct { + opt_ctx: OptionalCtx, + error_token: Token, + }; + + const ParamDeclEndCtx = struct { + fn_proto: &ast.Node.FnProto, + param_decl: &ast.Node.ParamDecl, + }; + + const ComptimeStatementCtx = struct { + comptime_token: Token, + block: &ast.Node.Block, + }; + + const OptionalCtx = union(enum) { + Optional: &?&ast.Node, + RequiredNull: &?&ast.Node, + Required: &&ast.Node, + + pub fn store(self: &const OptionalCtx, value: &ast.Node) void { + switch (*self) { + OptionalCtx.Optional => |ptr| *ptr = value, + OptionalCtx.RequiredNull => |ptr| *ptr = value, + OptionalCtx.Required => |ptr| *ptr = value, + } + } + + pub fn get(self: &const OptionalCtx) ?&ast.Node { + switch (*self) { + OptionalCtx.Optional => |ptr| return *ptr, + OptionalCtx.RequiredNull => |ptr| return ??*ptr, + OptionalCtx.Required => |ptr| return *ptr, + } + } + + pub fn toRequired(self: &const OptionalCtx) OptionalCtx { + switch (*self) { + OptionalCtx.Optional => |ptr| { + return OptionalCtx { .RequiredNull = ptr }; + }, + OptionalCtx.RequiredNull => |ptr| return *self, + OptionalCtx.Required => |ptr| return *self, + } + } }; const State = union(enum) { @@ -144,92 +187,116 @@ pub const Parser = struct { TopLevelExtern: TopLevelDeclCtx, TopLevelLibname: TopLevelDeclCtx, TopLevelDecl: TopLevelDeclCtx, - ContainerExtern: ContainerExternCtx, - ContainerDecl: &ast.NodeContainerDecl, - SliceOrArrayAccess: &ast.NodeSuffixOp, - AddrOfModifiers: &ast.NodePrefixOp.AddrOfInfo, - VarDecl: &ast.NodeVarDecl, - VarDeclAlign: &ast.NodeVarDecl, - VarDeclEq: &ast.NodeVarDecl, + TopLevelExternOrField: TopLevelExternOrFieldCtx, + + ContainerKind: ContainerKindCtx, + ContainerInitArgStart: &ast.Node.ContainerDecl, + ContainerInitArg: &ast.Node.ContainerDecl, + ContainerDecl: &ast.Node.ContainerDecl, + + VarDecl: VarDeclCtx, + VarDeclAlign: &ast.Node.VarDecl, + VarDeclEq: &ast.Node.VarDecl, + + FnDef: &ast.Node.FnProto, + FnProto: &ast.Node.FnProto, + FnProtoAlign: &ast.Node.FnProto, + FnProtoReturnType: &ast.Node.FnProto, + + ParamDecl: &ast.Node.FnProto, + ParamDeclAliasOrComptime: &ast.Node.ParamDecl, + ParamDeclName: &ast.Node.ParamDecl, + ParamDeclEnd: ParamDeclEndCtx, + ParamDeclComma: &ast.Node.FnProto, + + MaybeLabeledExpression: MaybeLabeledExpressionCtx, + LabeledExpression: LabelCtx, + Inline: InlineCtx, + While: LoopCtx, + WhileContinueExpr: &?&ast.Node, + For: LoopCtx, + Else: &?&ast.Node.Else, + + Block: &ast.Node.Block, + Statement: &ast.Node.Block, + ComptimeStatement: ComptimeStatementCtx, + Semicolon: &&ast.Node, + + AsmOutputItems: &ArrayList(&ast.Node.AsmOutput), + AsmOutputReturnOrType: &ast.Node.AsmOutput, + AsmInputItems: &ArrayList(&ast.Node.AsmInput), + AsmClopperItems: &ArrayList(&ast.Node), + + ExprListItemOrEnd: ExprListCtx, + ExprListCommaOrEnd: ExprListCtx, + FieldInitListItemOrEnd: ListSave(&ast.Node.FieldInitializer), + FieldInitListCommaOrEnd: ListSave(&ast.Node.FieldInitializer), + FieldListCommaOrEnd: &ast.Node.ContainerDecl, + IdentifierListItemOrEnd: ListSave(&ast.Node), + IdentifierListCommaOrEnd: ListSave(&ast.Node), + SwitchCaseOrEnd: ListSave(&ast.Node.SwitchCase), + SwitchCaseCommaOrEnd: ListSave(&ast.Node.SwitchCase), + SwitchCaseFirstItem: &ArrayList(&ast.Node), + SwitchCaseItem: &ArrayList(&ast.Node), + SwitchCaseItemCommaOrEnd: &ArrayList(&ast.Node), + + SuspendBody: &ast.Node.Suspend, + AsyncAllocator: &ast.Node.AsyncAttribute, + AsyncEnd: AsyncEndCtx, + + ExternType: ExternTypeCtx, + SliceOrArrayAccess: &ast.Node.SuffixOp, + SliceOrArrayType: &ast.Node.PrefixOp, + AddrOfModifiers: &ast.Node.PrefixOp.AddrOfInfo, + + Payload: OptionalCtx, + PointerPayload: OptionalCtx, + PointerIndexPayload: OptionalCtx, + + Expression: OptionalCtx, + RangeExpressionBegin: OptionalCtx, + RangeExpressionEnd: OptionalCtx, + AssignmentExpressionBegin: OptionalCtx, + AssignmentExpressionEnd: OptionalCtx, + UnwrapExpressionBegin: OptionalCtx, + UnwrapExpressionEnd: OptionalCtx, + BoolOrExpressionBegin: OptionalCtx, + BoolOrExpressionEnd: OptionalCtx, + BoolAndExpressionBegin: OptionalCtx, + BoolAndExpressionEnd: OptionalCtx, + ComparisonExpressionBegin: OptionalCtx, + ComparisonExpressionEnd: OptionalCtx, + BinaryOrExpressionBegin: OptionalCtx, + BinaryOrExpressionEnd: OptionalCtx, + BinaryXorExpressionBegin: OptionalCtx, + BinaryXorExpressionEnd: OptionalCtx, + BinaryAndExpressionBegin: OptionalCtx, + BinaryAndExpressionEnd: OptionalCtx, + BitShiftExpressionBegin: OptionalCtx, + BitShiftExpressionEnd: OptionalCtx, + AdditionExpressionBegin: OptionalCtx, + AdditionExpressionEnd: OptionalCtx, + MultiplyExpressionBegin: OptionalCtx, + MultiplyExpressionEnd: OptionalCtx, + CurlySuffixExpressionBegin: OptionalCtx, + CurlySuffixExpressionEnd: OptionalCtx, + TypeExprBegin: OptionalCtx, + TypeExprEnd: OptionalCtx, + PrefixOpExpression: OptionalCtx, + SuffixOpExpressionBegin: OptionalCtx, + SuffixOpExpressionEnd: OptionalCtx, + PrimaryExpression: OptionalCtx, + + ErrorTypeOrSetDecl: ErrorTypeOrSetDeclCtx, + StringLiteral: OptionalCtx, + Identifier: OptionalCtx, + + IfToken: @TagType(Token.Id), IfTokenSave: ExpectTokenSave, ExpectToken: @TagType(Token.Id), ExpectTokenSave: ExpectTokenSave, - FnProto: &ast.NodeFnProto, - FnProtoAlign: &ast.NodeFnProto, - FnProtoReturnType: &ast.NodeFnProto, - ParamDecl: &ast.NodeFnProto, - ParamDeclComma, - FnDef: &ast.NodeFnProto, - LabeledExpression: LabelCtx, - Inline: InlineCtx, - While: LoopCtx, - For: LoopCtx, - Block: &ast.NodeBlock, - Else: &?&ast.NodeElse, - WhileContinueExpr: &?&ast.Node, - Statement: &ast.NodeBlock, - Semicolon: &const &const ast.Node, - AsmOutputItems: &ArrayList(&ast.NodeAsmOutput), - AsmInputItems: &ArrayList(&ast.NodeAsmInput), - AsmClopperItems: &ArrayList(&ast.Node), - ExprListItemOrEnd: ExprListCtx, - ExprListCommaOrEnd: ExprListCtx, - FieldInitListItemOrEnd: ListSave(&ast.NodeFieldInitializer), - FieldInitListCommaOrEnd: ListSave(&ast.NodeFieldInitializer), - FieldListCommaOrEnd: &ast.NodeContainerDecl, - IdentifierListItemOrEnd: ListSave(&ast.NodeIdentifier), - IdentifierListCommaOrEnd: ListSave(&ast.NodeIdentifier), - SwitchCaseOrEnd: ListSave(&ast.NodeSwitchCase), - SuspendBody: &ast.NodeSuspend, - AsyncEnd: AsyncEndCtx, - Payload: &?&ast.NodePayload, - PointerPayload: &?&ast.NodePointerPayload, - PointerIndexPayload: &?&ast.NodePointerIndexPayload, - SwitchCaseCommaOrEnd: ListSave(&ast.NodeSwitchCase), - SwitchCaseItem: &ArrayList(&ast.Node), - SwitchCaseItemCommaOrEnd: &ArrayList(&ast.Node), - - /// A state that can be appended before any other State. If an error occures, - /// the parser will first try looking for the closest optional state. If an - /// optional state is found, the parser will revert to the state it was in - /// when the optional was added. This will polute the arena allocator with - /// "leaked" nodes. TODO: Figure out if it's nessesary to handle leaked nodes. - Optional: RevertState, - - Expression: DestPtr, - RangeExpressionBegin: DestPtr, - RangeExpressionEnd: DestPtr, - AssignmentExpressionBegin: DestPtr, - AssignmentExpressionEnd: DestPtr, - UnwrapExpressionBegin: DestPtr, - UnwrapExpressionEnd: DestPtr, - BoolOrExpressionBegin: DestPtr, - BoolOrExpressionEnd: DestPtr, - BoolAndExpressionBegin: DestPtr, - BoolAndExpressionEnd: DestPtr, - ComparisonExpressionBegin: DestPtr, - ComparisonExpressionEnd: DestPtr, - BinaryOrExpressionBegin: DestPtr, - BinaryOrExpressionEnd: DestPtr, - BinaryXorExpressionBegin: DestPtr, - BinaryXorExpressionEnd: DestPtr, - BinaryAndExpressionBegin: DestPtr, - BinaryAndExpressionEnd: DestPtr, - BitShiftExpressionBegin: DestPtr, - BitShiftExpressionEnd: DestPtr, - AdditionExpressionBegin: DestPtr, - AdditionExpressionEnd: DestPtr, - MultiplyExpressionBegin: DestPtr, - MultiplyExpressionEnd: DestPtr, - CurlySuffixExpressionBegin: DestPtr, - CurlySuffixExpressionEnd: DestPtr, - TypeExprBegin: DestPtr, - TypeExprEnd: DestPtr, - PrefixOpExpression: DestPtr, - SuffixOpExpressionBegin: DestPtr, - SuffixOpExpressionEnd: DestPtr, - PrimaryExpression: DestPtr, + OptionalTokenSave: OptionalTokenSave, }; /// Returns an AST tree, allocated with the parser's allocator. @@ -243,8 +310,8 @@ pub const Parser = struct { errdefer arena_allocator.deinit(); const arena = &arena_allocator.allocator; - const root_node = try self.createNode(arena, ast.NodeRoot, - ast.NodeRoot { + const root_node = try self.createNode(arena, ast.Node.Root, + ast.Node.Root { .base = undefined, .decls = ArrayList(&ast.Node).init(arena), // initialized when we get the eof token @@ -267,66 +334,43 @@ pub const Parser = struct { // warn("\n"); //} - // look for line comments - while (true) { - if (self.eatToken(Token.Id.LineComment)) |line_comment| { - const node = blk: { - if (self.pending_line_comment_node) |comment_node| { - break :blk comment_node; - } else { - const comment_node = try arena.create(ast.NodeLineComment); - *comment_node = ast.NodeLineComment { - .base = ast.Node { - .id = ast.Node.Id.LineComment, - .comment = null, - }, - .lines = ArrayList(Token).init(arena), - }; - self.pending_line_comment_node = comment_node; - break :blk comment_node; - } - }; - try node.lines.append(line_comment); - continue; - } - break; - } - // This gives us 1 free append that can't fail const state = stack.pop(); switch (state) { State.TopLevel => { + const comments = try self.eatComments(arena); const token = self.getNextToken(); switch (token.id) { Token.Id.Keyword_test => { stack.append(State.TopLevel) catch unreachable; - const name_token = self.getNextToken(); - const name = (try self.parseStringLiteral(arena, name_token)) ?? { - try self.parseError(&stack, name_token, "expected string literal, found {}", @tagName(name_token.id)); - continue; - }; - const lbrace = (try self.expectToken(&stack, Token.Id.LBrace)) ?? continue; - - const block = try self.createNode(arena, ast.NodeBlock, - ast.NodeBlock { + const block = try self.createNode(arena, ast.Node.Block, + ast.Node.Block { .base = undefined, .label = null, - .lbrace = lbrace, + .lbrace = undefined, .statements = ArrayList(&ast.Node).init(arena), .rbrace = undefined, } ); - _ = try self.createAttachNode(arena, &root_node.decls, ast.NodeTestDecl, - ast.NodeTestDecl { + const test_node = try self.createAttachNode(arena, &root_node.decls, ast.Node.TestDecl, + ast.Node.TestDecl { .base = undefined, + .comments = comments, .test_token = token, - .name = name, + .name = undefined, .body_node = &block.base, } ); stack.append(State { .Block = block }) catch unreachable; + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.LBrace, + .ptr = &block.rbrace, + } + }); + try stack.append(State { .StringLiteral = OptionalCtx { .Required = &test_node.name } }); continue; }, Token.Id.Eof => { @@ -346,15 +390,30 @@ pub const Parser = struct { continue; }, Token.Id.Keyword_comptime => { - const node = try self.createAttachNode(arena, &root_node.decls, ast.NodeComptime, - ast.NodeComptime { + const block = try self.createNode(arena, ast.Node.Block, + ast.Node.Block { + .base = undefined, + .label = null, + .lbrace = undefined, + .statements = ArrayList(&ast.Node).init(arena), + .rbrace = undefined, + } + ); + const node = try self.createAttachNode(arena, &root_node.decls, ast.Node.Comptime, + ast.Node.Comptime { .base = undefined, .comptime_token = token, - .expr = undefined, + .expr = &block.base, } ); stack.append(State.TopLevel) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); + try stack.append(State { .Block = block }); + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.LBrace, + .ptr = &block.rbrace, + } + }); continue; }, else => { @@ -404,7 +463,6 @@ pub const Parser = struct { } } }, - State.TopLevelLibname => |ctx| { const lib_name = blk: { const lib_name_token = self.getNextToken(); @@ -422,19 +480,19 @@ pub const Parser = struct { .lib_name = lib_name, }, }) catch unreachable; + continue; }, - State.TopLevelDecl => |ctx| { + const comments = try self.eatComments(arena); const token = self.getNextToken(); switch (token.id) { Token.Id.Keyword_use => { if (ctx.extern_export_inline_token != null) { - try self.parseError(&stack, token, "Invalid token {}", @tagName((??ctx.extern_export_inline_token).id)); - continue; + return self.parseError(token, "Invalid token {}", @tagName((??ctx.extern_export_inline_token).id)); } - const node = try self.createAttachNode(arena, ctx.decls, ast.NodeUse, - ast.NodeUse { + const node = try self.createAttachNode(arena, ctx.decls, ast.Node.Use, + ast.Node.Use { .base = undefined, .visib_token = ctx.visib_token, .expr = undefined, @@ -447,44 +505,38 @@ pub const Parser = struct { .ptr = &node.semicolon_token, } }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); continue; }, Token.Id.Keyword_var, Token.Id.Keyword_const => { if (ctx.extern_export_inline_token) |extern_export_inline_token| { if (extern_export_inline_token.id == Token.Id.Keyword_inline) { - try self.parseError(&stack, token, "Invalid token {}", @tagName(extern_export_inline_token.id)); - continue; + return self.parseError(token, "Invalid token {}", @tagName(extern_export_inline_token.id)); } } - const var_decl_node = try self.createAttachNode(arena, ctx.decls, ast.NodeVarDecl, - ast.NodeVarDecl { - .base = undefined, + stack.append(State { + .VarDecl = VarDeclCtx { + .comments = comments, .visib_token = ctx.visib_token, - .mut_token = token, + .lib_name = ctx.lib_name, .comptime_token = null, .extern_export_token = ctx.extern_export_inline_token, - .type_node = null, - .align_node = null, - .init_node = null, - .lib_name = ctx.lib_name, - // initialized later - .name_token = undefined, - .eq_token = undefined, - .semicolon_token = undefined, + .mut_token = token, + .list = ctx.decls } - ); - stack.append(State { .VarDecl = var_decl_node }) catch unreachable; + }) catch unreachable; continue; }, - Token.Id.Keyword_fn => { - const fn_proto = try self.createAttachNode(arena, ctx.decls, ast.NodeFnProto, - ast.NodeFnProto { + Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc, + Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => { + const fn_proto = try self.createAttachNode(arena, ctx.decls, ast.Node.FnProto, + ast.Node.FnProto { .base = undefined, + .comments = comments, .visib_token = ctx.visib_token, .name_token = null, - .fn_token = token, + .fn_token = undefined, .params = ArrayList(&ast.Node).init(arena), .return_type = undefined, .var_args_token = null, @@ -498,166 +550,101 @@ pub const Parser = struct { ); stack.append(State { .FnDef = fn_proto }) catch unreachable; try stack.append(State { .FnProto = fn_proto }); - continue; - }, - Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => { - const fn_proto = try self.createAttachNode(arena, ctx.decls, ast.NodeFnProto, - ast.NodeFnProto { - .base = undefined, - .visib_token = ctx.visib_token, - .name_token = null, - .fn_token = undefined, - .params = ArrayList(&ast.Node).init(arena), - .return_type = undefined, - .var_args_token = null, - .extern_export_inline_token = ctx.extern_export_inline_token, - .cc_token = token, - .async_attr = null, - .body_node = null, - .lib_name = ctx.lib_name, - .align_expr = null, - } - ); - stack.append(State { .FnDef = fn_proto }) catch unreachable; - try stack.append(State { .FnProto = fn_proto }); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Keyword_fn, - .ptr = &fn_proto.fn_token, - } - }); - continue; - }, - Token.Id.Keyword_async => { - const async_node = try self.createNode(arena, ast.NodeAsyncAttribute, - ast.NodeAsyncAttribute { - .base = undefined, - .async_token = token, - .allocator_type = null, - .rangle_bracket = null, - } - ); - const fn_proto = try self.createAttachNode(arena, ctx.decls, ast.NodeFnProto, - ast.NodeFnProto { - .base = undefined, - .visib_token = ctx.visib_token, - .name_token = null, - .fn_token = undefined, - .params = ArrayList(&ast.Node).init(arena), - .return_type = undefined, - .var_args_token = null, - .extern_export_inline_token = ctx.extern_export_inline_token, - .cc_token = null, - .async_attr = async_node, - .body_node = null, - .lib_name = ctx.lib_name, - .align_expr = null, - } - ); - stack.append(State { .FnDef = fn_proto }) catch unreachable; - try stack.append(State { .FnProto = fn_proto }); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Keyword_fn, - .ptr = &fn_proto.fn_token, - } - }); + switch (token.id) { + Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => { + fn_proto.cc_token = token; + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Keyword_fn, + .ptr = &fn_proto.fn_token, + } + }); + continue; + }, + Token.Id.Keyword_async => { + const async_node = try self.createNode(arena, ast.Node.AsyncAttribute, + ast.Node.AsyncAttribute { + .base = undefined, + .async_token = token, + .allocator_type = null, + .rangle_bracket = null, + } + ); + fn_proto.async_attr = async_node; - const langle_bracket = self.getNextToken(); - if (langle_bracket.id != Token.Id.AngleBracketLeft) { - self.putBackToken(langle_bracket); - continue; + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Keyword_fn, + .ptr = &fn_proto.fn_token, + } + }); + try stack.append(State { .AsyncAllocator = async_node }); + continue; + }, + Token.Id.Keyword_fn => { + fn_proto.fn_token = token; + continue; + }, + else => unreachable, } - - async_node.rangle_bracket = Token(undefined); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.AngleBracketRight, - .ptr = &??async_node.rangle_bracket, - } - }); - try stack.append(State { .TypeExprBegin = DestPtr { .NullableField = &async_node.allocator_type } }); - continue; }, else => { - try self.parseError(&stack, token, "expected variable declaration or function, found {}", @tagName(token.id)); - continue; + return self.parseError(token, "expected variable declaration or function, found {}", @tagName(token.id)); }, } }, - State.VarDecl => |var_decl| { - stack.append(State { .VarDeclAlign = var_decl }) catch unreachable; - try stack.append(State { .TypeExprBegin = DestPtr {.NullableField = &var_decl.type_node} }); - try stack.append(State { .IfToken = Token.Id.Colon }); + State.TopLevelExternOrField => |ctx| { + if (self.eatToken(Token.Id.Identifier)) |identifier| { + std.debug.assert(ctx.container_decl.kind == ast.Node.ContainerDecl.Kind.Struct); + const node = try self.createAttachNode(arena, &ctx.container_decl.fields_and_decls, ast.Node.StructField, + ast.Node.StructField { + .base = undefined, + .visib_token = ctx.visib_token, + .name_token = identifier, + .type_expr = undefined, + } + ); + + stack.append(State { .FieldListCommaOrEnd = ctx.container_decl }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = &node.type_expr } }); + try stack.append(State { .ExpectToken = Token.Id.Colon }); + continue; + } + + stack.append(State{ .ContainerDecl = ctx.container_decl }) catch unreachable; try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Identifier, - .ptr = &var_decl.name_token, + .TopLevelExtern = TopLevelDeclCtx { + .decls = &ctx.container_decl.fields_and_decls, + .visib_token = ctx.visib_token, + .extern_export_inline_token = null, + .lib_name = null, } }); continue; }, - State.VarDeclAlign => |var_decl| { - stack.append(State { .VarDeclEq = var_decl }) catch unreachable; - const next_token = self.getNextToken(); - if (next_token.id == Token.Id.Keyword_align) { - try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr{.NullableField = &var_decl.align_node} }); - try stack.append(State { .ExpectToken = Token.Id.LParen }); - continue; - } - self.putBackToken(next_token); - continue; - }, - State.VarDeclEq => |var_decl| { + State.ContainerKind => |ctx| { const token = self.getNextToken(); - switch (token.id) { - Token.Id.Equal => { - var_decl.eq_token = token; - stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Semicolon, - .ptr = &var_decl.semicolon_token, - }, - }) catch unreachable; - try stack.append(State { .Expression = DestPtr {.NullableField = &var_decl.init_node} }); - continue; - }, - Token.Id.Semicolon => { - var_decl.semicolon_token = token; - continue; - }, - else => { - try self.parseError(&stack, token, "expected '=' or ';', found {}", @tagName(token.id)); - continue; - } - } - }, - - State.ContainerExtern => |ctx| { - const token = self.getNextToken(); - const node = try self.createToDestNode(arena, ctx.dest_ptr, ast.NodeContainerDecl, - ast.NodeContainerDecl { + const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.ContainerDecl, + ast.Node.ContainerDecl { .base = undefined, .ltoken = ctx.ltoken, .layout = ctx.layout, .kind = switch (token.id) { - Token.Id.Keyword_struct => ast.NodeContainerDecl.Kind.Struct, - Token.Id.Keyword_union => ast.NodeContainerDecl.Kind.Union, - Token.Id.Keyword_enum => ast.NodeContainerDecl.Kind.Enum, + Token.Id.Keyword_struct => ast.Node.ContainerDecl.Kind.Struct, + Token.Id.Keyword_union => ast.Node.ContainerDecl.Kind.Union, + Token.Id.Keyword_enum => ast.Node.ContainerDecl.Kind.Enum, else => { - try self.parseError(&stack, token, "expected {}, {} or {}, found {}", + return self.parseError(token, "expected {}, {} or {}, found {}", @tagName(Token.Id.Keyword_struct), @tagName(Token.Id.Keyword_union), @tagName(Token.Id.Keyword_enum), @tagName(token.id)); - continue; }, }, - .init_arg_expr = undefined, + .init_arg_expr = ast.Node.ContainerDecl.InitArg.None, .fields_and_decls = ArrayList(&ast.Node).init(arena), .rbrace_token = undefined, } @@ -665,43 +652,42 @@ pub const Parser = struct { stack.append(State { .ContainerDecl = node }) catch unreachable; try stack.append(State { .ExpectToken = Token.Id.LBrace }); + try stack.append(State { .ContainerInitArgStart = node }); + continue; + }, - const lparen = self.getNextToken(); - if (lparen.id != Token.Id.LParen) { - self.putBackToken(lparen); - node.init_arg_expr = ast.NodeContainerDecl.InitArg.None; + State.ContainerInitArgStart => |container_decl| { + if (self.eatToken(Token.Id.LParen) == null) { continue; } - try stack.append(State { .ExpectToken = Token.Id.RParen }); + stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable; + try stack.append(State { .ContainerInitArg = container_decl }); + continue; + }, + State.ContainerInitArg => |container_decl| { const init_arg_token = self.getNextToken(); switch (init_arg_token.id) { Token.Id.Keyword_enum => { - node.init_arg_expr = ast.NodeContainerDecl.InitArg.Enum; + container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg.Enum; }, else => { self.putBackToken(init_arg_token); - node.init_arg_expr = ast.NodeContainerDecl.InitArg { .Type = undefined }; - try stack.append(State { - .Expression = DestPtr { - .Field = &node.init_arg_expr.Type - } - }); + container_decl.init_arg_expr = ast.Node.ContainerDecl.InitArg { .Type = undefined }; + stack.append(State { .Expression = OptionalCtx { .Required = &container_decl.init_arg_expr.Type } }) catch unreachable; }, } continue; }, - State.ContainerDecl => |container_decl| { const token = self.getNextToken(); - switch (token.id) { Token.Id.Identifier => { switch (container_decl.kind) { - ast.NodeContainerDecl.Kind.Struct => { - const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.NodeStructField, - ast.NodeStructField { + ast.Node.ContainerDecl.Kind.Struct => { + const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.Node.StructField, + ast.Node.StructField { .base = undefined, .visib_token = null, .name_token = token, @@ -710,13 +696,13 @@ pub const Parser = struct { ); stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.type_expr } }); + try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.type_expr } }); try stack.append(State { .ExpectToken = Token.Id.Colon }); continue; }, - ast.NodeContainerDecl.Kind.Union => { - const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.NodeUnionTag, - ast.NodeUnionTag { + ast.Node.ContainerDecl.Kind.Union => { + const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.Node.UnionTag, + ast.Node.UnionTag { .base = undefined, .name_token = token, .type_expr = null, @@ -724,19 +710,13 @@ pub const Parser = struct { ); stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable; - - const next = self.getNextToken(); - if (next.id != Token.Id.Colon) { - self.putBackToken(next); - continue; - } - - try stack.append(State { .Expression = DestPtr { .NullableField = &node.type_expr } }); + try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &node.type_expr } }); + try stack.append(State { .IfToken = Token.Id.Colon }); continue; }, - ast.NodeContainerDecl.Kind.Enum => { - const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.NodeEnumTag, - ast.NodeEnumTag { + ast.Node.ContainerDecl.Kind.Enum => { + const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.Node.EnumTag, + ast.Node.EnumTag { .base = undefined, .name_token = token, .value = null, @@ -744,52 +724,36 @@ pub const Parser = struct { ); stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable; - - const next = self.getNextToken(); - if (next.id != Token.Id.Equal) { - self.putBackToken(next); - continue; - } - - try stack.append(State { .Expression = DestPtr { .NullableField = &node.value } }); + try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &node.value } }); + try stack.append(State { .IfToken = Token.Id.Equal }); continue; }, } }, Token.Id.Keyword_pub => { - if (self.eatToken(Token.Id.Identifier)) |identifier| { - switch (container_decl.kind) { - ast.NodeContainerDecl.Kind.Struct => { - const node = try self.createAttachNode(arena, &container_decl.fields_and_decls, ast.NodeStructField, - ast.NodeStructField { - .base = undefined, - .visib_token = token, - .name_token = identifier, - .type_expr = undefined, - } - ); - - stack.append(State { .FieldListCommaOrEnd = container_decl }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.type_expr } }); - try stack.append(State { .ExpectToken = Token.Id.Colon }); - continue; - }, - else => { - self.putBackToken(identifier); - } + switch (container_decl.kind) { + ast.Node.ContainerDecl.Kind.Struct => { + try stack.append(State { + .TopLevelExternOrField = TopLevelExternOrFieldCtx { + .visib_token = token, + .container_decl = container_decl, + } + }); + continue; + }, + else => { + stack.append(State{ .ContainerDecl = container_decl }) catch unreachable; + try stack.append(State { + .TopLevelExtern = TopLevelDeclCtx { + .decls = &container_decl.fields_and_decls, + .visib_token = token, + .extern_export_inline_token = null, + .lib_name = null, + } + }); + continue; } } - - stack.append(State{ .ContainerDecl = container_decl }) catch unreachable; - try stack.append(State { - .TopLevelExtern = TopLevelDeclCtx { - .decls = &container_decl.fields_and_decls, - .visib_token = token, - .extern_export_inline_token = null, - .lib_name = null, - } - }); - continue; }, Token.Id.Keyword_export => { stack.append(State{ .ContainerDecl = container_decl }) catch unreachable; @@ -823,170 +787,1131 @@ pub const Parser = struct { } }, - State.ExpectToken => |token_id| { - _ = (try self.expectToken(&stack, token_id)) ?? continue; + + State.VarDecl => |ctx| { + const var_decl = try self.createAttachNode(arena, ctx.list, ast.Node.VarDecl, + ast.Node.VarDecl { + .base = undefined, + .comments = ctx.comments, + .visib_token = ctx.visib_token, + .mut_token = ctx.mut_token, + .comptime_token = ctx.comptime_token, + .extern_export_token = ctx.extern_export_token, + .type_node = null, + .align_node = null, + .init_node = null, + .lib_name = ctx.lib_name, + // initialized later + .name_token = undefined, + .eq_token = undefined, + .semicolon_token = undefined, + } + ); + + stack.append(State { .VarDeclAlign = var_decl }) catch unreachable; + try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &var_decl.type_node} }); + try stack.append(State { .IfToken = Token.Id.Colon }); + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Identifier, + .ptr = &var_decl.name_token, + } + }); continue; }, + State.VarDeclAlign => |var_decl| { + stack.append(State { .VarDeclEq = var_decl }) catch unreachable; - State.ExpectTokenSave => |expect_token_save| { - *expect_token_save.ptr = (try self.expectToken(&stack, expect_token_save.id)) ?? continue; - continue; - }, - - State.IfToken => |token_id| { - const token = self.getNextToken(); - if (@TagType(Token.Id)(token.id) != token_id) { - self.putBackToken(token); - _ = stack.pop(); - continue; - } - continue; - }, - - State.IfTokenSave => |if_token_save| { - const token = self.getNextToken(); - if (@TagType(Token.Id)(token.id) != if_token_save.id) { - self.putBackToken(token); - _ = stack.pop(); + const next_token = self.getNextToken(); + if (next_token.id == Token.Id.Keyword_align) { + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.align_node} }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); continue; } - *if_token_save.ptr = token; + self.putBackToken(next_token); continue; }, - - State.Optional => { }, - - State.Expression => |dest_ptr| { + State.VarDeclEq => |var_decl| { const token = self.getNextToken(); switch (token.id) { - Token.Id.Keyword_return => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeControlFlowExpression, - ast.NodeControlFlowExpression { - .base = undefined, - .ltoken = token, - .kind = ast.NodeControlFlowExpression.Kind.Return, - .rhs = undefined, - } - ); - - // TODO: Find another way to do optional expressions + Token.Id.Equal => { + var_decl.eq_token = token; stack.append(State { - .Optional = RevertState { - .parser = *self, - .tokenizer = *self.tokenizer, - .ptr = &node.rhs, - } + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Semicolon, + .ptr = &var_decl.semicolon_token, + }, }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .NullableField = &node.rhs } }); + try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &var_decl.init_node } }); continue; }, - Token.Id.Keyword_break, Token.Id.Keyword_continue => { - const label = blk: { - const colon = self.getNextToken(); - if (colon.id != Token.Id.Colon) { - self.putBackToken(colon); - break :blk null; - } - - break :blk (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - }; - - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeControlFlowExpression, - ast.NodeControlFlowExpression { - .base = undefined, - .ltoken = token, - .kind = switch (token.id) { - Token.Id.Keyword_break => ast.NodeControlFlowExpression.Kind { .Break = label }, - Token.Id.Keyword_continue => ast.NodeControlFlowExpression.Kind { .Continue = label }, - else => unreachable, - }, - .rhs = undefined, - } - ); - - // TODO: Find another way to do optional expressions - stack.append(State { - .Optional = RevertState { - .parser = *self, - .tokenizer = *self.tokenizer, - .ptr = &node.rhs, - } - }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .NullableField = &node.rhs } }); - continue; - }, - Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodePrefixOp, - ast.NodePrefixOp { - .base = undefined, - .op_token = token, - .op = switch (token.id) { - Token.Id.Keyword_try => ast.NodePrefixOp.PrefixOp { .Try = void{} }, - Token.Id.Keyword_cancel => ast.NodePrefixOp.PrefixOp { .Cancel = void{} }, - Token.Id.Keyword_resume => ast.NodePrefixOp.PrefixOp { .Resume = void{} }, - else => unreachable, - }, - .rhs = undefined, - } - ); - - stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable; + Token.Id.Semicolon => { + var_decl.semicolon_token = token; continue; }, else => { - if (!try self.parseBlockExpr(&stack, arena, dest_ptr, token)) { + return self.parseError(token, "expected '=' or ';', found {}", @tagName(token.id)); + } + } + }, + + + State.FnDef => |fn_proto| { + const token = self.getNextToken(); + switch(token.id) { + Token.Id.LBrace => { + const block = try self.createNode(arena, ast.Node.Block, + ast.Node.Block { + .base = undefined, + .label = null, + .lbrace = token, + .statements = ArrayList(&ast.Node).init(arena), + .rbrace = undefined, + } + ); + fn_proto.body_node = &block.base; + stack.append(State { .Block = block }) catch unreachable; + continue; + }, + Token.Id.Semicolon => continue, + else => { + return self.parseError(token, "expected ';' or '{{', found {}", @tagName(token.id)); + }, + } + }, + State.FnProto => |fn_proto| { + stack.append(State { .FnProtoAlign = fn_proto }) catch unreachable; + try stack.append(State { .ParamDecl = fn_proto }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + + if (self.eatToken(Token.Id.Identifier)) |name_token| { + fn_proto.name_token = name_token; + } + continue; + }, + State.FnProtoAlign => |fn_proto| { + stack.append(State { .FnProtoReturnType = fn_proto }) catch unreachable; + + if (self.eatToken(Token.Id.Keyword_align)) |align_token| { + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &fn_proto.align_expr } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + } + continue; + }, + State.FnProtoReturnType => |fn_proto| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Bang => { + fn_proto.return_type = ast.Node.FnProto.ReturnType { .InferErrorSet = undefined }; + stack.append(State { + .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.InferErrorSet }, + }) catch unreachable; + continue; + }, + else => { + // TODO: this is a special case. Remove this when #760 is fixed + if (token.id == Token.Id.Keyword_error) { + if (self.isPeekToken(Token.Id.LBrace)) { + fn_proto.return_type = ast.Node.FnProto.ReturnType { + .Explicit = &(try self.createLiteral(arena, ast.Node.ErrorType, token)).base + }; + continue; + } + } + + self.putBackToken(token); + fn_proto.return_type = ast.Node.FnProto.ReturnType { .Explicit = undefined }; + stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &fn_proto.return_type.Explicit }, }) catch unreachable; + continue; + }, + } + }, + + + State.ParamDecl => |fn_proto| { + if (self.eatToken(Token.Id.RParen)) |_| { + continue; + } + const param_decl = try self.createAttachNode(arena, &fn_proto.params, ast.Node.ParamDecl, + ast.Node.ParamDecl { + .base = undefined, + .comptime_token = null, + .noalias_token = null, + .name_token = null, + .type_node = undefined, + .var_args_token = null, + }, + ); + + stack.append(State { + .ParamDeclEnd = ParamDeclEndCtx { + .param_decl = param_decl, + .fn_proto = fn_proto, + } + }) catch unreachable; + try stack.append(State { .ParamDeclName = param_decl }); + try stack.append(State { .ParamDeclAliasOrComptime = param_decl }); + continue; + }, + State.ParamDeclAliasOrComptime => |param_decl| { + if (self.eatToken(Token.Id.Keyword_comptime)) |comptime_token| { + param_decl.comptime_token = comptime_token; + } else if (self.eatToken(Token.Id.Keyword_noalias)) |noalias_token| { + param_decl.noalias_token = noalias_token; + } + continue; + }, + State.ParamDeclName => |param_decl| { + // TODO: Here, we eat two tokens in one state. This means that we can't have + // comments between these two tokens. + if (self.eatToken(Token.Id.Identifier)) |ident_token| { + if (self.eatToken(Token.Id.Colon)) |_| { + param_decl.name_token = ident_token; + } else { + self.putBackToken(ident_token); + } + } + continue; + }, + State.ParamDeclEnd => |ctx| { + if (self.eatToken(Token.Id.Ellipsis3)) |ellipsis3| { + ctx.param_decl.var_args_token = ellipsis3; + stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable; + continue; + } + + try stack.append(State { .ParamDeclComma = ctx.fn_proto }); + try stack.append(State { + .TypeExprBegin = OptionalCtx { .Required = &ctx.param_decl.type_node } + }); + continue; + }, + State.ParamDeclComma => |fn_proto| { + if ((try self.expectCommaOrEnd(Token.Id.RParen)) == null) { + stack.append(State { .ParamDecl = fn_proto }) catch unreachable; + } + continue; + }, + + State.MaybeLabeledExpression => |ctx| { + if (self.eatToken(Token.Id.Colon)) |_| { + stack.append(State { + .LabeledExpression = LabelCtx { + .label = ctx.label, + .opt_ctx = ctx.opt_ctx, + } + }) catch unreachable; + continue; + } + + _ = try self.createToCtxLiteral(arena, ctx.opt_ctx, ast.Node.Identifier, ctx.label); + continue; + }, + State.LabeledExpression => |ctx| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.LBrace => { + const block = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.Block, + ast.Node.Block { + .base = undefined, + .label = ctx.label, + .lbrace = token, + .statements = ArrayList(&ast.Node).init(arena), + .rbrace = undefined, + } + ); + stack.append(State { .Block = block }) catch unreachable; + continue; + }, + Token.Id.Keyword_while => { + stack.append(State { + .While = LoopCtx { + .label = ctx.label, + .inline_token = null, + .loop_token = token, + .opt_ctx = ctx.opt_ctx.toRequired(), + } + }) catch unreachable; + continue; + }, + Token.Id.Keyword_for => { + stack.append(State { + .For = LoopCtx { + .label = ctx.label, + .inline_token = null, + .loop_token = token, + .opt_ctx = ctx.opt_ctx.toRequired(), + } + }) catch unreachable; + continue; + }, + Token.Id.Keyword_inline => { + stack.append(State { + .Inline = InlineCtx { + .label = ctx.label, + .inline_token = token, + .opt_ctx = ctx.opt_ctx.toRequired(), + } + }) catch unreachable; + continue; + }, + else => { + if (ctx.opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected 'while', 'for', 'inline' or '{{', found {}", @tagName(token.id)); + } + + self.putBackToken(token); + continue; + }, + } + }, + State.Inline => |ctx| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Keyword_while => { + stack.append(State { + .While = LoopCtx { + .inline_token = ctx.inline_token, + .label = ctx.label, + .loop_token = token, + .opt_ctx = ctx.opt_ctx.toRequired(), + } + }) catch unreachable; + continue; + }, + Token.Id.Keyword_for => { + stack.append(State { + .For = LoopCtx { + .inline_token = ctx.inline_token, + .label = ctx.label, + .loop_token = token, + .opt_ctx = ctx.opt_ctx.toRequired(), + } + }) catch unreachable; + continue; + }, + else => { + if (ctx.opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected 'while' or 'for', found {}", @tagName(token.id)); + } + + self.putBackToken(token); + continue; + }, + } + }, + State.While => |ctx| { + const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.While, + ast.Node.While { + .base = undefined, + .label = ctx.label, + .inline_token = ctx.inline_token, + .while_token = ctx.loop_token, + .condition = undefined, + .payload = null, + .continue_expr = null, + .body = undefined, + .@"else" = null, + } + ); + stack.append(State { .Else = &node.@"else" }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }); + try stack.append(State { .WhileContinueExpr = &node.continue_expr }); + try stack.append(State { .IfToken = Token.Id.Colon }); + try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } }); + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + continue; + }, + State.WhileContinueExpr => |dest| { + stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable; + try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = dest } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + continue; + }, + State.For => |ctx| { + const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.For, + ast.Node.For { + .base = undefined, + .label = ctx.label, + .inline_token = ctx.inline_token, + .for_token = ctx.loop_token, + .array_expr = undefined, + .payload = null, + .body = undefined, + .@"else" = null, + } + ); + stack.append(State { .Else = &node.@"else" }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }); + try stack.append(State { .PointerIndexPayload = OptionalCtx { .Optional = &node.payload } }); + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.array_expr } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + continue; + }, + State.Else => |dest| { + if (self.eatToken(Token.Id.Keyword_else)) |else_token| { + const node = try self.createNode(arena, ast.Node.Else, + ast.Node.Else { + .base = undefined, + .else_token = else_token, + .payload = null, + .body = undefined, + } + ); + *dest = node; + + stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }) catch unreachable; + try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } }); + continue; + } else { + continue; + } + }, + + + State.Block => |block| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.RBrace => { + block.rbrace = token; + continue; + }, + else => { + self.putBackToken(token); + stack.append(State { .Block = block }) catch unreachable; + try stack.append(State { .Statement = block }); + continue; + }, + } + }, + State.Statement => |block| { + const comments = try self.eatComments(arena); + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Keyword_comptime => { + stack.append(State { + .ComptimeStatement = ComptimeStatementCtx { + .comptime_token = token, + .block = block, + } + }) catch unreachable; + continue; + }, + Token.Id.Keyword_var, Token.Id.Keyword_const => { + stack.append(State { + .VarDecl = VarDeclCtx { + .comments = comments, + .visib_token = null, + .comptime_token = null, + .extern_export_token = null, + .lib_name = null, + .mut_token = token, + .list = &block.statements, + } + }) catch unreachable; + continue; + }, + Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => { + const node = try self.createAttachNode(arena, &block.statements, ast.Node.Defer, + ast.Node.Defer { + .base = undefined, + .defer_token = token, + .kind = switch (token.id) { + Token.Id.Keyword_defer => ast.Node.Defer.Kind.Unconditional, + Token.Id.Keyword_errdefer => ast.Node.Defer.Kind.Error, + else => unreachable, + }, + .expr = undefined, + } + ); + stack.append(State { .Semicolon = &&node.base }) catch unreachable; + try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } }); + continue; + }, + Token.Id.LBrace => { + const inner_block = try self.createAttachNode(arena, &block.statements, ast.Node.Block, + ast.Node.Block { + .base = undefined, + .label = null, + .lbrace = token, + .statements = ArrayList(&ast.Node).init(arena), + .rbrace = undefined, + } + ); + stack.append(State { .Block = inner_block }) catch unreachable; + continue; + }, + else => { + self.putBackToken(token); + const statememt = try block.statements.addOne(); + stack.append(State { .Semicolon = statememt }) catch unreachable; + try stack.append(State { .AssignmentExpressionBegin = OptionalCtx{ .Required = statememt } }); + continue; + } + } + }, + State.ComptimeStatement => |ctx| { + const comments = try self.eatComments(arena); + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Keyword_var, Token.Id.Keyword_const => { + stack.append(State { + .VarDecl = VarDeclCtx { + .comments = comments, + .visib_token = null, + .comptime_token = ctx.comptime_token, + .extern_export_token = null, + .lib_name = null, + .mut_token = token, + .list = &ctx.block.statements, + } + }) catch unreachable; + continue; + }, + else => { + self.putBackToken(token); + self.putBackToken(ctx.comptime_token); + const statememt = try ctx.block.statements.addOne(); + stack.append(State { .Semicolon = statememt }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = statememt } }); + continue; + } + } + }, + State.Semicolon => |node_ptr| { + const node = *node_ptr; + if (requireSemiColon(node)) { + stack.append(State { .ExpectToken = Token.Id.Semicolon }) catch unreachable; + continue; + } + continue; + }, + + + State.AsmOutputItems => |items| { + const lbracket = self.getNextToken(); + if (lbracket.id != Token.Id.LBracket) { + self.putBackToken(lbracket); + continue; + } + + const node = try self.createNode(arena, ast.Node.AsmOutput, + ast.Node.AsmOutput { + .base = undefined, + .symbolic_name = undefined, + .constraint = undefined, + .kind = undefined, + } + ); + try items.append(node); + + stack.append(State { .AsmOutputItems = items }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.Comma }); + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .AsmOutputReturnOrType = node }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } }); + try stack.append(State { .ExpectToken = Token.Id.RBracket }); + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } }); + continue; + }, + State.AsmOutputReturnOrType => |node| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Identifier => { + node.kind = ast.Node.AsmOutput.Kind { .Variable = try self.createLiteral(arena, ast.Node.Identifier, token) }; + continue; + }, + Token.Id.Arrow => { + node.kind = ast.Node.AsmOutput.Kind { .Return = undefined }; + try stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.kind.Return } }); + continue; + }, + else => { + return self.parseError(token, "expected '->' or {}, found {}", + @tagName(Token.Id.Identifier), + @tagName(token.id)); + }, + } + }, + State.AsmInputItems => |items| { + const lbracket = self.getNextToken(); + if (lbracket.id != Token.Id.LBracket) { + self.putBackToken(lbracket); + continue; + } + + const node = try self.createNode(arena, ast.Node.AsmInput, + ast.Node.AsmInput { + .base = undefined, + .symbolic_name = undefined, + .constraint = undefined, + .expr = undefined, + } + ); + try items.append(node); + + stack.append(State { .AsmInputItems = items }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.Comma }); + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.constraint } }); + try stack.append(State { .ExpectToken = Token.Id.RBracket }); + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.symbolic_name } }); + continue; + }, + State.AsmClopperItems => |items| { + stack.append(State { .AsmClopperItems = items }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.Comma }); + try stack.append(State { .StringLiteral = OptionalCtx { .Required = try items.addOne() } }); + continue; + }, + + + State.ExprListItemOrEnd => |list_state| { + if (self.eatToken(list_state.end)) |token| { + *list_state.ptr = token; + continue; + } + + stack.append(State { .ExprListCommaOrEnd = list_state }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = try list_state.list.addOne() } }); + continue; + }, + State.ExprListCommaOrEnd => |list_state| { + if (try self.expectCommaOrEnd(list_state.end)) |end| { + *list_state.ptr = end; + continue; + } else { + stack.append(State { .ExprListItemOrEnd = list_state }) catch unreachable; + continue; + } + }, + State.FieldInitListItemOrEnd => |list_state| { + if (self.eatToken(Token.Id.RBrace)) |rbrace| { + *list_state.ptr = rbrace; + continue; + } + + const node = try self.createNode(arena, ast.Node.FieldInitializer, + ast.Node.FieldInitializer { + .base = undefined, + .period_token = undefined, + .name_token = undefined, + .expr = undefined, + } + ); + try list_state.list.append(node); + + stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx{ .Required = &node.expr } }); + try stack.append(State { .ExpectToken = Token.Id.Equal }); + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Identifier, + .ptr = &node.name_token, + } + }); + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Period, + .ptr = &node.period_token, + } + }); + continue; + }, + State.FieldInitListCommaOrEnd => |list_state| { + if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| { + *list_state.ptr = end; + continue; + } else { + stack.append(State { .FieldInitListItemOrEnd = list_state }) catch unreachable; + continue; + } + }, + State.FieldListCommaOrEnd => |container_decl| { + if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| { + container_decl.rbrace_token = end; + continue; + } else { + stack.append(State { .ContainerDecl = container_decl }) catch unreachable; + continue; + } + }, + State.IdentifierListItemOrEnd => |list_state| { + if (self.eatToken(Token.Id.RBrace)) |rbrace| { + *list_state.ptr = rbrace; + continue; + } + + stack.append(State { .IdentifierListCommaOrEnd = list_state }) catch unreachable; + try stack.append(State { .Identifier = OptionalCtx { .Required = try list_state.list.addOne() } }); + continue; + }, + State.IdentifierListCommaOrEnd => |list_state| { + if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| { + *list_state.ptr = end; + continue; + } else { + stack.append(State { .IdentifierListItemOrEnd = list_state }) catch unreachable; + continue; + } + }, + State.SwitchCaseOrEnd => |list_state| { + if (self.eatToken(Token.Id.RBrace)) |rbrace| { + *list_state.ptr = rbrace; + continue; + } + + const node = try self.createNode(arena, ast.Node.SwitchCase, + ast.Node.SwitchCase { + .base = undefined, + .items = ArrayList(&ast.Node).init(arena), + .payload = null, + .expr = undefined, + } + ); + try list_state.list.append(node); + stack.append(State { .SwitchCaseCommaOrEnd = list_state }) catch unreachable; + try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .Required = &node.expr } }); + try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } }); + try stack.append(State { .SwitchCaseFirstItem = &node.items }); + continue; + }, + State.SwitchCaseCommaOrEnd => |list_state| { + if (try self.expectCommaOrEnd(Token.Id.RBrace)) |end| { + *list_state.ptr = end; + continue; + } else { + stack.append(State { .SwitchCaseOrEnd = list_state }) catch unreachable; + continue; + } + }, + State.SwitchCaseFirstItem => |case_items| { + const token = self.getNextToken(); + if (token.id == Token.Id.Keyword_else) { + const else_node = try self.createAttachNode(arena, case_items, ast.Node.SwitchElse, + ast.Node.SwitchElse { + .base = undefined, + .token = token, + } + ); + try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight }); + continue; + } else { + self.putBackToken(token); + try stack.append(State { .SwitchCaseItem = case_items }); + continue; + } + }, + State.SwitchCaseItem => |case_items| { + stack.append(State { .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable; + try stack.append(State { .RangeExpressionBegin = OptionalCtx { .Required = try case_items.addOne() } }); + }, + State.SwitchCaseItemCommaOrEnd => |case_items| { + if ((try self.expectCommaOrEnd(Token.Id.EqualAngleBracketRight)) == null) { + stack.append(State { .SwitchCaseItem = case_items }) catch unreachable; + } + continue; + }, + + + State.SuspendBody => |suspend_node| { + if (suspend_node.payload != null) { + try stack.append(State { .AssignmentExpressionBegin = OptionalCtx { .RequiredNull = &suspend_node.body } }); + } + continue; + }, + State.AsyncAllocator => |async_node| { + if (self.eatToken(Token.Id.AngleBracketLeft) == null) { + continue; + } + + async_node.rangle_bracket = Token(undefined); + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.AngleBracketRight, + .ptr = &??async_node.rangle_bracket, + } + }); + try stack.append(State { .TypeExprBegin = OptionalCtx { .RequiredNull = &async_node.allocator_type } }); + continue; + }, + State.AsyncEnd => |ctx| { + const node = ctx.ctx.get() ?? continue; + + switch (node.id) { + ast.Node.Id.FnProto => { + const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", node); + fn_proto.async_attr = ctx.attribute; + continue; + }, + ast.Node.Id.SuffixOp => { + const suffix_op = @fieldParentPtr(ast.Node.SuffixOp, "base", node); + if (suffix_op.op == ast.Node.SuffixOp.Op.Call) { + suffix_op.op.Call.async_attr = ctx.attribute; + continue; + } + + return self.parseError(node.firstToken(), "expected {}, found {}.", + @tagName(ast.Node.SuffixOp.Op.Call), + @tagName(suffix_op.op)); + }, + else => { + return self.parseError(node.firstToken(), "expected {} or {}, found {}.", + @tagName(ast.Node.SuffixOp.Op.Call), + @tagName(ast.Node.Id.FnProto), + @tagName(node.id)); + } + } + }, + + + State.ExternType => |ctx| { + if (self.eatToken(Token.Id.Keyword_fn)) |fn_token| { + const fn_proto = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.FnProto, + ast.Node.FnProto { + .base = undefined, + .comments = ctx.comments, + .visib_token = null, + .name_token = null, + .fn_token = fn_token, + .params = ArrayList(&ast.Node).init(arena), + .return_type = undefined, + .var_args_token = null, + .extern_export_inline_token = ctx.extern_token, + .cc_token = null, + .async_attr = null, + .body_node = null, + .lib_name = null, + .align_expr = null, + } + ); + stack.append(State { .FnProto = fn_proto }) catch unreachable; + continue; + } + + stack.append(State { + .ContainerKind = ContainerKindCtx { + .opt_ctx = ctx.opt_ctx, + .ltoken = ctx.extern_token, + .layout = ast.Node.ContainerDecl.Layout.Extern, + }, + }) catch unreachable; + continue; + }, + State.SliceOrArrayAccess => |node| { + var token = self.getNextToken(); + switch (token.id) { + Token.Id.Ellipsis2 => { + const start = node.op.ArrayAccess; + node.op = ast.Node.SuffixOp.Op { + .Slice = ast.Node.SuffixOp.SliceRange { + .start = start, + .end = null, + } + }; + + stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.RBracket, + .ptr = &node.rtoken, + } + }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Optional = &node.op.Slice.end } }); + continue; + }, + Token.Id.RBracket => { + node.rtoken = token; + continue; + }, + else => { + return self.parseError(token, "expected ']' or '..', found {}", @tagName(token.id)); + } + } + }, + State.SliceOrArrayType => |node| { + if (self.eatToken(Token.Id.RBracket)) |_| { + node.op = ast.Node.PrefixOp.Op { + .SliceType = ast.Node.PrefixOp.AddrOfInfo { + .align_expr = null, + .bit_offset_start_token = null, + .bit_offset_end_token = null, + .const_token = null, + .volatile_token = null, + } + }; + stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable; + try stack.append(State { .AddrOfModifiers = &node.op.SliceType }); + continue; + } + + node.op = ast.Node.PrefixOp.Op { .ArrayType = undefined }; + stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable; + try stack.append(State { .ExpectToken = Token.Id.RBracket }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayType } }); + continue; + }, + State.AddrOfModifiers => |addr_of_info| { + var token = self.getNextToken(); + switch (token.id) { + Token.Id.Keyword_align => { + stack.append(state) catch unreachable; + if (addr_of_info.align_expr != null) { + return self.parseError(token, "multiple align qualifiers"); + } + try stack.append(State { .ExpectToken = Token.Id.RParen }); + try stack.append(State { .Expression = OptionalCtx { .RequiredNull = &addr_of_info.align_expr} }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + continue; + }, + Token.Id.Keyword_const => { + stack.append(state) catch unreachable; + if (addr_of_info.const_token != null) { + return self.parseError(token, "duplicate qualifier: const"); + } + addr_of_info.const_token = token; + continue; + }, + Token.Id.Keyword_volatile => { + stack.append(state) catch unreachable; + if (addr_of_info.volatile_token != null) { + return self.parseError(token, "duplicate qualifier: volatile"); + } + addr_of_info.volatile_token = token; + continue; + }, + else => { + self.putBackToken(token); + continue; + }, + } + }, + + + State.Payload => |opt_ctx| { + const token = self.getNextToken(); + if (token.id != Token.Id.Pipe) { + if (opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected {}, found {}.", + @tagName(Token.Id.Pipe), + @tagName(token.id)); + } + + self.putBackToken(token); + continue; + } + + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.Payload, + ast.Node.Payload { + .base = undefined, + .lpipe = token, + .error_symbol = undefined, + .rpipe = undefined + } + ); + + stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Pipe, + .ptr = &node.rpipe, + } + }) catch unreachable; + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.error_symbol } }); + continue; + }, + State.PointerPayload => |opt_ctx| { + const token = self.getNextToken(); + if (token.id != Token.Id.Pipe) { + if (opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected {}, found {}.", + @tagName(Token.Id.Pipe), + @tagName(token.id)); + } + + self.putBackToken(token); + continue; + } + + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.PointerPayload, + ast.Node.PointerPayload { + .base = undefined, + .lpipe = token, + .ptr_token = null, + .value_symbol = undefined, + .rpipe = undefined + } + ); + + stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Pipe, + .ptr = &node.rpipe, + } + }) catch unreachable; + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } }); + try stack.append(State { + .OptionalTokenSave = OptionalTokenSave { + .id = Token.Id.Asterisk, + .ptr = &node.ptr_token, + } + }); + continue; + }, + State.PointerIndexPayload => |opt_ctx| { + const token = self.getNextToken(); + if (token.id != Token.Id.Pipe) { + if (opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected {}, found {}.", + @tagName(Token.Id.Pipe), + @tagName(token.id)); + } + + self.putBackToken(token); + continue; + } + + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.PointerIndexPayload, + ast.Node.PointerIndexPayload { + .base = undefined, + .lpipe = token, + .ptr_token = null, + .value_symbol = undefined, + .index_symbol = null, + .rpipe = undefined + } + ); + + stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Pipe, + .ptr = &node.rpipe, + } + }) catch unreachable; + try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.index_symbol } }); + try stack.append(State { .IfToken = Token.Id.Comma }); + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.value_symbol } }); + try stack.append(State { + .OptionalTokenSave = OptionalTokenSave { + .id = Token.Id.Asterisk, + .ptr = &node.ptr_token, + } + }); + continue; + }, + + + State.Expression => |opt_ctx| { + const token = self.getNextToken(); + switch (token.id) { + Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.ControlFlowExpression, + ast.Node.ControlFlowExpression { + .base = undefined, + .ltoken = token, + .kind = undefined, + .rhs = null, + } + ); + + stack.append(State { .Expression = OptionalCtx { .Optional = &node.rhs } }) catch unreachable; + + switch (token.id) { + Token.Id.Keyword_break => { + node.kind = ast.Node.ControlFlowExpression.Kind { .Break = null }; + try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Break } }); + try stack.append(State { .IfToken = Token.Id.Colon }); + }, + Token.Id.Keyword_continue => { + node.kind = ast.Node.ControlFlowExpression.Kind { .Continue = null }; + try stack.append(State { .Identifier = OptionalCtx { .RequiredNull = &node.kind.Continue } }); + try stack.append(State { .IfToken = Token.Id.Colon }); + }, + Token.Id.Keyword_return => { + node.kind = ast.Node.ControlFlowExpression.Kind.Return; + }, + else => unreachable, + } + continue; + }, + Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.PrefixOp, + ast.Node.PrefixOp { + .base = undefined, + .op_token = token, + .op = switch (token.id) { + Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{} }, + Token.Id.Keyword_cancel => ast.Node.PrefixOp.Op { .Cancel = void{} }, + Token.Id.Keyword_resume => ast.Node.PrefixOp.Op { .Resume = void{} }, + else => unreachable, + }, + .rhs = undefined, + } + ); + + stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable; + continue; + }, + else => { + if (!try self.parseBlockExpr(&stack, arena, opt_ctx, token)) { self.putBackToken(token); - stack.append(State { .UnwrapExpressionBegin = dest_ptr }) catch unreachable; + stack.append(State { .UnwrapExpressionBegin = opt_ctx }) catch unreachable; } continue; } } }, - - State.RangeExpressionBegin => |dest_ptr| { - stack.append(State { .RangeExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .Expression = dest_ptr }); + State.RangeExpressionBegin => |opt_ctx| { + stack.append(State { .RangeExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .Expression = opt_ctx }); continue; }, + State.RangeExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; - State.RangeExpressionEnd => |dest_ptr| { if (self.eatToken(Token.Id.Ellipsis3)) |ellipsis3| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = ellipsis3, - .op = ast.NodeInfixOp.InfixOp.Range, + .op = ast.Node.InfixOp.Op.Range, .rhs = undefined, } ); - stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }) catch unreachable; + stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }) catch unreachable; + continue; } - + }, + State.AssignmentExpressionBegin => |opt_ctx| { + stack.append(State { .AssignmentExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .Expression = opt_ctx }); continue; }, - State.AssignmentExpressionBegin => |dest_ptr| { - stack.append(State { .AssignmentExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .Expression = dest_ptr }); - continue; - }, + State.AssignmentExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; - State.AssignmentExpressionEnd => |dest_ptr| { const token = self.getNextToken(); if (tokenIdToAssignment(token.id)) |ass_id| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, .op = ass_id, .rhs = undefined, } ); - stack.append(State { .AssignmentExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }); + stack.append(State { .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }); continue; } else { self.putBackToken(token); @@ -994,125 +1919,112 @@ pub const Parser = struct { } }, - State.UnwrapExpressionBegin => |dest_ptr| { - stack.append(State { .UnwrapExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BoolOrExpressionBegin = dest_ptr }); + State.UnwrapExpressionBegin => |opt_ctx| { + stack.append(State { .UnwrapExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BoolOrExpressionBegin = opt_ctx }); continue; }, - State.UnwrapExpressionEnd => |dest_ptr| { + State.UnwrapExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_catch, Token.Id.QuestionMarkQuestionMark => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = switch (token.id) { - Token.Id.Keyword_catch => ast.NodeInfixOp.InfixOp { .Catch = null }, - Token.Id.QuestionMarkQuestionMark => ast.NodeInfixOp.InfixOp { .UnwrapMaybe = void{} }, - else => unreachable, - }, - .rhs = undefined, - } - ); - - stack.append(State { .UnwrapExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.rhs } }); - - if (node.op == ast.NodeInfixOp.InfixOp.Catch) { - try stack.append(State { .Payload = &node.op.Catch }); + if (tokenIdToUnwrapExpr(token.id)) |unwrap_id| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = token, + .op = unwrap_id, + .rhs = undefined, } - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + ); + + stack.append(State { .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .Expression = OptionalCtx { .Required = &node.rhs } }); + + if (node.op == ast.Node.InfixOp.Op.Catch) { + try stack.append(State { .Payload = OptionalCtx { .Optional = &node.op.Catch } }); + } + continue; + } else { + self.putBackToken(token); + continue; } }, - State.BoolOrExpressionBegin => |dest_ptr| { - stack.append(State { .BoolOrExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BoolAndExpressionBegin = dest_ptr }); + State.BoolOrExpressionBegin => |opt_ctx| { + stack.append(State { .BoolOrExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BoolAndExpressionBegin = opt_ctx }); continue; }, - State.BoolOrExpressionEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_or => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.BoolOr, - .rhs = undefined, - } - ); - stack.append(State { .BoolOrExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BoolAndExpressionBegin = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + State.BoolOrExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Keyword_or)) |or_token| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = or_token, + .op = ast.Node.InfixOp.Op.BoolOr, + .rhs = undefined, + } + ); + stack.append(State { .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .BoolAndExpressionBegin = OptionalCtx { .Required = &node.rhs } }); + continue; } }, - State.BoolAndExpressionBegin => |dest_ptr| { - stack.append(State { .BoolAndExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .ComparisonExpressionBegin = dest_ptr }); + State.BoolAndExpressionBegin => |opt_ctx| { + stack.append(State { .BoolAndExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .ComparisonExpressionBegin = opt_ctx }); continue; }, - State.BoolAndExpressionEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_and => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.BoolAnd, - .rhs = undefined, - } - ); - stack.append(State { .BoolAndExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .ComparisonExpressionBegin = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + State.BoolAndExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Keyword_and)) |and_token| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = and_token, + .op = ast.Node.InfixOp.Op.BoolAnd, + .rhs = undefined, + } + ); + stack.append(State { .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .ComparisonExpressionBegin = OptionalCtx { .Required = &node.rhs } }); + continue; } }, - State.ComparisonExpressionBegin => |dest_ptr| { - stack.append(State { .ComparisonExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryOrExpressionBegin = dest_ptr }); + State.ComparisonExpressionBegin => |opt_ctx| { + stack.append(State { .ComparisonExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BinaryOrExpressionBegin = opt_ctx }); continue; }, - State.ComparisonExpressionEnd => |dest_ptr| { + State.ComparisonExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); if (tokenIdToComparison(token.id)) |comp_id| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, .op = comp_id, .rhs = undefined, } ); - stack.append(State { .ComparisonExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryOrExpressionBegin = DestPtr { .Field = &node.rhs } }); + stack.append(State { .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .BinaryOrExpressionBegin = OptionalCtx { .Required = &node.rhs } }); continue; } else { self.putBackToken(token); @@ -1120,116 +2032,103 @@ pub const Parser = struct { } }, - State.BinaryOrExpressionBegin => |dest_ptr| { - stack.append(State { .BinaryOrExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryXorExpressionBegin = dest_ptr }); + State.BinaryOrExpressionBegin => |opt_ctx| { + stack.append(State { .BinaryOrExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BinaryXorExpressionBegin = opt_ctx }); continue; }, - State.BinaryOrExpressionEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Pipe => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.BitOr, - .rhs = undefined, - } - ); - stack.append(State { .BinaryOrExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryXorExpressionBegin = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + State.BinaryOrExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Pipe)) |pipe| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = pipe, + .op = ast.Node.InfixOp.Op.BitOr, + .rhs = undefined, + } + ); + stack.append(State { .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .BinaryXorExpressionBegin = OptionalCtx { .Required = &node.rhs } }); + continue; } }, - State.BinaryXorExpressionBegin => |dest_ptr| { - stack.append(State { .BinaryXorExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryAndExpressionBegin = dest_ptr }); + State.BinaryXorExpressionBegin => |opt_ctx| { + stack.append(State { .BinaryXorExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BinaryAndExpressionBegin = opt_ctx }); continue; }, - State.BinaryXorExpressionEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Caret => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.BitXor, - .rhs = undefined, - } - ); - stack.append(State { .BinaryXorExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BinaryAndExpressionBegin = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + State.BinaryXorExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Caret)) |caret| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = caret, + .op = ast.Node.InfixOp.Op.BitXor, + .rhs = undefined, + } + ); + stack.append(State { .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .BinaryAndExpressionBegin = OptionalCtx { .Required = &node.rhs } }); + continue; } }, - State.BinaryAndExpressionBegin => |dest_ptr| { - stack.append(State { .BinaryAndExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BitShiftExpressionBegin = dest_ptr }); + State.BinaryAndExpressionBegin => |opt_ctx| { + stack.append(State { .BinaryAndExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .BitShiftExpressionBegin = opt_ctx }); continue; }, - State.BinaryAndExpressionEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Ampersand => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.BitAnd, - .rhs = undefined, - } - ); - stack.append(State { .BinaryAndExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .BitShiftExpressionBegin = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, + State.BinaryAndExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Ampersand)) |ampersand| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { + .base = undefined, + .lhs = lhs, + .op_token = ampersand, + .op = ast.Node.InfixOp.Op.BitAnd, + .rhs = undefined, + } + ); + stack.append(State { .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .BitShiftExpressionBegin = OptionalCtx { .Required = &node.rhs } }); + continue; } }, - State.BitShiftExpressionBegin => |dest_ptr| { - stack.append(State { .BitShiftExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .AdditionExpressionBegin = dest_ptr }); + State.BitShiftExpressionBegin => |opt_ctx| { + stack.append(State { .BitShiftExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .AdditionExpressionBegin = opt_ctx }); continue; }, - State.BitShiftExpressionEnd => |dest_ptr| { + State.BitShiftExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); if (tokenIdToBitShift(token.id)) |bitshift_id| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, .op = bitshift_id, .rhs = undefined, } ); - stack.append(State { .BitShiftExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .AdditionExpressionBegin = DestPtr { .Field = &node.rhs } }); + stack.append(State { .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .AdditionExpressionBegin = OptionalCtx { .Required = &node.rhs } }); continue; } else { self.putBackToken(token); @@ -1237,26 +2136,28 @@ pub const Parser = struct { } }, - State.AdditionExpressionBegin => |dest_ptr| { - stack.append(State { .AdditionExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .MultiplyExpressionBegin = dest_ptr }); + State.AdditionExpressionBegin => |opt_ctx| { + stack.append(State { .AdditionExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .MultiplyExpressionBegin = opt_ctx }); continue; }, - State.AdditionExpressionEnd => |dest_ptr| { + State.AdditionExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); if (tokenIdToAddition(token.id)) |add_id| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, .op = add_id, .rhs = undefined, } ); - stack.append(State { .AdditionExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .MultiplyExpressionBegin = DestPtr { .Field = &node.rhs } }); + stack.append(State { .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .MultiplyExpressionBegin = OptionalCtx { .Required = &node.rhs } }); continue; } else { self.putBackToken(token); @@ -1264,26 +2165,28 @@ pub const Parser = struct { } }, - State.MultiplyExpressionBegin => |dest_ptr| { - stack.append(State { .MultiplyExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .CurlySuffixExpressionBegin = dest_ptr }); + State.MultiplyExpressionBegin => |opt_ctx| { + stack.append(State { .MultiplyExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .CurlySuffixExpressionBegin = opt_ctx }); continue; }, - State.MultiplyExpressionEnd => |dest_ptr| { + State.MultiplyExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); if (tokenIdToMultiply(token.id)) |mult_id| { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, .op = mult_id, .rhs = undefined, } ); - stack.append(State { .MultiplyExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .CurlySuffixExpressionBegin = DestPtr { .Field = &node.rhs } }); + stack.append(State { .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .CurlySuffixExpressionBegin = OptionalCtx { .Required = &node.rhs } }); continue; } else { self.putBackToken(token); @@ -1291,94 +2194,90 @@ pub const Parser = struct { } }, - State.CurlySuffixExpressionBegin => |dest_ptr| { - stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .TypeExprBegin = dest_ptr }); + State.CurlySuffixExpressionBegin => |opt_ctx| { + stack.append(State { .CurlySuffixExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.LBrace }); + try stack.append(State { .TypeExprBegin = opt_ctx }); continue; }, - State.CurlySuffixExpressionEnd => |dest_ptr| { - if (self.eatToken(Token.Id.LBrace) == null) { - continue; - } + State.CurlySuffixExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; if (self.isPeekToken(Token.Id.Period)) { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp, - ast.NodeSuffixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.SuffixOp, + ast.Node.SuffixOp { .base = undefined, - .lhs = dest_ptr.get(), - .op = ast.NodeSuffixOp.SuffixOp { - .StructInitializer = ArrayList(&ast.NodeFieldInitializer).init(arena), + .lhs = lhs, + .op = ast.Node.SuffixOp.Op { + .StructInitializer = ArrayList(&ast.Node.FieldInitializer).init(arena), }, .rtoken = undefined, } ); - stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable; + stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.LBrace }); try stack.append(State { - .FieldInitListItemOrEnd = ListSave(&ast.NodeFieldInitializer) { + .FieldInitListItemOrEnd = ListSave(&ast.Node.FieldInitializer) { .list = &node.op.StructInitializer, .ptr = &node.rtoken, } }); continue; - } else { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp, - ast.NodeSuffixOp { + } + + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.SuffixOp, + ast.Node.SuffixOp { + .base = undefined, + .lhs = lhs, + .op = ast.Node.SuffixOp.Op { + .ArrayInitializer = ArrayList(&ast.Node).init(arena), + }, + .rtoken = undefined, + } + ); + stack.append(State { .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .IfToken = Token.Id.LBrace }); + try stack.append(State { + .ExprListItemOrEnd = ExprListCtx { + .list = &node.op.ArrayInitializer, + .end = Token.Id.RBrace, + .ptr = &node.rtoken, + } + }); + continue; + }, + + State.TypeExprBegin => |opt_ctx| { + stack.append(State { .TypeExprEnd = opt_ctx }) catch unreachable; + try stack.append(State { .PrefixOpExpression = opt_ctx }); + continue; + }, + + State.TypeExprEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + + if (self.eatToken(Token.Id.Bang)) |bang| { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), - .op = ast.NodeSuffixOp.SuffixOp { - .ArrayInitializer = ArrayList(&ast.Node).init(arena), - }, - .rtoken = undefined, + .lhs = lhs, + .op_token = bang, + .op = ast.Node.InfixOp.Op.ErrorUnion, + .rhs = undefined, } ); - stack.append(State { .CurlySuffixExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { - .ExprListItemOrEnd = ExprListCtx { - .list = &node.op.ArrayInitializer, - .end = Token.Id.RBrace, - .ptr = &node.rtoken, - } - }); + stack.append(State { .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .PrefixOpExpression = OptionalCtx { .Required = &node.rhs } }); continue; } }, - State.TypeExprBegin => |dest_ptr| { - stack.append(State { .TypeExprEnd = dest_ptr }) catch unreachable; - try stack.append(State { .PrefixOpExpression = dest_ptr }); - continue; - }, - - State.TypeExprEnd => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Bang => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { - .base = undefined, - .lhs = dest_ptr.get(), - .op_token = token, - .op = ast.NodeInfixOp.InfixOp.ErrorUnion, - .rhs = undefined, - } - ); - stack.append(State { .TypeExprEnd = dest_ptr }) catch unreachable; - try stack.append(State { .PrefixOpExpression = DestPtr { .Field = &node.rhs } }); - continue; - }, - else => { - self.putBackToken(token); - continue; - }, - } - }, - - State.PrefixOpExpression => |dest_ptr| { + State.PrefixOpExpression => |opt_ctx| { const token = self.getNextToken(); if (tokenIdToPrefixOp(token.id)) |prefix_id| { - var node = try self.createToDestNode(arena, dest_ptr, ast.NodePrefixOp, - ast.NodePrefixOp { + var node = try self.createToCtxNode(arena, opt_ctx, ast.Node.PrefixOp, + ast.Node.PrefixOp { .base = undefined, .op_token = token, .op = prefix_id, @@ -1386,9 +2285,10 @@ pub const Parser = struct { } ); + // Treat '**' token as two derefs if (token.id == Token.Id.AsteriskAsterisk) { - const child = try self.createNode(arena, ast.NodePrefixOp, - ast.NodePrefixOp { + const child = try self.createNode(arena, ast.Node.PrefixOp, + ast.Node.PrefixOp { .base = undefined, .op_token = token, .op = prefix_id, @@ -1399,74 +2299,57 @@ pub const Parser = struct { node = child; } - stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.rhs } }) catch unreachable; - if (node.op == ast.NodePrefixOp.PrefixOp.AddrOf) { + stack.append(State { .TypeExprBegin = OptionalCtx { .Required = &node.rhs } }) catch unreachable; + if (node.op == ast.Node.PrefixOp.Op.AddrOf) { try stack.append(State { .AddrOfModifiers = &node.op.AddrOf }); } continue; } else { self.putBackToken(token); - stack.append(State { .SuffixOpExpressionBegin = dest_ptr }) catch unreachable; + stack.append(State { .SuffixOpExpressionBegin = opt_ctx }) catch unreachable; continue; } }, - State.SuffixOpExpressionBegin => |dest_ptr| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_async => { - const async_node = try self.createNode(arena, ast.NodeAsyncAttribute, - ast.NodeAsyncAttribute { - .base = undefined, - .async_token = token, - .allocator_type = null, - .rangle_bracket = null, - } - ); - stack.append(State { - .AsyncEnd = AsyncEndCtx { - .dest_ptr = dest_ptr, - .attribute = async_node, - } - }) catch unreachable; - try stack.append(State { .SuffixOpExpressionEnd = dest_ptr }); - try stack.append(State { .PrimaryExpression = dest_ptr }); - - const langle_bracket = self.getNextToken(); - if (langle_bracket.id != Token.Id.AngleBracketLeft) { - self.putBackToken(langle_bracket); - continue; + State.SuffixOpExpressionBegin => |opt_ctx| { + if (self.eatToken(Token.Id.Keyword_async)) |async_token| { + const async_node = try self.createNode(arena, ast.Node.AsyncAttribute, + ast.Node.AsyncAttribute { + .base = undefined, + .async_token = async_token, + .allocator_type = null, + .rangle_bracket = null, } - - async_node.rangle_bracket = Token(undefined); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.AngleBracketRight, - .ptr = &??async_node.rangle_bracket, - } - }); - try stack.append(State { .TypeExprBegin = DestPtr { .NullableField = &async_node.allocator_type } }); - continue; - }, - else => { - self.putBackToken(token); - stack.append(State { .SuffixOpExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { .PrimaryExpression = dest_ptr }); - continue; - } + ); + stack.append(State { + .AsyncEnd = AsyncEndCtx { + .ctx = opt_ctx, + .attribute = async_node, + } + }) catch unreachable; + try stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }); + try stack.append(State { .PrimaryExpression = opt_ctx.toRequired() }); + try stack.append(State { .AsyncAllocator = async_node }); + continue; } + + stack.append(State { .SuffixOpExpressionEnd = opt_ctx }) catch unreachable; + try stack.append(State { .PrimaryExpression = opt_ctx }); + continue; }, - State.SuffixOpExpressionEnd => |dest_ptr| { + State.SuffixOpExpressionEnd => |opt_ctx| { + const lhs = opt_ctx.get() ?? continue; + const token = self.getNextToken(); switch (token.id) { Token.Id.LParen => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp, - ast.NodeSuffixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.SuffixOp, + ast.Node.SuffixOp { .base = undefined, - .lhs = dest_ptr.get(), - .op = ast.NodeSuffixOp.SuffixOp { - .Call = ast.NodeSuffixOp.CallInfo { + .lhs = lhs, + .op = ast.Node.SuffixOp.Op { + .Call = ast.Node.SuffixOp.CallInfo { .params = ArrayList(&ast.Node).init(arena), .async_attr = null, } @@ -1474,7 +2357,7 @@ pub const Parser = struct { .rtoken = undefined, } ); - stack.append(State { .SuffixOpExpressionEnd = dest_ptr }) catch unreachable; + stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State { .ExprListItemOrEnd = ExprListCtx { .list = &node.op.Call.params, @@ -1485,39 +2368,33 @@ pub const Parser = struct { continue; }, Token.Id.LBracket => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuffixOp, - ast.NodeSuffixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.SuffixOp, + ast.Node.SuffixOp { .base = undefined, - .lhs = dest_ptr.get(), - .op = ast.NodeSuffixOp.SuffixOp { + .lhs = lhs, + .op = ast.Node.SuffixOp.Op { .ArrayAccess = undefined, }, .rtoken = undefined } ); - stack.append(State { .SuffixOpExpressionEnd = dest_ptr }) catch unreachable; + stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State { .SliceOrArrayAccess = node }); - try stack.append(State { .Expression = DestPtr { .Field = &node.op.ArrayAccess }}); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.op.ArrayAccess }}); continue; }, Token.Id.Period => { - const identifier = try self.createLiteral(arena, ast.NodeIdentifier, Token(undefined)); - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeInfixOp, - ast.NodeInfixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.InfixOp, + ast.Node.InfixOp { .base = undefined, - .lhs = dest_ptr.get(), + .lhs = lhs, .op_token = token, - .op = ast.NodeInfixOp.InfixOp.Period, - .rhs = &identifier.base, + .op = ast.Node.InfixOp.Op.Period, + .rhs = undefined, } ); - stack.append(State { .SuffixOpExpressionEnd = dest_ptr }) catch unreachable; - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Identifier, - .ptr = &identifier.token - } - }); + stack.append(State { .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; + try stack.append(State { .Identifier = OptionalCtx { .Required = &node.rhs } }); continue; }, else => { @@ -1527,51 +2404,52 @@ pub const Parser = struct { } }, - State.PrimaryExpression => |dest_ptr| { + State.PrimaryExpression => |opt_ctx| { const token = self.getNextToken(); switch (token.id) { Token.Id.IntegerLiteral => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeStringLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.StringLiteral, token); continue; }, Token.Id.FloatLiteral => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeFloatLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.FloatLiteral, token); continue; }, Token.Id.CharLiteral => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeCharLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.CharLiteral, token); continue; }, Token.Id.Keyword_undefined => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeUndefinedLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.UndefinedLiteral, token); continue; }, Token.Id.Keyword_true, Token.Id.Keyword_false => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeBoolLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.BoolLiteral, token); continue; }, Token.Id.Keyword_null => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeNullLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.NullLiteral, token); continue; }, Token.Id.Keyword_this => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeThisLiteral, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.ThisLiteral, token); continue; }, Token.Id.Keyword_var => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeVarType, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.VarType, token); continue; }, Token.Id.Keyword_unreachable => { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeUnreachable, token)).base); + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.Unreachable, token); continue; }, Token.Id.StringLiteral, Token.Id.MultilineStringLiteralLine => { - dest_ptr.store((try self.parseStringLiteral(arena, token)) ?? unreachable); + opt_ctx.store((try self.parseStringLiteral(arena, token)) ?? unreachable); + continue; }, Token.Id.LParen => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeGroupedExpression, - ast.NodeGroupedExpression { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.GroupedExpression, + ast.Node.GroupedExpression { .base = undefined, .lparen = token, .expr = undefined, @@ -1584,12 +2462,12 @@ pub const Parser = struct { .ptr = &node.rparen, } }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); continue; }, Token.Id.Builtin => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeBuiltinCall, - ast.NodeBuiltinCall { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.BuiltinCall, + ast.Node.BuiltinCall { .base = undefined, .builtin_token = token, .params = ArrayList(&ast.Node).init(arena), @@ -1607,142 +2485,71 @@ pub const Parser = struct { continue; }, Token.Id.LBracket => { - const rbracket_token = self.getNextToken(); - if (rbracket_token.id == Token.Id.RBracket) { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodePrefixOp, - ast.NodePrefixOp { - .base = undefined, - .op_token = token, - .op = ast.NodePrefixOp.PrefixOp{ - .SliceType = ast.NodePrefixOp.AddrOfInfo { - .align_expr = null, - .bit_offset_start_token = null, - .bit_offset_end_token = null, - .const_token = null, - .volatile_token = null, - } - }, - .rhs = undefined, - } - ); - dest_ptr.store(&node.base); - stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.rhs } }) catch unreachable; - try stack.append(State { .AddrOfModifiers = &node.op.SliceType }); - continue; - } - - self.putBackToken(rbracket_token); - - const node = try self.createToDestNode(arena, dest_ptr, ast.NodePrefixOp, - ast.NodePrefixOp { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.PrefixOp, + ast.Node.PrefixOp { .base = undefined, .op_token = token, - .op = ast.NodePrefixOp.PrefixOp{ - .ArrayType = undefined, - }, + .op = undefined, .rhs = undefined, } ); - stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.rhs } }) catch unreachable; - try stack.append(State { .ExpectToken = Token.Id.RBracket }); - try stack.append(State { .Expression = DestPtr { .Field = &node.op.ArrayType } }); - + stack.append(State { .SliceOrArrayType = node }) catch unreachable; + continue; }, Token.Id.Keyword_error => { - if (self.eatToken(Token.Id.LBrace) == null) { - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeErrorType, token)).base); - continue; - } - - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeErrorSetDecl, - ast.NodeErrorSetDecl { - .base = undefined, - .error_token = token, - .decls = ArrayList(&ast.NodeIdentifier).init(arena), - .rbrace_token = undefined, - } - ); - stack.append(State { - .IdentifierListItemOrEnd = ListSave(&ast.NodeIdentifier) { - .list = &node.decls, - .ptr = &node.rbrace_token, + .ErrorTypeOrSetDecl = ErrorTypeOrSetDeclCtx { + .error_token = token, + .opt_ctx = opt_ctx } }) catch unreachable; continue; }, Token.Id.Keyword_packed => { stack.append(State { - .ContainerExtern = ContainerExternCtx { - .dest_ptr = dest_ptr, + .ContainerKind = ContainerKindCtx { + .opt_ctx = opt_ctx, .ltoken = token, - .layout = ast.NodeContainerDecl.Layout.Packed, + .layout = ast.Node.ContainerDecl.Layout.Packed, }, }) catch unreachable; + continue; }, Token.Id.Keyword_extern => { - const next = self.getNextToken(); - if (next.id == Token.Id.Keyword_fn) { - const fn_proto = try self.createToDestNode(arena, dest_ptr, ast.NodeFnProto, - ast.NodeFnProto { - .base = undefined, - .visib_token = null, - .name_token = null, - .fn_token = next, - .params = ArrayList(&ast.Node).init(arena), - .return_type = undefined, - .var_args_token = null, - .extern_export_inline_token = token, - .cc_token = null, - .async_attr = null, - .body_node = null, - .lib_name = null, - .align_expr = null, - } - ); - stack.append(State { .FnProto = fn_proto }) catch unreachable; - continue; - } - - self.putBackToken(next); stack.append(State { - .ContainerExtern = ContainerExternCtx { - .dest_ptr = dest_ptr, - .ltoken = token, - .layout = ast.NodeContainerDecl.Layout.Extern, + .ExternType = ExternTypeCtx { + .opt_ctx = opt_ctx, + .extern_token = token, + .comments = null, }, }) catch unreachable; + continue; }, Token.Id.Keyword_struct, Token.Id.Keyword_union, Token.Id.Keyword_enum => { self.putBackToken(token); stack.append(State { - .ContainerExtern = ContainerExternCtx { - .dest_ptr = dest_ptr, + .ContainerKind = ContainerKindCtx { + .opt_ctx = opt_ctx, .ltoken = token, - .layout = ast.NodeContainerDecl.Layout.Auto, + .layout = ast.Node.ContainerDecl.Layout.Auto, }, }) catch unreachable; + continue; }, Token.Id.Identifier => { - const next = self.getNextToken(); - if (next.id != Token.Id.Colon) { - self.putBackToken(next); - dest_ptr.store(&(try self.createLiteral(arena, ast.NodeIdentifier, token)).base); - continue; - } - stack.append(State { - .LabeledExpression = LabelCtx { + .MaybeLabeledExpression = MaybeLabeledExpressionCtx { .label = token, - .dest_ptr = dest_ptr + .opt_ctx = opt_ctx } }) catch unreachable; continue; }, Token.Id.Keyword_fn => { - const fn_proto = try self.createToDestNode(arena, dest_ptr, ast.NodeFnProto, - ast.NodeFnProto { + const fn_proto = try self.createToCtxNode(arena, opt_ctx, ast.Node.FnProto, + ast.Node.FnProto { .base = undefined, + .comments = null, .visib_token = null, .name_token = null, .fn_token = token, @@ -1761,13 +2568,13 @@ pub const Parser = struct { continue; }, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => { - const fn_token = (try self.expectToken(&stack, Token.Id.Keyword_fn)) ?? continue; - const fn_proto = try self.createToDestNode(arena, dest_ptr, ast.NodeFnProto, - ast.NodeFnProto { + const fn_proto = try self.createToCtxNode(arena, opt_ctx, ast.Node.FnProto, + ast.Node.FnProto { .base = undefined, + .comments = null, .visib_token = null, .name_token = null, - .fn_token = fn_token, + .fn_token = undefined, .params = ArrayList(&ast.Node).init(arena), .return_type = undefined, .var_args_token = null, @@ -1780,35 +2587,24 @@ pub const Parser = struct { } ); stack.append(State { .FnProto = fn_proto }) catch unreachable; + try stack.append(State { + .ExpectTokenSave = ExpectTokenSave { + .id = Token.Id.Keyword_fn, + .ptr = &fn_proto.fn_token + } + }); continue; }, Token.Id.Keyword_asm => { - const is_volatile = blk: { - const volatile_token = self.getNextToken(); - if (volatile_token.id != Token.Id.Keyword_volatile) { - self.putBackToken(volatile_token); - break :blk false; - } - break :blk true; - }; - _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue; - - const template_token = self.getNextToken(); - const template = (try self.parseStringLiteral(arena, template_token)) ?? { - try self.parseError(&stack, template_token, "expected string literal, found {}", @tagName(template_token.id)); - continue; - }; - // TODO parse template - - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeAsm, - ast.NodeAsm { + const node = try self.createToCtxNode(arena, opt_ctx, ast.Node.Asm, + ast.Node.Asm { .base = undefined, .asm_token = token, - .is_volatile = is_volatile, - .template = template, - //.tokens = ArrayList(ast.NodeAsm.AsmToken).init(arena), - .outputs = ArrayList(&ast.NodeAsmOutput).init(arena), - .inputs = ArrayList(&ast.NodeAsmInput).init(arena), + .volatile_token = null, + .template = undefined, + //.tokens = ArrayList(ast.Node.Asm.AsmToken).init(arena), + .outputs = ArrayList(&ast.Node.AsmOutput).init(arena), + .inputs = ArrayList(&ast.Node.AsmInput).init(arena), .cloppers = ArrayList(&ast.Node).init(arena), .rparen = undefined, } @@ -1825,887 +2621,151 @@ pub const Parser = struct { try stack.append(State { .IfToken = Token.Id.Colon }); try stack.append(State { .AsmOutputItems = &node.outputs }); try stack.append(State { .IfToken = Token.Id.Colon }); + try stack.append(State { .StringLiteral = OptionalCtx { .Required = &node.template } }); + try stack.append(State { .ExpectToken = Token.Id.LParen }); + try stack.append(State { + .OptionalTokenSave = OptionalTokenSave { + .id = Token.Id.Keyword_volatile, + .ptr = &node.volatile_token, + } + }); }, Token.Id.Keyword_inline => { stack.append(State { .Inline = InlineCtx { .label = null, .inline_token = token, - .dest_ptr = dest_ptr, + .opt_ctx = opt_ctx, } }) catch unreachable; continue; }, else => { - if (!try self.parseBlockExpr(&stack, arena, dest_ptr, token)) { - try self.parseError(&stack, token, "expected primary expression, found {}", @tagName(token.id)); - } - continue; - } - } - }, - - State.SliceOrArrayAccess => |node| { - var token = self.getNextToken(); - - switch (token.id) { - Token.Id.Ellipsis2 => { - const start = node.op.ArrayAccess; - node.op = ast.NodeSuffixOp.SuffixOp { - .Slice = ast.NodeSuffixOp.SliceRange { - .start = start, - .end = undefined, + if (!try self.parseBlockExpr(&stack, arena, opt_ctx, token)) { + self.putBackToken(token); + if (opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)); } - }; - - const rbracket_token = self.getNextToken(); - if (rbracket_token.id != Token.Id.RBracket) { - self.putBackToken(rbracket_token); - stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.RBracket, - .ptr = &node.rtoken, - } - }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .NullableField = &node.op.Slice.end } }); - } else { - node.rtoken = rbracket_token; } continue; - }, - Token.Id.RBracket => { - node.rtoken = token; - continue; - }, - else => { - try self.parseError(&stack, token, "expected ']' or '..', found {}", @tagName(token.id)); - continue; } } }, - State.AsmOutputItems => |items| { - const lbracket = self.getNextToken(); - if (lbracket.id != Token.Id.LBracket) { - self.putBackToken(lbracket); + State.ErrorTypeOrSetDecl => |ctx| { + if (self.eatToken(Token.Id.LBrace) == null) { + _ = try self.createToCtxLiteral(arena, ctx.opt_ctx, ast.Node.ErrorType, ctx.error_token); continue; } - stack.append(State { .AsmOutputItems = items }) catch unreachable; - try stack.append(State { .IfToken = Token.Id.Comma }); - - const symbolic_name = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - _ = (try self.expectToken(&stack, Token.Id.RBracket)) ?? continue; - - const constraint_token = self.getNextToken(); - const constraint = (try self.parseStringLiteral(arena, constraint_token)) ?? { - try self.parseError(&stack, constraint_token, "expected string literal, found {}", @tagName(constraint_token.id)); - continue; - }; - - _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue; - try stack.append(State { .ExpectToken = Token.Id.RParen }); - - const node = try self.createNode(arena, ast.NodeAsmOutput, - ast.NodeAsmOutput { + const node = try self.createToCtxNode(arena, ctx.opt_ctx, ast.Node.ErrorSetDecl, + ast.Node.ErrorSetDecl { .base = undefined, - .symbolic_name = try self.createLiteral(arena, ast.NodeIdentifier, symbolic_name), - .constraint = constraint, - .kind = undefined, + .error_token = ctx.error_token, + .decls = ArrayList(&ast.Node).init(arena), + .rbrace_token = undefined, } ); - try items.append(node); - const symbol_or_arrow = self.getNextToken(); - switch (symbol_or_arrow.id) { - Token.Id.Identifier => { - node.kind = ast.NodeAsmOutput.Kind { .Variable = try self.createLiteral(arena, ast.NodeIdentifier, symbol_or_arrow) }; - }, - Token.Id.Arrow => { - node.kind = ast.NodeAsmOutput.Kind { .Return = undefined }; - try stack.append(State { .TypeExprBegin = DestPtr { .Field = &node.kind.Return } }); - }, - else => { - try self.parseError(&stack, symbol_or_arrow, "expected '->' or {}, found {}", - @tagName(Token.Id.Identifier), - @tagName(symbol_or_arrow.id)); - continue; - }, - } - }, - - State.AsmInputItems => |items| { - const lbracket = self.getNextToken(); - if (lbracket.id != Token.Id.LBracket) { - self.putBackToken(lbracket); - continue; - } - - stack.append(State { .AsmInputItems = items }) catch unreachable; - try stack.append(State { .IfToken = Token.Id.Comma }); - - const symbolic_name = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - _ = (try self.expectToken(&stack, Token.Id.RBracket)) ?? continue; - - const constraint_token = self.getNextToken(); - const constraint = (try self.parseStringLiteral(arena, constraint_token)) ?? { - try self.parseError(&stack, constraint_token, "expected string literal, found {}", @tagName(constraint_token.id)); - continue; - }; - - _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue; - try stack.append(State { .ExpectToken = Token.Id.RParen }); - - const node = try self.createNode(arena, ast.NodeAsmInput, - ast.NodeAsmInput { - .base = undefined, - .symbolic_name = try self.createLiteral(arena, ast.NodeIdentifier, symbolic_name), - .constraint = constraint, - .expr = undefined, - } - ); - try items.append(node); - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); - }, - - State.AsmClopperItems => |items| { - const string_token = self.getNextToken(); - const string = (try self.parseStringLiteral(arena, string_token)) ?? { - self.putBackToken(string_token); - continue; - }; - try items.append(string); - - stack.append(State { .AsmClopperItems = items }) catch unreachable; - try stack.append(State { .IfToken = Token.Id.Comma }); - }, - - State.ExprListItemOrEnd => |list_state| { - var token = self.getNextToken(); - - const IdTag = @TagType(Token.Id); - if (IdTag(list_state.end) == token.id) { - *list_state.ptr = token; - continue; - } - - self.putBackToken(token); - stack.append(State { .ExprListCommaOrEnd = list_state }) catch unreachable; - try stack.append(State { .Expression = DestPtr{ .Field = try list_state.list.addOne() } }); - }, - - State.FieldInitListItemOrEnd => |list_state| { - if (self.eatToken(Token.Id.RBrace)) |rbrace| { - *list_state.ptr = rbrace; - continue; - } - - const node = try self.createNode(arena, ast.NodeFieldInitializer, - ast.NodeFieldInitializer { - .base = undefined, - .period_token = undefined, - .name_token = undefined, - .expr = undefined, - } - ); - try list_state.list.append(node); - - stack.append(State { .FieldInitListCommaOrEnd = list_state }) catch unreachable; - try stack.append(State { .Expression = DestPtr{.Field = &node.expr} }); - try stack.append(State { .ExpectToken = Token.Id.Equal }); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Identifier, - .ptr = &node.name_token, - } - }); - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Period, - .ptr = &node.period_token, - } - }); - }, - - State.IdentifierListItemOrEnd => |list_state| { - if (self.eatToken(Token.Id.RBrace)) |rbrace| { - *list_state.ptr = rbrace; - continue; - } - - const node = try self.createLiteral(arena, ast.NodeIdentifier, Token(undefined)); - try list_state.list.append(node); - - stack.append(State { .IdentifierListCommaOrEnd = list_state }) catch unreachable; - try stack.append(State { - .ExpectTokenSave = ExpectTokenSave { - .id = Token.Id.Identifier, - .ptr = &node.token, - } - }); - }, - - State.SwitchCaseOrEnd => |list_state| { - if (self.eatToken(Token.Id.RBrace)) |rbrace| { - *list_state.ptr = rbrace; - continue; - } - - const node = try self.createNode(arena, ast.NodeSwitchCase, - ast.NodeSwitchCase { - .base = undefined, - .items = ArrayList(&ast.Node).init(arena), - .payload = null, - .expr = undefined, - } - ); - try list_state.list.append(node); - stack.append(State { .SwitchCaseCommaOrEnd = list_state }) catch unreachable; - try stack.append(State { .AssignmentExpressionBegin = DestPtr{ .Field = &node.expr } }); - try stack.append(State { .PointerPayload = &node.payload }); - - const maybe_else = self.getNextToken(); - if (maybe_else.id == Token.Id.Keyword_else) { - const else_node = try self.createAttachNode(arena, &node.items, ast.NodeSwitchElse, - ast.NodeSwitchElse { - .base = undefined, - .token = maybe_else, - } - ); - try stack.append(State { .ExpectToken = Token.Id.EqualAngleBracketRight }); - continue; - } else { - self.putBackToken(maybe_else); - try stack.append(State { .SwitchCaseItem = &node.items }); - continue; - } - }, - - State.SwitchCaseItem => |case_items| { - stack.append(State { .SwitchCaseItemCommaOrEnd = case_items }) catch unreachable; - try stack.append(State { .RangeExpressionBegin = DestPtr{ .Field = try case_items.addOne() } }); - }, - - State.ExprListCommaOrEnd => |list_state| { - try self.commaOrEnd(&stack, list_state.end, list_state.ptr, State { .ExprListItemOrEnd = list_state }); - continue; - }, - - State.FieldInitListCommaOrEnd => |list_state| { - try self.commaOrEnd(&stack, Token.Id.RBrace, list_state.ptr, State { .FieldInitListItemOrEnd = list_state }); - continue; - }, - - State.FieldListCommaOrEnd => |container_decl| { - try self.commaOrEnd(&stack, Token.Id.RBrace, &container_decl.rbrace_token, - State { .ContainerDecl = container_decl }); - continue; - }, - - State.IdentifierListCommaOrEnd => |list_state| { - try self.commaOrEnd(&stack, Token.Id.RBrace, list_state.ptr, State { .IdentifierListItemOrEnd = list_state }); - continue; - }, - - State.SwitchCaseCommaOrEnd => |list_state| { - try self.commaOrEnd(&stack, Token.Id.RBrace, list_state.ptr, State { .SwitchCaseOrEnd = list_state }); - continue; - }, - - State.SwitchCaseItemCommaOrEnd => |case_items| { - try self.commaOrEnd(&stack, Token.Id.EqualAngleBracketRight, null, State { .SwitchCaseItem = case_items }); - continue; - }, - - State.Else => |dest| { - const else_token = self.getNextToken(); - if (else_token.id != Token.Id.Keyword_else) { - self.putBackToken(else_token); - continue; - } - - const node = try self.createNode(arena, ast.NodeElse, - ast.NodeElse { - .base = undefined, - .else_token = else_token, - .payload = null, - .body = undefined, - } - ); - *dest = node; - - stack.append(State { .Expression = DestPtr { .Field = &node.body } }) catch unreachable; - try stack.append(State { .Payload = &node.payload }); - }, - - State.WhileContinueExpr => |dest| { - const colon = self.getNextToken(); - if (colon.id != Token.Id.Colon) { - self.putBackToken(colon); - continue; - } - - _ = (try self.expectToken(&stack, Token.Id.LParen)) ?? continue; - stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable; - try stack.append(State { .AssignmentExpressionBegin = DestPtr { .NullableField = dest } }); - }, - - State.SuspendBody => |suspend_node| { - if (suspend_node.payload != null) { - try stack.append(State { .AssignmentExpressionBegin = DestPtr { .NullableField = &suspend_node.body } }); - } - continue; - }, - - State.AsyncEnd => |ctx| { - const node = ctx.dest_ptr.get(); - - switch (node.id) { - ast.Node.Id.FnProto => { - const fn_proto = @fieldParentPtr(ast.NodeFnProto, "base", node); - fn_proto.async_attr = ctx.attribute; - }, - ast.Node.Id.SuffixOp => { - const suffix_op = @fieldParentPtr(ast.NodeSuffixOp, "base", node); - if (suffix_op.op == ast.NodeSuffixOp.SuffixOp.Call) { - suffix_op.op.Call.async_attr = ctx.attribute; - continue; - } - - try self.parseError(&stack, node.firstToken(), "expected call or fn proto, found {}.", - @tagName(suffix_op.op)); - continue; - }, - else => { - try self.parseError(&stack, node.firstToken(), "expected call or fn proto, found {}.", - @tagName(node.id)); - continue; - } - } - }, - - State.Payload => |dest| { - const lpipe = self.getNextToken(); - if (lpipe.id != Token.Id.Pipe) { - self.putBackToken(lpipe); - continue; - } - - const error_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue; - *dest = try self.createNode(arena, ast.NodePayload, - ast.NodePayload { - .base = undefined, - .lpipe = lpipe, - .error_symbol = try self.createLiteral(arena, ast.NodeIdentifier, error_symbol), - .rpipe = rpipe - } - ); - }, - - State.PointerPayload => |dest| { - const lpipe = self.getNextToken(); - if (lpipe.id != Token.Id.Pipe) { - self.putBackToken(lpipe); - continue; - } - - const is_ptr = blk: { - const asterik = self.getNextToken(); - if (asterik.id == Token.Id.Asterisk) { - break :blk true; - } else { - self.putBackToken(asterik); - break :blk false; - } - }; - - const value_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue; - *dest = try self.createNode(arena, ast.NodePointerPayload, - ast.NodePointerPayload { - .base = undefined, - .lpipe = lpipe, - .is_ptr = is_ptr, - .value_symbol = try self.createLiteral(arena, ast.NodeIdentifier, value_symbol), - .rpipe = rpipe - } - ); - }, - - State.PointerIndexPayload => |dest| { - const lpipe = self.getNextToken(); - if (lpipe.id != Token.Id.Pipe) { - self.putBackToken(lpipe); - continue; - } - - const is_ptr = blk: { - const asterik = self.getNextToken(); - if (asterik.id == Token.Id.Asterisk) { - break :blk true; - } else { - self.putBackToken(asterik); - break :blk false; - } - }; - - const value_symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - const index_symbol = blk: { - const comma = self.getNextToken(); - if (comma.id != Token.Id.Comma) { - self.putBackToken(comma); - break :blk null; - } - - const symbol = (try self.expectToken(&stack, Token.Id.Identifier)) ?? continue; - break :blk try self.createLiteral(arena, ast.NodeIdentifier, symbol); - }; - - const rpipe = (try self.expectToken(&stack, Token.Id.Pipe)) ?? continue; - *dest = try self.createNode(arena, ast.NodePointerIndexPayload, - ast.NodePointerIndexPayload { - .base = undefined, - .lpipe = lpipe, - .is_ptr = is_ptr, - .value_symbol = try self.createLiteral(arena, ast.NodeIdentifier, value_symbol), - .index_symbol = index_symbol, - .rpipe = rpipe - } - ); - }, - - State.AddrOfModifiers => |addr_of_info| { - var token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_align => { - stack.append(state) catch unreachable; - if (addr_of_info.align_expr != null) { - try self.parseError(&stack, token, "multiple align qualifiers"); - continue; - } - try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr{.NullableField = &addr_of_info.align_expr} }); - try stack.append(State { .ExpectToken = Token.Id.LParen }); - continue; - }, - Token.Id.Keyword_const => { - stack.append(state) catch unreachable; - if (addr_of_info.const_token != null) { - try self.parseError(&stack, token, "duplicate qualifier: const"); - continue; - } - addr_of_info.const_token = token; - continue; - }, - Token.Id.Keyword_volatile => { - stack.append(state) catch unreachable; - if (addr_of_info.volatile_token != null) { - try self.parseError(&stack, token, "duplicate qualifier: volatile"); - continue; - } - addr_of_info.volatile_token = token; - continue; - }, - else => { - self.putBackToken(token); - continue; - }, - } - }, - - State.FnProto => |fn_proto| { - stack.append(State { .FnProtoAlign = fn_proto }) catch unreachable; - try stack.append(State { .ParamDecl = fn_proto }); - try stack.append(State { .ExpectToken = Token.Id.LParen }); - - const next_token = self.getNextToken(); - if (next_token.id == Token.Id.Identifier) { - fn_proto.name_token = next_token; - continue; - } - self.putBackToken(next_token); - continue; - }, - - State.FnProtoAlign => |fn_proto| { - if (self.eatToken(Token.Id.Keyword_align)) |align_token| { - @panic("TODO fn proto align"); - } stack.append(State { - .FnProtoReturnType = fn_proto, + .IdentifierListItemOrEnd = ListSave(&ast.Node) { + .list = &node.decls, + .ptr = &node.rbrace_token, + } }) catch unreachable; continue; }, - - State.FnProtoReturnType => |fn_proto| { + State.StringLiteral => |opt_ctx| { const token = self.getNextToken(); - switch (token.id) { - Token.Id.Bang => { - fn_proto.return_type = ast.NodeFnProto.ReturnType { .InferErrorSet = undefined }; - stack.append(State { - .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.InferErrorSet}, - }) catch unreachable; - continue; - }, - Token.Id.Keyword_align => { - @panic("TODO fn proto align"); - continue; - }, - else => { - // TODO: this is a special case. Remove this when #760 is fixed - if (token.id == Token.Id.Keyword_error) { - if (self.isPeekToken(Token.Id.LBrace)) { - fn_proto.return_type = ast.NodeFnProto.ReturnType { - .Explicit = &(try self.createLiteral(arena, ast.NodeErrorType, token)).base - }; - continue; - } + opt_ctx.store( + (try self.parseStringLiteral(arena, token)) ?? { + self.putBackToken(token); + if (opt_ctx != OptionalCtx.Optional) { + return self.parseError(token, "expected primary expression, found {}", @tagName(token.id)); } - self.putBackToken(token); - fn_proto.return_type = ast.NodeFnProto.ReturnType { .Explicit = undefined }; - stack.append(State { - .TypeExprBegin = DestPtr {.Field = &fn_proto.return_type.Explicit}, - }) catch unreachable; continue; - }, + } + ); + }, + State.Identifier => |opt_ctx| { + if (self.eatToken(Token.Id.Identifier)) |ident_token| { + _ = try self.createToCtxLiteral(arena, opt_ctx, ast.Node.Identifier, ident_token); + continue; + } + + if (opt_ctx != OptionalCtx.Optional) { + const token = self.getNextToken(); + return self.parseError(token, "expected identifier, found {}", @tagName(token.id)); } }, - State.ParamDecl => |fn_proto| { - if (self.eatToken(Token.Id.RParen)) |_| { - continue; - } - const param_decl = try self.createAttachNode(arena, &fn_proto.params, ast.NodeParamDecl, - ast.NodeParamDecl { - .base = undefined, - .comptime_token = null, - .noalias_token = null, - .name_token = null, - .type_node = undefined, - .var_args_token = null, - }, - ); - if (self.eatToken(Token.Id.Keyword_comptime)) |comptime_token| { - param_decl.comptime_token = comptime_token; - } else if (self.eatToken(Token.Id.Keyword_noalias)) |noalias_token| { - param_decl.noalias_token = noalias_token; - } - if (self.eatToken(Token.Id.Identifier)) |identifier| { - if (self.eatToken(Token.Id.Colon)) |_| { - param_decl.name_token = identifier; - } else { - self.putBackToken(identifier); - } - } - if (self.eatToken(Token.Id.Ellipsis3)) |ellipsis3| { - param_decl.var_args_token = ellipsis3; - stack.append(State { .ExpectToken = Token.Id.RParen }) catch unreachable; - continue; - } - stack.append(State { .ParamDecl = fn_proto }) catch unreachable; - try stack.append(State.ParamDeclComma); - try stack.append(State { - .TypeExprBegin = DestPtr {.Field = ¶m_decl.type_node} - }); + State.ExpectToken => |token_id| { + _ = try self.expectToken(token_id); continue; }, - - State.ParamDeclComma => { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.RParen => { - _ = stack.pop(); // pop off the ParamDecl - continue; - }, - Token.Id.Comma => continue, - else => { - try self.parseError(&stack, token, "expected ',' or ')', found {}", @tagName(token.id)); - continue; - }, - } + State.ExpectTokenSave => |expect_token_save| { + *expect_token_save.ptr = try self.expectToken(expect_token_save.id); + continue; }, - - State.FnDef => |fn_proto| { - const token = self.getNextToken(); - switch(token.id) { - Token.Id.LBrace => { - const block = try self.createNode(arena, ast.NodeBlock, - ast.NodeBlock { - .base = undefined, - .label = null, - .lbrace = token, - .statements = ArrayList(&ast.Node).init(arena), - .rbrace = undefined, - } - ); - fn_proto.body_node = &block.base; - stack.append(State { .Block = block }) catch unreachable; - continue; - }, - Token.Id.Semicolon => continue, - else => { - try self.parseError(&stack, token, "expected ';' or '{{', found {}", @tagName(token.id)); - continue; - }, - } - }, - - State.LabeledExpression => |ctx| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.LBrace => { - const block = try self.createToDestNode(arena, ctx.dest_ptr, ast.NodeBlock, - ast.NodeBlock { - .base = undefined, - .label = ctx.label, - .lbrace = token, - .statements = ArrayList(&ast.Node).init(arena), - .rbrace = undefined, - } - ); - stack.append(State { .Block = block }) catch unreachable; - continue; - }, - Token.Id.Keyword_while => { - stack.append(State { - .While = LoopCtx { - .label = ctx.label, - .inline_token = null, - .loop_token = token, - .dest_ptr = ctx.dest_ptr, - } - }) catch unreachable; - continue; - }, - Token.Id.Keyword_for => { - stack.append(State { - .For = LoopCtx { - .label = ctx.label, - .inline_token = null, - .loop_token = token, - .dest_ptr = ctx.dest_ptr, - } - }) catch unreachable; - continue; - }, - Token.Id.Keyword_inline => { - stack.append(State { - .Inline = InlineCtx { - .label = ctx.label, - .inline_token = token, - .dest_ptr = ctx.dest_ptr, - } - }) catch unreachable; - continue; - }, - else => { - try self.parseError(&stack, token, "expected 'while', 'for', 'inline' or '{{', found {}", @tagName(token.id)); - continue; - }, - } - }, - - State.Inline => |ctx| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.Keyword_while => { - stack.append(State { - .While = LoopCtx { - .inline_token = ctx.inline_token, - .label = ctx.label, - .loop_token = token, - .dest_ptr = ctx.dest_ptr, - } - }) catch unreachable; - continue; - }, - Token.Id.Keyword_for => { - stack.append(State { - .For = LoopCtx { - .inline_token = ctx.inline_token, - .label = ctx.label, - .loop_token = token, - .dest_ptr = ctx.dest_ptr, - } - }) catch unreachable; - continue; - }, - else => { - try self.parseError(&stack, token, "expected 'while' or 'for', found {}", @tagName(token.id)); - continue; - }, - } - }, - - State.While => |ctx| { - const node = try self.createToDestNode(arena, ctx.dest_ptr, ast.NodeWhile, - ast.NodeWhile { - .base = undefined, - .label = ctx.label, - .inline_token = ctx.inline_token, - .while_token = ctx.loop_token, - .condition = undefined, - .payload = null, - .continue_expr = null, - .body = undefined, - .@"else" = null, - } - ); - stack.append(State { .Else = &node.@"else" }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.body } }); - try stack.append(State { .WhileContinueExpr = &node.continue_expr }); - try stack.append(State { .PointerPayload = &node.payload }); - try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr { .Field = &node.condition } }); - try stack.append(State { .ExpectToken = Token.Id.LParen }); - }, - - State.For => |ctx| { - const node = try self.createToDestNode(arena, ctx.dest_ptr, ast.NodeFor, - ast.NodeFor { - .base = undefined, - .label = ctx.label, - .inline_token = ctx.inline_token, - .for_token = ctx.loop_token, - .array_expr = undefined, - .payload = null, - .body = undefined, - .@"else" = null, - } - ); - stack.append(State { .Else = &node.@"else" }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.body } }); - try stack.append(State { .PointerIndexPayload = &node.payload }); - try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr { .Field = &node.array_expr } }); - try stack.append(State { .ExpectToken = Token.Id.LParen }); - }, - - State.Block => |block| { - const token = self.getNextToken(); - switch (token.id) { - Token.Id.RBrace => { - block.rbrace = token; - continue; - }, - else => { - self.putBackToken(token); - stack.append(State { .Block = block }) catch unreachable; - try stack.append(State { .Statement = block }); - continue; - }, - } - }, - - State.Statement => |block| { - const next = self.getNextToken(); - switch (next.id) { - Token.Id.Keyword_comptime => { - const mut_token = self.getNextToken(); - if (mut_token.id == Token.Id.Keyword_var or mut_token.id == Token.Id.Keyword_const) { - const var_decl = try self.createAttachNode(arena, &block.statements, ast.NodeVarDecl, - ast.NodeVarDecl { - .base = undefined, - .visib_token = null, - .mut_token = mut_token, - .comptime_token = next, - .extern_export_token = null, - .type_node = null, - .align_node = null, - .init_node = null, - .lib_name = null, - // initialized later - .name_token = undefined, - .eq_token = undefined, - .semicolon_token = undefined, - } - ); - stack.append(State { .VarDecl = var_decl }) catch unreachable; - continue; - } else { - self.putBackToken(mut_token); - self.putBackToken(next); - const statememt = try block.statements.addOne(); - stack.append(State { .Semicolon = statememt }) catch unreachable; - try stack.append(State { .Expression = DestPtr{.Field = statememt } }); - } - }, - Token.Id.Keyword_var, Token.Id.Keyword_const => { - const var_decl = try self.createAttachNode(arena, &block.statements, ast.NodeVarDecl, - ast.NodeVarDecl { - .base = undefined, - .visib_token = null, - .mut_token = next, - .comptime_token = null, - .extern_export_token = null, - .type_node = null, - .align_node = null, - .init_node = null, - .lib_name = null, - // initialized later - .name_token = undefined, - .eq_token = undefined, - .semicolon_token = undefined, - } - ); - stack.append(State { .VarDecl = var_decl }) catch unreachable; - continue; - }, - Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => { - const node = try self.createAttachNode(arena, &block.statements, ast.NodeDefer, - ast.NodeDefer { - .base = undefined, - .defer_token = next, - .kind = switch (next.id) { - Token.Id.Keyword_defer => ast.NodeDefer.Kind.Unconditional, - Token.Id.Keyword_errdefer => ast.NodeDefer.Kind.Error, - else => unreachable, - }, - .expr = undefined, - } - ); - stack.append(State { .Semicolon = &node.base }) catch unreachable; - try stack.append(State { .AssignmentExpressionBegin = DestPtr{.Field = &node.expr } }); - continue; - }, - Token.Id.LBrace => { - const inner_block = try self.createAttachNode(arena, &block.statements, ast.NodeBlock, - ast.NodeBlock { - .base = undefined, - .label = null, - .lbrace = next, - .statements = ArrayList(&ast.Node).init(arena), - .rbrace = undefined, - } - ); - stack.append(State { .Block = inner_block }) catch unreachable; - continue; - }, - else => { - self.putBackToken(next); - const statememt = try block.statements.addOne(); - stack.append(State { .Semicolon = statememt }) catch unreachable; - try stack.append(State { .AssignmentExpressionBegin = DestPtr{.Field = statememt } }); - continue; - } + State.IfToken => |token_id| { + if (self.eatToken(token_id)) |_| { + continue; } + _ = stack.pop(); + continue; }, - - State.Semicolon => |node_ptr| { - const node = *node_ptr; - if (requireSemiColon(node)) { - _ = (try self.expectToken(&stack, Token.Id.Semicolon)) ?? continue; + State.IfTokenSave => |if_token_save| { + if (self.eatToken(if_token_save.id)) |token| { + *if_token_save.ptr = token; + continue; } - } + + _ = stack.pop(); + continue; + }, + State.OptionalTokenSave => |optional_token_save| { + if (self.eatToken(optional_token_save.id)) |token| { + *optional_token_save.ptr = token; + continue; + } + + continue; + }, } } } + fn eatComments(self: &Parser, arena: &mem.Allocator) !?&ast.Node.LineComment { + var result: ?&ast.Node.LineComment = null; + while (true) { + if (self.eatToken(Token.Id.LineComment)) |line_comment| { + const node = blk: { + if (result) |comment_node| { + break :blk comment_node; + } else { + const comment_node = try arena.create(ast.Node.LineComment); + *comment_node = ast.Node.LineComment { + .base = ast.Node { + .id = ast.Node.Id.LineComment, + }, + .lines = ArrayList(Token).init(arena), + }; + result = comment_node; + break :blk comment_node; + } + }; + try node.lines.append(line_comment); + continue; + } + break; + } + return result; + } + fn requireSemiColon(node: &const ast.Node) bool { var n = node; while (true) { @@ -2726,7 +2786,7 @@ pub const Parser = struct { ast.Node.Id.LineComment, ast.Node.Id.TestDecl => return false, ast.Node.Id.While => { - const while_node = @fieldParentPtr(ast.NodeWhile, "base", n); + const while_node = @fieldParentPtr(ast.Node.While, "base", n); if (while_node.@"else") |@"else"| { n = @"else".base; continue; @@ -2735,7 +2795,7 @@ pub const Parser = struct { return while_node.body.id != ast.Node.Id.Block; }, ast.Node.Id.For => { - const for_node = @fieldParentPtr(ast.NodeFor, "base", n); + const for_node = @fieldParentPtr(ast.Node.For, "base", n); if (for_node.@"else") |@"else"| { n = @"else".base; continue; @@ -2744,7 +2804,7 @@ pub const Parser = struct { return for_node.body.id != ast.Node.Id.Block; }, ast.Node.Id.If => { - const if_node = @fieldParentPtr(ast.NodeIf, "base", n); + const if_node = @fieldParentPtr(ast.Node.If, "base", n); if (if_node.@"else") |@"else"| { n = @"else".base; continue; @@ -2753,20 +2813,20 @@ pub const Parser = struct { return if_node.body.id != ast.Node.Id.Block; }, ast.Node.Id.Else => { - const else_node = @fieldParentPtr(ast.NodeElse, "base", n); + const else_node = @fieldParentPtr(ast.Node.Else, "base", n); n = else_node.body; continue; }, ast.Node.Id.Defer => { - const defer_node = @fieldParentPtr(ast.NodeDefer, "base", n); + const defer_node = @fieldParentPtr(ast.Node.Defer, "base", n); return defer_node.expr.id != ast.Node.Id.Block; }, ast.Node.Id.Comptime => { - const comptime_node = @fieldParentPtr(ast.NodeComptime, "base", n); + const comptime_node = @fieldParentPtr(ast.Node.Comptime, "base", n); return comptime_node.expr.id != ast.Node.Id.Block; }, ast.Node.Id.Suspend => { - const suspend_node = @fieldParentPtr(ast.NodeSuspend, "base", n); + const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", n); if (suspend_node.body) |body| { return body.id != ast.Node.Id.Block; } @@ -2781,11 +2841,11 @@ pub const Parser = struct { fn parseStringLiteral(self: &Parser, arena: &mem.Allocator, token: &const Token) !?&ast.Node { switch (token.id) { Token.Id.StringLiteral => { - return &(try self.createLiteral(arena, ast.NodeStringLiteral, token)).base; + return &(try self.createLiteral(arena, ast.Node.StringLiteral, token)).base; }, Token.Id.MultilineStringLiteralLine => { - const node = try self.createNode(arena, ast.NodeMultilineStringLiteral, - ast.NodeMultilineStringLiteral { + const node = try self.createNode(arena, ast.Node.MultilineStringLiteral, + ast.Node.MultilineStringLiteral { .base = undefined, .tokens = ArrayList(Token).init(arena), } @@ -2809,11 +2869,11 @@ pub const Parser = struct { } } - fn parseBlockExpr(self: &Parser, stack: &ArrayList(State), arena: &mem.Allocator, dest_ptr: &const DestPtr, token: &const Token) !bool { + fn parseBlockExpr(self: &Parser, stack: &ArrayList(State), arena: &mem.Allocator, ctx: &const OptionalCtx, token: &const Token) !bool { switch (token.id) { Token.Id.Keyword_suspend => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSuspend, - ast.NodeSuspend { + const node = try self.createToCtxNode(arena, ctx, ast.Node.Suspend, + ast.Node.Suspend { .base = undefined, .suspend_token = *token, .payload = null, @@ -2822,12 +2882,12 @@ pub const Parser = struct { ); stack.append(State { .SuspendBody = node }) catch unreachable; - try stack.append(State { .Payload = &node.payload }); + try stack.append(State { .Payload = OptionalCtx { .Optional = &node.payload } }); return true; }, Token.Id.Keyword_if => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeIf, - ast.NodeIf { + const node = try self.createToCtxNode(arena, ctx, ast.Node.If, + ast.Node.If { .base = undefined, .if_token = *token, .condition = undefined, @@ -2838,10 +2898,10 @@ pub const Parser = struct { ); stack.append(State { .Else = &node.@"else" }) catch unreachable; - try stack.append(State { .Expression = DestPtr { .Field = &node.body } }); - try stack.append(State { .PointerPayload = &node.payload }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.body } }); + try stack.append(State { .PointerPayload = OptionalCtx { .Optional = &node.payload } }); try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr { .Field = &node.condition } }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.condition } }); try stack.append(State { .ExpectToken = Token.Id.LParen }); return true; }, @@ -2851,7 +2911,7 @@ pub const Parser = struct { .label = null, .inline_token = null, .loop_token = *token, - .dest_ptr = *dest_ptr, + .opt_ctx = *ctx, } }) catch unreachable; return true; @@ -2862,48 +2922,48 @@ pub const Parser = struct { .label = null, .inline_token = null, .loop_token = *token, - .dest_ptr = *dest_ptr, + .opt_ctx = *ctx, } }) catch unreachable; return true; }, Token.Id.Keyword_switch => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeSwitch, - ast.NodeSwitch { + const node = try self.createToCtxNode(arena, ctx, ast.Node.Switch, + ast.Node.Switch { .base = undefined, .switch_token = *token, .expr = undefined, - .cases = ArrayList(&ast.NodeSwitchCase).init(arena), + .cases = ArrayList(&ast.Node.SwitchCase).init(arena), .rbrace = undefined, } ); stack.append(State { - .SwitchCaseOrEnd = ListSave(&ast.NodeSwitchCase) { + .SwitchCaseOrEnd = ListSave(&ast.Node.SwitchCase) { .list = &node.cases, .ptr = &node.rbrace, }, }) catch unreachable; try stack.append(State { .ExpectToken = Token.Id.LBrace }); try stack.append(State { .ExpectToken = Token.Id.RParen }); - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); try stack.append(State { .ExpectToken = Token.Id.LParen }); return true; }, Token.Id.Keyword_comptime => { - const node = try self.createToDestNode(arena, dest_ptr, ast.NodeComptime, - ast.NodeComptime { + const node = try self.createToCtxNode(arena, ctx, ast.Node.Comptime, + ast.Node.Comptime { .base = undefined, .comptime_token = *token, .expr = undefined, } ); - try stack.append(State { .Expression = DestPtr { .Field = &node.expr } }); + try stack.append(State { .Expression = OptionalCtx { .Required = &node.expr } }); return true; }, Token.Id.LBrace => { - const block = try self.createToDestNode(arena, dest_ptr, ast.NodeBlock, - ast.NodeBlock { + const block = try self.createToCtxNode(arena, ctx, ast.Node.Block, + ast.Node.Block { .base = undefined, .label = null, .lbrace = *token, @@ -2920,100 +2980,102 @@ pub const Parser = struct { } } - fn commaOrEnd(self: &Parser, stack: &ArrayList(State), end: &const Token.Id, maybe_ptr: ?&Token, state_after_comma: &const State) !void { + fn expectCommaOrEnd(self: &Parser, end: @TagType(Token.Id)) !?Token { var token = self.getNextToken(); switch (token.id) { - Token.Id.Comma => { - stack.append(state_after_comma) catch unreachable; - }, + Token.Id.Comma => return null, else => { - const IdTag = @TagType(Token.Id); - if (IdTag(*end) == token.id) { - if (maybe_ptr) |ptr| { - *ptr = token; - } - return; + if (end == token.id) { + return token; } - try self.parseError(stack, token, "expected ',' or {}, found {}", @tagName(*end), @tagName(token.id)); + return self.parseError(token, "expected ',' or {}, found {}", @tagName(end), @tagName(token.id)); }, } } - fn tokenIdToAssignment(id: &const Token.Id) ?ast.NodeInfixOp.InfixOp { + fn tokenIdToAssignment(id: &const Token.Id) ?ast.Node.InfixOp.Op { // TODO: We have to cast all cases because of this: // error: expected type '?InfixOp', found '?@TagType(InfixOp)' return switch (*id) { - Token.Id.AmpersandEqual => ast.NodeInfixOp.InfixOp { .AssignBitAnd = void{} }, - Token.Id.AngleBracketAngleBracketLeftEqual => ast.NodeInfixOp.InfixOp { .AssignBitShiftLeft = void{} }, - Token.Id.AngleBracketAngleBracketRightEqual => ast.NodeInfixOp.InfixOp { .AssignBitShiftRight = void{} }, - Token.Id.AsteriskEqual => ast.NodeInfixOp.InfixOp { .AssignTimes = void{} }, - Token.Id.AsteriskPercentEqual => ast.NodeInfixOp.InfixOp { .AssignTimesWarp = void{} }, - Token.Id.CaretEqual => ast.NodeInfixOp.InfixOp { .AssignBitXor = void{} }, - Token.Id.Equal => ast.NodeInfixOp.InfixOp { .Assign = void{} }, - Token.Id.MinusEqual => ast.NodeInfixOp.InfixOp { .AssignMinus = void{} }, - Token.Id.MinusPercentEqual => ast.NodeInfixOp.InfixOp { .AssignMinusWrap = void{} }, - Token.Id.PercentEqual => ast.NodeInfixOp.InfixOp { .AssignMod = void{} }, - Token.Id.PipeEqual => ast.NodeInfixOp.InfixOp { .AssignBitOr = void{} }, - Token.Id.PlusEqual => ast.NodeInfixOp.InfixOp { .AssignPlus = void{} }, - Token.Id.PlusPercentEqual => ast.NodeInfixOp.InfixOp { .AssignPlusWrap = void{} }, - Token.Id.SlashEqual => ast.NodeInfixOp.InfixOp { .AssignDiv = void{} }, + Token.Id.AmpersandEqual => ast.Node.InfixOp.Op { .AssignBitAnd = void{} }, + Token.Id.AngleBracketAngleBracketLeftEqual => ast.Node.InfixOp.Op { .AssignBitShiftLeft = void{} }, + Token.Id.AngleBracketAngleBracketRightEqual => ast.Node.InfixOp.Op { .AssignBitShiftRight = void{} }, + Token.Id.AsteriskEqual => ast.Node.InfixOp.Op { .AssignTimes = void{} }, + Token.Id.AsteriskPercentEqual => ast.Node.InfixOp.Op { .AssignTimesWarp = void{} }, + Token.Id.CaretEqual => ast.Node.InfixOp.Op { .AssignBitXor = void{} }, + Token.Id.Equal => ast.Node.InfixOp.Op { .Assign = void{} }, + Token.Id.MinusEqual => ast.Node.InfixOp.Op { .AssignMinus = void{} }, + Token.Id.MinusPercentEqual => ast.Node.InfixOp.Op { .AssignMinusWrap = void{} }, + Token.Id.PercentEqual => ast.Node.InfixOp.Op { .AssignMod = void{} }, + Token.Id.PipeEqual => ast.Node.InfixOp.Op { .AssignBitOr = void{} }, + Token.Id.PlusEqual => ast.Node.InfixOp.Op { .AssignPlus = void{} }, + Token.Id.PlusPercentEqual => ast.Node.InfixOp.Op { .AssignPlusWrap = void{} }, + Token.Id.SlashEqual => ast.Node.InfixOp.Op { .AssignDiv = void{} }, else => null, }; } - fn tokenIdToComparison(id: &const Token.Id) ?ast.NodeInfixOp.InfixOp { - return switch (*id) { - Token.Id.BangEqual => ast.NodeInfixOp.InfixOp { .BangEqual = void{} }, - Token.Id.EqualEqual => ast.NodeInfixOp.InfixOp { .EqualEqual = void{} }, - Token.Id.AngleBracketLeft => ast.NodeInfixOp.InfixOp { .LessThan = void{} }, - Token.Id.AngleBracketLeftEqual => ast.NodeInfixOp.InfixOp { .LessOrEqual = void{} }, - Token.Id.AngleBracketRight => ast.NodeInfixOp.InfixOp { .GreaterThan = void{} }, - Token.Id.AngleBracketRightEqual => ast.NodeInfixOp.InfixOp { .GreaterOrEqual = void{} }, + fn tokenIdToUnwrapExpr(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op { + return switch (id) { + Token.Id.Keyword_catch => ast.Node.InfixOp.Op { .Catch = null }, + Token.Id.QuestionMarkQuestionMark => ast.Node.InfixOp.Op { .UnwrapMaybe = void{} }, else => null, }; } - fn tokenIdToBitShift(id: &const Token.Id) ?ast.NodeInfixOp.InfixOp { - return switch (*id) { - Token.Id.AngleBracketAngleBracketLeft => ast.NodeInfixOp.InfixOp { .BitShiftLeft = void{} }, - Token.Id.AngleBracketAngleBracketRight => ast.NodeInfixOp.InfixOp { .BitShiftRight = void{} }, + fn tokenIdToComparison(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op { + return switch (id) { + Token.Id.BangEqual => ast.Node.InfixOp.Op { .BangEqual = void{} }, + Token.Id.EqualEqual => ast.Node.InfixOp.Op { .EqualEqual = void{} }, + Token.Id.AngleBracketLeft => ast.Node.InfixOp.Op { .LessThan = void{} }, + Token.Id.AngleBracketLeftEqual => ast.Node.InfixOp.Op { .LessOrEqual = void{} }, + Token.Id.AngleBracketRight => ast.Node.InfixOp.Op { .GreaterThan = void{} }, + Token.Id.AngleBracketRightEqual => ast.Node.InfixOp.Op { .GreaterOrEqual = void{} }, else => null, }; } - fn tokenIdToAddition(id: &const Token.Id) ?ast.NodeInfixOp.InfixOp { - return switch (*id) { - Token.Id.Minus => ast.NodeInfixOp.InfixOp { .Sub = void{} }, - Token.Id.MinusPercent => ast.NodeInfixOp.InfixOp { .SubWrap = void{} }, - Token.Id.Plus => ast.NodeInfixOp.InfixOp { .Add = void{} }, - Token.Id.PlusPercent => ast.NodeInfixOp.InfixOp { .AddWrap = void{} }, - Token.Id.PlusPlus => ast.NodeInfixOp.InfixOp { .ArrayCat = void{} }, + fn tokenIdToBitShift(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op { + return switch (id) { + Token.Id.AngleBracketAngleBracketLeft => ast.Node.InfixOp.Op { .BitShiftLeft = void{} }, + Token.Id.AngleBracketAngleBracketRight => ast.Node.InfixOp.Op { .BitShiftRight = void{} }, else => null, }; } - fn tokenIdToMultiply(id: &const Token.Id) ?ast.NodeInfixOp.InfixOp { - return switch (*id) { - Token.Id.Slash => ast.NodeInfixOp.InfixOp { .Div = void{} }, - Token.Id.Asterisk => ast.NodeInfixOp.InfixOp { .Mult = void{} }, - Token.Id.AsteriskAsterisk => ast.NodeInfixOp.InfixOp { .ArrayMult = void{} }, - Token.Id.AsteriskPercent => ast.NodeInfixOp.InfixOp { .MultWrap = void{} }, - Token.Id.Percent => ast.NodeInfixOp.InfixOp { .Mod = void{} }, - Token.Id.PipePipe => ast.NodeInfixOp.InfixOp { .MergeErrorSets = void{} }, + fn tokenIdToAddition(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op { + return switch (id) { + Token.Id.Minus => ast.Node.InfixOp.Op { .Sub = void{} }, + Token.Id.MinusPercent => ast.Node.InfixOp.Op { .SubWrap = void{} }, + Token.Id.Plus => ast.Node.InfixOp.Op { .Add = void{} }, + Token.Id.PlusPercent => ast.Node.InfixOp.Op { .AddWrap = void{} }, + Token.Id.PlusPlus => ast.Node.InfixOp.Op { .ArrayCat = void{} }, else => null, }; } - fn tokenIdToPrefixOp(id: &const Token.Id) ?ast.NodePrefixOp.PrefixOp { - return switch (*id) { - Token.Id.Bang => ast.NodePrefixOp.PrefixOp { .BoolNot = void{} }, - Token.Id.Tilde => ast.NodePrefixOp.PrefixOp { .BitNot = void{} }, - Token.Id.Minus => ast.NodePrefixOp.PrefixOp { .Negation = void{} }, - Token.Id.MinusPercent => ast.NodePrefixOp.PrefixOp { .NegationWrap = void{} }, - Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.NodePrefixOp.PrefixOp { .Deref = void{} }, - Token.Id.Ampersand => ast.NodePrefixOp.PrefixOp { - .AddrOf = ast.NodePrefixOp.AddrOfInfo { + fn tokenIdToMultiply(id: @TagType(Token.Id)) ?ast.Node.InfixOp.Op { + return switch (id) { + Token.Id.Slash => ast.Node.InfixOp.Op { .Div = void{} }, + Token.Id.Asterisk => ast.Node.InfixOp.Op { .Mult = void{} }, + Token.Id.AsteriskAsterisk => ast.Node.InfixOp.Op { .ArrayMult = void{} }, + Token.Id.AsteriskPercent => ast.Node.InfixOp.Op { .MultWrap = void{} }, + Token.Id.Percent => ast.Node.InfixOp.Op { .Mod = void{} }, + Token.Id.PipePipe => ast.Node.InfixOp.Op { .MergeErrorSets = void{} }, + else => null, + }; + } + + fn tokenIdToPrefixOp(id: @TagType(Token.Id)) ?ast.Node.PrefixOp.Op { + return switch (id) { + Token.Id.Bang => ast.Node.PrefixOp.Op { .BoolNot = void{} }, + Token.Id.Tilde => ast.Node.PrefixOp.Op { .BitNot = void{} }, + Token.Id.Minus => ast.Node.PrefixOp.Op { .Negation = void{} }, + Token.Id.MinusPercent => ast.Node.PrefixOp.Op { .NegationWrap = void{} }, + Token.Id.Asterisk, Token.Id.AsteriskAsterisk => ast.Node.PrefixOp.Op { .Deref = void{} }, + Token.Id.Ampersand => ast.Node.PrefixOp.Op { + .AddrOf = ast.Node.PrefixOp.AddrOfInfo { .align_expr = null, .bit_offset_start_token = null, .bit_offset_end_token = null, @@ -3021,10 +3083,10 @@ pub const Parser = struct { .volatile_token = null, }, }, - Token.Id.QuestionMark => ast.NodePrefixOp.PrefixOp { .MaybeType = void{} }, - Token.Id.QuestionMarkQuestionMark => ast.NodePrefixOp.PrefixOp { .UnwrapMaybe = void{} }, - Token.Id.Keyword_await => ast.NodePrefixOp.PrefixOp { .Await = void{} }, - Token.Id.Keyword_try => ast.NodePrefixOp.PrefixOp { .Try = void{ } }, + Token.Id.QuestionMark => ast.Node.PrefixOp.Op { .MaybeType = void{} }, + Token.Id.QuestionMarkQuestionMark => ast.Node.PrefixOp.Op { .UnwrapMaybe = void{} }, + Token.Id.Keyword_await => ast.Node.PrefixOp.Op { .Await = void{} }, + Token.Id.Keyword_try => ast.Node.PrefixOp.Op { .Try = void{ } }, else => null, }; } @@ -3034,11 +3096,7 @@ pub const Parser = struct { *node = *init_to; node.base = blk: { const id = ast.Node.typeToId(T); - if (self.pending_line_comment_node) |comment_node| { - self.pending_line_comment_node = null; - break :blk ast.Node {.id = id, .comment = comment_node}; - } - break :blk ast.Node {.id = id, .comment = null }; + break :blk ast.Node {.id = id}; }; return node; @@ -3051,9 +3109,9 @@ pub const Parser = struct { return node; } - fn createToDestNode(self: &Parser, arena: &mem.Allocator, dest_ptr: &const DestPtr, comptime T: type, init_to: &const T) !&T { + fn createToCtxNode(self: &Parser, arena: &mem.Allocator, opt_ctx: &const OptionalCtx, comptime T: type, init_to: &const T) !&T { const node = try self.createNode(arena, T, init_to); - dest_ptr.store(&node.base); + opt_ctx.store(&node.base); return node; } @@ -3067,51 +3125,38 @@ pub const Parser = struct { ); } - fn parseError(self: &Parser, stack: &ArrayList(State), token: &const Token, comptime fmt: []const u8, args: ...) !void { - // Before reporting an error. We pop the stack to see if our state was optional - self.revertIfOptional(stack) catch { - const loc = self.tokenizer.getTokenLocation(0, token); - warn("{}:{}:{}: error: " ++ fmt ++ "\n", self.source_file_name, loc.line + 1, loc.column + 1, args); - warn("{}\n", self.tokenizer.buffer[loc.line_start..loc.line_end]); - { - var i: usize = 0; - while (i < loc.column) : (i += 1) { - warn(" "); - } - } - { - const caret_count = token.end - token.start; - var i: usize = 0; - while (i < caret_count) : (i += 1) { - warn("~"); - } - } - warn("\n"); - return error.ParseError; - }; + fn createToCtxLiteral(self: &Parser, arena: &mem.Allocator, opt_ctx: &const OptionalCtx, comptime T: type, token: &const Token) !&T { + const node = try self.createLiteral(arena, T, token); + opt_ctx.store(&node.base); + + return node; } - fn revertIfOptional(self: &Parser, stack: &ArrayList(State)) !void { - while (stack.popOrNull()) |state| { - switch (state) { - State.Optional => |revert| { - *self = revert.parser; - *self.tokenizer = revert.tokenizer; - *revert.ptr = null; - return; - }, - else => { } + fn parseError(self: &Parser, token: &const Token, comptime fmt: []const u8, args: ...) (error{ParseError}) { + const loc = self.tokenizer.getTokenLocation(0, token); + warn("{}:{}:{}: error: " ++ fmt ++ "\n", self.source_file_name, loc.line + 1, loc.column + 1, args); + warn("{}\n", self.tokenizer.buffer[loc.line_start..loc.line_end]); + { + var i: usize = 0; + while (i < loc.column) : (i += 1) { + warn(" "); } } - - return error.NoOptionalStateFound; + { + const caret_count = token.end - token.start; + var i: usize = 0; + while (i < caret_count) : (i += 1) { + warn("~"); + } + } + warn("\n"); + return error.ParseError; } - fn expectToken(self: &Parser, stack: &ArrayList(State), id: @TagType(Token.Id)) !?Token { + fn expectToken(self: &Parser, id: @TagType(Token.Id)) !Token { const token = self.getNextToken(); if (token.id != id) { - try self.parseError(stack, token, "expected {}, found {}", @tagName(id), @tagName(token.id)); - return null; + return self.parseError(token, "expected {}, found {}", @tagName(id), @tagName(token.id)); } return token; } @@ -3150,7 +3195,7 @@ pub const Parser = struct { indent: usize, }; - pub fn renderAst(self: &Parser, stream: var, root_node: &ast.NodeRoot) !void { + pub fn renderAst(self: &Parser, stream: var, root_node: &ast.Node.Root) !void { var stack = self.initUtilityArrayList(RenderAstFrame); defer self.deinitUtilityArrayList(stack); @@ -3179,18 +3224,17 @@ pub const Parser = struct { const RenderState = union(enum) { TopLevelDecl: &ast.Node, - FnProtoRParen: &ast.NodeFnProto, ParamDecl: &ast.Node, Text: []const u8, Expression: &ast.Node, - VarDecl: &ast.NodeVarDecl, + VarDecl: &ast.Node.VarDecl, Statement: &ast.Node, - FieldInitializer: &ast.NodeFieldInitializer, + FieldInitializer: &ast.Node.FieldInitializer, PrintIndent, Indent: usize, }; - pub fn renderSource(self: &Parser, stream: var, root_node: &ast.NodeRoot) !void { + pub fn renderSource(self: &Parser, stream: var, root_node: &ast.Node.Root) !void { var stack = self.initUtilityArrayList(RenderState); defer self.deinitUtilityArrayList(stack); @@ -3224,7 +3268,8 @@ pub const Parser = struct { RenderState.TopLevelDecl => |decl| { switch (decl.id) { ast.Node.Id.FnProto => { - const fn_proto = @fieldParentPtr(ast.NodeFnProto, "base", decl); + const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl); + try self.renderComments(stream, fn_proto, indent); if (fn_proto.body_node) |body_node| { stack.append(RenderState { .Expression = body_node}) catch unreachable; @@ -3236,7 +3281,7 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = decl }); }, ast.Node.Id.Use => { - const use_decl = @fieldParentPtr(ast.NodeUse, "base", decl); + const use_decl = @fieldParentPtr(ast.Node.Use, "base", decl); if (use_decl.visib_token) |visib_token| { try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); } @@ -3245,18 +3290,19 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = use_decl.expr }); }, ast.Node.Id.VarDecl => { - const var_decl = @fieldParentPtr(ast.NodeVarDecl, "base", decl); + const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", decl); try stack.append(RenderState { .VarDecl = var_decl}); }, ast.Node.Id.TestDecl => { - const test_decl = @fieldParentPtr(ast.NodeTestDecl, "base", decl); + const test_decl = @fieldParentPtr(ast.Node.TestDecl, "base", decl); + try self.renderComments(stream, test_decl, indent); try stream.print("test "); try stack.append(RenderState { .Expression = test_decl.body_node }); try stack.append(RenderState { .Text = " " }); try stack.append(RenderState { .Expression = test_decl.name }); }, ast.Node.Id.StructField => { - const field = @fieldParentPtr(ast.NodeStructField, "base", decl); + const field = @fieldParentPtr(ast.Node.StructField, "base", decl); if (field.visib_token) |visib_token| { try stream.print("{} ", self.tokenizer.getTokenSlice(visib_token)); } @@ -3264,7 +3310,7 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = field.type_expr}); }, ast.Node.Id.UnionTag => { - const tag = @fieldParentPtr(ast.NodeUnionTag, "base", decl); + const tag = @fieldParentPtr(ast.Node.UnionTag, "base", decl); try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); if (tag.type_expr) |type_expr| { @@ -3273,7 +3319,7 @@ pub const Parser = struct { } }, ast.Node.Id.EnumTag => { - const tag = @fieldParentPtr(ast.NodeEnumTag, "base", decl); + const tag = @fieldParentPtr(ast.Node.EnumTag, "base", decl); try stream.print("{}", self.tokenizer.getTokenSlice(tag.name_token)); if (tag.value) |value| { @@ -3292,6 +3338,7 @@ pub const Parser = struct { }, RenderState.FieldInitializer => |field_init| { + //TODO try self.renderComments(stream, field_init, indent); try stream.print(".{}", self.tokenizer.getTokenSlice(field_init.name_token)); try stream.print(" = "); try stack.append(RenderState { .Expression = field_init.expr }); @@ -3337,7 +3384,8 @@ pub const Parser = struct { }, RenderState.ParamDecl => |base| { - const param_decl = @fieldParentPtr(ast.NodeParamDecl, "base", base); + const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", base); + // TODO try self.renderComments(stream, param_decl, indent); if (param_decl.comptime_token) |comptime_token| { try stream.print("{} ", self.tokenizer.getTokenSlice(comptime_token)); } @@ -3358,11 +3406,11 @@ pub const Parser = struct { }, RenderState.Expression => |base| switch (base.id) { ast.Node.Id.Identifier => { - const identifier = @fieldParentPtr(ast.NodeIdentifier, "base", base); + const identifier = @fieldParentPtr(ast.Node.Identifier, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(identifier.token)); }, ast.Node.Id.Block => { - const block = @fieldParentPtr(ast.NodeBlock, "base", base); + const block = @fieldParentPtr(ast.Node.Block, "base", base); if (block.label) |label| { try stream.print("{}: ", self.tokenizer.getTokenSlice(label)); } @@ -3398,17 +3446,17 @@ pub const Parser = struct { } }, ast.Node.Id.Defer => { - const defer_node = @fieldParentPtr(ast.NodeDefer, "base", base); + const defer_node = @fieldParentPtr(ast.Node.Defer, "base", base); try stream.print("{} ", self.tokenizer.getTokenSlice(defer_node.defer_token)); try stack.append(RenderState { .Expression = defer_node.expr }); }, ast.Node.Id.Comptime => { - const comptime_node = @fieldParentPtr(ast.NodeComptime, "base", base); + const comptime_node = @fieldParentPtr(ast.Node.Comptime, "base", base); try stream.print("{} ", self.tokenizer.getTokenSlice(comptime_node.comptime_token)); try stack.append(RenderState { .Expression = comptime_node.expr }); }, ast.Node.Id.AsyncAttribute => { - const async_attr = @fieldParentPtr(ast.NodeAsyncAttribute, "base", base); + const async_attr = @fieldParentPtr(ast.Node.AsyncAttribute, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(async_attr.async_token)); if (async_attr.allocator_type) |allocator_type| { @@ -3418,7 +3466,7 @@ pub const Parser = struct { } }, ast.Node.Id.Suspend => { - const suspend_node = @fieldParentPtr(ast.NodeSuspend, "base", base); + const suspend_node = @fieldParentPtr(ast.Node.Suspend, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(suspend_node.suspend_token)); if (suspend_node.body) |body| { @@ -3427,65 +3475,65 @@ pub const Parser = struct { } if (suspend_node.payload) |payload| { - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); try stack.append(RenderState { .Text = " " }); } }, ast.Node.Id.InfixOp => { - const prefix_op_node = @fieldParentPtr(ast.NodeInfixOp, "base", base); + const prefix_op_node = @fieldParentPtr(ast.Node.InfixOp, "base", base); try stack.append(RenderState { .Expression = prefix_op_node.rhs }); - if (prefix_op_node.op == ast.NodeInfixOp.InfixOp.Catch) { + if (prefix_op_node.op == ast.Node.InfixOp.Op.Catch) { if (prefix_op_node.op.Catch) |payload| { try stack.append(RenderState { .Text = " " }); - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); } try stack.append(RenderState { .Text = " catch " }); } else { const text = switch (prefix_op_node.op) { - ast.NodeInfixOp.InfixOp.Add => " + ", - ast.NodeInfixOp.InfixOp.AddWrap => " +% ", - ast.NodeInfixOp.InfixOp.ArrayCat => " ++ ", - ast.NodeInfixOp.InfixOp.ArrayMult => " ** ", - ast.NodeInfixOp.InfixOp.Assign => " = ", - ast.NodeInfixOp.InfixOp.AssignBitAnd => " &= ", - ast.NodeInfixOp.InfixOp.AssignBitOr => " |= ", - ast.NodeInfixOp.InfixOp.AssignBitShiftLeft => " <<= ", - ast.NodeInfixOp.InfixOp.AssignBitShiftRight => " >>= ", - ast.NodeInfixOp.InfixOp.AssignBitXor => " ^= ", - ast.NodeInfixOp.InfixOp.AssignDiv => " /= ", - ast.NodeInfixOp.InfixOp.AssignMinus => " -= ", - ast.NodeInfixOp.InfixOp.AssignMinusWrap => " -%= ", - ast.NodeInfixOp.InfixOp.AssignMod => " %= ", - ast.NodeInfixOp.InfixOp.AssignPlus => " += ", - ast.NodeInfixOp.InfixOp.AssignPlusWrap => " +%= ", - ast.NodeInfixOp.InfixOp.AssignTimes => " *= ", - ast.NodeInfixOp.InfixOp.AssignTimesWarp => " *%= ", - ast.NodeInfixOp.InfixOp.BangEqual => " != ", - ast.NodeInfixOp.InfixOp.BitAnd => " & ", - ast.NodeInfixOp.InfixOp.BitOr => " | ", - ast.NodeInfixOp.InfixOp.BitShiftLeft => " << ", - ast.NodeInfixOp.InfixOp.BitShiftRight => " >> ", - ast.NodeInfixOp.InfixOp.BitXor => " ^ ", - ast.NodeInfixOp.InfixOp.BoolAnd => " and ", - ast.NodeInfixOp.InfixOp.BoolOr => " or ", - ast.NodeInfixOp.InfixOp.Div => " / ", - ast.NodeInfixOp.InfixOp.EqualEqual => " == ", - ast.NodeInfixOp.InfixOp.ErrorUnion => "!", - ast.NodeInfixOp.InfixOp.GreaterOrEqual => " >= ", - ast.NodeInfixOp.InfixOp.GreaterThan => " > ", - ast.NodeInfixOp.InfixOp.LessOrEqual => " <= ", - ast.NodeInfixOp.InfixOp.LessThan => " < ", - ast.NodeInfixOp.InfixOp.MergeErrorSets => " || ", - ast.NodeInfixOp.InfixOp.Mod => " % ", - ast.NodeInfixOp.InfixOp.Mult => " * ", - ast.NodeInfixOp.InfixOp.MultWrap => " *% ", - ast.NodeInfixOp.InfixOp.Period => ".", - ast.NodeInfixOp.InfixOp.Sub => " - ", - ast.NodeInfixOp.InfixOp.SubWrap => " -% ", - ast.NodeInfixOp.InfixOp.UnwrapMaybe => " ?? ", - ast.NodeInfixOp.InfixOp.Range => " ... ", - ast.NodeInfixOp.InfixOp.Catch => unreachable, + ast.Node.InfixOp.Op.Add => " + ", + ast.Node.InfixOp.Op.AddWrap => " +% ", + ast.Node.InfixOp.Op.ArrayCat => " ++ ", + ast.Node.InfixOp.Op.ArrayMult => " ** ", + ast.Node.InfixOp.Op.Assign => " = ", + ast.Node.InfixOp.Op.AssignBitAnd => " &= ", + ast.Node.InfixOp.Op.AssignBitOr => " |= ", + ast.Node.InfixOp.Op.AssignBitShiftLeft => " <<= ", + ast.Node.InfixOp.Op.AssignBitShiftRight => " >>= ", + ast.Node.InfixOp.Op.AssignBitXor => " ^= ", + ast.Node.InfixOp.Op.AssignDiv => " /= ", + ast.Node.InfixOp.Op.AssignMinus => " -= ", + ast.Node.InfixOp.Op.AssignMinusWrap => " -%= ", + ast.Node.InfixOp.Op.AssignMod => " %= ", + ast.Node.InfixOp.Op.AssignPlus => " += ", + ast.Node.InfixOp.Op.AssignPlusWrap => " +%= ", + ast.Node.InfixOp.Op.AssignTimes => " *= ", + ast.Node.InfixOp.Op.AssignTimesWarp => " *%= ", + ast.Node.InfixOp.Op.BangEqual => " != ", + ast.Node.InfixOp.Op.BitAnd => " & ", + ast.Node.InfixOp.Op.BitOr => " | ", + ast.Node.InfixOp.Op.BitShiftLeft => " << ", + ast.Node.InfixOp.Op.BitShiftRight => " >> ", + ast.Node.InfixOp.Op.BitXor => " ^ ", + ast.Node.InfixOp.Op.BoolAnd => " and ", + ast.Node.InfixOp.Op.BoolOr => " or ", + ast.Node.InfixOp.Op.Div => " / ", + ast.Node.InfixOp.Op.EqualEqual => " == ", + ast.Node.InfixOp.Op.ErrorUnion => "!", + ast.Node.InfixOp.Op.GreaterOrEqual => " >= ", + ast.Node.InfixOp.Op.GreaterThan => " > ", + ast.Node.InfixOp.Op.LessOrEqual => " <= ", + ast.Node.InfixOp.Op.LessThan => " < ", + ast.Node.InfixOp.Op.MergeErrorSets => " || ", + ast.Node.InfixOp.Op.Mod => " % ", + ast.Node.InfixOp.Op.Mult => " * ", + ast.Node.InfixOp.Op.MultWrap => " *% ", + ast.Node.InfixOp.Op.Period => ".", + ast.Node.InfixOp.Op.Sub => " - ", + ast.Node.InfixOp.Op.SubWrap => " -% ", + ast.Node.InfixOp.Op.UnwrapMaybe => " ?? ", + ast.Node.InfixOp.Op.Range => " ... ", + ast.Node.InfixOp.Op.Catch => unreachable, }; try stack.append(RenderState { .Text = text }); @@ -3493,10 +3541,10 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = prefix_op_node.lhs }); }, ast.Node.Id.PrefixOp => { - const prefix_op_node = @fieldParentPtr(ast.NodePrefixOp, "base", base); + const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base); try stack.append(RenderState { .Expression = prefix_op_node.rhs }); switch (prefix_op_node.op) { - ast.NodePrefixOp.PrefixOp.AddrOf => |addr_of_info| { + ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| { try stream.write("&"); if (addr_of_info.volatile_token != null) { try stack.append(RenderState { .Text = "volatile "}); @@ -3510,7 +3558,7 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = align_expr}); } }, - ast.NodePrefixOp.PrefixOp.SliceType => |addr_of_info| { + ast.Node.PrefixOp.Op.SliceType => |addr_of_info| { try stream.write("[]"); if (addr_of_info.volatile_token != null) { try stack.append(RenderState { .Text = "volatile "}); @@ -3524,29 +3572,29 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = align_expr}); } }, - ast.NodePrefixOp.PrefixOp.ArrayType => |array_index| { + ast.Node.PrefixOp.Op.ArrayType => |array_index| { try stack.append(RenderState { .Text = "]"}); try stack.append(RenderState { .Expression = array_index}); try stack.append(RenderState { .Text = "["}); }, - ast.NodePrefixOp.PrefixOp.BitNot => try stream.write("~"), - ast.NodePrefixOp.PrefixOp.BoolNot => try stream.write("!"), - ast.NodePrefixOp.PrefixOp.Deref => try stream.write("*"), - ast.NodePrefixOp.PrefixOp.Negation => try stream.write("-"), - ast.NodePrefixOp.PrefixOp.NegationWrap => try stream.write("-%"), - ast.NodePrefixOp.PrefixOp.Try => try stream.write("try "), - ast.NodePrefixOp.PrefixOp.UnwrapMaybe => try stream.write("??"), - ast.NodePrefixOp.PrefixOp.MaybeType => try stream.write("?"), - ast.NodePrefixOp.PrefixOp.Await => try stream.write("await "), - ast.NodePrefixOp.PrefixOp.Cancel => try stream.write("cancel "), - ast.NodePrefixOp.PrefixOp.Resume => try stream.write("resume "), + ast.Node.PrefixOp.Op.BitNot => try stream.write("~"), + ast.Node.PrefixOp.Op.BoolNot => try stream.write("!"), + ast.Node.PrefixOp.Op.Deref => try stream.write("*"), + ast.Node.PrefixOp.Op.Negation => try stream.write("-"), + ast.Node.PrefixOp.Op.NegationWrap => try stream.write("-%"), + ast.Node.PrefixOp.Op.Try => try stream.write("try "), + ast.Node.PrefixOp.Op.UnwrapMaybe => try stream.write("??"), + ast.Node.PrefixOp.Op.MaybeType => try stream.write("?"), + ast.Node.PrefixOp.Op.Await => try stream.write("await "), + ast.Node.PrefixOp.Op.Cancel => try stream.write("cancel "), + ast.Node.PrefixOp.Op.Resume => try stream.write("resume "), } }, ast.Node.Id.SuffixOp => { - const suffix_op = @fieldParentPtr(ast.NodeSuffixOp, "base", base); + const suffix_op = @fieldParentPtr(ast.Node.SuffixOp, "base", base); switch (suffix_op.op) { - ast.NodeSuffixOp.SuffixOp.Call => |call_info| { + ast.Node.SuffixOp.Op.Call => |call_info| { try stack.append(RenderState { .Text = ")"}); var i = call_info.params.len; while (i != 0) { @@ -3565,13 +3613,13 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = &async_attr.base }); } }, - ast.NodeSuffixOp.SuffixOp.ArrayAccess => |index_expr| { + ast.Node.SuffixOp.Op.ArrayAccess => |index_expr| { try stack.append(RenderState { .Text = "]"}); try stack.append(RenderState { .Expression = index_expr}); try stack.append(RenderState { .Text = "["}); try stack.append(RenderState { .Expression = suffix_op.lhs }); }, - ast.NodeSuffixOp.SuffixOp.Slice => |range| { + ast.Node.SuffixOp.Op.Slice => |range| { try stack.append(RenderState { .Text = "]"}); if (range.end) |end| { try stack.append(RenderState { .Expression = end}); @@ -3581,162 +3629,177 @@ pub const Parser = struct { try stack.append(RenderState { .Text = "["}); try stack.append(RenderState { .Expression = suffix_op.lhs }); }, - ast.NodeSuffixOp.SuffixOp.StructInitializer => |field_inits| { - try stack.append(RenderState { .Text = " }"}); + ast.Node.SuffixOp.Op.StructInitializer => |field_inits| { + if (field_inits.len == 0) { + try stack.append(RenderState { .Text = "{}" }); + try stack.append(RenderState { .Expression = suffix_op.lhs }); + continue; + } + try stack.append(RenderState { .Text = "}"}); + try stack.append(RenderState.PrintIndent); + try stack.append(RenderState { .Indent = indent }); var i = field_inits.len; while (i != 0) { i -= 1; const field_init = field_inits.at(i); + try stack.append(RenderState { .Text = ",\n" }); try stack.append(RenderState { .FieldInitializer = field_init }); - try stack.append(RenderState { .Text = " " }); - if (i != 0) { - try stack.append(RenderState { .Text = "," }); - } + try stack.append(RenderState.PrintIndent); } - try stack.append(RenderState { .Text = "{"}); + try stack.append(RenderState { .Indent = indent + indent_delta }); + try stack.append(RenderState { .Text = " {\n"}); try stack.append(RenderState { .Expression = suffix_op.lhs }); }, - ast.NodeSuffixOp.SuffixOp.ArrayInitializer => |exprs| { - try stack.append(RenderState { .Text = " }"}); + ast.Node.SuffixOp.Op.ArrayInitializer => |exprs| { + if (exprs.len == 0) { + try stack.append(RenderState { .Text = "{}" }); + try stack.append(RenderState { .Expression = suffix_op.lhs }); + continue; + } + try stack.append(RenderState { .Text = "}"}); + try stack.append(RenderState.PrintIndent); + try stack.append(RenderState { .Indent = indent }); var i = exprs.len; while (i != 0) { i -= 1; const expr = exprs.at(i); + try stack.append(RenderState { .Text = ",\n" }); try stack.append(RenderState { .Expression = expr }); - try stack.append(RenderState { .Text = " " }); - if (i != 0) { - try stack.append(RenderState { .Text = "," }); - } + try stack.append(RenderState.PrintIndent); } - try stack.append(RenderState { .Text = "{"}); + try stack.append(RenderState { .Indent = indent + indent_delta }); + try stack.append(RenderState { .Text = " {\n"}); try stack.append(RenderState { .Expression = suffix_op.lhs }); }, } }, ast.Node.Id.ControlFlowExpression => { - const flow_expr = @fieldParentPtr(ast.NodeControlFlowExpression, "base", base); + const flow_expr = @fieldParentPtr(ast.Node.ControlFlowExpression, "base", base); + + if (flow_expr.rhs) |rhs| { + try stack.append(RenderState { .Expression = rhs }); + try stack.append(RenderState { .Text = " " }); + } + switch (flow_expr.kind) { - ast.NodeControlFlowExpression.Kind.Break => |maybe_blk_token| { + ast.Node.ControlFlowExpression.Kind.Break => |maybe_label| { try stream.print("break"); - if (maybe_blk_token) |blk_token| { - try stream.print(" :{}", self.tokenizer.getTokenSlice(blk_token)); + if (maybe_label) |label| { + try stream.print(" :"); + try stack.append(RenderState { .Expression = label }); } }, - ast.NodeControlFlowExpression.Kind.Continue => |maybe_blk_token| { + ast.Node.ControlFlowExpression.Kind.Continue => |maybe_label| { try stream.print("continue"); - if (maybe_blk_token) |blk_token| { - try stream.print(" :{}", self.tokenizer.getTokenSlice(blk_token)); + if (maybe_label) |label| { + try stream.print(" :"); + try stack.append(RenderState { .Expression = label }); } }, - ast.NodeControlFlowExpression.Kind.Return => { + ast.Node.ControlFlowExpression.Kind.Return => { try stream.print("return"); }, } - - if (flow_expr.rhs) |rhs| { - try stream.print(" "); - try stack.append(RenderState { .Expression = rhs }); - } }, ast.Node.Id.Payload => { - const payload = @fieldParentPtr(ast.NodePayload, "base", base); + const payload = @fieldParentPtr(ast.Node.Payload, "base", base); try stack.append(RenderState { .Text = "|"}); - try stack.append(RenderState { .Expression = &payload.error_symbol.base }); + try stack.append(RenderState { .Expression = payload.error_symbol }); try stack.append(RenderState { .Text = "|"}); }, ast.Node.Id.PointerPayload => { - const payload = @fieldParentPtr(ast.NodePointerPayload, "base", base); + const payload = @fieldParentPtr(ast.Node.PointerPayload, "base", base); try stack.append(RenderState { .Text = "|"}); - try stack.append(RenderState { .Expression = &payload.value_symbol.base }); + try stack.append(RenderState { .Expression = payload.value_symbol }); - if (payload.is_ptr) { - try stack.append(RenderState { .Text = "*"}); + if (payload.ptr_token) |ptr_token| { + try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(ptr_token) }); } try stack.append(RenderState { .Text = "|"}); }, ast.Node.Id.PointerIndexPayload => { - const payload = @fieldParentPtr(ast.NodePointerIndexPayload, "base", base); + const payload = @fieldParentPtr(ast.Node.PointerIndexPayload, "base", base); try stack.append(RenderState { .Text = "|"}); if (payload.index_symbol) |index_symbol| { - try stack.append(RenderState { .Expression = &index_symbol.base }); + try stack.append(RenderState { .Expression = index_symbol }); try stack.append(RenderState { .Text = ", "}); } - try stack.append(RenderState { .Expression = &payload.value_symbol.base }); + try stack.append(RenderState { .Expression = payload.value_symbol }); - if (payload.is_ptr) { - try stack.append(RenderState { .Text = "*"}); + if (payload.ptr_token) |ptr_token| { + try stack.append(RenderState { .Text = self.tokenizer.getTokenSlice(ptr_token) }); } try stack.append(RenderState { .Text = "|"}); }, ast.Node.Id.GroupedExpression => { - const grouped_expr = @fieldParentPtr(ast.NodeGroupedExpression, "base", base); + const grouped_expr = @fieldParentPtr(ast.Node.GroupedExpression, "base", base); try stack.append(RenderState { .Text = ")"}); try stack.append(RenderState { .Expression = grouped_expr.expr }); try stack.append(RenderState { .Text = "("}); }, ast.Node.Id.FieldInitializer => { - const field_init = @fieldParentPtr(ast.NodeFieldInitializer, "base", base); + const field_init = @fieldParentPtr(ast.Node.FieldInitializer, "base", base); try stream.print(".{} = ", self.tokenizer.getTokenSlice(field_init.name_token)); try stack.append(RenderState { .Expression = field_init.expr }); }, ast.Node.Id.IntegerLiteral => { - const integer_literal = @fieldParentPtr(ast.NodeIntegerLiteral, "base", base); + const integer_literal = @fieldParentPtr(ast.Node.IntegerLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(integer_literal.token)); }, ast.Node.Id.FloatLiteral => { - const float_literal = @fieldParentPtr(ast.NodeFloatLiteral, "base", base); + const float_literal = @fieldParentPtr(ast.Node.FloatLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(float_literal.token)); }, ast.Node.Id.StringLiteral => { - const string_literal = @fieldParentPtr(ast.NodeStringLiteral, "base", base); + const string_literal = @fieldParentPtr(ast.Node.StringLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(string_literal.token)); }, ast.Node.Id.CharLiteral => { - const char_literal = @fieldParentPtr(ast.NodeCharLiteral, "base", base); + const char_literal = @fieldParentPtr(ast.Node.CharLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(char_literal.token)); }, ast.Node.Id.BoolLiteral => { - const bool_literal = @fieldParentPtr(ast.NodeCharLiteral, "base", base); + const bool_literal = @fieldParentPtr(ast.Node.CharLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(bool_literal.token)); }, ast.Node.Id.NullLiteral => { - const null_literal = @fieldParentPtr(ast.NodeNullLiteral, "base", base); + const null_literal = @fieldParentPtr(ast.Node.NullLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(null_literal.token)); }, ast.Node.Id.ThisLiteral => { - const this_literal = @fieldParentPtr(ast.NodeThisLiteral, "base", base); + const this_literal = @fieldParentPtr(ast.Node.ThisLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(this_literal.token)); }, ast.Node.Id.Unreachable => { - const unreachable_node = @fieldParentPtr(ast.NodeUnreachable, "base", base); + const unreachable_node = @fieldParentPtr(ast.Node.Unreachable, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(unreachable_node.token)); }, ast.Node.Id.ErrorType => { - const error_type = @fieldParentPtr(ast.NodeErrorType, "base", base); + const error_type = @fieldParentPtr(ast.Node.ErrorType, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(error_type.token)); }, ast.Node.Id.VarType => { - const var_type = @fieldParentPtr(ast.NodeVarType, "base", base); + const var_type = @fieldParentPtr(ast.Node.VarType, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(var_type.token)); }, ast.Node.Id.ContainerDecl => { - const container_decl = @fieldParentPtr(ast.NodeContainerDecl, "base", base); + const container_decl = @fieldParentPtr(ast.Node.ContainerDecl, "base", base); switch (container_decl.layout) { - ast.NodeContainerDecl.Layout.Packed => try stream.print("packed "), - ast.NodeContainerDecl.Layout.Extern => try stream.print("extern "), - ast.NodeContainerDecl.Layout.Auto => { }, + ast.Node.ContainerDecl.Layout.Packed => try stream.print("packed "), + ast.Node.ContainerDecl.Layout.Extern => try stream.print("extern "), + ast.Node.ContainerDecl.Layout.Auto => { }, } switch (container_decl.kind) { - ast.NodeContainerDecl.Kind.Struct => try stream.print("struct"), - ast.NodeContainerDecl.Kind.Enum => try stream.print("enum"), - ast.NodeContainerDecl.Kind.Union => try stream.print("union"), + ast.Node.ContainerDecl.Kind.Struct => try stream.print("struct"), + ast.Node.ContainerDecl.Kind.Enum => try stream.print("enum"), + ast.Node.ContainerDecl.Kind.Union => try stream.print("union"), } try stack.append(RenderState { .Text = "}"}); @@ -3749,6 +3812,14 @@ pub const Parser = struct { while (i != 0) { i -= 1; const node = fields_and_decls[i]; + switch (node.id) { + ast.Node.Id.StructField, + ast.Node.Id.UnionTag, + ast.Node.Id.EnumTag => { + try stack.append(RenderState { .Text = "," }); + }, + else => { } + } try stack.append(RenderState { .TopLevelDecl = node}); try stack.append(RenderState.PrintIndent); try stack.append(RenderState { @@ -3763,26 +3834,14 @@ pub const Parser = struct { break :blk "\n"; }, }); - - if (i != 0) { - const prev_node = fields_and_decls[i - 1]; - switch (prev_node.id) { - ast.Node.Id.StructField, - ast.Node.Id.UnionTag, - ast.Node.Id.EnumTag => { - try stack.append(RenderState { .Text = "," }); - }, - else => { } - } - } } try stack.append(RenderState { .Indent = indent + indent_delta}); try stack.append(RenderState { .Text = "{"}); switch (container_decl.init_arg_expr) { - ast.NodeContainerDecl.InitArg.None => try stack.append(RenderState { .Text = " "}), - ast.NodeContainerDecl.InitArg.Enum => try stack.append(RenderState { .Text = "(enum) "}), - ast.NodeContainerDecl.InitArg.Type => |type_expr| { + ast.Node.ContainerDecl.InitArg.None => try stack.append(RenderState { .Text = " "}), + ast.Node.ContainerDecl.InitArg.Enum => try stack.append(RenderState { .Text = "(enum) "}), + ast.Node.ContainerDecl.InitArg.Type => |type_expr| { try stack.append(RenderState { .Text = ") "}); try stack.append(RenderState { .Expression = type_expr}); try stack.append(RenderState { .Text = "("}); @@ -3790,7 +3849,7 @@ pub const Parser = struct { } }, ast.Node.Id.ErrorSetDecl => { - const err_set_decl = @fieldParentPtr(ast.NodeErrorSetDecl, "base", base); + const err_set_decl = @fieldParentPtr(ast.Node.ErrorSetDecl, "base", base); try stream.print("error "); try stack.append(RenderState { .Text = "}"}); @@ -3803,7 +3862,8 @@ pub const Parser = struct { while (i != 0) { i -= 1; const node = decls[i]; - try stack.append(RenderState { .Expression = &node.base}); + try stack.append(RenderState { .Text = "," }); + try stack.append(RenderState { .Expression = node }); try stack.append(RenderState.PrintIndent); try stack.append(RenderState { .Text = blk: { @@ -3817,16 +3877,12 @@ pub const Parser = struct { break :blk "\n"; }, }); - - if (i != 0) { - try stack.append(RenderState { .Text = "," }); - } } try stack.append(RenderState { .Indent = indent + indent_delta}); try stack.append(RenderState { .Text = "{"}); }, ast.Node.Id.MultilineStringLiteral => { - const multiline_str_literal = @fieldParentPtr(ast.NodeMultilineStringLiteral, "base", base); + const multiline_str_literal = @fieldParentPtr(ast.Node.MultilineStringLiteral, "base", base); try stream.print("\n"); var i : usize = 0; @@ -3838,11 +3894,11 @@ pub const Parser = struct { try stream.writeByteNTimes(' ', indent + indent_delta); }, ast.Node.Id.UndefinedLiteral => { - const undefined_literal = @fieldParentPtr(ast.NodeUndefinedLiteral, "base", base); + const undefined_literal = @fieldParentPtr(ast.Node.UndefinedLiteral, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(undefined_literal.token)); }, ast.Node.Id.BuiltinCall => { - const builtin_call = @fieldParentPtr(ast.NodeBuiltinCall, "base", base); + const builtin_call = @fieldParentPtr(ast.Node.BuiltinCall, "base", base); try stream.print("{}(", self.tokenizer.getTokenSlice(builtin_call.builtin_token)); try stack.append(RenderState { .Text = ")"}); var i = builtin_call.params.len; @@ -3856,20 +3912,22 @@ pub const Parser = struct { } }, ast.Node.Id.FnProto => { - const fn_proto = @fieldParentPtr(ast.NodeFnProto, "base", base); + const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", base); switch (fn_proto.return_type) { - ast.NodeFnProto.ReturnType.Explicit => |node| { + ast.Node.FnProto.ReturnType.Explicit => |node| { try stack.append(RenderState { .Expression = node}); }, - ast.NodeFnProto.ReturnType.InferErrorSet => |node| { + ast.Node.FnProto.ReturnType.InferErrorSet => |node| { try stack.append(RenderState { .Expression = node}); try stack.append(RenderState { .Text = "!"}); }, } - if (fn_proto.align_expr != null) { - @panic("TODO"); + if (fn_proto.align_expr) |align_expr| { + try stack.append(RenderState { .Text = ") " }); + try stack.append(RenderState { .Expression = align_expr}); + try stack.append(RenderState { .Text = "align(" }); } try stack.append(RenderState { .Text = ") " }); @@ -3918,7 +3976,7 @@ pub const Parser = struct { }, ast.Node.Id.LineComment => @panic("TODO render line comment in an expression"), ast.Node.Id.Switch => { - const switch_node = @fieldParentPtr(ast.NodeSwitch, "base", base); + const switch_node = @fieldParentPtr(ast.Node.Switch, "base", base); try stream.print("{} (", self.tokenizer.getTokenSlice(switch_node.switch_token)); try stack.append(RenderState { .Text = "}"}); @@ -3931,6 +3989,7 @@ pub const Parser = struct { while (i != 0) { i -= 1; const node = cases[i]; + try stack.append(RenderState { .Text = ","}); try stack.append(RenderState { .Expression = &node.base}); try stack.append(RenderState.PrintIndent); try stack.append(RenderState { @@ -3945,22 +4004,18 @@ pub const Parser = struct { break :blk "\n"; }, }); - - if (i != 0) { - try stack.append(RenderState { .Text = "," }); - } } try stack.append(RenderState { .Indent = indent + indent_delta}); try stack.append(RenderState { .Text = ") {"}); try stack.append(RenderState { .Expression = switch_node.expr }); }, ast.Node.Id.SwitchCase => { - const switch_case = @fieldParentPtr(ast.NodeSwitchCase, "base", base); + const switch_case = @fieldParentPtr(ast.Node.SwitchCase, "base", base); try stack.append(RenderState { .Expression = switch_case.expr }); if (switch_case.payload) |payload| { try stack.append(RenderState { .Text = " " }); - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); } try stack.append(RenderState { .Text = " => "}); @@ -3971,16 +4026,17 @@ pub const Parser = struct { try stack.append(RenderState { .Expression = items[i] }); if (i != 0) { - try stack.append(RenderState { .Text = ", " }); + try stack.append(RenderState.PrintIndent); + try stack.append(RenderState { .Text = ",\n" }); } } }, ast.Node.Id.SwitchElse => { - const switch_else = @fieldParentPtr(ast.NodeSwitchElse, "base", base); + const switch_else = @fieldParentPtr(ast.Node.SwitchElse, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(switch_else.token)); }, ast.Node.Id.Else => { - const else_node = @fieldParentPtr(ast.NodeElse, "base", base); + const else_node = @fieldParentPtr(ast.Node.Else, "base", base); try stream.print("{}", self.tokenizer.getTokenSlice(else_node.else_token)); switch (else_node.body.id) { @@ -4001,11 +4057,11 @@ pub const Parser = struct { if (else_node.payload) |payload| { try stack.append(RenderState { .Text = " " }); - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); } }, ast.Node.Id.While => { - const while_node = @fieldParentPtr(ast.NodeWhile, "base", base); + const while_node = @fieldParentPtr(ast.Node.While, "base", base); if (while_node.label) |label| { try stream.print("{}: ", self.tokenizer.getTokenSlice(label)); } @@ -4046,7 +4102,7 @@ pub const Parser = struct { } if (while_node.payload) |payload| { - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); try stack.append(RenderState { .Text = " " }); } @@ -4055,7 +4111,7 @@ pub const Parser = struct { try stack.append(RenderState { .Text = "(" }); }, ast.Node.Id.For => { - const for_node = @fieldParentPtr(ast.NodeFor, "base", base); + const for_node = @fieldParentPtr(ast.Node.For, "base", base); if (for_node.label) |label| { try stream.print("{}: ", self.tokenizer.getTokenSlice(label)); } @@ -4089,7 +4145,7 @@ pub const Parser = struct { } if (for_node.payload) |payload| { - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); try stack.append(RenderState { .Text = " " }); } @@ -4098,7 +4154,7 @@ pub const Parser = struct { try stack.append(RenderState { .Text = "(" }); }, ast.Node.Id.If => { - const if_node = @fieldParentPtr(ast.NodeIf, "base", base); + const if_node = @fieldParentPtr(ast.Node.If, "base", base); try stream.print("{} ", self.tokenizer.getTokenSlice(if_node.if_token)); switch (if_node.body.id) { @@ -4122,7 +4178,7 @@ pub const Parser = struct { if (@"else".payload) |payload| { try stack.append(RenderState { .Text = " " }); - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); } try stack.append(RenderState { .Text = " " }); @@ -4136,7 +4192,7 @@ pub const Parser = struct { try stack.append(RenderState { .Text = " " }); if (if_node.payload) |payload| { - try stack.append(RenderState { .Expression = &payload.base }); + try stack.append(RenderState { .Expression = payload }); try stack.append(RenderState { .Text = " " }); } @@ -4145,11 +4201,11 @@ pub const Parser = struct { try stack.append(RenderState { .Text = "(" }); }, ast.Node.Id.Asm => { - const asm_node = @fieldParentPtr(ast.NodeAsm, "base", base); + const asm_node = @fieldParentPtr(ast.Node.Asm, "base", base); try stream.print("{} ", self.tokenizer.getTokenSlice(asm_node.asm_token)); - if (asm_node.is_volatile) { - try stream.write("volatile "); + if (asm_node.volatile_token) |volatile_token| { + try stream.print("{} ", self.tokenizer.getTokenSlice(volatile_token)); } try stack.append(RenderState { .Indent = indent }); @@ -4232,25 +4288,25 @@ pub const Parser = struct { try stack.append(RenderState { .Text = "(" }); }, ast.Node.Id.AsmInput => { - const asm_input = @fieldParentPtr(ast.NodeAsmInput, "base", base); + const asm_input = @fieldParentPtr(ast.Node.AsmInput, "base", base); try stack.append(RenderState { .Text = ")"}); try stack.append(RenderState { .Expression = asm_input.expr}); try stack.append(RenderState { .Text = " ("}); try stack.append(RenderState { .Expression = asm_input.constraint }); try stack.append(RenderState { .Text = "] "}); - try stack.append(RenderState { .Expression = &asm_input.symbolic_name.base}); + try stack.append(RenderState { .Expression = asm_input.symbolic_name }); try stack.append(RenderState { .Text = "["}); }, ast.Node.Id.AsmOutput => { - const asm_output = @fieldParentPtr(ast.NodeAsmOutput, "base", base); + const asm_output = @fieldParentPtr(ast.Node.AsmOutput, "base", base); try stack.append(RenderState { .Text = ")"}); switch (asm_output.kind) { - ast.NodeAsmOutput.Kind.Variable => |variable_name| { + ast.Node.AsmOutput.Kind.Variable => |variable_name| { try stack.append(RenderState { .Expression = &variable_name.base}); }, - ast.NodeAsmOutput.Kind.Return => |return_type| { + ast.Node.AsmOutput.Kind.Return => |return_type| { try stack.append(RenderState { .Expression = return_type}); try stack.append(RenderState { .Text = "-> "}); }, @@ -4258,7 +4314,7 @@ pub const Parser = struct { try stack.append(RenderState { .Text = " ("}); try stack.append(RenderState { .Expression = asm_output.constraint }); try stack.append(RenderState { .Text = "] "}); - try stack.append(RenderState { .Expression = &asm_output.symbolic_name.base}); + try stack.append(RenderState { .Expression = asm_output.symbolic_name }); try stack.append(RenderState { .Text = "["}); }, @@ -4271,36 +4327,11 @@ pub const Parser = struct { ast.Node.Id.TestDecl, ast.Node.Id.ParamDecl => unreachable, }, - RenderState.FnProtoRParen => |fn_proto| { - try stream.print(")"); - if (fn_proto.align_expr != null) { - @panic("TODO"); - } - try stream.print(" "); - if (fn_proto.body_node) |body_node| { - try stack.append(RenderState { .Expression = body_node}); - try stack.append(RenderState { .Text = " "}); - } - switch (fn_proto.return_type) { - ast.NodeFnProto.ReturnType.Explicit => |node| { - try stack.append(RenderState { .Expression = node}); - }, - ast.NodeFnProto.ReturnType.InferErrorSet => |node| { - try stream.print("!"); - try stack.append(RenderState { .Expression = node}); - }, - } - }, RenderState.Statement => |base| { - if (base.comment) |comment| { - for (comment.lines.toSliceConst()) |line_token| { - try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token)); - try stream.writeByteNTimes(' ', indent); - } - } switch (base.id) { ast.Node.Id.VarDecl => { - const var_decl = @fieldParentPtr(ast.NodeVarDecl, "base", base); + const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", base); + try self.renderComments(stream, var_decl, indent); try stack.append(RenderState { .VarDecl = var_decl}); }, else => { @@ -4317,6 +4348,14 @@ pub const Parser = struct { } } + fn renderComments(self: &Parser, stream: var, node: var, indent: usize) !void { + const comment = node.comments ?? return; + for (comment.lines.toSliceConst()) |line_token| { + try stream.print("{}\n", self.tokenizer.getTokenSlice(line_token)); + try stream.writeByteNTimes(' ', indent); + } + } + fn initUtilityArrayList(self: &Parser, comptime T: type) ArrayList(T) { const new_byte_count = self.utility_bytes.len - self.utility_bytes.len % @sizeOf(T); self.utility_bytes = self.util_allocator.alignedShrink(u8, utility_bytes_align, self.utility_bytes, new_byte_count); @@ -4391,6 +4430,14 @@ fn testCanonical(source: []const u8) !void { } } +test "zig fmt: preserve top level comments" { + try testCanonical( + \\// top level comment + \\test "hi" {} + \\ + ); +} + test "zig fmt: get stdout or fail" { try testCanonical( \\const std = @import("std"); @@ -4583,10 +4630,10 @@ test "zig fmt: precedence" { \\ (a!b)(); \\ !a!b; \\ !(a!b); - \\ !a{ }; - \\ !(a{ }); - \\ a + b{ }; - \\ (a + b){ }; + \\ !a{}; + \\ !(a{}); + \\ a + b{}; + \\ (a + b){}; \\ a << b + c; \\ (a << b) + c; \\ a & b << c; @@ -4644,12 +4691,20 @@ test "zig fmt: var type" { ); } -test "zig fmt: extern function" { +test "zig fmt: functions" { try testCanonical( \\extern fn puts(s: &const u8) c_int; \\extern "c" fn puts(s: &const u8) c_int; \\export fn puts(s: &const u8) c_int; \\inline fn puts(s: &const u8) c_int; + \\pub extern fn puts(s: &const u8) c_int; + \\pub extern "c" fn puts(s: &const u8) c_int; + \\pub export fn puts(s: &const u8) c_int; + \\pub inline fn puts(s: &const u8) c_int; + \\pub extern fn puts(s: &const u8) align(2 + 2) c_int; + \\pub extern "c" fn puts(s: &const u8) align(2 + 2) c_int; + \\pub export fn puts(s: &const u8) align(2 + 2) c_int; + \\pub inline fn puts(s: &const u8) align(2 + 2) c_int; \\ ); } @@ -4715,21 +4770,21 @@ test "zig fmt: struct declaration" { \\ return *self; \\ } \\ - \\ f2: u8 + \\ f2: u8, \\}; \\ \\const Ps = packed struct { \\ a: u8, \\ pub b: u8, \\ - \\ c: u8 + \\ c: u8, \\}; \\ \\const Es = extern struct { \\ a: u8, \\ pub b: u8, \\ - \\ c: u8 + \\ c: u8, \\}; \\ ); @@ -4739,25 +4794,25 @@ test "zig fmt: enum declaration" { try testCanonical( \\const E = enum { \\ Ok, - \\ SomethingElse = 0 + \\ SomethingElse = 0, \\}; \\ \\const E2 = enum(u8) { \\ Ok, \\ SomethingElse = 255, - \\ SomethingThird + \\ SomethingThird, \\}; \\ \\const Ee = extern enum { \\ Ok, \\ SomethingElse, - \\ SomethingThird + \\ SomethingThird, \\}; \\ \\const Ep = packed enum { \\ Ok, \\ SomethingElse, - \\ SomethingThird + \\ SomethingThird, \\}; \\ ); @@ -4769,35 +4824,35 @@ test "zig fmt: union declaration" { \\ Int: u8, \\ Float: f32, \\ None, - \\ Bool: bool + \\ Bool: bool, \\}; \\ \\const Ue = union(enum) { \\ Int: u8, \\ Float: f32, \\ None, - \\ Bool: bool + \\ Bool: bool, \\}; \\ \\const E = enum { \\ Int, \\ Float, \\ None, - \\ Bool + \\ Bool, \\}; \\ \\const Ue2 = union(E) { \\ Int: u8, \\ Float: f32, \\ None, - \\ Bool: bool + \\ Bool: bool, \\}; \\ \\const Eu = extern union { \\ Int: u8, \\ Float: f32, \\ None, - \\ Bool: bool + \\ Bool: bool, \\}; \\ ); @@ -4809,7 +4864,7 @@ test "zig fmt: error set declaration" { \\ A, \\ B, \\ - \\ C + \\ C, \\}; \\ ); @@ -4818,9 +4873,15 @@ test "zig fmt: error set declaration" { test "zig fmt: arrays" { try testCanonical( \\test "test array" { - \\ const a: [2]u8 = [2]u8{ 1, 2 }; - \\ const a: [2]u8 = []u8{ 1, 2 }; - \\ const a: [0]u8 = []u8{ }; + \\ const a: [2]u8 = [2]u8 { + \\ 1, + \\ 2, + \\ }; + \\ const a: [2]u8 = []u8 { + \\ 1, + \\ 2, + \\ }; + \\ const a: [0]u8 = []u8{}; \\} \\ ); @@ -4828,10 +4889,18 @@ test "zig fmt: arrays" { test "zig fmt: container initializers" { try testCanonical( - \\const a1 = []u8{ }; - \\const a2 = []u8{ 1, 2, 3, 4 }; - \\const s1 = S{ }; - \\const s2 = S{ .a = 1, .b = 2 }; + \\const a1 = []u8{}; + \\const a2 = []u8 { + \\ 1, + \\ 2, + \\ 3, + \\ 4, + \\}; + \\const s1 = S{}; + \\const s2 = S { + \\ .a = 1, + \\ .b = 2, + \\}; \\ ); } @@ -4875,31 +4944,34 @@ test "zig fmt: switch" { \\ switch (0) { \\ 0 => {}, \\ 1 => unreachable, - \\ 2, 3 => {}, + \\ 2, + \\ 3 => {}, \\ 4 ... 7 => {}, \\ 1 + 4 * 3 + 22 => {}, \\ else => { \\ const a = 1; \\ const b = a; - \\ } + \\ }, \\ } \\ \\ const res = switch (0) { \\ 0 => 0, \\ 1 => 2, \\ 1 => a = 4, - \\ else => 4 + \\ else => 4, \\ }; \\ \\ const Union = union(enum) { \\ Int: i64, - \\ Float: f64 + \\ Float: f64, \\ }; \\ - \\ const u = Union{ .Int = 0 }; + \\ const u = Union { + \\ .Int = 0, + \\ }; \\ switch (u) { \\ Union.Int => |int| {}, - \\ Union.Float => |*float| unreachable + \\ Union.Float => |*float| unreachable, \\ } \\} \\ @@ -4975,7 +5047,11 @@ test "zig fmt: while" { test "zig fmt: for" { try testCanonical( \\test "for" { - \\ const a = []u8{ 1, 2, 3 }; + \\ const a = []u8 { + \\ 1, + \\ 2, + \\ 3, + \\ }; \\ for (a) |v| { \\ continue; \\ } @@ -5205,3 +5281,12 @@ test "zig fmt: error return" { \\ ); } + +test "zig fmt: struct literals with fields on each line" { + try testCanonical( + \\var self = BufSet { + \\ .hash_map = BufSetHashMap.init(a), + \\}; + \\ + ); +} diff --git a/test/behavior.zig b/test/behavior.zig index de39b20dad..2c10c6d71b 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -12,6 +12,7 @@ comptime { _ = @import("cases/bugs/655.zig"); _ = @import("cases/bugs/656.zig"); _ = @import("cases/bugs/828.zig"); + _ = @import("cases/bugs/920.zig"); _ = @import("cases/cast.zig"); _ = @import("cases/const_slice_child.zig"); _ = @import("cases/coroutines.zig"); diff --git a/test/cases/atomics.zig b/test/cases/atomics.zig index e8e81b76e6..d406285d29 100644 --- a/test/cases/atomics.zig +++ b/test/cases/atomics.zig @@ -1,12 +1,24 @@ -const assert = @import("std").debug.assert; +const std = @import("std"); +const assert = std.debug.assert; const builtin = @import("builtin"); const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; test "cmpxchg" { var x: i32 = 1234; - while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} + if (@cmpxchgWeak(i32, &x, 99, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { + assert(x1 == 1234); + } else { + @panic("cmpxchg should have failed"); + } + + while (@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { + assert(x1 == 1234); + } assert(x == 5678); + + assert(@cmpxchgStrong(i32, &x, 5678, 42, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); + assert(x == 42); } test "fence" { @@ -15,13 +27,45 @@ test "fence" { x = 5678; } -test "atomicrmw" { +test "atomicrmw and atomicload" { var data: u8 = 200; testAtomicRmw(&data); assert(data == 42); + testAtomicLoad(&data); } fn testAtomicRmw(ptr: &u8) void { const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst); assert(prev_value == 200); + comptime { + var x: i32 = 1234; + const y: i32 = 12345; + assert(@atomicLoad(i32, &x, AtomicOrder.SeqCst) == 1234); + assert(@atomicLoad(i32, &y, AtomicOrder.SeqCst) == 12345); + } +} + +fn testAtomicLoad(ptr: &u8) void { + const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst); + assert(x == 42); +} + +test "cmpxchg with ptr" { + var data1: i32 = 1234; + var data2: i32 = 5678; + var data3: i32 = 9101; + var x: &i32 = &data1; + if (@cmpxchgWeak(&i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { + assert(x1 == &data1); + } else { + @panic("cmpxchg should have failed"); + } + + while (@cmpxchgWeak(&i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { + assert(x1 == &data1); + } + assert(x == &data3); + + assert(@cmpxchgStrong(&i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); + assert(x == &data2); } diff --git a/test/cases/bugs/920.zig b/test/cases/bugs/920.zig new file mode 100644 index 0000000000..13c03a304f --- /dev/null +++ b/test/cases/bugs/920.zig @@ -0,0 +1,60 @@ +const std = @import("std"); +const math = std.math; +const Random = std.rand.Random; + +const ZigTable = struct { + r: f64, + x: [257]f64, + f: [257]f64, + + pdf: fn(f64) f64, + is_symmetric: bool, + zero_case: fn(&Random, f64) f64, +}; + +fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64, + comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable { + var tables: ZigTable = undefined; + + tables.is_symmetric = is_symmetric; + tables.r = r; + tables.pdf = f; + tables.zero_case = zero_case; + + tables.x[0] = v / f(r); + tables.x[1] = r; + + for (tables.x[2..256]) |*entry, i| { + const last = tables.x[2 + i - 1]; + *entry = f_inv(v / last + f(last)); + } + tables.x[256] = 0; + + for (tables.f[0..]) |*entry, i| { + *entry = f(tables.x[i]); + } + + return tables; +} + +const norm_r = 3.6541528853610088; +const norm_v = 0.00492867323399; + +fn norm_f(x: f64) f64 { return math.exp(-x * x / 2.0); } +fn norm_f_inv(y: f64) f64 { return math.sqrt(-2.0 * math.ln(y)); } +fn norm_zero_case(random: &Random, u: f64) f64 { return 0.0; } + +const NormalDist = blk: { + @setEvalBranchQuota(30000); + break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case); +}; + +test "bug 920 fixed" { + const NormalDist1 = blk: { + break :blk ZigTableGen(true, norm_r, norm_v, norm_f, norm_f_inv, norm_zero_case); + }; + + for (NormalDist1.f) |_, i| { + std.debug.assert(NormalDist1.f[i] == NormalDist.f[i]); + } +} diff --git a/test/cases/coroutines.zig b/test/cases/coroutines.zig index 6d28b98c9d..46055d7469 100644 --- a/test/cases/coroutines.zig +++ b/test/cases/coroutines.zig @@ -224,3 +224,21 @@ async fn printTrace(p: promise->error!void) void { } }; } + +test "break from suspend" { + var buf: [500]u8 = undefined; + var a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; + var my_result: i32 = 1; + const p = try async testBreakFromSuspend(&my_result); + cancel p; + std.debug.assert(my_result == 2); +} + +async fn testBreakFromSuspend(my_result: &i32) void { + s: suspend |p| { + break :s; + } + *my_result += 1; + suspend; + *my_result += 1; +} diff --git a/test/cases/defer.zig b/test/cases/defer.zig index a989af18c2..5470b4bbd0 100644 --- a/test/cases/defer.zig +++ b/test/cases/defer.zig @@ -41,3 +41,14 @@ fn testBreakContInDefer(x: usize) void { assert(i == 5); } } + +test "defer and labeled break" { + var i = usize(0); + + blk: { + defer i += 1; + break :blk; + } + + assert(i == 1); +} diff --git a/test/cases/eval.zig b/test/cases/eval.zig index d6f7afe864..e13d4340e7 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -513,3 +513,19 @@ test "array concat of slices gives slice" { assert(std.mem.eql(u8, c, "aoeuasdf")); } } + +test "comptime shlWithOverflow" { + const ct_shifted: u64 = comptime amt: { + var amt = u64(0); + _ = @shlWithOverflow(u64, ~u64(0), 16, &amt); + break :amt amt; + }; + + const rt_shifted: u64 = amt: { + var amt = u64(0); + _ = @shlWithOverflow(u64, ~u64(0), 16, &amt); + break :amt amt; + }; + + assert(ct_shifted == rt_shifted); +} diff --git a/test/cases/fn.zig b/test/cases/fn.zig index c125d98d8c..5388deac10 100644 --- a/test/cases/fn.zig +++ b/test/cases/fn.zig @@ -104,3 +104,10 @@ test "number literal as an argument" { fn numberLiteralArg(a: var) void { assert(a == 3); } + +test "assign inline fn to const variable" { + const a = inlineFn; + a(); +} + +inline fn inlineFn() void { } diff --git a/test/cases/math.zig b/test/cases/math.zig index 574aa39bb1..47d001a590 100644 --- a/test/cases/math.zig +++ b/test/cases/math.zig @@ -402,3 +402,19 @@ test "comptime float rem int" { assert(x == 1.0); } } + +test "@sqrt" { + testSqrt(f64, 12.0); + comptime testSqrt(f64, 12.0); + testSqrt(f32, 13.0); + comptime testSqrt(f32, 13.0); + + const x = 14.0; + const y = x * x; + const z = @sqrt(@typeOf(y), y); + comptime assert(z == x); +} + +fn testSqrt(comptime T: type, x: T) void { + assert(@sqrt(T, x * x) == x); +} diff --git a/test/cases/reflection.zig b/test/cases/reflection.zig index 18a766d9fc..0abc46c9de 100644 --- a/test/cases/reflection.zig +++ b/test/cases/reflection.zig @@ -1,5 +1,6 @@ const assert = @import("std").debug.assert; const mem = @import("std").mem; +const reflection = this; test "reflection: array, pointer, nullable, error union type child" { comptime { @@ -56,7 +57,30 @@ test "reflection: enum member types and names" { } +test "reflection: @field" { + var f = Foo { + .one = 42, + .two = true, + .three = void{}, + }; + + assert(f.one == f.one); + assert(@field(f, "o" ++ "ne") == f.one); + assert(@field(f, "t" ++ "wo") == f.two); + assert(@field(f, "th" ++ "ree") == f.three); + assert(@field(Foo, "const" ++ "ant") == Foo.constant); + assert(@field(Bar, "O" ++ "ne") == Bar.One); + assert(@field(Bar, "T" ++ "wo") == Bar.Two); + assert(@field(Bar, "Th" ++ "ree") == Bar.Three); + assert(@field(Bar, "F" ++ "our") == Bar.Four); + assert(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2)); + @field(f, "o" ++ "ne") = 4; + assert(f.one == 4); +} + const Foo = struct { + const constant = 52; + one: i32, two: bool, three: void, diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 21e384e389..f8febc27b8 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,55 @@ const tests = @import("tests.zig"); pub fn addCases(cases: &tests.CompileErrorContext) void { + cases.add("invalid deref on switch target", + \\comptime { + \\ var tile = Tile.Empty; + \\ switch (*tile) { + \\ Tile.Empty => {}, + \\ Tile.Filled => {}, + \\ } + \\} + \\const Tile = enum { + \\ Empty, + \\ Filled, + \\}; + , + ".tmp_source.zig:3:13: error: invalid deref on switch target"); + + cases.add("invalid field access in comptime", + \\comptime { var x = doesnt_exist.whatever; } + , + ".tmp_source.zig:1:20: error: use of undeclared identifier 'doesnt_exist'"); + + cases.add("suspend inside suspend block", + \\const std = @import("std"); + \\ + \\export fn entry() void { + \\ var buf: [500]u8 = undefined; + \\ var a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator; + \\ const p = (async foo()) catch unreachable; + \\ cancel p; + \\} + \\ + \\async fn foo() void { + \\ suspend |p| { + \\ suspend |p1| { + \\ } + \\ } + \\} + , + ".tmp_source.zig:12:9: error: cannot suspend inside suspend block", + ".tmp_source.zig:11:5: note: other suspend block here"); + + cases.add("assign inline fn to non-comptime var", + \\export fn entry() void { + \\ var a = b; + \\} + \\inline fn b() void { } + , + ".tmp_source.zig:2:5: error: functions marked inline must be stored in const or comptime var", + ".tmp_source.zig:4:8: note: declared here"); + cases.add("wrong type passed to @panic", \\export fn entry() void { \\ var e = error.Foo; @@ -1385,17 +1434,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { \\const AtomicOrder = @import("builtin").AtomicOrder; \\export fn f() void { \\ var x: i32 = 1234; - \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {} + \\ while (!@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {} \\} - , ".tmp_source.zig:4:72: error: failure atomic ordering must be no stricter than success"); + , ".tmp_source.zig:4:81: error: failure atomic ordering must be no stricter than success"); cases.add("atomic orderings of cmpxchg - success Monotonic or stricter", \\const AtomicOrder = @import("builtin").AtomicOrder; \\export fn f() void { \\ var x: i32 = 1234; - \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {} + \\ while (!@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {} \\} - , ".tmp_source.zig:4:49: error: success atomic ordering must be Monotonic or stricter"); + , ".tmp_source.zig:4:58: error: success atomic ordering must be Monotonic or stricter"); cases.add("negation overflow in function evaluation", \\const y = neg(-128); @@ -2451,11 +2500,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { \\const AtomicOrder = @import("builtin").AtomicOrder; \\export fn entry() bool { \\ var x: i32 align(1) = 1234; - \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} + \\ while (!@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) {} \\ return x == 5678; \\} , - ".tmp_source.zig:4:23: error: expected pointer alignment of at least 4, found 1"); + ".tmp_source.zig:4:32: error: expected type '&i32', found '&align(1) i32'"); cases.add("wrong size to an array literal", \\comptime { @@ -2525,10 +2574,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { cases.add("wrong types given to atomic order args in cmpxchg", \\export fn entry() void { \\ var x: i32 = 1234; - \\ while (!@cmpxchg(&x, 1234, 5678, u32(1234), u32(1234))) {} + \\ while (!@cmpxchgWeak(i32, &x, 1234, 5678, u32(1234), u32(1234))) {} \\} , - ".tmp_source.zig:3:41: error: expected type 'AtomicOrder', found 'u32'"); + ".tmp_source.zig:3:50: error: expected type 'AtomicOrder', found 'u32'"); cases.add("wrong types given to @export", \\extern fn entry() void { } diff --git a/test/tests.zig b/test/tests.zig index 19a4f82b74..c3c7bf9d4b 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -583,6 +583,7 @@ pub const CompileErrorContext = struct { Mode.Debug => {}, Mode.ReleaseSafe => zig_args.append("--release-safe") catch unreachable, Mode.ReleaseFast => zig_args.append("--release-fast") catch unreachable, + Mode.ReleaseSmall => zig_args.append("--release-small") catch unreachable, } warn("Test {}/{} {}...", self.test_index+1, self.context.test_index, self.name);