Skip to content

Commit fd7c7be

Browse files
LemonBoyandrewrk
authored andcommitted
Pick up WinMain with proper CC
1 parent 0d48b60 commit fd7c7be

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

src/analyze.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3333,21 +3333,15 @@ void add_var_export(CodeGen *g, ZigVar *var, const char *symbol_name, GlobalLink
33333333
global_export->linkage = linkage;
33343334
}
33353335

3336-
void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, bool ccc) {
3337-
if (ccc) {
3338-
if (strcmp(symbol_name, "main") == 0 && g->libc_link_lib != nullptr) {
3339-
g->have_c_main = true;
3340-
} else if (strcmp(symbol_name, "WinMain") == 0 &&
3341-
g->zig_target->os == OsWindows)
3342-
{
3336+
void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc) {
3337+
if (cc == CallingConventionC && strcmp(symbol_name, "main") == 0 && g->libc_link_lib != nullptr) {
3338+
g->have_c_main = true;
3339+
} else if (cc == CallingConventionStdcall && g->zig_target->os == OsWindows) {
3340+
if (strcmp(symbol_name, "WinMain") == 0) {
33433341
g->have_winmain = true;
3344-
} else if (strcmp(symbol_name, "WinMainCRTStartup") == 0 &&
3345-
g->zig_target->os == OsWindows)
3346-
{
3342+
} else if (strcmp(symbol_name, "WinMainCRTStartup") == 0) {
33473343
g->have_winmain_crt_startup = true;
3348-
} else if (strcmp(symbol_name, "DllMainCRTStartup") == 0 &&
3349-
g->zig_target->os == OsWindows)
3350-
{
3344+
} else if (strcmp(symbol_name, "DllMainCRTStartup") == 0) {
33513345
g->have_dllmain_crt_startup = true;
33523346
}
33533347
}
@@ -3377,8 +3371,24 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
33773371
}
33783372

33793373
if (fn_proto->is_export) {
3380-
bool ccc = (fn_proto->cc == CallingConventionUnspecified || fn_proto->cc == CallingConventionC);
3381-
add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name), GlobalLinkageIdStrong, ccc);
3374+
switch (fn_proto->cc) {
3375+
case CallingConventionAsync: {
3376+
add_node_error(g, fn_def_node,
3377+
buf_sprintf("exported function cannot be async"));
3378+
} break;
3379+
case CallingConventionC:
3380+
case CallingConventionNaked:
3381+
case CallingConventionCold:
3382+
case CallingConventionStdcall:
3383+
case CallingConventionUnspecified:
3384+
// An exported function without a specific calling
3385+
// convention defaults to C
3386+
CallingConvention cc = (fn_proto->cc != CallingConventionUnspecified) ?
3387+
fn_proto->cc : CallingConventionC;
3388+
add_fn_export(g, fn_table_entry, buf_ptr(&fn_table_entry->symbol_name),
3389+
GlobalLinkageIdStrong, cc);
3390+
break;
3391+
}
33823392
}
33833393

33843394
if (!is_extern) {

src/analyze.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ ZigType *get_align_amt_type(CodeGen *g);
202202
ZigPackage *new_anonymous_package(void);
203203

204204
Buf *const_value_to_buffer(ZigValue *const_val);
205-
void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, bool ccc);
205+
void add_fn_export(CodeGen *g, ZigFn *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage, CallingConvention cc);
206206
void add_var_export(CodeGen *g, ZigVar *fn_table_entry, const char *symbol_name, GlobalLinkageId linkage);
207207

208208

src/ir.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16134,8 +16134,7 @@ static IrInstruction *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructio
1613416134
case CallingConventionNaked:
1613516135
case CallingConventionCold:
1613616136
case CallingConventionStdcall:
16137-
add_fn_export(ira->codegen, fn_entry, buf_ptr(symbol_name), global_linkage_id,
16138-
cc == CallingConventionC);
16137+
add_fn_export(ira->codegen, fn_entry, buf_ptr(symbol_name), global_linkage_id, cc);
1613916138
break;
1614016139
}
1614116140
} break;

test/compile_errors.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub fn addCases(cases: *tests.CompileErrorContext) void {
5+
cases.add(
6+
\\export async fn foo() void {}
7+
, "tmp.zig:1:1: error: exported function cannot be async");
8+
59
cases.addCase(x: {
610
var tc = cases.create("@newStackCall on unsupported target",
711
\\export fn entry() void {

0 commit comments

Comments
 (0)