www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - C++ to D

reply "Jeff Jones" <Jeff Jones.com> writes:
Is it possible to modify either a C++ compiler or in code to make 
classes more D ish? e.g., instead of just the vtable(which, IIRC 
is slightly different between the two) add the metadata pointer 
and whatever else?

What I'm getting at, is there a somewhat simple way(not creating 
a new C++ but either through macros, small compiler changes, or 
whatever) to make C++ objects D compatible instead of the other 
way?

I.e., we can't take anything away from D classes since we will 
break D. But we should be able to add to C++ classes without 
breaking C++. If we can add the info that D needs then both 
should get along happily? I'm mainly talking about a simple code 
based solution but I'm also curious about how easily this would 
be by compiler modification.
Mar 31 2015
next sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Tuesday, 31 March 2015 at 20:37:37 UTC, Jeff Jones wrote:
 Is it possible to modify either a C++ compiler or in code to 
 make classes more D ish? e.g., instead of just the 
 vtable(which, IIRC is slightly different between the two) add 
 the metadata pointer and whatever else?

 What I'm getting at, is there a somewhat simple way(not 
 creating a new C++ but either through macros, small compiler 
 changes, or whatever) to make C++ objects D compatible instead 
 of the other way?

 I.e., we can't take anything away from D classes since we will 
 break D. But we should be able to add to C++ classes without 
 breaking C++. If we can add the info that D needs then both 
 should get along happily? I'm mainly talking about a simple 
 code based solution but I'm also curious about how easily this 
 would be by compiler modification.
As far as I understand, the memory layout is already compatible. The problematic part seems to be construction. I'd say it's difficult to change anything about that on the C++ side, but I'll let others answer who are more familiar with the inner workings of the compiler.
Apr 01 2015
prev sibling next sibling parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Jeff Jones"  wrote in message news:bdfjhhkmmwzhdmqtvbzx forum.dlang.org...

 Is it possible to modify either a C++ compiler or in code to make classes 
 more D ish? e.g., instead of just the vtable(which, IIRC is slightly 
 different between the two) add the metadata pointer and whatever else?

 What I'm getting at, is there a somewhat simple way(not creating a new C++ 
 but either through macros, small compiler changes, or whatever) to make 
 C++ objects D compatible instead of the other way?
It sounds like what you're suggesting is similar to the way COM interfaces can be implemented in C - manually matching everything that C doesn't have, like vtables. You could do it with C++ for D, or patch a C++ compiler to do it automatically, but I'd guess there's an easier way for whatever you're trying to accomplish.
Apr 01 2015
parent reply "017hnoor" <hanifnoor077 gmail.com> writes:
  I'm mainly talking about a simple
 code based solution but I'm also curious about how easily this 
 would be by compiler modification.
______________________ NOOR
Apr 02 2015
parent reply "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"017hnoor"  wrote in message news:vxieyipmafnyksquehfq forum.dlang.org...

  I'm mainly talking about a simple
 code based solution but I'm also curious about how easily this would be by 
 compiler modification.
It would be easier to comment on if I knew what problem you were trying to solve. Porting an application? D plugin for C++ app? C++ library D app?
Apr 02 2015
parent "Jeff Jones" <Jeff Jones.com> writes:
On Thursday, 2 April 2015 at 12:15:31 UTC, Daniel Murphy wrote:
 "017hnoor"  wrote in message 
 news:vxieyipmafnyksquehfq forum.dlang.org...

 I'm mainly talking about a simple
 code based solution but I'm also curious about how easily this 
 would be by compiler modification.
It would be easier to comment on if I knew what problem you were trying to solve. Porting an application? D plugin for C++ app? C++ library D app?
NOOOO!!!! You are completely off your rocker!! I'm not trying to solve any problems! I'm simply curious if the above is possible? We know we can bind D to C but if we could write C++ code that is compatible with D at the binary level then maybe it would be easier to bind C to D? IIRC the issue was that C++'s class layout doesn't have a metadata pointer and it's vtable is just a list(can't contain pointers to other vtables or whatever). But suppose it was possible to "finagle" C++ so that we had the metadata pointer in the right place. e.g., suppose we could do something like this in C++: class CWrapper { .... void* operator new(std::size_t sz) { // e.g., int size = sizeof(CWrapper) + 4; void* p = malloc(size); p[0] = CWrapperMetaDataPtr; return p+2*4; } } So, the idea with the above scratch-code is that we have CWrapper laid out like MetaData ptr vtable ptr CWrapper Which, I think is now similar to D. In theory, if the metadata is correct then D should be able to use CWrapper directly as it would then look like a D Class? We could even probably mimic the build in operators in D in C++? E.g., opdispatch can be added. Obviously the C++ compiler has no idea about that so we would have to call it explicitly and this may be bug prone(confusing C++ with D) but we could call it explicitly if we need it on the C++ side. Anyways, Just an idea. I'm curious to how well it well and easy it would work. There is no real point at it, no application, etc. Just call it a "thought experiment".
Apr 02 2015
prev sibling parent "Elie Morisse" <syniurge gmail.com> writes:
On Tuesday, 31 March 2015 at 20:37:37 UTC, Jeff Jones wrote:
 Is it possible to modify either a C++ compiler or in code to 
 make classes more D ish? e.g., instead of just the 
 vtable(which, IIRC is slightly different between the two) add 
 the metadata pointer and whatever else?

 What I'm getting at, is there a somewhat simple way(not 
 creating a new C++ but either through macros, small compiler 
 changes, or whatever) to make C++ objects D compatible instead 
 of the other way?

 I.e., we can't take anything away from D classes since we will 
 break D. But we should be able to add to C++ classes without 
 breaking C++. If we can add the info that D needs then both 
 should get along happily? I'm mainly talking about a simple 
 code based solution but I'm also curious about how easily this 
 would be by compiler modification.
You can't make the layout of C++ classes inheriting from multiple bases match D's class layouts because D's multiple inheritance is more limited. Also a big downside is that "D-flavored ABI" C++ binaries couldn't interact easily with "normal ABI" C++ binaries because even if the modified C++ compiler is explicitly told which library/set of headers is normal and which isn't, the usage of any class from the normal C++ libs by D-flavored ones will form a mixture of both ABI so not usable by D, and it's not helping that some classes can be codegen'd into several libs. So every C++ lib would need to be recompiled to avoid the ramifications of interacting with both ABI? From my time poking around Clang's code it just seems easier and more convenient to reimplement C++ multiple inheritance and downcasts in DMD.
Apr 03 2015