Skip to content

Commit

Permalink
[FEATURE] Wrap superclass methods
Browse files Browse the repository at this point in the history
- CHANGED ::
  - Class wrappers now include wrappers for the superclass methods as well.
    Current implementation simply copy-pastes all wrapped types once more,
    but ideally this should be optimized, either using
    #1 with `{.byref.}` hack,
    or by a proper language support.
  • Loading branch information
haxscramper committed Nov 3, 2021
1 parent dad2233 commit 524d541
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
27 changes: 18 additions & 9 deletions src/hcparse/hc_codegen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ proc toNNode*[N](
isDistinct = false
))

func getCbindAs*(pr: CxxProc): CxxBind =
func getCbindAs*(pr: CxxProc, parent: Option[CxxObject]): CxxBind =
result = pr.cbind
if result.icpp.len == 0:
if pr.isConstructor:
Expand All @@ -273,7 +273,7 @@ func getCbindAs*(pr: CxxProc): CxxBind =
if pr.isMethod():
if pr.isStatic():
result.icpp = staticMethod(
pr.getMethodOf().cxxStr(), pr.getIcppName())
parent.get().name().cxxStr(), pr.getIcppName())

else:
result.icpp.dotMethod(pr.getIcppName())
Expand All @@ -287,7 +287,8 @@ proc toNNode*[N](
def: CxxProc,
conf: CodegenConf,
anon: var seq[NimDecl[N]],
onConstructor: NimConstructorTarget = nctRegular
onConstructor: NimConstructorTarget = nctRegular,
parent: Option[CxxObject] = none CxxObject
): ProcDecl[N] =

result = newProcDecl[N](def.nimName)
Expand Down Expand Up @@ -327,7 +328,7 @@ proc toNNode*[N](
var ret = toNNode[N](
#[ V FIXME - does not account for template type parameters in parent
classes ]#
def.methodOf.get().cxxTypeUse(),
parent.get().name().cxxTypeUse(),
conf,
anon)

Expand All @@ -336,10 +337,10 @@ proc toNNode*[N](

result.addArgument("this", ret)

result.addPragma toNNode[N](def.getCbindAs(), conf, def.nimName)
result.addPragma toNNode[N](def.getCbindAs(parent), conf, def.nimName)

else:
result.addPragma toNNode[N](def.getCbindAs(), conf, def.nimName)
result.addPragma toNNode[N](def.getCbindAs(parent), conf, def.nimName)



Expand Down Expand Up @@ -385,14 +386,22 @@ proc toNNode*[N](
result.add toNimDecl(res)

for meth in obj.methods:
result.add toNNode[N](meth, conf, anon, nctPtr)
result.add toNNode[N](meth, conf, anon, nctPtr, some obj)

for n in obj.nested:
result.add toNNode[N](n, conf, anon)

echov "super of", obj
# echov "super of", obj
for super in obj.decl.store.getSuperTypes(obj.decl):
echov ">>", super
# echov ">>", super
var anon: seq[NimDecl[N]]
for meth in super.methods:
if not (
meth.isStatic() or
meth.isConstructor() or
meth.isDestructor()
):
result.add toNNode[N](meth, conf, anon, nctPtr, some obj)


proc toNNode*[N](obj: CxxForward, conf: CodegenConf): ObjectDecl[N] =
Expand Down
2 changes: 1 addition & 1 deletion src/hcparse/hc_irgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ proc toCxxProc*(
result.constructorOf = some parent.get().name

elif parent.isSome():
result.methodOf = some parent.get().name
result.flags.incl cpfMethod # = some parent.get().name

for argIdx, arg in pr.arguments:
result.add toCxxArg(arg, conf, cache)
Expand Down
2 changes: 1 addition & 1 deletion src/hcparse/hc_tsreader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ proc toCxxProc*(
)

if parent.isSome():
result.methodOf = some parent.get().name()
result.flags.incl cpfMethod

if node[0] of cppTypeQualifier:
result.returnType.flags.incl ctfConst
Expand Down
10 changes: 3 additions & 7 deletions src/hcparse/interop_ir/wrap_store.nim
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ type
cpfSignal
cpfVirtual
cpfVariadic
cpfMethod

CxxProc* = object of CxxBase
kind*: CxxProcKind
Expand All @@ -303,7 +304,6 @@ type

constructorOf*: Option[CxxNamePair]
destructorOf*: Option[CxxNamePair]
methodOf*: Option[CxxNamePair]

CxxExprKind = enum
cekIntLit
Expand Down Expand Up @@ -905,9 +905,8 @@ func isConst*(pr: CxxProc): bool = cpfConst in pr.flags
func isStatic*(pr: CxxProc): bool = cpfStatic in pr.flags
func isConstructor*(pr: CxxProc): bool = pr.constructorOf.isSome()
func isDestructor*(pr: CxxProc): bool = pr.destructorOf.isSome()
func isMethod*(pr: CxxProc): bool = pr.methodOf.isSome()
func getMethodOf*(pr: CxxProc): CxxNamePair =
pr.methodOf.get()
func isMethod*(pr: CxxProc): bool = cpfMethod in pr.flags


func isEmpty*(name: CxxName): bool =
name.scopes.len == 0 or
Expand Down Expand Up @@ -1586,9 +1585,6 @@ proc fixIdentsRec*(
auxArg(use, cache)

proc aux(decl: var CxxProc, cache: var StringNameCache) =
if decl.isMethod():
aux(decl.methodOf.get())

if decl.isConstructor():
aux(decl.constructorOf.get())

Expand Down

0 comments on commit 524d541

Please sign in to comment.