mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-10 06:54:16 +00:00
[C++] Allow member to define static funcs (#23387)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user