[Dlang-internal] Best way to pass template parameters information to the mangler

Mathias Lang pro.mathias.lang at gmail.com
Wed Jun 27 04:50:54 UTC 2018


Hi everyone,
I'm currently looking at expanding the C++ interop to work for 
what I thought was a very limited used case. While C++ interop 
works great for non-templated code, any non-trivial code 
involving templates is broken on POSIX, because template 
parameters are not substituted.

Take for example:
```
extern(C++) T* foo (T) ();
pragma(msg, foo!(int).mangleof);
```

This should mangle to `_Z3fooIiEPT_v` but DMD mangles it to 
`_Z3fooIiEPiv`. I crafted a bunch of more advanced test cases 
(https://github.com/Geod24/dmd/commit/9928cb28ba099676caaa8d0defdc8e48c5f3de6d) but that's the gist of it. It's reported as https://issues.dlang.org/show_bug.cgi?id=16479

I've done a bit of digging and saw that the frontend "installs" 
an alias to the actual type in the scope 
(https://github.com/dlang/dmd/blob/fb22391c75e91ffe27c78e3daf02f44ef70c7c17/src/dmd/dtemplate.d#L2003-L2006), and AFAICT that alias is later peeled off (via `toAlias`), meaning when we reach the mangling phase there's no way to tell if the type was a template parameter or not.

In order to fix the previously mentioned issue, I need to keep 
the information that the type is a template parameter. I am 
considering keeping the `AliasDeclaration` around and adding a 
`dependent` field, but I suspect that would require extensive 
changes to not peel it off. The least invasive solution so far 
would be to add yet another field to keep this information, but 
it sounds hacky and brittle.

If anyone has any suggestion, it is very welcome!


More information about the Dlang-internal mailing list