pragma(mangle) is only accepted on hard functions and variables. Adding support to AggregateDecl will allow the user to customise the mangling for type names, and that mangle override should be used when composing mangled names for functions/templates. This is the key to allowing comprehensive C++ class interop. For instance, this becomes possible: struct ScopeClass(C) if (is(C == class) { static if (__traits(getLinkage, C) == "C++") { extern(C++, class) pragma(mangle, C.mangleof) // <- here's the magic! struct ScopeClass { char[__traits(classInstanceSize, C)] buffer; //... all the things ... } } } And then consider these functions: void fun(MyClass); // mangles MyClass* void fun(const MyClass); // mangles const MyClass* const void fun(ScopedClass!MyClass); // mangles MyClass void fun(const ScopedClass!MyClass); // mangles const MyClass void fun(ref ScopedClass!MyClass); // mangles MyClass& void fun(ref const ScopedClass!MyClass); // mangles const MyClass& By allowing pragma(mangle) to apply to AggregateDecl, we can override the mangling for the type names, and with this, we can write in-language tooling that can handle classes in ALL the ways that C++ can express.
Sorry, that should read: template ScopeClass(C) { ... } !!
And I missed the most important one: void fun(const(ScopedClass!MyClass)*); // mangles const MyClass* That's the money shot! (I wish you could edit in bugzilla!!!)
+1 Also we currently cannot add a binding to std::function. Because function is a keyword. And pragma(mangle) doesn't work on structs.
I think this is a really big deal! It'll enable in-language solutions for a whole heap of our biggest extern(C++) problems... I'm surprised it's not receiving more attention.
Runnable example: template ScopeClass(C) // if (is(C == class)) { static if (__traits(getLinkage, C) == "C++") { extern(C++, class) pragma(mangle, C.mangleof) // <- here's the magic! struct ScopeClass { char[__traits(classInstanceSize, C)] buffer; //... all the things ... } } } extern(C++): class MyClass {} void funa(MyClass); // mangles MyClass* void funb(const MyClass); // mangles const MyClass* const void func(ScopeClass!MyClass); // mangles MyClass void fund(const ScopeClass!MyClass); // mangles const MyClass void fune(ref ScopeClass!MyClass); // mangles MyClass& void funf(ref const ScopeClass!MyClass); // mangles const MyClass& void fung(const(ScopeClass!MyClass)*); // currently prints pragma(msg,funa.mangleof); // _Z4funaP7MyClass pragma(msg,funb.mangleof); // _Z4funbPK7MyClass pragma(msg,func.mangleof); // _Z4func10ScopeClassIP7MyClassE pragma(msg,fund.mangleof); // _Z4fund10ScopeClassIP7MyClassE pragma(msg,fune.mangleof); // _Z4funeR10ScopeClassIP7MyClassE pragma(msg,funf.mangleof); // _Z4funfRK10ScopeClassIP7MyClassE pragma(msg,fung.mangleof); // _Z4fungPK10ScopeClassIP7MyClassE
@thewilsonator updated dlang/dmd pull request #11835 "Fix issue 21203: Accept pragma(mangle) on aggregate types" fixing this issue: - Fix issue 21203: Accept pragma(mangle) on aggregate types https://github.com/dlang/dmd/pull/11835
dlang/dmd pull request #11835 "Fix issue 21203: Accept pragma(mangle) on aggregate types" was merged into master: - 49a184cc6d619dbc6b6bb7e7393e0181171de659 by Nicholas Lindsay Wilson: Fix issue 21203: Accept pragma(mangle) on aggregate types https://github.com/dlang/dmd/pull/11835