C++ Binding Generator

Paul O'Neil redballoon36 at gmail.com
Fri Feb 14 15:52:47 PST 2014


I've been thinking about how to generate bindings for C++ (esp. Qt!) for 
a little while now.  Unfortuneatly, I won't have time to implement 
anything until my semester is over, but with the recent talk of 
projects, maybe someone else will pick it up before then.

Qt bindings come up every so often; they are not going to be written 
manually.  The existing Qdd[1] was a port of Qt Jambi[2], which was the 
Java binding.  This makes some sense, since Java is also an 
object-oriented language that isn't C++.  Qt Jambi consists of two 
parts: a generator and generated code.  The generator reads the Qt 
headers as well as XML files that describe how to map the C++ into Java. 
  The generator creates both C++ and Java files that get compiled into a 
JNI library.  Given that D is conceptually closer to C++ than Java, I 
think that binding to D should be easier than Java.  Another issue here 
is that both Qt Jambi and QtD target Qt 4 and not version 5.

Also relevant is that the libclang bindings in Deimos[3] are 2 years and 
several versions old (3.1 -> 3.4 is out).  This is probably due to the 
lack of interest in libclang from D programmers (No surprise.  Who wants 
to write code in D to parse C(++)?).  It binds to libclang's C interface 
and may have been generated with DStep.

Here's my proposed path forward:
Write a binding generator in C++ that uses libclang to parse headers and 
auxillary XML / json / etc. files to guide the generation of D source. 
Clang provides data structures for traversing the C++ AST.  This tool 
could be used to bind Qt 5 and libclang so the conversion tool can 
eventually be migrated to D.  My hope is that using clang to avoid 
writing another C++ parser and D's similarity to C++ make this an easier 
(though still significant) undertaking than the Java bindings.

I suspect that there are several patterns in C++ and D that are 
implemented using slightly different constructions in each of them.  For 
instance, figuring out which C++ classes should map to D structs. 
Automatically identifying these or finding a description language for 
them would be a huge benefit to automation.

On the other hand, I don't yet see a way to map generic containers (such 
as QList<T>) into D yet.  But Qt Jambi found some way to solve this 
problem even when dealing with type erasure in Java.

Thoughts?

Thanks for reading my wall of text.
Paul O'Neil

[1]
QtD is not actively maintained.  It existed on
BitBucket: https://bitbucket.org/qtd/repo
before someone else forked it and moved to
GitHub: https://github.com/qtd-developers/qtd

[2]
Homepage (appears dead): http://qt-jambi.org/
Gitorious (shows recent activity): https://qt.gitorious.org/qt-jambi

[3]
Github: https://github.com/D-Programming-Deimos/libclang

[4]
Clang: http://clang.llvm.org
An example of using Clang to find CXXRecordDecl (classes, unions, 
structs, and the like):
http://clang.llvm.org/docs/RAVFrontendAction.html


More information about the Digitalmars-d mailing list