Issue 19260 - extern(C++) `T* const` mangling
Summary: extern(C++) `T* const` mangling
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: C++, industry
Depends on:
Blocks:
 
Reported: 2018-09-23 04:25 UTC by Manu
Modified: 2022-12-17 10:31 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 2018-09-23 04:25:18 UTC
This function:
  void deallocate(T* const ptr, size_t count);

Is critical for linking STL, but I can't mangle the function.

D should obviously declare the function as:
  void deallocate(T* ptr, size_t count);

Which is semantically identical in terms of usage, but the mangling of the const pointer is different and therefore can't link.

What can we do?
I can't use pragma(mangle), because `T` is a template arg, and could be anything!

This has stopped me in my tracks :/
Comment 1 kinke 2018-09-23 11:21:24 UTC
Patching the mangle at compile-time like this (MSVC only) might work:

extern(C++) template deallocate(T)
{
    void _deallocate(T* ptr, size_t count);

    private extern(D) string getMangle()
    {
        import std.array;
        return _deallocate.mangleof.replace("PEAU", "QEAU");
    }

    pragma(mangle, getMangle())
    void deallocate(T* ptr, size_t count);
}

D:   https://run.dlang.io/is/q4kF2e
C++: https://godbolt.org/z/GpXfqV
Comment 2 Manu 2018-09-29 21:24:24 UTC
I have used that hack to move forward... but it's not a reasonable solution.
Comment 3 kinke 2018-09-29 21:34:21 UTC
I don't think there's a need for a proper solution for this on the D side:

1) `T* const` is rare in my experience.
2) This `const` affecting the C++ mangle is a big mistake IMO. The caller can't care less about it, it's just an implementation detail of the callee (not gonna mutate the param, i.e., my private pointer copy) leaking to the outside world and causing issues like this.
Comment 4 Manu 2018-09-29 21:40:45 UTC
But it's impossible to link. That's really important...
Comment 5 kinke 2018-09-29 21:53:39 UTC
Well, as shown, D is flexible enough to overcome crappy situations like this, albeit in cumbersome ways.

Just saying that I don't think it's worthwhile to come up, implement and document solutions to exotic problems like this, which could also be tackled on the C++ side. I take it you cannot simply add another C++ template, as that one probably doesn't get instantiated on the C++ side, and aren't willing to hack the STL either. Btw, cppreference says the signature is `T*` [https://en.cppreference.com/w/cpp/memory/allocator/deallocate], so does MS have to go its own route once again? ;)
Comment 6 Manu 2018-09-29 22:19:42 UTC
Of course! This is MS after all!
This is not the first time this class of issue has emerged however, but in this case, I'm powerless to make any change to help...
Comment 7 Mathias LANG 2018-10-22 18:24:28 UTC
I strongly agree with Kinke here and think we should not pursue mangling head const, as it's already possible to do it with a small hack (and it's a one-time cost).
Comment 8 Manu 2018-10-23 18:21:09 UTC
We could give the argument a @MangleHeadConst attribute or something that the mangler recognises...?
Comment 9 kinke 2018-10-23 20:12:05 UTC
-1, as that won't work for `T *const *`, i.e., only for a very specific case, so I find a dedicated UDA + compiler complexity a total overkill.