aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted
diff options
context:
space:
mode:
authorhryx <codroid@gmail.com>2019-05-27 17:24:21 -0700
committerhryx <codroid@gmail.com>2019-05-27 17:24:21 -0700
commite1f3eec9cc05535b3f3b81f2fb7cd65dd4d1e841 (patch)
tree5f408ed68a686491eaf759f9cbba02beac829b38 /src-self-hosted
parent2aa1c5da5dded6b1b346c3a1b57443f2c459ebe9 (diff)
parent3fccc0747903f0726d6cc8ee73832cb62f1304bb (diff)
downloadzig-e1f3eec9cc05535b3f3b81f2fb7cd65dd4d1e841.tar.gz
zig-e1f3eec9cc05535b3f3b81f2fb7cd65dd4d1e841.zip
Merge branch 'master' into translate-c-userland
Diffstat (limited to 'src-self-hosted')
-rw-r--r--src-self-hosted/compilation.zig24
-rw-r--r--src-self-hosted/errmsg.zig9
-rw-r--r--src-self-hosted/introspect.zig12
-rw-r--r--src-self-hosted/libc_installation.zig37
-rw-r--r--src-self-hosted/link.zig6
-rw-r--r--src-self-hosted/main.zig99
-rw-r--r--src-self-hosted/stage1.zig59
-rw-r--r--src-self-hosted/test.zig22
-rw-r--r--src-self-hosted/translate_c.zig28
9 files changed, 150 insertions, 146 deletions
diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig
index 7a30bbad98..450fde7219 100644
--- a/src-self-hosted/compilation.zig
+++ b/src-self-hosted/compilation.zig
@@ -1,5 +1,4 @@
const std = @import("std");
-const os = std.os;
const io = std.io;
const mem = std.mem;
const Allocator = mem.Allocator;
@@ -54,7 +53,7 @@ pub const ZigCompiler = struct {
};
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
- try std.os.getRandomBytes(seed_bytes[0..]);
+ try std.crypto.randomBytes(seed_bytes[0..]);
const seed = mem.readIntNative(u64, &seed_bytes);
return ZigCompiler{
@@ -302,6 +301,7 @@ pub const Compilation = struct {
InvalidUtf8,
BadPathName,
DeviceBusy,
+ CurrentWorkingDirectoryUnlinked,
};
pub const Event = union(enum) {
@@ -487,7 +487,7 @@ pub const Compilation = struct {
comp.name = try Buffer.init(comp.arena(), name);
comp.llvm_triple = try target.getTriple(comp.arena());
comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple);
- comp.zig_std_dir = try std.os.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" });
+ comp.zig_std_dir = try std.fs.path.join(comp.arena(), [][]const u8{ zig_lib_dir, "std" });
const opt_level = switch (build_mode) {
builtin.Mode.Debug => llvm.CodeGenLevelNone,
@@ -529,8 +529,8 @@ pub const Compilation = struct {
defer comp.events.destroy();
if (root_src_path) |root_src| {
- const dirname = std.os.path.dirname(root_src) orelse ".";
- const basename = std.os.path.basename(root_src);
+ const dirname = std.fs.path.dirname(root_src) orelse ".";
+ const basename = std.fs.path.basename(root_src);
comp.root_package = try Package.create(comp.arena(), dirname, basename);
comp.std_package = try Package.create(comp.arena(), comp.zig_std_dir, "std.zig");
@@ -558,7 +558,7 @@ pub const Compilation = struct {
if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {
// TODO evented I/O?
- os.deleteTree(comp.arena(), tmp_dir) catch {};
+ std.fs.deleteTree(comp.arena(), tmp_dir) catch {};
} else |_| {};
}
@@ -970,8 +970,8 @@ pub const Compilation = struct {
async fn initialCompile(self: *Compilation) !void {
if (self.root_src_path) |root_src_path| {
const root_scope = blk: {
- // TODO async/await os.path.real
- const root_src_real_path = os.path.realAlloc(self.gpa(), root_src_path) catch |err| {
+ // TODO async/await std.fs.realpath
+ const root_src_real_path = std.fs.realpathAlloc(self.gpa(), root_src_path) catch |err| {
try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err));
return;
};
@@ -1196,7 +1196,7 @@ pub const Compilation = struct {
const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", file_prefix[0..], suffix);
defer self.gpa().free(file_name);
- const full_path = try os.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] });
+ const full_path = try std.fs.path.join(self.gpa(), [][]const u8{ tmp_dir, file_name[0..] });
errdefer self.gpa().free(full_path);
return Buffer.fromOwnedSlice(self.gpa(), full_path);
@@ -1217,8 +1217,8 @@ pub const Compilation = struct {
const zig_dir_path = try getZigDir(self.gpa());
defer self.gpa().free(zig_dir_path);
- const tmp_dir = try os.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] });
- try os.makePath(self.gpa(), tmp_dir);
+ const tmp_dir = try std.fs.path.join(self.arena(), [][]const u8{ zig_dir_path, comp_dir_name[0..] });
+ try std.fs.makePath(self.gpa(), tmp_dir);
return tmp_dir;
}
@@ -1384,7 +1384,7 @@ async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void {
}
fn getZigDir(allocator: *mem.Allocator) ![]u8 {
- return os.getAppDataDir(allocator, "zig");
+ return std.fs.getAppDataDir(allocator, "zig");
}
async fn analyzeFnType(
diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig
index fc49fad410..eef5817d58 100644
--- a/src-self-hosted/errmsg.zig
+++ b/src-self-hosted/errmsg.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const mem = std.mem;
-const os = std.os;
+const fs = std.fs;
+const process = std.process;
const Token = std.zig.Token;
const ast = std.zig.ast;
const TokenIndex = std.zig.ast.TokenIndex;
@@ -239,10 +240,10 @@ pub const Msg = struct {
const allocator = msg.getAllocator();
const tree = msg.getTree();
- const cwd = try os.getCwdAlloc(allocator);
+ const cwd = try process.getCwdAlloc(allocator);
defer allocator.free(cwd);
- const relpath = try os.path.relative(allocator, cwd, msg.realpath);
+ const relpath = try fs.path.relative(allocator, cwd, msg.realpath);
defer allocator.free(relpath);
const path = if (relpath.len < msg.realpath.len) relpath else msg.realpath;
@@ -276,7 +277,7 @@ pub const Msg = struct {
try stream.write("\n");
}
- pub fn printToFile(msg: *const Msg, file: os.File, color: Color) !void {
+ pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void {
const color_on = switch (color) {
Color.Auto => file.isTty(),
Color.On => true,
diff --git a/src-self-hosted/introspect.zig b/src-self-hosted/introspect.zig
index 8f859a82ce..538232e620 100644
--- a/src-self-hosted/introspect.zig
+++ b/src-self-hosted/introspect.zig
@@ -2,19 +2,19 @@
const std = @import("std");
const mem = std.mem;
-const os = std.os;
+const fs = std.fs;
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, [][]const u8{ test_path, "lib", "zig" });
+ const test_zig_dir = try fs.path.join(allocator, [][]const u8{ test_path, "lib", "zig" });
errdefer allocator.free(test_zig_dir);
- const test_index_file = try os.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" });
+ const test_index_file = try fs.path.join(allocator, [][]const u8{ test_zig_dir, "std", "std.zig" });
defer allocator.free(test_index_file);
- var file = try os.File.openRead(test_index_file);
+ var file = try fs.File.openRead(test_index_file);
file.close();
return test_zig_dir;
@@ -22,12 +22,12 @@ pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![
/// Caller must free result
pub fn findZigLibDir(allocator: *mem.Allocator) ![]u8 {
- const self_exe_path = try os.selfExeDirPathAlloc(allocator);
+ const self_exe_path = try fs.selfExeDirPathAlloc(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) orelse ".";
+ const test_dir = fs.path.dirname(cur_path) orelse ".";
if (mem.eql(u8, test_dir, cur_path)) {
break;
diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig
index 6a530da1f0..7ca849d10c 100644
--- a/src-self-hosted/libc_installation.zig
+++ b/src-self-hosted/libc_installation.zig
@@ -3,6 +3,7 @@ const builtin = @import("builtin");
const event = std.event;
const Target = @import("target.zig").Target;
const c = @import("c.zig");
+const fs = std.fs;
/// See the render function implementation for documentation of the fields.
pub const LibCInstallation = struct {
@@ -30,7 +31,7 @@ pub const LibCInstallation = struct {
self: *LibCInstallation,
allocator: *std.mem.Allocator,
libc_file: []const u8,
- stderr: *std.io.OutStream(std.os.File.WriteError),
+ stderr: *std.io.OutStream(fs.File.WriteError),
) !void {
self.initEmpty();
@@ -100,7 +101,7 @@ pub const LibCInstallation = struct {
}
}
- pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(std.os.File.WriteError)) !void {
+ pub fn render(self: *const LibCInstallation, out: *std.io.OutStream(fs.File.WriteError)) !void {
@setEvalBranchQuota(4000);
try out.print(
\\# The directory that contains `stdlib.h`.
@@ -148,7 +149,7 @@ pub const LibCInstallation = struct {
errdefer if (windows_sdk) |sdk| c.zig_free_windows_sdk(@ptrCast(?[*]c.ZigWindowsSDK, sdk));
switch (builtin.os) {
- builtin.Os.windows => {
+ .windows => {
var sdk: *c.ZigWindowsSDK = undefined;
switch (c.zig_find_windows_sdk(@ptrCast(?[*]?[*]c.ZigWindowsSDK, &sdk))) {
c.ZigFindWindowsSdkError.None => {
@@ -166,13 +167,13 @@ pub const LibCInstallation = struct {
c.ZigFindWindowsSdkError.PathTooLong => return error.NotFound,
}
},
- builtin.Os.linux => {
+ .linux => {
try group.call(findNativeIncludeDirLinux, self, loop);
try group.call(findNativeLibDirLinux, self, loop);
try group.call(findNativeStaticLibDir, self, loop);
try group.call(findNativeDynamicLinker, self, loop);
},
- builtin.Os.macosx, builtin.Os.freebsd, builtin.Os.netbsd => {
+ .macosx, .freebsd, .netbsd => {
self.include_dir = try std.mem.dupe(loop.allocator, u8, "/usr/include");
},
else => @compileError("unimplemented: find libc for this OS"),
@@ -181,7 +182,7 @@ pub const LibCInstallation = struct {
}
async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void {
- const cc_exe = std.os.getEnvPosix("CC") orelse "cc";
+ const cc_exe = std.os.getenv("CC") orelse "cc";
const argv = []const []const u8{
cc_exe,
"-E",
@@ -190,7 +191,7 @@ pub const LibCInstallation = struct {
"/dev/null",
};
// TODO make this use event loop
- const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
+ const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
const exec_result = if (std.debug.runtime_safety) blk: {
break :blk errorable_result catch unreachable;
} else blk: {
@@ -205,7 +206,7 @@ pub const LibCInstallation = struct {
}
switch (exec_result.term) {
- std.os.ChildProcess.Term.Exited => |code| {
+ std.ChildProcess.Term.Exited => |code| {
if (code != 0) return error.CCompilerExitCode;
},
else => {
@@ -230,7 +231,7 @@ pub const LibCInstallation = struct {
while (path_i < search_paths.len) : (path_i += 1) {
const search_path_untrimmed = search_paths.at(search_paths.len - path_i - 1);
const search_path = std.mem.trimLeft(u8, search_path_untrimmed, " ");
- const stdlib_path = try std.os.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" });
+ const stdlib_path = try fs.path.join(loop.allocator, [][]const u8{ search_path, "stdlib.h" });
defer loop.allocator.free(stdlib_path);
if (try fileExists(stdlib_path)) {
@@ -254,7 +255,7 @@ pub const LibCInstallation = struct {
const stream = &std.io.BufferOutStream.init(&result_buf).stream;
try stream.print("{}\\Include\\{}\\ucrt", search.path, search.version);
- const stdlib_path = try std.os.path.join(
+ const stdlib_path = try fs.path.join(
loop.allocator,
[][]const u8{ result_buf.toSliceConst(), "stdlib.h" },
);
@@ -286,7 +287,7 @@ pub const LibCInstallation = struct {
builtin.Arch.aarch64 => try stream.write("arm"),
else => return error.UnsupportedArchitecture,
}
- const ucrt_lib_path = try std.os.path.join(
+ const ucrt_lib_path = try fs.path.join(
loop.allocator,
[][]const u8{ result_buf.toSliceConst(), "ucrt.lib" },
);
@@ -364,7 +365,7 @@ pub const LibCInstallation = struct {
builtin.Arch.aarch64 => try stream.write("arm\\"),
else => return error.UnsupportedArchitecture,
}
- const kernel32_path = try std.os.path.join(
+ const kernel32_path = try fs.path.join(
loop.allocator,
[][]const u8{ result_buf.toSliceConst(), "kernel32.lib" },
);
@@ -391,14 +392,14 @@ pub const LibCInstallation = struct {
/// caller owns returned memory
async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 {
- const cc_exe = std.os.getEnvPosix("CC") orelse "cc";
+ const cc_exe = std.os.getenv("CC") orelse "cc";
const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file);
defer loop.allocator.free(arg1);
const argv = []const []const u8{ cc_exe, arg1 };
// TODO This simulates evented I/O for the child process exec
await (async loop.yield() catch unreachable);
- const errorable_result = std.os.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
+ const errorable_result = std.ChildProcess.exec(loop.allocator, argv, null, null, 1024 * 1024);
const exec_result = if (std.debug.runtime_safety) blk: {
break :blk errorable_result catch unreachable;
} else blk: {
@@ -412,7 +413,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
loop.allocator.free(exec_result.stderr);
}
switch (exec_result.term) {
- std.os.ChildProcess.Term.Exited => |code| {
+ .Exited => |code| {
if (code != 0) return error.CCompilerExitCode;
},
else => {
@@ -421,7 +422,7 @@ async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bo
}
var it = std.mem.tokenize(exec_result.stdout, "\n\r");
const line = it.next() orelse return error.LibCRuntimeNotFound;
- const dirname = std.os.path.dirname(line) orelse return error.LibCRuntimeNotFound;
+ const dirname = fs.path.dirname(line) orelse return error.LibCRuntimeNotFound;
if (want_dirname) {
return std.mem.dupe(loop.allocator, u8, dirname);
@@ -459,10 +460,10 @@ fn fillSearch(search_buf: *[2]Search, sdk: *c.ZigWindowsSDK) []Search {
}
fn fileExists(path: []const u8) !bool {
- if (std.os.File.access(path)) |_| {
+ if (fs.File.access(path)) |_| {
return true;
} else |err| switch (err) {
- error.FileNotFound, error.PermissionDenied => return false,
+ error.FileNotFound => return false,
else => return error.FileSystem,
}
}
diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig
index 5689ee7925..a47dbbbe92 100644
--- a/src-self-hosted/link.zig
+++ b/src-self-hosted/link.zig
@@ -302,7 +302,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
try ctx.args.append(c"--allow-shlib-undefined");
}
- if (ctx.comp.target.getOs() == builtin.Os.zen) {
+ if (ctx.comp.target.getOs() == .zen) {
try ctx.args.append(c"-e");
try ctx.args.append(c"_start");
@@ -311,7 +311,7 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
}
fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void {
- const full_path = try std.os.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename });
+ const full_path = try std.fs.path.join(&ctx.arena.allocator, [][]const u8{ dirname, basename });
const full_path_with_null = try std.cstr.addNullByte(&ctx.arena.allocator, full_path);
try ctx.args.append(full_path_with_null.ptr);
}
@@ -668,7 +668,7 @@ const DarwinPlatform = struct {
break :blk ver;
},
Compilation.DarwinVersionMin.None => blk: {
- assert(comp.target.getOs() == builtin.Os.macosx);
+ assert(comp.target.getOs() == .macosx);
result.kind = Kind.MacOS;
break :blk "10.10";
},
diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig
index cbbf73f3f5..4095caa8c4 100644
--- a/src-self-hosted/main.zig
+++ b/src-self-hosted/main.zig
@@ -4,7 +4,9 @@ const builtin = @import("builtin");
const event = std.event;
const os = std.os;
const io = std.io;
+const fs = std.fs;
const mem = std.mem;
+const process = std.process;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const Buffer = std.Buffer;
@@ -20,9 +22,9 @@ const Target = @import("target.zig").Target;
const errmsg = @import("errmsg.zig");
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
-var stderr_file: os.File = undefined;
-var stderr: *io.OutStream(os.File.WriteError) = undefined;
-var stdout: *io.OutStream(os.File.WriteError) = undefined;
+var stderr_file: fs.File = undefined;
+var stderr: *io.OutStream(fs.File.WriteError) = undefined;
+var stdout: *io.OutStream(fs.File.WriteError) = undefined;
pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
@@ -62,14 +64,14 @@ pub fn main() !void {
var stderr_out_stream = stderr_file.outStream();
stderr = &stderr_out_stream.stream;
- const args = try os.argsAlloc(allocator);
+ const args = try process.argsAlloc(allocator);
// TODO I'm getting unreachable code here, which shouldn't happen
- //defer os.argsFree(allocator, args);
+ //defer process.argsFree(allocator, args);
if (args.len <= 1) {
try stderr.write("expected command argument\n\n");
try stderr.write(usage);
- os.exit(1);
+ process.exit(1);
}
const commands = []Command{
@@ -125,7 +127,7 @@ pub fn main() !void {
try stderr.print("unknown command: {}\n\n", args[1]);
try stderr.write(usage);
- os.exit(1);
+ process.exit(1);
}
const usage_build_generic =
@@ -256,7 +258,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
if (flags.present("help")) {
try stdout.write(usage_build_generic);
- os.exit(0);
+ process.exit(0);
}
const build_mode = blk: {
@@ -324,14 +326,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
cur_pkg = parent;
} else {
try stderr.print("encountered --pkg-end with no matching --pkg-begin\n");
- os.exit(1);
+ process.exit(1);
}
}
}
if (cur_pkg.parent != null) {
try stderr.print("unmatched --pkg-begin\n");
- os.exit(1);
+ process.exit(1);
}
const provided_name = flags.single("name");
@@ -340,18 +342,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
1 => flags.positionals.at(0),
else => {
try stderr.print("unexpected extra parameter: {}\n", flags.positionals.at(1));
- os.exit(1);
+ process.exit(1);
},
};
const root_name = if (provided_name) |n| n else blk: {
if (root_source_file) |file| {
- const basename = os.path.basename(file);
+ const basename = fs.path.basename(file);
var it = mem.separate(basename, ".");
break :blk it.next() orelse basename;
} else {
try stderr.write("--name [name] not provided and unable to infer\n");
- os.exit(1);
+ process.exit(1);
}
};
@@ -361,12 +363,12 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
const link_objects = flags.many("object");
if (root_source_file == null and link_objects.len == 0 and assembly_files.len == 0) {
try stderr.write("Expected source file argument or at least one --object or --assembly argument\n");
- os.exit(1);
+ process.exit(1);
}
if (out_type == Compilation.Kind.Obj and link_objects.len != 0) {
try stderr.write("When building an object file, --object arguments are invalid\n");
- os.exit(1);
+ process.exit(1);
}
var clang_argv_buf = ArrayList([]const u8).init(allocator);
@@ -379,7 +381,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
}
try ZigCompiler.setLlvmArgv(allocator, mllvm_flags);
- const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
+ const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch process.exit(1);
defer allocator.free(zig_lib_dir);
var override_libc: LibCInstallation = undefined;
@@ -448,7 +450,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
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);
+ process.exit(1);
}
if (flags.single("mmacosx-version-min")) |ver| {
@@ -478,16 +480,16 @@ async fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
switch (build_event) {
Compilation.Event.Ok => {
- stderr.print("Build {} succeeded\n", count) catch os.exit(1);
+ stderr.print("Build {} succeeded\n", count) catch process.exit(1);
},
Compilation.Event.Error => |err| {
- stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch os.exit(1);
+ stderr.print("Build {} failed: {}\n", count, @errorName(err)) catch process.exit(1);
},
Compilation.Event.Fail => |msgs| {
- stderr.print("Build {} compile errors:\n", count) catch os.exit(1);
+ stderr.print("Build {} compile errors:\n", count) catch process.exit(1);
for (msgs) |msg| {
defer msg.destroy();
- msg.printToFile(stderr_file, color) catch os.exit(1);
+ msg.printToFile(stderr_file, color) catch process.exit(1);
}
},
}
@@ -550,8 +552,8 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil
"Try running `zig libc` to see an example for the native target.\n",
libc_paths_file,
@errorName(err),
- ) catch os.exit(1);
- os.exit(1);
+ ) catch process.exit(1);
+ process.exit(1);
};
}
@@ -565,7 +567,7 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
},
else => {
try stderr.print("unexpected extra parameter: {}\n", args[1]);
- os.exit(1);
+ process.exit(1);
},
}
@@ -584,10 +586,10 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
async fn findLibCAsync(zig_compiler: *ZigCompiler) void {
const libc = (await (async zig_compiler.getNativeLibC() catch unreachable)) catch |err| {
- stderr.print("unable to find libc: {}\n", @errorName(err)) catch os.exit(1);
- os.exit(1);
+ stderr.print("unable to find libc: {}\n", @errorName(err)) catch process.exit(1);
+ process.exit(1);
};
- libc.render(stdout) catch os.exit(1);
+ libc.render(stdout) catch process.exit(1);
}
fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
@@ -596,7 +598,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
if (flags.present("help")) {
try stdout.write(usage_fmt);
- os.exit(0);
+ process.exit(0);
}
const color = blk: {
@@ -616,7 +618,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
if (flags.present("stdin")) {
if (flags.positionals.len != 0) {
try stderr.write("cannot use --stdin with positional arguments\n");
- os.exit(1);
+ process.exit(1);
}
var stdin_file = try io.getStdIn();
@@ -627,7 +629,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
const tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing stdin: {}\n", err);
- os.exit(1);
+ process.exit(1);
};
defer tree.deinit();
@@ -639,12 +641,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
try msg.printToFile(stderr_file, color);
}
if (tree.errors.len != 0) {
- os.exit(1);
+ process.exit(1);
}
if (flags.present("check")) {
const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree);
const code = if (anything_changed) u8(1) else u8(0);
- os.exit(code);
+ process.exit(code);
}
_ = try std.zig.render(allocator, stdout, tree);
@@ -653,7 +655,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
if (flags.positionals.len == 0) {
try stderr.write("expected at least one source file argument\n");
- os.exit(1);
+ process.exit(1);
}
var loop: event.Loop = undefined;
@@ -700,7 +702,8 @@ const FmtError = error{
ReadOnlyFileSystem,
LinkQuotaExceeded,
FileBusy,
-} || os.File.OpenError;
+ CurrentWorkingDirectoryUnlinked,
+} || fs.File.OpenError;
async fn asyncFmtMain(
loop: *event.Loop,
@@ -725,7 +728,7 @@ async fn asyncFmtMain(
}
try await (async group.wait() catch unreachable);
if (fmt.any_error) {
- os.exit(1);
+ process.exit(1);
}
}
@@ -747,13 +750,13 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
)) catch |err| switch (err) {
error.IsDir, error.AccessDenied => {
// TODO make event based (and dir.next())
- var dir = try std.os.Dir.open(fmt.loop.allocator, file_path);
+ var dir = try fs.Dir.open(fmt.loop.allocator, file_path);
defer dir.close();
var group = event.Group(FmtError!void).init(fmt.loop);
while (try dir.next()) |entry| {
- if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
- const full_path = try os.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name });
+ if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
+ const full_path = try fs.path.join(fmt.loop.allocator, [][]const u8{ file_path, entry.name });
try group.call(fmtPath, fmt, full_path, check_mode);
}
}
@@ -849,7 +852,7 @@ fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void {
}
fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
- try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING));
+ try stdout.print("{}\n", std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING));
}
const args_test_spec = []Flag{Flag.Bool("--help")};
@@ -891,7 +894,7 @@ const usage_internal =
fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
if (args.len == 0) {
try stderr.write(usage_internal);
- os.exit(1);
+ process.exit(1);
}
const sub_commands = []Command{Command{
@@ -922,14 +925,14 @@ fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
\\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),
+ std.mem.toSliceConst(u8, c.ZIG_CMAKE_BINARY_DIR),
+ std.mem.toSliceConst(u8, c.ZIG_CXX_COMPILER),
+ std.mem.toSliceConst(u8, c.ZIG_LLVM_CONFIG_EXE),
+ std.mem.toSliceConst(u8, c.ZIG_LLD_INCLUDE_PATH),
+ std.mem.toSliceConst(u8, c.ZIG_LLD_LIBRARIES),
+ std.mem.toSliceConst(u8, c.ZIG_STD_FILES),
+ std.mem.toSliceConst(u8, c.ZIG_C_HEADER_FILES),
+ std.mem.toSliceConst(u8, c.ZIG_DIA_GUIDS_LIB),
);
}
diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig
index 7a44e8f3a0..dd1c137c74 100644
--- a/src-self-hosted/stage1.zig
+++ b/src-self-hosted/stage1.zig
@@ -1,8 +1,24 @@
// This is Zig code that is used by both stage1 and stage2.
// The prototypes in src/userland.h must match these definitions.
-const std = @import("std");
const builtin = @import("builtin");
+const std = @import("std");
+const io = std.io;
+const mem = std.mem;
+const fs = std.fs;
+const process = std.process;
+const Allocator = mem.Allocator;
+const ArrayList = std.ArrayList;
+const Buffer = std.Buffer;
+const arg = @import("arg.zig");
+const self_hosted_main = @import("main.zig");
+const Args = arg.Args;
+const Flag = arg.Flag;
+const errmsg = @import("errmsg.zig");
+
+var stderr_file: fs.File = undefined;
+var stderr: *io.OutStream(fs.File.WriteError) = undefined;
+var stdout: *io.OutStream(fs.File.WriteError) = undefined;
// ABI warning
export fn stage2_zen(ptr: *[*]const u8, len: *usize) void {
@@ -157,7 +173,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
if (flags.present("help")) {
try stdout.write(self_hosted_main.usage_fmt);
- os.exit(0);
+ process.exit(0);
}
const color = blk: {
@@ -177,7 +193,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
if (flags.present("stdin")) {
if (flags.positionals.len != 0) {
try stderr.write("cannot use --stdin with positional arguments\n");
- os.exit(1);
+ process.exit(1);
}
var stdin_file = try io.getStdIn();
@@ -188,7 +204,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
const tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing stdin: {}\n", err);
- os.exit(1);
+ process.exit(1);
};
defer tree.deinit();
@@ -197,12 +213,12 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
try printErrMsgToFile(allocator, parse_error, tree, "<stdin>", stderr_file, color);
}
if (tree.errors.len != 0) {
- os.exit(1);
+ process.exit(1);
}
if (flags.present("check")) {
const anything_changed = try std.zig.render(allocator, io.null_out_stream, tree);
const code = if (anything_changed) u8(1) else u8(0);
- os.exit(code);
+ process.exit(code);
}
_ = try std.zig.render(allocator, stdout, tree);
@@ -211,7 +227,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
if (flags.positionals.len == 0) {
try stderr.write("expected at least one source file argument\n");
- os.exit(1);
+ process.exit(1);
}
var fmt = Fmt{
@@ -227,7 +243,7 @@ fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void {
try fmtPath(&fmt, file_path, check_mode);
}
if (fmt.any_error) {
- os.exit(1);
+ process.exit(1);
}
}
@@ -250,7 +266,7 @@ const FmtError = error{
ReadOnlyFileSystem,
LinkQuotaExceeded,
FileBusy,
-} || os.File.OpenError;
+} || fs.File.OpenError;
fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {
const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);
@@ -261,12 +277,12 @@ fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void
const source_code = io.readFileAlloc(fmt.allocator, file_path) catch |err| switch (err) {
error.IsDir, error.AccessDenied => {
// TODO make event based (and dir.next())
- var dir = try std.os.Dir.open(fmt.allocator, file_path);
+ var dir = try fs.Dir.open(fmt.allocator, file_path);
defer dir.close();
while (try dir.next()) |entry| {
- if (entry.kind == std.os.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
- const full_path = try os.path.join(fmt.allocator, [][]const u8{ file_path, entry.name });
+ if (entry.kind == fs.Dir.Entry.Kind.Directory or mem.endsWith(u8, entry.name, ".zig")) {
+ const full_path = try fs.path.join(fmt.allocator, [][]const u8{ file_path, entry.name });
try fmtPath(fmt, full_path, check_mode);
}
}
@@ -329,7 +345,7 @@ fn printErrMsgToFile(
parse_error: *const ast.Error,
tree: *ast.Tree,
path: []const u8,
- file: os.File,
+ file: fs.File,
color: errmsg.Color,
) !void {
const color_on = switch (color) {
@@ -377,20 +393,3 @@ fn printErrMsgToFile(
try stream.writeByteNTimes('~', last_token.end - first_token.start);
try stream.write("\n");
}
-
-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 self_hosted_main = @import("main.zig");
-const Args = arg.Args;
-const Flag = arg.Flag;
-const errmsg = @import("errmsg.zig");
-
-var stderr_file: os.File = undefined;
-var stderr: *io.OutStream(os.File.WriteError) = undefined;
-var stdout: *io.OutStream(os.File.WriteError) = undefined;
diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig
index 4be6d53932..526518ca47 100644
--- a/src-self-hosted/test.zig
+++ b/src-self-hosted/test.zig
@@ -55,12 +55,12 @@ pub const TestContext = struct {
self.zig_lib_dir = try introspect.resolveZigLibDir(allocator);
errdefer allocator.free(self.zig_lib_dir);
- try std.os.makePath(allocator, tmp_dir_name);
- errdefer std.os.deleteTree(allocator, tmp_dir_name) catch {};
+ try std.fs.makePath(allocator, tmp_dir_name);
+ errdefer std.fs.deleteTree(allocator, tmp_dir_name) catch {};
}
fn deinit(self: *TestContext) void {
- std.os.deleteTree(allocator, tmp_dir_name) catch {};
+ std.fs.deleteTree(allocator, tmp_dir_name) catch {};
allocator.free(self.zig_lib_dir);
self.zig_compiler.deinit();
self.loop.deinit();
@@ -87,10 +87,10 @@ pub const TestContext = struct {
) !void {
var file_index_buf: [20]u8 = undefined;
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
- const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
+ const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
- if (std.os.path.dirname(file1_path)) |dirname| {
- try std.os.makePath(allocator, dirname);
+ if (std.fs.path.dirname(file1_path)) |dirname| {
+ try std.fs.makePath(allocator, dirname);
}
// TODO async I/O
@@ -120,11 +120,11 @@ pub const TestContext = struct {
) !void {
var file_index_buf: [20]u8 = undefined;
const file_index = try std.fmt.bufPrint(file_index_buf[0..], "{}", self.file_index.incr());
- const file1_path = try std.os.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
+ const file1_path = try std.fs.path.join(allocator, [][]const u8{ tmp_dir_name, file_index, file1 });
const output_file = try std.fmt.allocPrint(allocator, "{}-out{}", file1_path, Target(Target.Native).exeFileExt());
- if (std.os.path.dirname(file1_path)) |dirname| {
- try std.os.makePath(allocator, dirname);
+ if (std.fs.path.dirname(file1_path)) |dirname| {
+ try std.fs.makePath(allocator, dirname);
}
// TODO async I/O
@@ -164,9 +164,9 @@ pub const TestContext = struct {
Compilation.Event.Ok => {
const argv = []const []const u8{exe_file_2};
// TODO use event loop
- const child = try std.os.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
+ const child = try std.ChildProcess.exec(allocator, argv, null, null, 1024 * 1024);
switch (child.term) {
- std.os.ChildProcess.Term.Exited => |code| {
+ .Exited => |code| {
if (code != 0) {
return error.BadReturnCode;
}
diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig
index fe5022c0c1..3b60e65753 100644
--- a/src-self-hosted/translate_c.zig
+++ b/src-self-hosted/translate_c.zig
@@ -152,10 +152,21 @@ pub fn translate(
var tree_arena = std.heap.ArenaAllocator.init(backing_allocator);
errdefer tree_arena.deinit();
- var arena = &tree_arena.allocator;
- const root_node = try arena.create(ast.Node.Root);
- root_node.* = ast.Node.Root{
+ const tree = try tree_arena.allocator.create(ast.Tree);
+ tree.* = ast.Tree{
+ .source = undefined, // need to use Buffer.toOwnedSlice later
+ .root_node = undefined,
+ .arena_allocator = tree_arena,
+ .tokens = undefined, // can't reference the allocator yet
+ .errors = undefined, // can't reference the allocator yet
+ };
+ const arena = &tree.arena_allocator.allocator; // now we can reference the allocator
+ tree.tokens = ast.Tree.TokenList.init(arena);
+ tree.errors = ast.Tree.ErrorList.init(arena);
+
+ tree.root_node = try arena.create(ast.Node.Root);
+ tree.root_node.* = ast.Node.Root{
.base = ast.Node{ .id = ast.Node.Id.Root },
.decls = ast.Node.Root.DeclList.init(arena),
.doc_comments = null,
@@ -163,17 +174,6 @@ pub fn translate(
.eof_token = undefined,
};
- const tree = try arena.create(ast.Tree);
- tree.* = ast.Tree{
- .source = undefined, // need to use Buffer.toOwnedSlice later
- .root_node = root_node,
- .arena_allocator = undefined,
- .tokens = ast.Tree.TokenList.init(arena),
- .errors = ast.Tree.ErrorList.init(arena),
- };
- tree.arena_allocator = tree_arena;
- arena = &tree.arena_allocator.allocator;
-
var source_buffer = try std.Buffer.initSize(arena, 0);
var context = Context{