D-Programming-Language/dmd — pull request #3121 by ibuclaw (2014-01-19 11:49:38+00:00)
Title: Fix CppMangleVisitor::substitute and Remove CppMangleVisitor::exists

``CppMangleVisitor::substitute`` has been the reason behind a lot of mangling bugs, in particular, when the use of ``const`` comes into the mix, or when the type is complex in depth. TypeEnum, TypeStruct, TypeClass, TypePointer, TypeFunction, etc are victims to this.

The fix for this has always been to replace:
```
if (!substitute(t))
{
/* ... */
}
```
with
```
if (!exist(t))
{
/* ... */
store(t);
}
else
substitute(t);
```

This is because the way C++ mangling works, it's always in the logic of:
```
if (found substitute)
=> write substitute
else
=> write mangle
=> store substitute
```
But doing it in the way it's currently done creates a bit of unwanted overhead in both runtime and baggage in the source code.

So I say lets just clear up this confusion with ``substitute`` and make it behave more like ``exists`` when it comes to dealing with a symbol not yet ``stored``'d. This then hands over the storing of the substitute to the caller of ``substitute``. We then get the benefit of cleaning up the code and ensuring correct behaviour. :)

This pull has three patches, I'm just committing them one at a time to ensure things don't break at each step in the process.