pragma(mangle_prefix, "myclib_")

Tim tim.dlang at t-online.de
Sat Jul 23 09:36:55 UTC 2022


On Friday, 22 July 2022 at 22:47:35 UTC, ryuukk_ wrote:
> Due to lack of proper namespacing in C, library authors prefix 
> their symbols with the name of the library
>
> Example: 
> https://github.com/floooh/sokol/blob/master/sokol_gfx.h#L2471 
> (large file)
>
> ```C
> SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc);
> SOKOL_GFX_API_DECL void sg_shutdown(void);
> SOKOL_GFX_API_DECL bool sg_isvalid(void);
> SOKOL_GFX_API_DECL void sg_reset_state_cache(void);
> SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const 
> sg_trace_hooks* trace_hooks);
> SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name);
> SOKOL_GFX_API_DECL void sg_pop_debug_group(void);
> ```
>
> Would be cool to be able to do something like this:
>
>
> ```D
> module sokol;
>
> pragma(mangle_prefix, "sg_"):
> extern(C):
>
> void setup(const(desc)* desc);
> void shutdown();
> bool isvalid();
> void reset_state_cache();
> trace_hooks install_trace_hooks(const trace_hooks* trace_hooks);
> void push_debug_group(const(char)* name);
> void pop_debug_group();
> ```

This could be useful. An even more flexible approach could be to 
allow a template, which generates the mangling for a symbol, like 
this:

```D
template MangleCallback(alias Symbol)
{
     enum MangleCallback = "myclib_" ~ __traits(identifier, 
Symbol);
}

pragma(mangle_callback, MangleCallback):
extern(C):
void f();
```

This could be useful for libraries like OpenGL, which use 
suffixes with type information. Functions like glUniform1ui and 
glUniform4uiv could then use the same overload in D, but the 
mangle callback template would generate the correct name in C.

Maybe it would even be possible to implement the mangling for 
extern(C++) in druntime instead of dmd with this.


More information about the Digitalmars-d mailing list