Commit graph

31 commits

Author SHA1 Message Date
Manu Evans
092b923b2f This was in fact necessary to find symbols outside the local namespace. 2018-11-05 23:17:24 -08:00
Manu Evans
db36d97324 Tests 2018-11-01 23:37:27 -07:00
Geod24
b75c9f1107 Fix issue 16479: No namespace substitution for C++ mangling on POSIX
The C++ ABI used by POSIX is the Itanium C++ ABI.
Reference document available here: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling

One important (and tricky) part of the ABI is the substitutions being done,
in order to reduce the bloat introduced by long symbol names,
a typical issue when using templates heavily (of which D was not exempt).

There are 2 kinds of substitutions: component substitution and template parameter substitution.
Component substitution replaces repeated parts of the symbol with `S[X]_`,
template parameter substitution replaces occurences of template parameters with `T[X]_`.
`X` represents a base36 index into the array of components or
template parameters already encountered so far, respectively.

This substitution is done on an identity basis, which means that the templated function
`template<typename T> int foo()` instantiated with `int` will be mangled as `_Z3fooIiE*i*v`
(asterisks are emphasis and not part of the mangling) while it would be mangled as `_Z3fooIiE*T_*v`
if the definition was `template<typename T> T foo()`.

Moreover, experience with C++ compilers shows that component substitution is prefered over
template parameter substitution, such as `template<typename T> T foo(T)` is mangled as
`_Z3fooIiET_*S0_*` when instantiated with `int` and not `_Z3fooIiET_*T_*` as would be the case
if template substitution was prefered.

This is just brushing the surface of the problem, since only template type parameters have been
mentioned so far, but other kind (aliases, values) are also concerned.
Substitution also needs to happen if a template parameter is part of another type,
such as the `template<typename T> array<T>* foo (T, int)`, which, when instantiated with `int`,
is mangled as `_Z3fooIiEP5arrayIT_ES1_i`.

For more detailed test cases, see `test/compilable/cppmangle.d`.

