Generating .di header files from ImportC files

Adam Wilson flyboynw at gmail.com
Wed Oct 25 00:29:17 UTC 2023


On Saturday, 21 October 2023 at 02:13:24 UTC, Walter Bright wrote:
> Bruce and Adam and I met at a Red Robin in Seattle to chat 
> about D, and they both made a case for the D compiler being 
> able to translate C files into .di "header" files.

If anybody else is in the area on the third Wednesday of the 
month we usually meetup at the NWCPP talk and then head out 
somewhere afterwards to chat. Usually D, sometimes not. If you 
want more details you can DM me on Discord.

> The idea is to:
>
> 1. be able to work with D IDE programs that can only deal with 
> D code
>
> 2. be able to handle the vast bulk of converting .h to D code, 
> and then tweak by hand the bits that didn't translate well

3. Can be useful for debugging calls to ImportC methods so you 
can see where you messed up at the call site.

> Anyhow, I pontificated that this couldn't be done, because some 
> C code is not representable as D code. Silly me.
>
> But I got to thinking about it. It turns out that DMD can 
> currently do this:
>
>     dmd -c test.c -Hf=test.di
>
> and voila! It creates a .di file from the C file. Who knew?

This is pretty much how I stumbled on to it at DConf this year. I 
briefly started with a hand translation of the ODBC 4 headers, 
got bored, then Nick Wilson suggested I look into ImportC, and 
then I just decided to smash the -Hf onto the end to see if it 
would work. The first file I tried it on worked, so I kept going. 
Eventually I ran into an anonymous tagged enum that D doesn't 
support, but the trick was to just comment it out and retry.

D simply doesn't support every C semantic, and that's OK, it's 
not supposed to, so where it doesn't, some better error messages 
are all we need to make this system more accessible. But it 
works! One place I can see something like this being popular is 
building DUB-based Code-D enabled (VSCode and others) bindings 
for non-source-available C libraries. There is a universe of C 
libraries waiting for us to generate bindings and bring them to D.

And if you miss DConf, you're really missing out. The 
interactions are top-shelf, and even if you don't learn anything 
from the talks (but you will), the discussions you have with the 
attendees more than make up the price of admission.

> It could use some adjustment, but the bulk is there and it 
> didn't crash or launch nuclear missiles. (Phew!)

Some helpful adjustments (if possible):
- Ability to set an explicit module name for the DI file. (Useful 
for creating properly namespaced bindings for libraries like 
ODBC.)
- One module per header file. For example, in the ODBC headers, 
they all reference a common header for types. If D could follow 
that on the output side we wouldn't have undo the duplicated 
types by hand. Don't know if it's possible, but it'd be a serious 
time saver.

Mix all that together and there are some significant gains to 
made to the language's  capabilities just by importing C 
libraries. I love it!

-AW


More information about the Digitalmars-d mailing list