summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAdrianV <adrian@veith-system.de>2018-04-15 13:43:01 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-15 13:43:01 +0200
commit0ce28d15cc9aff526a7733e40ae1ae495d4e8e55 (patch)
tree5194210604627fc7117acae8165ca65360638dad
parent70c0ff1be21d2a1fda6432fe2e5b7ae0953840ff (diff)
downloadNim-0ce28d15cc9aff526a7733e40ae1ae495d4e8e55.tar.gz
Nim namespace for cpp (#7453)
- new option --usenamespace to generate nim cpp code in its own namespace Nim
- minor changes for compatibility with the new Embarcadero = Borland C++-Builder (bcc)
-rw-r--r--changelog.md4
-rw-r--r--compiler/cgen.nim15
-rw-r--r--compiler/commands.nim5
-rw-r--r--compiler/extccomp.nim10
-rw-r--r--compiler/options.nim1
-rw-r--r--doc/advopt.txt1
-rw-r--r--lib/nimbase.h13
-rw-r--r--lib/system/atomics.nim2
8 files changed, 44 insertions, 7 deletions
diff --git a/changelog.md b/changelog.md
index 6edcb5b56..20ea58b08 100644
--- a/changelog.md
+++ b/changelog.md
@@ -79,6 +79,10 @@
   target. To use it, compile your code with `--hotCodeReloading:on` and use a
   helper library such as LiveReload or BrowserSync.
 
+- A new compiler option `--cppCompileToNamespace` puts the generated C++ code
+  into the namespace "Nim" in order to avoid naming conflicts with existing
+  C++ code. This is done for all Nim code - internal and exported.
+
 - Added ``macros.getProjectPath`` and ``ospaths.putEnv`` procs to Nim's virtual
   machine.
 
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index 78847c3b8..4d3cabd3a 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -676,6 +676,12 @@ proc generateHeaders(m: BModule) =
   add(m.s[cfsHeaders], "#undef powerpc" & tnl)
   add(m.s[cfsHeaders], "#undef unix" & tnl)
 
+proc openNamespaceNim(): Rope =
+  result.add("namespace Nim {" & tnl)
+  
+proc closeNamespaceNim(): Rope =
+  result.add("}" & tnl)
+
 proc closureSetup(p: BProc, prc: PSym) =
   if tfCapturesEnv notin prc.typ.flags: return
   # prc.ast[paramsPos].last contains the type we're after:
@@ -918,6 +924,7 @@ proc addIntTypes(result: var Rope) {.inline.} =
   addf(result, "#define NIM_NEW_MANGLING_RULES" & tnl &
                "#define NIM_INTBITS $1" & tnl, [
     platform.CPU[targetCPU].intSize.rope])
+  if useNimNamespace : result.add("#define USE_NIM_NAMESPACE" & tnl)
 
 proc getCopyright(cfile: Cfile): Rope =
   if optCompileOnly in gGlobalOptions:
@@ -1083,7 +1090,11 @@ proc genMainProc(m: BModule) =
   appcg(m, m.s[cfsProcs], nimMain,
         [m.g.mainModInit, initStackBottomCall, rope(m.labels)])
   if optNoMain notin gGlobalOptions:
+    if useNimNamespace: 
+      m.s[cfsProcs].add closeNamespaceNim() & "using namespace Nim;" & tnl
+
     appcg(m, m.s[cfsProcs], otherMain, [])
+    if useNimNamespace: m.s[cfsProcs].add openNamespaceNim()
 
 proc getSomeInitName(m: PSym, suffix: string): Rope =
   assert m.kind == skModule
@@ -1191,7 +1202,9 @@ proc genModule(m: BModule, cfile: Cfile): Rope =
     add(result, genSectionStart(i))
     add(result, m.s[i])
     add(result, genSectionEnd(i))
+    if useNimNamespace and i == cfsHeaders: result.add openNamespaceNim()    
   add(result, m.s[cfsInitProc])
+  if useNimNamespace: result.add closeNamespaceNim()
 
 proc newPreInitProc(m: BModule): BProc =
   result = newProc(nil, m)
@@ -1330,11 +1343,13 @@ proc writeHeader(m: BModule) =
     add(result, genSectionStart(i))
     add(result, m.s[i])
     add(result, genSectionEnd(i))
+    if useNimNamespace and i == cfsHeaders: result.add openNamespaceNim()
   add(result, m.s[cfsInitProc])
 
   if optGenDynLib in gGlobalOptions:
     result.add("N_LIB_IMPORT ")
   result.addf("N_CDECL(void, NimMain)(void);$n", [])
+  if useNimNamespace: result.add closeNamespaceNim()
   result.addf("#endif /* $1 */$n", [guard])
   writeRope(result, m.filename)
 
diff --git a/compiler/commands.nim b/compiler/commands.nim
index 20727966b..820cb7e1a 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -698,6 +698,11 @@ proc processSwitch(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
     expectNoArg(switch, arg, pass, info)
     newDestructors = true
     defineSymbol("nimNewRuntime")
+  of "cppcompiletonamespace":
+    expectNoArg(switch, arg, pass, info)
+    useNimNamespace = true
+    defineSymbol("cppCompileToNamespace")
+    
   else:
     if strutils.find(switch, '.') >= 0: options.setConfigVar(switch, arg)
     else: invalidCmdLineOption(pass, switch, info)
diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim
index 8b5a3bf3d..f9b18a335 100644
--- a/compiler/extccomp.nim
+++ b/compiler/extccomp.nim
@@ -176,10 +176,10 @@ compiler bcc:
   result = (
     name: "bcc",
     objExt: "obj",
-    optSpeed: " -O2 -6 ",
+    optSpeed: " -O3 -6 ",
     optSize: " -O1 -6 ",
-    compilerExe: "bcc32",
-    cppCompiler: "",
+    compilerExe: "bcc32c",
+    cppCompiler: "cpp32c",
     compileTmpl: "-c $options $include -o$objfile $file",
     buildGui: " -tW",
     buildDll: " -tWD",
@@ -193,7 +193,9 @@ compiler bcc:
     pic: "",
     asmStmtFrmt: "__asm{$n$1$n}$n",
     structStmtFmt: "$1 $2",
-    props: {hasCpp})
+    props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, 
+            hasAttribute})
+    
 
 # Digital Mars C Compiler
 compiler dmc:
