From f1b64e4b96bc943d194e2e107b59b1c74cde911e Mon Sep 17 00:00:00 2001 From: Giovanni Date: Sat, 15 Sep 2018 20:06:26 +0900 Subject: [PATCH 1/2] improve the compiler option "cppCompileToNamespace", a custom namespace can now be set --- compiler/cgen.nim | 19 ++++++++++++------- compiler/commands.nim | 6 +++++- compiler/options.nim | 1 + doc/advopt.txt | 4 +++- lib/nimbase.h | 2 +- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 7a74d8a9be..95ac3999f0 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -679,8 +679,10 @@ proc generateHeaders(m: BModule) = add(m.s[cfsHeaders], "#undef powerpc\L") add(m.s[cfsHeaders], "#undef unix\L") -proc openNamespaceNim(): Rope = - result.add("namespace Nim {\L") +proc openNamespaceNim(namespace: string): Rope = + result.add("namespace ") + result.add(namespace) + result.add(" {\L") proc closeNamespaceNim(): Rope = result.add("}\L") @@ -1036,7 +1038,10 @@ proc addIntTypes(result: var Rope; conf: ConfigRef) {.inline.} = addf(result, "#define NIM_NEW_MANGLING_RULES\L" & "#define NIM_INTBITS $1\L", [ platform.CPU[conf.target.targetCPU].intSize.rope]) - if optUseNimNamespace in conf.globalOptions: result.add("#define USE_NIM_NAMESPACE\L") + if optUseNimNamespace in conf.globalOptions: + result.add("#define USE_NIM_NAMESPACE ") + result.add(conf.cppCustomNamespace) + result.add("\L") proc getCopyright(conf: ConfigRef; cfile: Cfile): Rope = if optCompileOnly in conf.globalOptions: @@ -1210,10 +1215,10 @@ proc genMainProc(m: BModule) = [m.g.mainModInit, initStackBottomCall, rope(m.labels)]) if optNoMain notin m.config.globalOptions: if optUseNimNamespace in m.config.globalOptions: - m.s[cfsProcs].add closeNamespaceNim() & "using namespace Nim;\L" + m.s[cfsProcs].add closeNamespaceNim() & "using namespace " & m.config.cppCustomNamespace & ";\L" appcg(m, m.s[cfsProcs], otherMain, []) - if optUseNimNamespace in m.config.globalOptions: m.s[cfsProcs].add openNamespaceNim() + if optUseNimNamespace in m.config.globalOptions: m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace) proc getSomeInitName(m: PSym, suffix: string): Rope = assert m.kind == skModule @@ -1336,7 +1341,7 @@ proc genModule(m: BModule, cfile: Cfile): Rope = add(result, m.s[i]) add(result, genSectionEnd(i, m.config)) if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: - result.add openNamespaceNim() + result.add openNamespaceNim(m.config.cppCustomNamespace) add(result, m.s[cfsInitProc]) if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim() @@ -1469,7 +1474,7 @@ proc writeHeader(m: BModule) = add(result, genSectionStart(i, m.config)) add(result, m.s[i]) add(result, genSectionEnd(i, m.config)) - if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: result.add openNamespaceNim() + if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: result.add openNamespaceNim(m.config.cppCustomNamespace) add(result, m.s[cfsInitProc]) if optGenDynLib in m.config.globalOptions: diff --git a/compiler/commands.nim b/compiler/commands.nim index fe820a5892..0b3bc1b2de 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -735,7 +735,11 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; of "nep1": processOnOffSwitchG(conf, {optCheckNep1}, arg, pass, info) of "cppcompiletonamespace": - expectNoArg(conf, switch, arg, pass, info) + if conf != nil: + if arg != "": + conf.cppCustomNamespace = arg + else: + conf.cppCustomNamespace = "Nim" incl conf.globalOptions, optUseNimNamespace defineSymbol(conf.symbols, "cppCompileToNamespace") else: diff --git a/compiler/options.nim b/compiler/options.nim index a95d9930a4..91066d607c 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -242,6 +242,7 @@ type writelnHook*: proc (output: string) {.closure.} structuredErrorHook*: proc (config: ConfigRef; info: TLineInfo; msg: string; severity: Severity) {.closure.} + cppCustomNamespace*: string template depConfigFields*(fn) {.dirty.} = fn(target) diff --git a/doc/advopt.txt b/doc/advopt.txt index 150025509a..a1b709f04b 100644 --- a/doc/advopt.txt +++ b/doc/advopt.txt @@ -81,7 +81,9 @@ Advanced options: --NimblePath:PATH add a path for Nimble support --noNimblePath deactivate the Nimble path --noCppExceptions use default exception handling with C++ backend - --cppCompileToNamespace use namespace "Nim" for the generated C++ code + --cppCompileToNamespace:namespace + use the provided namespace for the generated C++ code, + if no namespace is provided "Nim" will be used --excludePath:PATH exclude a path from the list of search paths --dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL has no effect and can be statically linked instead; diff --git a/lib/nimbase.h b/lib/nimbase.h index 5071087125..c04d378c54 100644 --- a/lib/nimbase.h +++ b/lib/nimbase.h @@ -268,7 +268,7 @@ __clang__ /* wrap all Nim typedefs into namespace Nim */ #ifdef USE_NIM_NAMESPACE -namespace Nim { +namespace USE_NIM_NAMESPACE { #endif /* bool types (C++ has it): */ From e21c3028cb974b94313bb97a178294f4bea95b46 Mon Sep 17 00:00:00 2001 From: Giovanni Date: Sun, 16 Sep 2018 08:41:44 +0900 Subject: [PATCH 2/2] Removed optUseNimNamespace, removed useless nil check --- compiler/cgen.nim | 14 +++++++------- compiler/commands.nim | 12 +++++------- compiler/options.nim | 1 - 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 95ac3999f0..2db92bc215 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1038,7 +1038,7 @@ proc addIntTypes(result: var Rope; conf: ConfigRef) {.inline.} = addf(result, "#define NIM_NEW_MANGLING_RULES\L" & "#define NIM_INTBITS $1\L", [ platform.CPU[conf.target.targetCPU].intSize.rope]) - if optUseNimNamespace in conf.globalOptions: + if conf.cppCustomNamespace.len > 0: result.add("#define USE_NIM_NAMESPACE ") result.add(conf.cppCustomNamespace) result.add("\L") @@ -1214,11 +1214,11 @@ proc genMainProc(m: BModule) = appcg(m, m.s[cfsProcs], nimMain, [m.g.mainModInit, initStackBottomCall, rope(m.labels)]) if optNoMain notin m.config.globalOptions: - if optUseNimNamespace in m.config.globalOptions: + if m.config.cppCustomNamespace.len > 0: m.s[cfsProcs].add closeNamespaceNim() & "using namespace " & m.config.cppCustomNamespace & ";\L" appcg(m, m.s[cfsProcs], otherMain, []) - if optUseNimNamespace in m.config.globalOptions: m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace) + if m.config.cppCustomNamespace.len > 0: m.s[cfsProcs].add openNamespaceNim(m.config.cppCustomNamespace) proc getSomeInitName(m: PSym, suffix: string): Rope = assert m.kind == skModule @@ -1340,10 +1340,10 @@ proc genModule(m: BModule, cfile: Cfile): Rope = add(result, genSectionStart(i, m.config)) add(result, m.s[i]) add(result, genSectionEnd(i, m.config)) - if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: + if m.config.cppCustomNamespace.len > 0 and i == cfsHeaders: result.add openNamespaceNim(m.config.cppCustomNamespace) add(result, m.s[cfsInitProc]) - if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim() + if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim() proc newPreInitProc(m: BModule): BProc = result = newProc(nil, m) @@ -1474,13 +1474,13 @@ proc writeHeader(m: BModule) = add(result, genSectionStart(i, m.config)) add(result, m.s[i]) add(result, genSectionEnd(i, m.config)) - if optUseNimNamespace in m.config.globalOptions and i == cfsHeaders: result.add openNamespaceNim(m.config.cppCustomNamespace) + if m.config.cppCustomNamespace.len > 0 and i == cfsHeaders: result.add openNamespaceNim(m.config.cppCustomNamespace) add(result, m.s[cfsInitProc]) if optGenDynLib in m.config.globalOptions: result.add("N_LIB_IMPORT ") result.addf("N_CDECL(void, NimMain)(void);$n", []) - if optUseNimNamespace in m.config.globalOptions: result.add closeNamespaceNim() + if m.config.cppCustomNamespace.len > 0: result.add closeNamespaceNim() result.addf("#endif /* $1 */$n", [guard]) if not writeRope(result, m.filename): rawMessage(m.config, errCannotOpenFile, m.filename.string) diff --git a/compiler/commands.nim b/compiler/commands.nim index 0b3bc1b2de..39967c4bc3 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -735,13 +735,11 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; of "nep1": processOnOffSwitchG(conf, {optCheckNep1}, arg, pass, info) of "cppcompiletonamespace": - if conf != nil: - if arg != "": - conf.cppCustomNamespace = arg - else: - conf.cppCustomNamespace = "Nim" - incl conf.globalOptions, optUseNimNamespace - defineSymbol(conf.symbols, "cppCompileToNamespace") + if arg.len > 0: + conf.cppCustomNamespace = arg + else: + conf.cppCustomNamespace = "Nim" + defineSymbol(conf.symbols, "cppCompileToNamespace", conf.cppCustomNamespace) else: if strutils.find(switch, '.') >= 0: options.setConfigVar(conf, switch, arg) else: invalidCmdLineOption(conf, pass, switch, info) diff --git a/compiler/options.nim b/compiler/options.nim index 91066d607c..b4d2bb64e1 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -78,7 +78,6 @@ type # please make sure we have under 32 options optListFullPaths optNoNimblePath optDynlibOverrideAll - optUseNimNamespace TGlobalOptions* = set[TGlobalOption]