digitalmars.D.announce - Tool for porting C to D
- Laeeth Isharc (45/45) Jul 23 Hi.
Hi. It's not finished yet, but in case it helps someone, here's a little tool I worked on for porting C to D. It will port declarations too, for C and some of C++ (works for imgui more or less). I didn't write much of the code, but I wouldn't say it's exactly vibe-coded either. Credit to C2Rust for the inspiration. It uses clang front end to dump the C++ AST to json. (I will move to CBOR when the tool is more mature). Then D parses the json into a Cxx AST and on the happy path transforms that into a D AST, which is serialised to make your programme. It uses clang to advise on which/which order to process headers and it should pick up a compile db or in theory makefile or cmake. The fallback/less happy path is just raw text. I tried it on NASA spice library, 1700 odd numerical C files and most of them worked. Some problems with imports and dangling elses that I think/hope are mostly fixed or should be soon. There's a provenance tracing log facility (so you can see why it did what it did) and a fallback log (if the transformation didn't work for some reason then it just falls back to raw text, which will usually have more problems). It should preserve comments but I'm sure there's a bug or two left. There's an override facility for macros and some other things, not battle tested yet. There's an mcp server tool with the idea that an LLM could guide the process. In practice so far I just used it to port C in cli mode. Probably though, especially for C++ you need to make some decisions about what to do when generating bindings and if I had a better way to express that policy it could do more there. Generating high-level idiomatic D wrappers guided by policy could be another future extension. It's not the fastest - spice took 40 minutes or so, although that's not a small project. I tried on libgit2 - slightly worse than spice but not horrendous. My focus isn't mostly on the tool right now, but I hope to keep improving it over time. To build it cd tools/clangparse-dump and run make. Then dub build -c c2d in the root. Sorry for the state of the docs but I guess it's easy enough to figure out with modern tools. I use Linux so probably some minor fixes needed for Windows and MacOS. You need clang, bear etc. https://codeberg.org/dlang-public/clang-parser-d I have a tool for porting zig C declarations to D if anyone would find it helpful - much less used though.
Jul 23








Laeeth Isharc <noreply laeeth.com>