D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21203 - Accept pragma(mangle) on aggregate types
Summary: Accept pragma(mangle) on aggregate types
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-08-27 23:05 UTC by Manu
Modified: 2021-03-24 23:50 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2020-08-27 23:05:58 UTC
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 Manu 2020-08-27 23:25:57 UTC
Sorry, that should read:

template ScopeClass(C)
{
  ...
}

!!
Comment 2 Manu 2020-08-27 23:27:24 UTC
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 Andrej Mitrovic 2020-09-29 03:54:03 UTC
+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 Manu 2020-09-29 04:52:03 UTC
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 Nicholas Wilson 2020-10-05 10:17:36 UTC
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 Dlang Bot 2020-11-10 01:42:29 UTC
@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 Dlang Bot 2021-03-24 23:50:41 UTC
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