Skip to content

Commit 7783e94

Browse files
author
Alex Rønne Petersen
committed
Merge pull request #624 from dawgfoto/mangleFunc
core.demangle.mangleFunc for different linkage types
2 parents 12d5350 + 0df1c65 commit 7783e94

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/core/demangle.d

+62-1
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,24 @@ unittest
16831683
*/
16841684
char[] mangleFunc(T:FT*, FT)(const(char)[] fqn, char[] dst = null) @safe pure nothrow if (is(FT == function))
16851685
{
1686-
return mangle!FT(fqn, dst);
1686+
static if (isExternD!FT)
1687+
{
1688+
return mangle!FT(fqn, dst);
1689+
}
1690+
else static if (hasPlainMangling!FT)
1691+
{
1692+
dst.length = fqn.length;
1693+
dst[] = fqn[];
1694+
return dst;
1695+
}
1696+
else static if (isExternCPP!FT)
1697+
{
1698+
static assert(0, "Can't mangle extern(C++) functions.");
1699+
}
1700+
else
1701+
{
1702+
static assert(0, "Can't mangle function with unknown linkage ("~FT.stringof~").");
1703+
}
16871704
}
16881705

16891706

@@ -1701,6 +1718,50 @@ unittest
17011718
assert(mangle!(typeof(*fp))("demangle.test") == "_D8demangle4testFLAiYi");
17021719
}
17031720

1721+
private template isExternD(FT) if (is(FT == function))
1722+
{
1723+
enum isExternD = FT.mangleof[0] == 'F';
1724+
}
1725+
1726+
private template isExternCPP(FT) if (is(FT == function))
1727+
{
1728+
enum isExternCPP = FT.mangleof[0] == 'R';
1729+
}
1730+
1731+
private template hasPlainMangling(FT) if (is(FT == function))
1732+
{
1733+
enum c = FT.mangleof[0];
1734+
// C || Pascal || Windows
1735+
enum hasPlainMangling = c == 'U' || c == 'V' || c == 'W';
1736+
}
1737+
1738+
unittest
1739+
{
1740+
static extern(D) void fooD();
1741+
static extern(C) void fooC();
1742+
static extern(Pascal) void fooP();
1743+
static extern(Windows) void fooW();
1744+
static extern(C++) void fooCPP();
1745+
1746+
bool check(FT)(bool isD, bool isCPP, bool isPlain)
1747+
{
1748+
return isExternD!FT == isD && isExternCPP!FT == isCPP &&
1749+
hasPlainMangling!FT == isPlain;
1750+
}
1751+
static assert(check!(typeof(fooD))(true, false, false));
1752+
static assert(check!(typeof(fooC))(false, false, true));
1753+
static assert(check!(typeof(fooP))(false, false, true));
1754+
static assert(check!(typeof(fooW))(false, false, true));
1755+
static assert(check!(typeof(fooCPP))(false, true, false));
1756+
1757+
static assert(__traits(compiles, mangleFunc!(typeof(&fooD))("")));
1758+
static assert(__traits(compiles, mangleFunc!(typeof(&fooC))("")));
1759+
static assert(__traits(compiles, mangleFunc!(typeof(&fooP))("")));
1760+
static assert(__traits(compiles, mangleFunc!(typeof(&fooW))("")));
1761+
static assert(!__traits(compiles, mangleFunc!(typeof(&fooCPP))("")));
1762+
}
1763+
1764+
17041765
version(unittest)
17051766
{
17061767
immutable string[2][] table =

0 commit comments

Comments
 (0)