Trying to add C++ header generation to dmd

Jacob Carlborg doob at me.com
Wed Mar 20 10:27:27 UTC 2019


On 2019-03-19 21:55, Gregor Mückl wrote:
> Hi!
> 
> I tried to hack a bit on dmd to have it generate a C++ header for a D 
> module. I've copied the visitor in hdrgen.d and did truly terrible 
> things to it, to the point where I don't feel confident that I want to 
> show that code to anyone.
> 
> So now I get something almost resembling C++ code for a few really 
> trivial definitions in the input D module. But that is for all 
> definitions, not just the ones marked external(C++).
> 
> This is the point where I'm actually stumped now. The 
> PrettyPrintVisitor, which I used as a template, does not really make it 
> easy to discover the context of a visited AST node. In particular, I 
> don't know how to determine if one of the parent nodes possess the 
> required extern(C++) linkage specification to be considered for exporting.
> 
> If there was a way for the visitor to know when the subtree below a 
> certain node has been visited completely, it could then trivially track 
> linkage as a kind of a contextual information.
> 
> So to anyone who knows dmd better than me: how would you go about 
> implementing the visitor in such a way that it only exports the correct 
> AST subtrees?

I'm not sure if you need to look at the parent. Every declaration has a 
"linkage" field [1] identifying if it's C++, D or something else. 
Classes and structs, which don't inherit from Declaration, has a 
"classKind" field [2].

You can also have a look at this work in progress PR [3].

[1] 
https://github.com/dlang/dmd/blob/3a298e504707ccd8d633c6ea5511d46c5758fa8b/src/dmd/declaration.d#L289

[2] 
https://github.com/dlang/dmd/blob/3a298e504707ccd8d633c6ea5511d46c5758fa8b/src/dmd/aggregate.d#L86

[3] https://github.com/dlang/dmd/pull/8591

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list