Jump to page: 1 2 3
Thread overview
Automated source translation of C++ to D
Aug 21, 2014
Joakim
Aug 21, 2014
ketmar
Aug 21, 2014
Jacob Carlborg
Aug 21, 2014
Daniel Murphy
Aug 21, 2014
Joakim
Aug 21, 2014
po
Aug 21, 2014
Brad Anderson
Aug 21, 2014
Walter Bright
Aug 21, 2014
Joakim
Sep 06, 2014
Andrej Mitrovic
Sep 06, 2014
Walter Bright
Sep 06, 2014
deadalnix
Sep 06, 2014
ketmar
Aug 22, 2014
Daniel Murphy
Aug 22, 2014
David Nadlinger
Aug 22, 2014
Daniel Murphy
Aug 22, 2014
ketmar
Aug 22, 2014
David Gileadi
Sep 03, 2014
Xavier Bigand
Sep 04, 2014
Daniel Murphy
Sep 06, 2014
Marco Leise
August 21, 2014
C++ support keeps coming up these days, with Andrei continually stressing it as something to work on.  How hard would it to be to write a C++->D translator, to allow people to translate C++ libraries to D?

I've been using tools like DStep and looking at libdparse, which seem to work very well.  I just translated a C sample app from the Android NDK to D, fairly simple stuff like turning -> into ., adding a default in a switch statement, rewriting casts from C-style to D-style casts, removing the struct label, nothing that couldn't be automated.

I'm sure there's stuff that'd need to be done by hand, but if you can automate 97%, that's good enough.  Could this be a viable option for many cases?
August 21, 2014
On Thu, 21 Aug 2014 06:35:53 +0000
Joakim via Digitalmars-d <digitalmars-d@puremagic.com> wrote:

i believe that all code that using STL/Boost will not be translated (and this is the majority of C++ code, i think). and only very-very primitive templates can be translated automatically.

so maybe i'm not right, but i think that automatic translation can do something about 3% of existing code. ;-)


August 21, 2014
On 21/08/14 08:35, Joakim wrote:
> C++ support keeps coming up these days, with Andrei continually
> stressing it as something to work on.  How hard would it to be to write
> a C++->D translator, to allow people to translate C++ libraries to D?
>
> I've been using tools like DStep and looking at libdparse, which seem to
> work very well.  I just translated a C sample app from the Android NDK
> to D, fairly simple stuff like turning -> into ., adding a default in a
> switch statement, rewriting casts from C-style to D-style casts,
> removing the struct label, nothing that couldn't be automated.
>
> I'm sure there's stuff that'd need to be done by hand, but if you can
> automate 97%, that's good enough.  Could this be a viable option for
> many cases?

I think it will be quite difficult. There are probably many cases of C++ code where there is no direct translation in D. libclang, which DStep uses, is probably not enough to do a complete source translation of C++. That means using the C++ Clang API's which are unstable in written in C++.

C is completely different. D was basically designed to allow to easy translate C to D. If a C syntax compiles in D it's supposed to have the same semantics as in C.

It might be possible. As a start you could help out with DStep. Starting to add support for C++ for example.

-- 
/Jacob Carlborg
August 21, 2014
On 8/20/14, 11:35 PM, Joakim wrote:
> C++ support keeps coming up these days, with Andrei continually
> stressing it as something to work on.  How hard would it to be to write
> a C++->D translator, to allow people to translate C++ libraries to D?
>
> I've been using tools like DStep and looking at libdparse, which seem to
> work very well.  I just translated a C sample app from the Android NDK
> to D, fairly simple stuff like turning -> into ., adding a default in a
> switch statement, rewriting casts from C-style to D-style casts,
> removing the struct label, nothing that couldn't be automated.
>
> I'm sure there's stuff that'd need to be done by hand, but if you can
> automate 97%, that's good enough.  Could this be a viable option for
> many cases?

I think the key here involves clang with hooks + a config file per C++ file to translate that instructs the translator how to proceed about corner cases (e.g. expand this macro but not this other etc). -- Andrei
August 21, 2014
"Joakim"  wrote in message news:ysntkmioyndreuiiyqxx@forum.dlang.org... 

