Skip to content

Commit

Permalink
C++: ptr fields now pulls the whole type if it's a member in nkDotExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgomez committed Oct 22, 2023
1 parent c13c485 commit e7bac9d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,12 @@ proc lookupFieldAgain(p: BProc, ty: PType; field: PSym; r: var Rope;

proc genRecordField(p: BProc, e: PNode, d: var TLoc) =
var a: TLoc = default(TLoc)
if p.module.compileToCpp and e.kind == nkDotExpr and e[1].kind == nkSym and e[1].typ.kind == tyPtr:
# special case for C++: we need to pull the type of the field as member and friends require the complete type.
let typ = e[1].typ[0]
if typ.itemId in p.module.g.graph.memberProcsPerType:
discard getTypeDesc(p.module, typ)

genRecordFieldAux(p, e, d, a)
var r = rdLoc(a)
var f = e[1].sym
Expand Down
10 changes: 9 additions & 1 deletion tests/cpp/tvirtual.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ proc test(self: Child): Box[Inner] {.virtual, asmnostackframe.} =
{.emit:"return res;".}


discard Child().test()
discard Child().test()

import virtualptr

#We dont want to pull Loo directly by using it as we are testing that the pointer pulls it.
proc makeMoo(): Moo {.importcpp:"{ new Loo() }".}

makeMoo().loo.salute()

9 changes: 9 additions & 0 deletions tests/cpp/virtualptr.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type
Loo* {.exportc.} = object
LooPtr* = ptr Loo
Moo* {.exportc.} = object
loo*: LooPtr


proc salute*(foo: LooPtr) {.virtual.} =
discard

0 comments on commit e7bac9d

Please sign in to comment.