C++ header generation: Mixin template to create C ABI from D class fails due to pragma(mangle) seeming not to work?

rikki cattermole rikki at cattermole.co.nz
Sun Nov 7 19:46:49 UTC 2021


On 08/11/2021 8:25 AM, Gavin Ray wrote:
> Hello all.
> 
> Rikki Cattermole gave me this useful mixin, which can wrap a D class and 
> create a "flattened" C ABI for it.
> 
> The issue is, trying to use:
> ```d
> pragma(mangle, "create_" ~ __traits(identifier, Type))
> void* creator(Parameters!(Type.__ctor) args) {}
> ```
> 
> Doesn't actually seem to apply the mangling =/

```d
class Type {
     this() {}
}

pragma(msg, creator.mangleof); // _D9onlineapp7creatorFZPv
pragma(mangle, "create_" ~ __traits(identifier, Type))
void* creator(Parameters!(Type.__ctor) args) {
     pragma(msg, creator.mangleof); // create_Type
	return cast(void*)(new Type(args));
}
pragma(msg, creator.mangleof); // create_Type
```

Order matters for this stuff unfortunately.

Now with regards to -HC
That is certainly a bug with the generator.

```asm
.text.create_Type	segment
	assume	CS:.text.create_Type
create_Type:
		push	RBP
		mov	RBP,RSP
		sub	RSP,010h
		mov	RDI,onlineapp.Type.__Class at GOTPCREL[RIP]
		call	  _d_newclass at PLT32
		mov	-8[RBP],RAX
		mov	RDI,RAX
		call	  onlineapp.Type onlineapp.Type.__ctor()@PLT32
		leave
		ret
		add	[RAX],AL
.text.create_Type	ends
```

https://issues.dlang.org/show_bug.cgi?id=22489


More information about the Digitalmars-d mailing list