Bug 19260 – extern(C++) `T* const` mangling

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-09-23T04:25:18Z
Last change time
2024-12-13T19:00:36Z
Keywords
C++, industry
Assigned to
No Owner
Creator
Manu
Moved to GitHub: dmd#17879 →

Comments

Comment #0 by turkeyman — 2018-09-23T04:25:18Z
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 by kinke — 2018-09-23T11:21:24Z
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 by turkeyman — 2018-09-29T21:24:24Z
I have used that hack to move forward... but it's not a reasonable solution.
Comment #3 by kinke — 2018-09-29T21:34:21Z
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 by turkeyman — 2018-09-29T21:40:45Z
But it's impossible to link. That's really important...
Comment #5 by kinke — 2018-09-29T21:53:39Z
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 by turkeyman — 2018-09-29T22:19:42Z
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 by pro.mathias.lang — 2018-10-22T18:24:28Z
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 by turkeyman — 2018-10-23T18:21:09Z
We could give the argument a @MangleHeadConst attribute or something that the mangler recognises...?
Comment #9 by kinke — 2018-10-23T20:12:05Z
-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.
Comment #10 by robert.schadek — 2024-12-13T19:00:36Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17879 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB