summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan M Gómez <info@jmgomez.me>2024-03-11 11:10:43 +0000
committerGitHub <noreply@github.com>2024-03-11 12:10:43 +0100
commit93399776c4640875c22c24e95bf8070dfd02d227 (patch)
tree054a2c959fd910d8c24b07439858b8c83f210ce4
parent94c599687796f4ee3872c8aa866827b9ed33f52b (diff)
downloadNim-93399776c4640875c22c24e95bf8070dfd02d227.tar.gz
[C++] Allow `member` to define static funcs (#23387)
-rw-r--r--compiler/ccgtypes.nim13
-rw-r--r--tests/cpp/tmember.nim13
2 files changed, 21 insertions, 5 deletions
diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim
index 83c226c98..baee02d20 100644
--- a/compiler/ccgtypes.nim
+++ b/compiler/ccgtypes.nim
@@ -1193,12 +1193,15 @@ proc isReloadable(m: BModule; prc: PSym): bool =
 proc isNonReloadable(m: BModule; prc: PSym): bool =
   return m.hcrOn and sfNonReloadable in prc.flags
 
-proc parseVFunctionDecl(val: string; name, params, retType, superCall: var string; isFnConst, isOverride, isMemberVirtual: var bool; isCtor: bool, isFunctor=false) =
+proc parseVFunctionDecl(val: string; name, params, retType, superCall: var string; isFnConst, isOverride, isMemberVirtual, isStatic: var bool; isCtor: bool, isFunctor=false) =
   var afterParams: string = ""
   if scanf(val, "$*($*)$s$*", name, params, afterParams):
     if name.strip() == "operator" and params == "": #isFunctor?
-      parseVFunctionDecl(afterParams, name, params, retType, superCall, isFnConst, isOverride, isMemberVirtual, isCtor, true)
+      parseVFunctionDecl(afterParams, name, params, retType, superCall, isFnConst, isOverride, isMemberVirtual, isStatic, isCtor, true)
       return
+    if name.find("static ") > -1:
+      isStatic = true
+      name = name.replace("static ", "")
     isFnConst = afterParams.find("const") > -1
     isOverride = afterParams.find("override") > -1
     isMemberVirtual = name.find("virtual ") > -1
@@ -1231,8 +1234,8 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Rope; asPtr: bool =
   var typDesc = getTypeDescWeak(m, typ, check, dkParam)
   let asPtrStr = rope(if asPtr: "_PTR" else: "")
   var name, params, rettype, superCall: string = ""
-  var isFnConst, isOverride, isMemberVirtual: bool = false
-  parseVFunctionDecl(prc.constraint.strVal, name, params, rettype, superCall, isFnConst, isOverride, isMemberVirtual, isCtor)
+  var isFnConst, isOverride, isMemberVirtual, isStatic: bool = false
+  parseVFunctionDecl(prc.constraint.strVal, name, params, rettype, superCall, isFnConst, isOverride, isMemberVirtual, isStatic, isCtor)
   genMemberProcParams(m, prc, superCall, rettype, name, params, check, true, false)
   let isVirtual = sfVirtual in prc.flags or isMemberVirtual
   var fnConst, override: string = ""
@@ -1241,6 +1244,8 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Rope; asPtr: bool =
   if isFnConst:
     fnConst = " const"
   if isFwdDecl:
+    if isStatic:
+      result.add "static "
     if isVirtual:
       rettype = "virtual " & rettype
       if isOverride:
diff --git a/tests/cpp/tmember.nim b/tests/cpp/tmember.nim
index 07bd5e0ee..1a5b6fd97 100644
--- a/tests/cpp/tmember.nim
+++ b/tests/cpp/tmember.nim
@@ -8,6 +8,8 @@ hello foo
 hello boo
 hello boo
 FunctorSupport!
+static
+static
 destructing
 destructing
 '''
@@ -34,7 +36,7 @@ echo doo == Doo(test: 1)
 #virtual
 proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
 type 
-  Foo = object of RootObj
+  Foo {.exportc.} = object of RootObj
   FooPtr = ptr Foo
   Boo = object of Foo
   BooPtr = ptr Boo
@@ -62,3 +64,12 @@ proc invoke(f: NimFunctor, n:int) {.member:"operator ()('2 #2)" .} =
 {.experimental: "callOperator".}
 proc `()`(f: NimFunctor, n:int) {.importcpp:"#(@)" .} 
 NimFunctor()(1)
+
+#static
+proc staticProc(self: FooPtr) {.member: "static $1()".} = 
+  echo "static"
+
+proc importedStaticProc() {.importcpp:"Foo::staticProc()".}
+
+foo.staticProc()
+importedStaticProc()