From e7bac9de2dcf8fe4079d21917c890e984f171f03 Mon Sep 17 00:00:00 2001 From: jmgomez Date: Sun, 22 Oct 2023 22:33:03 +0100 Subject: [PATCH] C++: ptr fields now pulls the whole type if it's a member in nkDotExpr --- compiler/ccgexprs.nim | 6 ++++++ tests/cpp/tvirtual.nim | 10 +++++++++- tests/cpp/virtualptr.nim | 9 +++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/cpp/virtualptr.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index eca6fa9958a1b..2612c5c12172b 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -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 diff --git a/tests/cpp/tvirtual.nim b/tests/cpp/tvirtual.nim index fb792380b3097..385d052b8fe85 100644 --- a/tests/cpp/tvirtual.nim +++ b/tests/cpp/tvirtual.nim @@ -115,4 +115,12 @@ proc test(self: Child): Box[Inner] {.virtual, asmnostackframe.} = {.emit:"return res;".} -discard Child().test() \ No newline at end of file +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() + diff --git a/tests/cpp/virtualptr.nim b/tests/cpp/virtualptr.nim new file mode 100644 index 0000000000000..f96264081c3e3 --- /dev/null +++ b/tests/cpp/virtualptr.nim @@ -0,0 +1,9 @@ +type + Loo* {.exportc.} = object + LooPtr* = ptr Loo + Moo* {.exportc.} = object + loo*: LooPtr + + +proc salute*(foo: LooPtr) {.virtual.} = + discard