pragma(mangle) on types

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Tue Jul 11 00:28:59 PDT 2017


In D it's possible to use pragma(mangle) on a function to set the name 
that will actually end up in the binary. This is useful if you need to 
link to a C function that uses the same name as a keyword in D.

I think we need the same thing for types as well. This is important when 
binding to C++ and Objective-C.

For C++ it's due the the types are mangled as part of function 
signatures. Other than conflicting D keywords, it's also important for 
another reason. In C, the name of structs live in their own namespace, 
making it possible to have a struct and a function with the same name. 
Since you always need to use "struct <name>" in C when referring to the 
type it works.

This caused a problem [1]. There's a function called "stat", which also 
accepts an argument of the type "stat". In D, the struct is declared 
"stat_t" to avoid the conflict with the function. When using C functions 
this works perfectly fine since no mangling is used. But when using a 
C++ function taking an argument of the type "stat_t" this will generate 
the wrong mangling since it's expected to be "stat" on the C++ side.

For Objective-C a similar scenario can occur. Because in Objective-C 
protocols (what we call interfaces in D) and classes live in different 
namespaces. In Objective-C there's a root class called "Object" just as 
in D. But in Objective-C this class also conforms to a protocol called 
"Object". For this to work properly from the D side, one needs to be 
renamed in the D code and use pragma(mangle) to set the actual name that 
would end up in the binary.

I suggest that we add support for using pragma(mangle) on types as well. 
Thoughts?

I have already implemented the support for Objective-C classes in an old 
branch several years ago.

[1] https://issues.dlang.org/show_bug.cgi?id=16650

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list