> C++ support keeps coming up these days, with Andrei continually stressing it as something to work on.  How hard would it to be to write a C++->D translator, to allow people to translate C++ libraries to D?

You might want to look at DDMD, which is automatically converted.
August 21, 2014
On Thursday, 21 August 2014 at 10:00:43 UTC, Daniel Murphy wrote:
> "Joakim"  wrote in message news:ysntkmioyndreuiiyqxx@forum.dlang.org...
>
>> C++ support keeps coming up these days, with Andrei continually stressing it as something to work on.  How hard would it to be to write a C++->D translator, to allow people to translate C++ libraries to D?
>
> You might want to look at DDMD, which is automatically converted.

Yes, I'm aware of ddmd.  You've mentioned many times that it only works because dmd is written using a very unC++-like style, to the point where github's source analyzer claims that dmd is written in 66.7% C, 28.4% D (presumably the tests right now), 4.4% C++, and 0.5% other. :)

Given tools like libclang, how hard do you think it'd be to translate most of actual C++ to D?  If writing such a tool would mean that C++->D translation is the path of least effort for D users who want to integrate with C++, maybe that's the approach that should be taken instead.

I should note that I have no interest in any C++ libraries: I'm just throwing out this idea as an alternative to all the C++ interfacing that's being considered for D right now.
August 21, 2014
 Might be pretty hard, C++ has some features D doesn't, not sure how you would emulate them.

C++ has these, I don't think D does:
  move only types
  r-value references
  SFINAE
  ADL
  Multiple inheritance
August 21, 2014
On Thursday, 21 August 2014 at 17:57:13 UTC, Joakim wrote:
> Yes, I'm aware of ddmd.  You've mentioned many times that it only works because dmd is written using a very unC++-like style, to the point where github's source analyzer claims that dmd is written in 66.7% C, 28.4% D (presumably the tests right now), 4.4% C++, and 0.5% other. :)

That's just because the C++ DMD files have a .c extension. The joke about it being written in C+ is fitting but the source code is pretty easily identified as C++ (if only because it makes frequent use of classes).
August 21, 2014
On 8/21/2014 10:57 AM, Joakim wrote:
> Given tools like libclang, how hard do you think it'd be to translate most of
> actual C++ to D?

I'd say the possibility of that is about zero. Heck, we can't even do it 100% for C.

The trouble is, D is not a perfect superset of C++, not even close:

1. multiple inheritance
2. SFINAE
3. Koenig lookup
4. tail mutability
5. overloading rules
6. operator overloading rules
7. fwd reference issues
8. macros (it's depressing how much modern C++ practice still heavily depends on the preprocessor)

Does that really matter? In my not-so-humble experience, C++ programmers often, far too often, find some odd corner case in the language and build an entire store on it. I personally find this baffling, but it happens with depressing regularity.

(In contrast, the C++ style used in DMD is very conservative and tends to run right down the middle of the road of C++, avoiding anything clever and corners and weird emergent behavior. This is the only reason why DDMD has even a prayer of working.)
August 21, 2014
On Thursday, 21 August 2014 at 21:06:09 UTC, Walter Bright wrote:
> On 8/21/2014 10:57 AM, Joakim wrote:
>> Given tools like libclang, how hard do you think it'd be to translate most of
>> actual C++ to D?
>
> I'd say the possibility of that is about zero. Heck, we can't even do it 100% for C.
>
> The trouble is, D is not a perfect superset of C++, not even close:
>
> 1. multiple inheritance
> 2. SFINAE
> 3. Koenig lookup
> 4. tail mutability
> 5. overloading rules
> 6. operator overloading rules
> 7. fwd reference issues
> 8. macros (it's depressing how much modern C++ practice still heavily depends on the preprocessor)
>
> Does that really matter? In my not-so-humble experience, C++ programmers often, far too often, find some odd corner case in the language and build an entire store on it. I personally find this baffling, but it happens with depressing regularity.
>
> (In contrast, the C++ style used in DMD is very conservative and tends to run right down the middle of the road of C++, avoiding anything clever and corners and weird emergent behavior. This is the only reason why DDMD has even a prayer of working.)
OK, you would know better than anyone, thanks for the considered answer.
« First   ‹ Prev
1 2 3