summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorGiovanni <sinkingsugar@gmail.com>2018-09-15 20:06:26 +0900
committerGiovanni <sinkingsugar@gmail.com>2018-09-15 20:10:54 +0900
commitf1b64e4b96bc943d194e2e107b59b1c74cde911e (patch)
tree444a5b56bd479138bebe27098a5d05a199d188bc /compiler
parent4342b79a3c05fcfb1e6fc97152daa0075b0b9603 (diff)
downloadNim-f1b64e4b96bc943d194e2e107b59b1c74cde911e.tar.gz
improve the compiler option "cppCompileToNamespace", a custom namespace can now be set
Diffstat (limited to 'compiler')
-rw-r--r--compiler/cgen.nim19
-rw-r--r--compiler/commands.nim6
-rw-r--r--compiler/options.nim1
3 files changed, 18 insertions, 8 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 7a74d8a9b..95ac3999f 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 fe820a589..0b3bc1b2d 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 a95d9930a..91066d607 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)