VisibilityMacros.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ========================
  2. Symbol Visibility Macros
  3. ========================
  4. .. contents::
  5. :local:
  6. Overview
  7. ========
  8. Libc++ uses various "visibility" macros in order to provide a stable ABI in
  9. both the library and the headers. These macros work by changing the
  10. visibility and inlining characteristics of the symbols they are applied to.
  11. Visibility Macros
  12. =================
  13. **_LIBCPP_HIDDEN**
  14. Mark a symbol as hidden so it will not be exported from shared libraries.
  15. **_LIBCPP_FUNC_VIS**
  16. Mark a symbol as being exported by the libc++ library. This attribute must
  17. be applied to the declaration of all functions exported by the libc++ dylib.
  18. **_LIBCPP_OVERRIDABLE_FUNC_VIS**
  19. Mark a symbol as being exported by the libc++ library, but allow it to be
  20. overridden locally. On non-Windows, this is equivalent to `_LIBCPP_FUNC_VIS`.
  21. This macro is applied to all `operator new` and `operator delete` overloads.
  22. **Windows Behavior**: Any symbol marked `dllimport` cannot be overridden
  23. locally, since `dllimport` indicates the symbol should be bound to a separate
  24. DLL. All `operator new` and `operator delete` overloads are required to be
  25. locally overridable, and therefore must not be marked `dllimport`. On Windows,
  26. this macro therefore expands to `__declspec(dllexport)` when building the
  27. library and has an empty definition otherwise.
  28. **_LIBCPP_INLINE_VISIBILITY**
  29. Mark a function as hidden and force inlining whenever possible.
  30. **_LIBCPP_ALWAYS_INLINE**
  31. A synonym for `_LIBCPP_INLINE_VISIBILITY`
  32. **_LIBCPP_TYPE_VIS**
  33. Mark a type's typeinfo and vtable as having default visibility.
  34. `_LIBCPP_TYPE_VIS`. This macro has no effect on the visibility of the
  35. type's member functions. This attribute cannot be used on class templates.
  36. **GCC Behavior**: GCC does not support Clang's `type_visibility(...)`
  37. attribute. With GCC the `visibility(...)` attribute is used and member
  38. functions are affected.
  39. **_LIBCPP_TEMPLATE_VIS**
  40. The same as `_LIBCPP_TYPE_VIS` except that it may be applied to class
  41. templates.
  42. **Windows Behavior**: DLLs do not support dllimport/export on class templates.
  43. The macro has an empty definition on this platform.
  44. **_LIBCPP_ENUM_VIS**
  45. Mark the typeinfo of an enum as having default visibility. This attribute
  46. should be applied to all enum declarations.
  47. **Windows Behavior**: DLLs do not support importing or exporting enumeration
  48. typeinfo. The macro has an empty definition on this platform.
  49. **GCC Behavior**: GCC un-hides the typeinfo for enumerations by default, even
  50. if `-fvisibility=hidden` is specified. Additionally applying a visibility
  51. attribute to an enum class results in a warning. The macro has an empty
  52. definition with GCC.
  53. **_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS**
  54. Mark the member functions, typeinfo, and vtable of the type named in
  55. a `_LIBCPP_EXTERN_TEMPLATE` declaration as being exported by the libc++ library.
  56. This attribute must be specified on all extern class template declarations.
  57. This macro is used to override the `_LIBCPP_TEMPLATE_VIS` attribute
  58. specified on the primary template and to export the member functions produced
  59. by the explicit instantiation in the dylib.
  60. **GCC Behavior**: GCC ignores visibility attributes applied the type in
  61. extern template declarations and applying an attribute results in a warning.
  62. However since `_LIBCPP_TEMPLATE_VIS` is the same as
  63. `__attribute__((visibility("default"))` the visibility is already correct.
  64. The macro has an empty definition with GCC.
  65. **Windows Behavior**: `extern template` and `dllexport` are fundamentally
  66. incompatible *on a template class* on Windows; the former suppresses
  67. instantiation, while the latter forces it. Specifying both on the same
  68. declaration makes the template class be instantiated, which is not desirable
  69. inside headers. This macro therefore expands to `dllimport` outside of libc++
  70. but nothing inside of it (rather than expanding to `dllexport`); instead, the
  71. explicit instantiations themselves are marked as exported. Note that this
  72. applies *only* to extern template *classes*. Extern template *functions* obey
  73. regular import/export semantics, and applying `dllexport` directly to the
  74. extern template declaration is the correct thing to do for them.
  75. **_LIBCPP_CLASS_TEMPLATE_INSTANTIATION_VIS**
  76. Mark the member functions, typeinfo, and vtable of an explicit instantiation
  77. of a class template as being exported by the libc++ library. This attribute
  78. must be specified on all template class explicit instantiations.
  79. It is only necessary to mark the explicit instantiation itself (as opposed to
  80. the extern template declaration) as exported on Windows, as discussed above.
  81. On all other platforms, this macro has an empty definition.
  82. **_LIBCPP_EXTERN_TEMPLATE_INLINE_VISIBILITY**
  83. Mark a member function of a class template as visible and always inline. This
  84. macro should only be applied to member functions of class templates that are
  85. externally instantiated. It is important that these symbols are not marked
  86. as hidden as that will prevent the dylib definition from being found.
  87. This macro is used to maintain ABI compatibility for symbols that have been
  88. historically exported by the libc++ library but are now marked inline.
  89. **_LIBCPP_EXCEPTION_ABI**
  90. Mark the member functions, typeinfo, and vtable of the type as being exported
  91. by the libc++ library. This macro must be applied to all *exception types*.
  92. Exception types should be defined directly in namespace `std` and not the
  93. versioning namespace. This allows throwing and catching some exception types
  94. between libc++ and libstdc++.
  95. Links
  96. =====
  97. * `[cfe-dev] Visibility in libc++ - 1 <http://lists.llvm.org/pipermail/cfe-dev/2013-July/030610.html>`_
  98. * `[cfe-dev] Visibility in libc++ - 2 <http://lists.llvm.org/pipermail/cfe-dev/2013-August/031195.html>`_
  99. * `[libcxx] Visibility fixes for Windows <http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20130805/085461.html>`_