Jump to page: 1 2
Thread overview
on interfacing w/C++
Apr 13, 2014
Ellery Newcomer
Apr 14, 2014
Jacob Carlborg
Apr 15, 2014
Daniel Murphy
Apr 15, 2014
Manu
Apr 15, 2014
Daniel Murphy
Apr 16, 2014
Moritz Maxeiner
Apr 16, 2014
Daniel Murphy
Apr 16, 2014
Moritz Maxeiner
Apr 16, 2014
monnoroch
Apr 16, 2014
Daniel Murphy
Apr 16, 2014
Daniel Murphy
Apr 16, 2014
John Colvin
Apr 17, 2014
Daniel Murphy
April 13, 2014
(Putting this out there because it sounds like I'm going to get scooped in the near future)

So last week I was dinking around with the idea of a library to support calling C++ functions. So I wrote some ct code to emulate g++ 4.8.2's mangling scheme, and wrote a bit more code to wrap it, and calling conventions seemed to work out, and this is what I got:

for the C++:

class X {
    public:
        int i;

        X();
        int y();
        void z(int j);

        static int abu(int j);
};

do this:

mixin CppClass!("X",
    typeof(new class {
        int i;
        int y();
        void z(int j);

        static int abu(int j);
    }));
mixin(X.Externs); // waa, issue 12575


it generates something like

struct X {
    int i;
    int y() {
        return _ZN1X1yEv(&this);
    }
    void z(int j) {
        _ZN1X1zEi(&this, j);
    }
    static int abu(int j) {
        return _ZN1X3abuEi(j);
    }
}
extern(C) int _ZN1X1yEv(X*);
extern(C) void _ZN1X1zEi(X*,int);
extern(C) int _ZN1X3abuEi(int);


And it all seems to work. pointer params, reference params, variadic params, operators, templates all seem within ready reach.

virtual functions are going to be difficult - I guess you'd have to have a complete and accurate list of all fields in the class and all its superclasses. And all virtual functions. But g++ inserts a vtable symbol for classes with virtual functions - does anyone know what the heck that is?

Same for constructing a C++ object from within D - you need a valid sizeof.

other compilers won't work as above, the mangling scheme used by eg DMC doesn't produce valid D identifiers. Is there a pragma or anything to get around this?

And I totally spaced on exceptions until I looked on this forum a few minutes ago.

So my plan is to get all these bits implemented, then build a tool to validate the C++ and D sides match up, then build a tool to generate the D side from C++ headers. And then start expanding the list of supported C++ compilers.

Does this sound like a reasonable project? The points of concern I am worried about most are:

safety associated with supporting virtual functions
if a c++ mangler is stateful, that would shut me down pretty quick.
it requires the C++ compiler/version be specified at a library level
exceptions probably won't work
April 14, 2014
On 14/04/14 00:54, Ellery Newcomer wrote:
> (Putting this out there because it sounds like I'm going to get scooped
> in the near future)
>
> So last week I was dinking around with the idea of a library to support
> calling C++ functions. So I wrote some ct code to emulate g++ 4.8.2's
> mangling scheme, and wrote a bit more code to wrap it, and calling
> conventions seemed to work out, and this is what I got:

You do know D supports extern(C++)? Although I don't know to which extent.

-- 
/Jacob Carlborg
April 15, 2014
"Jacob Carlborg"  wrote in message news:lig73r$nvn$1@digitalmars.com...

> You do know D supports extern(C++)? Although I don't know to which extent.

Pretty much everything works except special functions (ctors/dtors/operators). 

April 15, 2014
Huh? Do methods work now? Since when?
We still have no member function pointers either.


On 15 April 2014 14:25, Daniel Murphy via Digitalmars-d < digitalmars-d@puremagic.com> wrote:

> "Jacob Carlborg"  wrote in message news:lig73r$nvn$1@digitalmars.com...
>
>
>  You do know D supports extern(C++)? Although I don't know to which extent.
>>
>
> Pretty much everything works except special functions
> (ctors/dtors/operators).
>


April 15, 2014
"Manu via Digitalmars-d" <digitalmars-d@puremagic.com> wrote in message news:mailman.9.1397553786.2763.digitalmars-d@puremagic.com...

> Huh? Do methods work now? Since when?

Since I needed them for DDMD.

> We still have no member function pointers either.

Meh. 

April 15, 2014
On 4/15/14, 2:22 AM, Manu via Digitalmars-d wrote:
> We still have no member function pointers either.

I'd advocate against supporting C++ member function pointers. -- Andrei

April 16, 2014
On Tuesday, 15 April 2014 at 11:04:42 UTC, Daniel Murphy wrote:
> "Manu via Digitalmars-d" <digitalmars-d@puremagic.com> wrote in message news:mailman.9.1397553786.2763.digitalmars-d@puremagic.com...
>
>> Huh? Do methods work now? Since when?
>
> Since I needed them for DDMD.
>

Is this[1] then out of date and I can interface with non-virtual methods? Because that's what your post seems to imply (unless I misunderstood).

[1] http://dlang.org/cpp_interface.html

April 16, 2014
"Moritz Maxeiner"  wrote in message news:kvzwlecwougswrqkatcs@forum.dlang.org...

> Is this[1] then out of date and I can interface with non-virtual methods? Because that's what your post seems to imply (unless I misunderstood).
>
> [1] http://dlang.org/cpp_interface.html

Yes.  The best place to look for concrete examples of what is supported is probably the C++ tests in the test suite.  (ie files containing "EXTRA_CPP_SOURCES") 

April 16, 2014
On Wednesday, 16 April 2014 at 14:00:24 UTC, Daniel Murphy wrote:
> "Moritz Maxeiner"  wrote in message news:kvzwlecwougswrqkatcs@forum.dlang.org...
>
>> Is this[1] then out of date and I can interface with non-virtual methods? Because that's what your post seems to imply (unless I misunderstood).
>>
>> [1] http://dlang.org/cpp_interface.html
>
> Yes.  The best place to look for concrete examples of what is supported is probably the C++ tests in the test suite.  (ie files containing "EXTRA_CPP_SOURCES")

That sounds very cool, I've had a look at [1] and [2], which seem to be the two files with the new C++ class interfacing. As far as I could tell, you need to create any instances of C++ classes with C++ code / you don't bind to the constructors directly from D and the new instance will not be managed by D's GC? Because if I used this new interfacing for e.g. llvm-d, I need to be sure, that D's GC won't touch any of the instances under any circumstances, since they are freed by LLVM's internal logic they GC cannot track.

[1] https://github.com/D-Programming-Language/dmd/blob/master/test/runnable/externmangle.d
[2] https://github.com/D-Programming-Language/dmd/blob/master/test/runnable/extra-files/externmangle.cpp
April 16, 2014
What about namespaces?
« First   ‹ Prev
1 2