diff --git a/compiler/options.nim b/compiler/options.nim
index 40e97e94f..69a555b3f 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -149,6 +149,7 @@ var
   gExperimentalMode*: bool
   newDestructors*: bool
   gDynlibOverrideAll*: bool
+  useNimNamespace*: bool
 
 type
   SymbolFilesOption* = enum
diff --git a/doc/advopt.txt b/doc/advopt.txt
index 345e20fe4..214ac8dd2 100644
--- a/doc/advopt.txt
+++ b/doc/advopt.txt
@@ -76,6 +76,7 @@ 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 
   --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 a03407c4f..20ac9979b 100644
--- a/lib/nimbase.h
+++ b/lib/nimbase.h
@@ -264,6 +264,11 @@ __clang__
 #  define HAVE_STDINT_H
 #endif
 
+/* wrap all Nim typedefs into namespace Nim */
+#ifdef USE_NIM_NAMESPACE
+namespace Nim {
+#endif
+
 /* bool types (C++ has it): */
 #ifdef __cplusplus
 #  ifndef NIM_TRUE
@@ -413,8 +418,8 @@ typedef struct TStringDesc* string;
 #  endif
 #endif
 
-typedef struct TFrame TFrame;
-struct TFrame {
+typedef struct TFrame_ TFrame;
+struct TFrame_ {
   TFrame* prev;
   NCSTRING procname;
   NI line;
@@ -476,6 +481,10 @@ static inline void GCGuard (void *ptr) { asm volatile ("" :: "X" (ptr)); }
    "error: 'Nim_and_C_compiler_disagree_on_target_architecture' declared as an array with a negative size" */
 typedef int Nim_and_C_compiler_disagree_on_target_architecture[sizeof(NI) == sizeof(void*) && NIM_INTBITS == sizeof(NI)*8 ? 1 : -1];
 
+#ifdef USE_NIM_NAMESPACE
+}
+#endif
+
 #ifdef  __cplusplus
 #  define NIM_EXTERNC extern "C"
 #else
diff --git a/lib/system/atomics.nim b/lib/system/atomics.nim
index afc435638..fa3700190 100644
--- a/lib/system/atomics.nim
+++ b/lib/system/atomics.nim
@@ -295,7 +295,7 @@ else:
 
 when (defined(x86) or defined(amd64)) and defined(vcc):
   proc cpuRelax* {.importc: "YieldProcessor", header: "<windows.h>".}
-elif (defined(x86) or defined(amd64)) and someGcc:
+elif (defined(x86) or defined(amd64)) and (someGcc or defined(bcc)):
   proc cpuRelax* {.inline.} =
     {.emit: """asm volatile("pause" ::: "memory");""".}
 elif someGcc or defined(tcc):
'#n575'>575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992