Skip to content

Commit

Permalink
Fix issue 14086 - Invalid extern C++ name for constructor and virtual…
Browse files Browse the repository at this point in the history
… destructor
  • Loading branch information
TurkeyMan committed May 21, 2018
1 parent 6ed105b commit 0d9b4b3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog/mangle_ctor.dd
@@ -0,0 +1,4 @@
`extern (C++)` now mangles class constructor's correctly.

`extern (C++)` mangling of class constructor is working, also virtual and non-virtual destructors mangle correctly too.
C++ semantics are not yet perfectly matching, but some simple cases are working.
2 changes: 1 addition & 1 deletion src/dmd/backend/dt.d
Expand Up @@ -41,7 +41,7 @@ private:
dt_t** pTail;

public:
this()
extern (D) this()
{
pTail = &head;
}
Expand Down
10 changes: 8 additions & 2 deletions src/dmd/cppmangle.d
Expand Up @@ -590,7 +590,9 @@ private final class CppMangleVisitor : Visitor
buf.writeByte('N');
CV_qualifiers(d.type);
prefix_name(p);
if (d.isDtorDeclaration())
if (d.isCtorDeclaration())
buf.writestring("C1");
else if (d.isDtorDeclaration())
buf.writestring("D1");
else
source_name(ti);
Expand Down Expand Up @@ -621,7 +623,11 @@ private final class CppMangleVisitor : Visitor
prefix_name(p);
//printf("p: %s\n", buf.peekString());

if (d.isDtorDeclaration())
if (d.isCtorDeclaration())
{
buf.writestring("C1");
}
else if (d.isDtorDeclaration())
{
buf.writestring("D1");
}
Expand Down
9 changes: 7 additions & 2 deletions src/dmd/cppmanglewin.d
Expand Up @@ -538,7 +538,7 @@ private:
// Pivate methods always non-virtual in D and it should be mangled as non-virtual in C++
//printf("%s: isVirtualMethod = %d, isVirtual = %d, vtblIndex = %d, interfaceVirtual = %p\n",
//d.toChars(), d.isVirtualMethod(), d.isVirtual(), cast(int)d.vtblIndex, d.interfaceVirtual);
if (d.isVirtual() && (d.vtblIndex != -1 || d.interfaceVirtual || d.overrideInterface()))
if ((d.isVirtual() && (d.vtblIndex != -1 || d.interfaceVirtual || d.overrideInterface())) || (d.isDtorDeclaration() && d.parent.isClassDeclaration() && !d.isFinal()))
{
switch (d.protection.kind)
{
Expand Down Expand Up @@ -671,7 +671,12 @@ private:
//printf("mangleName('%s')\n", sym.toChars());
const(char)* name = null;
bool is_dmc_template = false;
if (sym.isDtorDeclaration())
if (sym.isCtorDeclaration())
{
buf.writestring("?0");
return;
}
else if (sym.isDtorDeclaration())
{
buf.writestring("?1");
return;
Expand Down
43 changes: 43 additions & 0 deletions test/compilable/cppmangle.d
Expand Up @@ -463,3 +463,46 @@ version (Windows)
{
static assert(test15388.mangleof == "?test15388@@YAX$$T@Z");
}

/**************************************/
// https://issues.dlang.org/show_bug.cgi?id=14086

extern (C++) class Test14086
{
this();
~this();
}
extern (C++) class Test14086_2
{
final ~this();
}
extern (C++) struct Test14086_S
{
this(int);
~this();
}

version(Posix)
{
static assert(Test14086.__ctor.mangleof == "_ZN9Test14086C1Ev");
static assert(Test14086.__dtor.mangleof == "_ZN9Test14086D1Ev");
static assert(Test14086_2.__dtor.mangleof == "_ZN11Test14086_2D1Ev");
static assert(Test14086_S.__ctor.mangleof == "_ZN11Test14086_SC1Ei");
static assert(Test14086_S.__dtor.mangleof == "_ZN11Test14086_SD1Ev");
}
version(Win32)
{
static assert(Test14086.__ctor.mangleof == "??0Test14086@@QAE@XZ");
static assert(Test14086.__dtor.mangleof == "??1Test14086@@UAE@XZ");
static assert(Test14086_2.__dtor.mangleof == "??1Test14086_2@@QAE@XZ");
static assert(Test14086_S.__ctor.mangleof == "??0Test14086_S@@QAE@H@Z");
static assert(Test14086_S.__dtor.mangleof == "??1Test14086_S@@QAE@XZ");
}
version(Win64)
{
static assert(Test14086.__ctor.mangleof == "??0Test14086@@QEAA@XZ");
static assert(Test14086.__dtor.mangleof == "??1Test14086@@UEAA@XZ");
static assert(Test14086_2.__dtor.mangleof == "??1Test14086_2@@QEAA@XZ");
static assert(Test14086_S.__ctor.mangleof == "??0Test14086_S@@QEAA@H@Z");
static assert(Test14086_S.__dtor.mangleof == "??1Test14086_S@@QEAA@XZ");
}

0 comments on commit 0d9b4b3

Please sign in to comment.