Skip to content

Commit

Permalink
[C++] Allow member to define static funcs (#23387)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgomez authored Mar 11, 2024
1 parent 94c5996 commit 9339977
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 9 additions & 4 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = ""
Expand All @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion tests/cpp/tmember.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ hello foo
hello boo
hello boo
FunctorSupport!
static
static
destructing
destructing
'''
Expand All @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 9339977

Please sign in to comment.