Fix duplicated member declarations in structs for C++ backend (#23512)

When forward declaration is used with pragmas `virtual` or `member`, the
declaration in struct is added twice. It happens because of missing
check for `sfWasForwarded` pragma.

Current compiler generates the following C++ code:
```cpp
struct tyObject_Foo__fFO9b6HU7kRnKB9aJA1RApKw {
N_LIB_PRIVATE N_NOCONV(void, abc)(NI x_p1);
N_LIB_PRIVATE N_NOCONV(virtual void, def)(NI y_p1);
N_LIB_PRIVATE N_NOCONV(void, abc)(NI x_p1);
N_LIB_PRIVATE N_NOCONV(virtual void, def)(NI y_p1);
};
```
This commit is contained in:
HexSegfaultCat
2024-04-18 21:57:06 +02:00
committed by GitHub
parent 229c125d2f
commit 558bbb7426
2 changed files with 28 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
discard """
targets: "cpp"
cmd: "nim cpp $file"
output: '''
abc called
def called
abc called
'''
"""
type Foo = object
proc abc(this: Foo, x: int): void {.member: "$1('2 #2)".}
proc def(this: Foo, y: int): void {.virtual: "$1('2 #2)".}
proc abc(this: Foo, x: int): void =
echo "abc called"
if x > 0:
this.def(x - 1)
proc def(this: Foo, y: int): void =
echo "def called"
this.abc(y)
var x = Foo()
x.abc(1)