Bug 21203 – Accept pragma(mangle) on aggregate types

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-27T23:05:58Z
Last change time
2021-03-24T23:50:41Z
Keywords
pull
Assigned to
No Owner
Creator
Manu

Comments

Comment #0 by turkeyman — 2020-08-27T23:05:58Z
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.
Comment #1 by turkeyman — 2020-08-27T23:25:57Z
Sorry, that should read: template ScopeClass(C) { ... } !!
Comment #2 by turkeyman — 2020-08-27T23:27:24Z
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!!!)
Comment #3 by andrej.mitrovich — 2020-09-29T03:54:03Z
+1 Also we currently cannot add a binding to std::function. Because function is a keyword. And pragma(mangle) doesn't work on structs.
Comment #4 by turkeyman — 2020-09-29T04:52:03Z
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.
Comment #5 by iamthewilsonator — 2020-10-05T10:17:36Z
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
Comment #6 by dlang-bot — 2020-11-10T01:42:29Z
@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
Comment #7 by dlang-bot — 2021-03-24T23:50:41Z
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