The main issue encountered while implementing this in DMD is that there's no easy way to know
if a type (which is part of the function's type, e.g. parameters and return value)
was a template parameter or not, as DMD merges types, so in the previously mentioned
`template<typename T> int foo()` vs `template<typename T> T foo()` the template instantiation
will come with the same exact two pointer to the singleton `int` type.

Moreover, DMD does destructive semantic analysis, meaning that objects gets mutated,
pointers get replaced, aliases get resolved, and information gets lost.

After different approaches where taken, the most practical and reliable approach devised was to
provide a `visit` overload for non-resolved AST type `TypeIdentifier` and `TypeInstance`,
and compare the identifier to that of the template definition.
Fallback to post-semantic type when it isn't found.

Note that no attempt has been made whatsoever to handle the mess that would result from
expressions themselves being mangled. The reference doc for the ABI mentions that
"[...] this mangling is quite similar to the source token stream. (C++ Standard reference 14.5.5.1p5.)".
Original quote:
https://itanium-cxx-abi.github.io/cxx-abi/abi.html#expressions (5.1.6 Expressions)
2018-10-29 19:28:50 +09:00
Iain Buclaw
7cd2d9659e [testsuite] Part 2 - Replace issue numbers with bugzilla urls 2018-10-21 02:19:06 +02:00
Iain Buclaw
1d1618ac29 [testsuite] Replace issue numbers with bugzilla urls 2018-10-19 01:09:19 +02:00
Ilya Yaroshenko
9cb46f8427 fix Issue 19248 2018-09-15 20:31:04 +07:00
Nicholas Lindsay Wilson
09921ce45c Fix issue 19043 2018-07-02 20:14:24 +08:00
Rainer Schuetze
ab0a7bf76d fix issue 15589 - cleanup after PR #8277
fix mangling, add shim functions for vtbl and type info, fix dtor inside Attribdeclaration, use Id-pool
2018-06-13 09:04:37 +02:00
Manu Evans
4b17e0b1a0 Fix issue 18957 - extern(C++) doesn't mangle 'std' correctly on posix systems 2018-06-07 22:55:53 -07:00
The Dlang Bot
6663ebf227
Merge pull request #8334 from TurkeyMan/cpp_mangle_operators
Posix extern(C++) operator mangling
merged-on-behalf-of: Mathias LANG <pro.mathias.lang@gmail.com>
2018-06-08 03:44:19 +02:00
Manu Evans
575e632d2d Posix operator mangling. 2018-06-06 22:22:12 -07:00
Geod24
9a1b4467cc Fix issue 18922: No substitutions for C++ namespaces in different module/file
DMD was just comparing the object by their reference instead of their string value.
Obviously when a namespace is declared in another module, the reference is different.
2018-06-06 18:35:10 +02:00
Manu Evans
0131e30e7e Tests 2018-05-26 15:16:55 -07:00
Manu Evans
9c43dbd58d Fix issue 18890 - extern(C++) mangles all destructors the same 2018-05-21 17:04:05 -07:00
Manu Evans
8863b8ed92 Fix issue 18891 - extern(C++) destructor prototype should just link; not generate field/aggregate dtor 2018-05-21 11:25:01 -07:00
Manu Evans
9e6ba61caf Fix issue 18888 - extern(C++) template arg/alias arg mangling issue 2018-05-21 00:59:59 -07:00
Manu Evans
0d9b4b39cb Fix issue 14086 - Invalid extern C++ name for constructor and virtual destructor 2018-05-20 17:50:42 -07:00
Manu Evans
c38ae836e3 Fix issue 15388 - extern(C++) - typeof(null) should mangle as nullptr_t 2018-05-08 15:30:37 -07:00
Walter Bright
5bb9d291f2 correct namespace mangling for Issue 17772 2018-02-22 23:55:44 -08:00
Iain Buclaw
db2976599d fix Issue 8207 - extern(D) symbols should include another underscore
1. Removed ABI name fiddling from the front-end.
    2. Removed `Target.prefixName` and C++ tests for matching `__Z`.
    3. Prepend prefixes for C++ and D symbols in the backend only
       for the targets that require it (Win32, OSX).
    4. Removed `RTLSYM__DINVARIANT` runtime library symbol, name is now
       the same on all targets.
    5. Correctly set C++ mangling on `LINK.cpp` symbols, before they
       were all being set as D mangled symbols.
    6. Use System mangling as synonym for do not modify symbol.
    7. Added workaround for `extern(D) ___tls_get_addr`.
2018-02-10 23:06:06 +01:00
Walter Bright
84a8686a9a fix Itanium C++ mangling 2 2017-11-01 01:50:26 -07:00
Walter Bright
e007885161 fix Itanium C++ mangling 2017-10-31 02:52:52 -07:00
Walter Bright
23cfd705a0 fix Itanium C++ mangling 2017-10-29 01:22:35 -07:00
Walter Bright
5af925ab55 expand cppmangle.d testing 2017-04-24 02:20:59 -07:00
Iain Buclaw
9f58989dbb Add C++ compilable test that was indirectly fixed by 15789 2017-01-08 19:07:00 +01:00
Guillaume Chatelet
a38205be75 fix Issue 13337 - Invalid extern C++ namespace resolution 2015-01-24 22:44:45 +01:00
Joakim
c9a9cd97bc Replace most usage of std.c.* with the druntime equivalents, in preparation for deprecation of the std.c.* modules. 2014-10-11 06:14:58 -05:00
k-hara
eca6fa41ec fix Issue 12436 - Opaque struct parameter type should not be allowed 2014-05-27 19:02:38 +09:00
Iain Buclaw
99826375f9 Fix Issue 11696 - incorrect static C++ member mangling 2013-12-10 15:08:18 +00:00
Iain Buclaw
d6d082a803 Fix pull request #2007 2013-06-01 11:09:22 +01:00
ibuclaw
2e5d2ded05 Fix Issue 10058. 2013-05-14 20:08:27 +01:00