From 2c904039fa7efac5603802f03c54238d9df5b7f6 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Sun, 1 Jul 2018 10:29:59 +0900 Subject: [PATCH] Make main functions static so they are visible only within main module --- compiler/cgen.nim | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 928d1fd22..4cbc3d3b1 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -953,13 +953,13 @@ proc genMainProc(m: BModule) = # prevents inlining of the NimMainInner function and dependent # functions, which might otherwise merge their stack frames. PreMainBody = - "void PreMainInner(void) {$N" & + "static void PreMainInner(void) {$N" & "\tsystemInit000();$N" & "$1" & "$2" & "$3" & "}$N$N" & - "void PreMain(void) {$N" & + "static void PreMain(void) {$N" & "\tvoid (*volatile inner)(void);$N" & "\tsystemDatInit000();$N" & "\tinner = PreMainInner;$N" & @@ -973,12 +973,12 @@ proc genMainProc(m: BModule) = MainProcsWithResult = MainProcs & "\treturn nim_program_result;$N" - NimMainInner = "N_CDECL(void, NimMainInner)(void) {$N" & + NimMainInner = "N_CDECL(static void, NimMainInner)(void) {$N" & "$1" & "}$N$N" NimMainProc = - "N_CDECL(void, NimMain)(void) {$N" & + "N_CDECL(static void, NimMain)(void) {$N" & "\tvoid (*volatile inner)(void);$N" & "\tPreMain();$N" & "\tinner = NimMainInner;$N" & @@ -1026,7 +1026,7 @@ proc genMainProc(m: BModule) = PosixNimDllMain = WinNimDllMain PosixCDllMain = - "void NIM_POSIX_INIT NimMainInit(void) {$N" & + "static void NIM_POSIX_INIT NimMainInit(void) {$N" & MainProcs & "}$N$N" @@ -1117,7 +1117,10 @@ proc registerModuleToMain(g: BModuleList; m: PSym) = var init = m.getInitName datInit = m.getDatInitName - addf(g.mainModProcs, "N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [init]) + if sfMainModule in m.flags: + addf(g.mainModProcs, "N_LIB_PRIVATE N_NIMCALL(static void, $1)(void);$N", [init]) + else: + addf(g.mainModProcs, "N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [init]) addf(g.mainModProcs, "N_LIB_PRIVATE N_NIMCALL(void, $1)(void);$N", [datInit]) if sfSystemModule notin m.flags: addf(g.mainDatInit, "\t$1();$N", [datInit]) @@ -1129,7 +1132,11 @@ proc registerModuleToMain(g: BModuleList; m: PSym) = proc genInitCode(m: BModule) = var initname = getInitName(m.module) - var prc = "N_LIB_PRIVATE N_NIMCALL(void, $1)(void) {$N" % [initname] + var prc: Rope + if sfMainModule in m.module.flags: + prc = "N_LIB_PRIVATE N_NIMCALL(static void, $1)(void) {$N" % [initname] + else: + prc = "N_LIB_PRIVATE N_NIMCALL(void, $1)(void) {$N" % [initname] if m.typeNodes > 0: appcg(m, m.s[cfsTypeInit1], "static #TNimNode $1[$2];$n", [m.typeNodesName, rope(m.typeNodes)])