-
-
Notifications
You must be signed in to change notification settings - Fork 652
[WIP] add extern (C++) struct Darray(T) and Dstring to simplify interop with C++ #7893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for your pull request and interest in making D better, @timotheecour! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
We already have this. https://github.com/dlang/dmd/blob/master/src/dmd/globals.h#L289-L295 |
Better yet, make more of the interface internal to D, that parts that really need it can just be extern(C++) wrappers. |
it's not being used at all, except for
the point of this PR is to allow replacing existing C++ wrappers: |
Would be nice to have #7458 working. That would allow to just create an alias with a specific mangling to avoid the need for a wrapper, |
@jacob-carlborg we'd still need something like:
but that'd avoid needing the need for |
Or just hard code the mangling. |
one could also do this: allow D mangling for extern(C++) declarations to understand D arrays, and mangle the same as would the following:
with this, |
Certainly useful. We already have this too in LDC: https://github.com/ldc-developers/ldc/blob/bfd412cdf9237c988f44dfc241f1f9fa7bbb0359/gen/ldctraits.h#L13-L17 Perhaps define opSlice? (also |
Whats the status of this? I'd like to start blasting away at |
Yes, Walter has said that |
} | ||
|
||
alias Dstring = Darray!char; | ||
alias Dcstring = Darray!(const(char)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alias string = immutable(char)[];
, (though imho it should have been const(char), but I guess it's way too late to change that.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(resolved privately on slack https://dlang.slack.com/archives/D8ZMKRGUV/p1521139433000456)
TLDR: immutable has no C++ equivalent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The slack conversation is private, so the link isn't helpful to other people.
in short: pragma(mangle)
doesn't work in alias
. It has been proposed in #7458, but closed due to inactivity :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not only that but pragma(mangle)
wouldn't help with arbitrary array types even if it were implemented. this PR aims at supporting arbitrary D arrays, not just const(char)[]
for example
328b39c
to
2c8005d
Compare
} | ||
+/ | ||
|
||
extern (C++) struct Darray(T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're going to create a wrapper type like this you'll probably need to do it in the same way that I've done in core.sentinel
. Namely, you'll need to support implicit conversion from:
Darray!(immutable(char)) > Darray!(const(char))
Darray!char > Darray!(const(char))
You may also need to support implicit conversion to/from native D arrays, but implicit conversion between multiple types requires multiple alias this.
Take a look at core.sentinel
to see how this is done.
Looks like the PR has completely changed the implementation? |
assert(t2); | ||
headOfType(t2); | ||
buf.writeByte('E'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve been thinking how to implement this. The idea I had was to add a new method to the Type
base class called typeForMangling
, which takes a mangling as its argument and returns the type that should be used for that mangling. This allows a type to pose as another type during mangling. The T[]
type would need to construct a DSlice!T
type during the semantic phase. In this case, typeForMangling
would be called when mangling T[]
and use the mangling of the new type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ this PR is now doing two things at once, or is this a cherry pick that is about to be merged? ]
@timotheecour What's the status of this? It doesn't look finished. |
/cc @klickverbot, @redstar @jacob-carlborg @JohanEngelen for LDC
/cc @ibuclaw for GDC
If this is deemed useful and has a chance to get merged I can finish this PR.
WIP support for extern(C++) mangling of D arrays:
alternatives considered
status quo (using char*) => misses length information, adds complications eg extra parameter or
strlen
pragma(mangle, ...) is not a viable alternative
it'd be required on every extern(C++) declaration that uses a D array, since Fix issue 18095 - Add support for pragma(mangle) on alias declarations #7458 (pragma(mangle) on alias declarations) was abandonned due to complexity
std::string, std::vector (Addition of C++ std::string, std::vector to D druntime#1316 ) is not viable as these would require copies (not slices)
[RFC] Add the core.sentinel module druntime#2123 (Add the core.sentinel module by @marler8997) won't help with D slices in general
old (needs updating)
eg usage:
see full working example here showing using such API from C++ with the function defined in D.
originally proposed here: #7870 (comment)
motivations:
Quoting a conversation with Walter here: