From 6a046c1bcda1d02d4f9a05ea125ef56b50cf6e7c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 3 Dec 2019 12:29:55 -0500 Subject: [PATCH] activate start code when pub main exists and rename LinkType->LinkMode, OutType->OutputMode --- lib/std/builtin.zig | 5 ++-- lib/std/special/start.zig | 51 ++++++++++++++++----------------------- src/codegen.cpp | 7 +++--- test/compile_errors.zig | 16 ++++++------ 4 files changed, 34 insertions(+), 45 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 789880dd8..b073d7300 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -350,8 +350,7 @@ pub const Endian = enum { /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. -pub const OutType = enum { - Unknown, +pub const OutputMode = enum { Exe, Lib, Obj, @@ -359,7 +358,7 @@ pub const OutType = enum { /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. -pub const LinkType = enum { +pub const LinkMode = enum { Static, Dynamic, }; diff --git a/lib/std/special/start.zig b/lib/std/special/start.zig index 33ecc6f1f..5d168490f 100644 --- a/lib/std/special/start.zig +++ b/lib/std/special/start.zig @@ -19,37 +19,28 @@ const is_mips = switch (builtin.arch) { }; comptime { - switch (builtin.output_type) { - .Unknown => unreachable, - .Exe => { - if (builtin.link_libc) { - if (@hasDecl(root, "main") and - @typeInfo(@typeOf(root.main)).Fn.calling_convention != .C) - { - @export("main", main, .Weak); - } - } else if (builtin.os == .windows) { - if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) { - @export("WinMainCRTStartup", WinMainCRTStartup, .Strong); - } - } else if (is_wasm and builtin.os == .freestanding) { - if (!@hasDecl(root, "_start")) @export("_start", wasm_freestanding_start, .Strong); - } else if (builtin.os == .uefi) { - if (!@hasDecl(root, "EfiMain")) @export("EfiMain", EfiMain, .Strong); - } else if (is_mips) { - if (!@hasDecl(root, "__start")) @export("__start", _start, .Strong); - } else { - if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong); + if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) { + if (builtin.os == .windows and !@hasDecl(root, "_DllMainCRTStartup")) { + @export("_DllMainCRTStartup", _DllMainCRTStartup, .Strong); + } + } else if (builtin.output_mode == .Exe or @hasDecl(root, "main")) { + if (builtin.link_libc and @hasDecl(root, "main") and + @typeInfo(@typeOf(root.main)).Fn.calling_convention != .C) + { + @export("main", main, .Weak); + } else if (builtin.os == .windows) { + if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup")) { + @export("WinMainCRTStartup", WinMainCRTStartup, .Strong); } - }, - .Lib => { - if (builtin.os == .windows and builtin.link_type == .Dynamic and - !@hasDecl(root, "_DllMainCRTStartup")) - { - @export("_DllMainCRTStartup", _DllMainCRTStartup, .Strong); - } - }, - .Obj => {}, + } else if (is_wasm and builtin.os == .freestanding) { + if (!@hasDecl(root, "_start")) @export("_start", wasm_freestanding_start, .Strong); + } else if (builtin.os == .uefi) { + if (!@hasDecl(root, "EfiMain")) @export("EfiMain", EfiMain, .Strong); + } else if (is_mips) { + if (!@hasDecl(root, "__start")) @export("__start", _start, .Strong); + } else { + if (!@hasDecl(root, "_start")) @export("_start", _start, .Strong); + } } } diff --git a/src/codegen.cpp b/src/codegen.cpp index f41ea7aba..8ba061333 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -8378,8 +8378,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { const char *out_type = nullptr; switch (g->out_type) { case OutTypeUnknown: - out_type = "Unknown"; - break; + zig_unreachable(); case OutTypeExe: out_type = "Exe"; break; @@ -8390,9 +8389,9 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { out_type = "Obj"; break; } - buf_appendf(contents, "pub const output_type = OutType.%s;\n", out_type); + buf_appendf(contents, "pub const output_mode = OutputMode.%s;\n", out_type); const char *link_type = g->is_dynamic ? "Dynamic" : "Static"; - buf_appendf(contents, "pub const link_type = LinkType.%s;\n", link_type); + buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", link_type); buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build)); buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded)); buf_appendf(contents, "pub const os = Os.%s;\n", cur_os); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 7e16f858e..d3d439aea 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -156,7 +156,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:9:27: error: @atomicRmw on enum only works with .Xchg", ); - cases.addExe( + cases.add( "disallow coercion from non-null-terminated pointer to null-terminated pointer", \\extern fn puts(s: [*:0]const u8) c_int; \\pub fn main() void { @@ -876,7 +876,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:32: note: cast discards const qualifier", ); - cases.addExe( + cases.add( "overflow in enum value allocation", \\const Moo = enum(u8) { \\ Last = 255, @@ -3000,14 +3000,14 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:9: error: encountered @panic at compile-time", ); - cases.addExe( + cases.add( "wrong return type for main", \\pub fn main() f32 { } , "error: expected return type of main to be 'void', '!void', 'noreturn', 'u8', or '!u8'", ); - cases.addExe( + cases.add( "double ?? on main return value", \\pub fn main() ??void { \\} @@ -4900,7 +4900,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:13: error: cannot assign to constant", ); - cases.addExe( + cases.add( "main function with bogus args type", \\pub fn main(args: [][]bogus) !void {} , @@ -5718,7 +5718,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:4:13: error: cannot continue out of defer expression", ); - cases.addExe( + cases.add( "calling a var args function only known at runtime", \\var foos = [_]fn(...) void { foo1, foo2 }; \\ @@ -5732,7 +5732,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:7:9: error: calling a generic function requires compile-time known function value", ); - cases.addExe( + cases.add( "calling a generic function only known at runtime", \\var foos = [_]fn(var) void { foo1, foo2 }; \\ @@ -6850,7 +6850,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:3:23: error: vector element type must be integer, float, bool, or pointer; 'comptime_int' is invalid", ); - cases.addExe("compileLog of tagged enum doesn't crash the compiler", + cases.add("compileLog of tagged enum doesn't crash the compiler", \\const Bar = union(enum(u32)) { \\ X: i32 = 1 \\};