Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/dmd/cppmangle.d
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,20 @@ public:
}
}

override void visit(TypeDArray t)
{
//if (substitute(t))
//return;

// from: template_args
buf.writestring("8__dslice");
buf.writeByte('I');
Type t2 = t.next;
assert(t2);
headOfType(t2);
buf.writeByte('E');
}
Copy link
Contributor

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.

Copy link
Contributor

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? ]


override void visit(TypeSArray t)
{
if (t.isImmutable() || t.isShared())
Expand Down
31 changes: 31 additions & 0 deletions src/dmd/interop.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/+
Example usage:
extern (C++) Dstring funLib(Dstring input)
{
import std.string : toUpper;
import std.conv : to;
return input.toNative.toUpper.to!Dstring;
}
+/

extern (C++) struct Darray(T)
Copy link
Contributor

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.

{
size_t length;
T* ptr;

extern (D) this(T[] a)
{
this.length = a.length;
this.ptr = a.ptr;
}

extern (D) T[] toNative()
{
// TODO: could even use a raw cast since ABI is same
return ptr[0 .. length];
}
}

alias Dstring = Darray!char;
alias Dcstring = Darray!(const(char));
Copy link
Contributor

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.)

Copy link
Contributor Author

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

Copy link
Contributor

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 :/

Copy link
Contributor Author

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