[GSoC] Header Generation for C/C++

Eduard Staniloiu edi33416 at gmail.com
Thu Sep 5 11:56:28 UTC 2019

> On Thursday, 5 September 2019 at 11:25:47 UTC, Eduard Staniloiu 
> wrote:
> [...]
> I'm currently working on correctly generating the ctors and 
> dtors declarations.

More details about the above.

When I began working on the project, the code was generating two 
ctors for each `struct`
   1. a default ctor that was initializing all the struct fields 
with their default value
   2. a ctor that took all the struct field types as arguments and 
assigned the supplied arguments to each struct field (Ex. 
`this->x = x`).

The above two replicate (almost) the D behavior of constructing a 
struct that has no ctor defined. I say almost because you can 
either use the default ctor, or you **must** pass all the field 
arguments to the ctor (in D you can pass as many as you want, not 

When I first saw this and ran it with Razvan and Andrei and we 
said "Thats odd. The tool should only generate declarations, not 
definitions". I asked Iain about it and he said he couldn't 
remember why it was there, but probably he needed it at some 
point. So, in the light of this, we took the decision of dropping 
the ctor definition generation until further notice.

Well, this week I had an epiphany: for any struct, we have to 
generate the default ctor definition because on the D side, 
fields are default initialized to their `.init` value. If one 
would construct a struct (Ex. on the stack) on the C++ side, then 
the code could break.

Let's see the following simple struct example
// D module

extern (C++) struct S
   int* ptr;
   void apiFun() {
     if (ptr) {
       /* do stuff */

void bar()
   S s;

// C++ code

// Generated struct header
struct S
   int* ptr;
   void apiFun();

void gun()
   S s;

The `bar` function, being on the D side, is fine.
The `gun` function on the other hand, will be UB.

This is bad.

Starting from this, I came to the following conclusions:
   - For any `extern (C++)` struct, a default ctor must be 
   - If a struct defines a dtor, a declaration must be generated: 
`virtual ~A();`
   - For any defined ctors, a declaration must be generated

I think that if a struct defines a copy ctor, an opAssign, then 
an equivalent C++ declaration must be generated.

What do you think, folks?


More information about the Digitalmars-d mailing list