c++defs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifndef _GL_CXXDEFS_H
  2. #define _GL_CXXDEFS_H
  3. /* The three most frequent use cases of these macros are:
  4. * For providing a substitute for a function that is missing on some
  5. platforms, but is declared and works fine on the platforms on which
  6. it exists:
  7. #if @GNULIB_FOO@
  8. # if !@HAVE_FOO@
  9. _GL_FUNCDECL_SYS (foo, ...);
  10. # endif
  11. _GL_CXXALIAS_SYS (foo, ...);
  12. _GL_CXXALIASWARN (foo);
  13. #elif defined GNULIB_POSIXCHECK
  14. ...
  15. #endif
  16. * For providing a replacement for a function that exists on all platforms,
  17. but is broken/insufficient and needs to be replaced on some platforms:
  18. #if @GNULIB_FOO@
  19. # if @REPLACE_FOO@
  20. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  21. # undef foo
  22. # define foo rpl_foo
  23. # endif
  24. _GL_FUNCDECL_RPL (foo, ...);
  25. _GL_CXXALIAS_RPL (foo, ...);
  26. # else
  27. _GL_CXXALIAS_SYS (foo, ...);
  28. # endif
  29. _GL_CXXALIASWARN (foo);
  30. #elif defined GNULIB_POSIXCHECK
  31. ...
  32. #endif
  33. * For providing a replacement for a function that exists on some platforms
  34. but is broken/insufficient and needs to be replaced on some of them and
  35. is additionally either missing or undeclared on some other platforms:
  36. #if @GNULIB_FOO@
  37. # if @REPLACE_FOO@
  38. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  39. # undef foo
  40. # define foo rpl_foo
  41. # endif
  42. _GL_FUNCDECL_RPL (foo, ...);
  43. _GL_CXXALIAS_RPL (foo, ...);
  44. # else
  45. # if !@HAVE_FOO@ or if !@HAVE_DECL_FOO@
  46. _GL_FUNCDECL_SYS (foo, ...);
  47. # endif
  48. _GL_CXXALIAS_SYS (foo, ...);
  49. # endif
  50. _GL_CXXALIASWARN (foo);
  51. #elif defined GNULIB_POSIXCHECK
  52. ...
  53. #endif
  54. */
  55. /* _GL_EXTERN_C declaration;
  56. performs the declaration with C linkage. */
  57. #if defined __cplusplus
  58. # define _GL_EXTERN_C extern "C"
  59. #else
  60. # define _GL_EXTERN_C extern
  61. #endif
  62. /* _GL_FUNCDECL_RPL (func, rettype, parameters_and_attributes);
  63. declares a replacement function, named rpl_func, with the given prototype,
  64. consisting of return type, parameters, and attributes.
  65. Example:
  66. _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...)
  67. _GL_ARG_NONNULL ((1)));
  68. */
  69. #define _GL_FUNCDECL_RPL(func,rettype,parameters_and_attributes) \
  70. _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters_and_attributes)
  71. #define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters_and_attributes) \
  72. _GL_EXTERN_C rettype rpl_func parameters_and_attributes
  73. /* _GL_FUNCDECL_SYS (func, rettype, parameters_and_attributes);
  74. declares the system function, named func, with the given prototype,
  75. consisting of return type, parameters, and attributes.
  76. Example:
  77. _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...)
  78. _GL_ARG_NONNULL ((1)));
  79. */
  80. #define _GL_FUNCDECL_SYS(func,rettype,parameters_and_attributes) \
  81. _GL_EXTERN_C rettype func parameters_and_attributes
  82. /* _GL_CXXALIAS_RPL (func, rettype, parameters);
  83. declares a C++ alias called GNULIB_NAMESPACE::func
  84. that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
  85. Example:
  86. _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
  87. */
  88. #define _GL_CXXALIAS_RPL(func,rettype,parameters) \
  89. _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
  90. #if defined __cplusplus && defined GNULIB_NAMESPACE
  91. # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
  92. namespace GNULIB_NAMESPACE \
  93. { \
  94. rettype (*const func) parameters = ::rpl_func; \
  95. } \
  96. _GL_EXTERN_C int _gl_cxxalias_dummy
  97. #else
  98. # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
  99. _GL_EXTERN_C int _gl_cxxalias_dummy
  100. #endif
  101. /* _GL_CXXALIAS_RPL_CAST_1 (func, rpl_func, rettype, parameters);
  102. is like _GL_CXXALIAS_RPL_1 (func, rpl_func, rettype, parameters);
  103. except that the C function rpl_func may have a slightly different
  104. declaration. A cast is used to silence the "invalid conversion" error
  105. that would otherwise occur. */
  106. #if defined __cplusplus && defined GNULIB_NAMESPACE
  107. # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
  108. namespace GNULIB_NAMESPACE \
  109. { \
  110. rettype (*const func) parameters = \
  111. reinterpret_cast<rettype(*)parameters>(::rpl_func); \
  112. } \
  113. _GL_EXTERN_C int _gl_cxxalias_dummy
  114. #else
  115. # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
  116. _GL_EXTERN_C int _gl_cxxalias_dummy
  117. #endif
  118. /* _GL_CXXALIAS_SYS (func, rettype, parameters);
  119. declares a C++ alias called GNULIB_NAMESPACE::func
  120. that redirects to the system provided function func, if GNULIB_NAMESPACE
  121. is defined.
  122. Example:
  123. _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
  124. */
  125. #if defined __cplusplus && defined GNULIB_NAMESPACE
  126. /* If we were to write
  127. rettype (*const func) parameters = ::func;
  128. like above in _GL_CXXALIAS_RPL_1, the compiler could optimize calls
  129. better (remove an indirection through a 'static' pointer variable),
  130. but then the _GL_CXXALIASWARN macro below would cause a warning not only
  131. for uses of ::func but also for uses of GNULIB_NAMESPACE::func. */
  132. # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
  133. namespace GNULIB_NAMESPACE \
  134. { \
  135. static rettype (*func) parameters = ::func; \
  136. } \
  137. _GL_EXTERN_C int _gl_cxxalias_dummy
  138. #else
  139. # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
  140. _GL_EXTERN_C int _gl_cxxalias_dummy
  141. #endif
  142. /* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
  143. is like _GL_CXXALIAS_SYS (func, rettype, parameters);
  144. except that the C function func may have a slightly different declaration.
  145. A cast is used to silence the "invalid conversion" error that would
  146. otherwise occur. */
  147. #if defined __cplusplus && defined GNULIB_NAMESPACE
  148. # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
  149. namespace GNULIB_NAMESPACE \
  150. { \
  151. static rettype (*func) parameters = \
  152. reinterpret_cast<rettype(*)parameters>(::func); \
  153. } \
  154. _GL_EXTERN_C int _gl_cxxalias_dummy
  155. #else
  156. # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
  157. _GL_EXTERN_C int _gl_cxxalias_dummy
  158. #endif
  159. /* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
  160. is like _GL_CXXALIAS_SYS (func, rettype, parameters);
  161. except that the C function is picked among a set of overloaded functions,
  162. namely the one with rettype2 and parameters2. Two consecutive casts
  163. are used to silence the "cannot find a match" and "invalid conversion"
  164. errors that would otherwise occur. */
  165. #if defined __cplusplus && defined GNULIB_NAMESPACE
  166. /* The outer cast must be a reinterpret_cast.
  167. The inner cast: When the function is defined as a set of overloaded
  168. functions, it works as a static_cast<>, choosing the designated variant.
  169. When the function is defined as a single variant, it works as a
  170. reinterpret_cast<>. The parenthesized cast syntax works both ways. */
  171. # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
  172. namespace GNULIB_NAMESPACE \
  173. { \
  174. static rettype (*func) parameters = \
  175. reinterpret_cast<rettype(*)parameters>( \
  176. (rettype2(*)parameters2)(::func)); \
  177. } \
  178. _GL_EXTERN_C int _gl_cxxalias_dummy
  179. #else
  180. # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
  181. _GL_EXTERN_C int _gl_cxxalias_dummy
  182. #endif
  183. /* _GL_CXXALIASWARN (func);
  184. causes a warning to be emitted when ::func is used but not when
  185. GNULIB_NAMESPACE::func is used. func must be defined without overloaded
  186. variants. */
  187. #if defined __cplusplus && defined GNULIB_NAMESPACE
  188. # define _GL_CXXALIASWARN(func) \
  189. _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
  190. # define _GL_CXXALIASWARN_1(func,namespace) \
  191. _GL_CXXALIASWARN_2 (func, namespace)
  192. /* To work around GCC bug <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
  193. we enable the warning only when not optimizing. */
  194. # if !__OPTIMIZE__
  195. # define _GL_CXXALIASWARN_2(func,namespace) \
  196. _GL_WARN_ON_USE (func, \
  197. "The symbol ::" #func " refers to the system function. " \
  198. "Use " #namespace "::" #func " instead.")
  199. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  200. # define _GL_CXXALIASWARN_2(func,namespace) \
  201. extern __typeof__ (func) func
  202. # else
  203. # define _GL_CXXALIASWARN_2(func,namespace) \
  204. _GL_EXTERN_C int _gl_cxxalias_dummy
  205. # endif
  206. #else
  207. # define _GL_CXXALIASWARN(func) \
  208. _GL_EXTERN_C int _gl_cxxalias_dummy
  209. #endif
  210. /* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
  211. causes a warning to be emitted when the given overloaded variant of ::func
  212. is used but not when GNULIB_NAMESPACE::func is used. */
  213. #if defined __cplusplus && defined GNULIB_NAMESPACE
  214. # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
  215. _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
  216. GNULIB_NAMESPACE)
  217. # define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
  218. _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
  219. /* To work around GCC bug <http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
  220. we enable the warning only when not optimizing. */
  221. # if !__OPTIMIZE__
  222. # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
  223. _GL_WARN_ON_USE_CXX (func, rettype, parameters_and_attributes, \
  224. "The symbol ::" #func " refers to the system function. " \
  225. "Use " #namespace "::" #func " instead.")
  226. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  227. # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
  228. extern __typeof__ (func) func
  229. # else
  230. # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
  231. _GL_EXTERN_C int _gl_cxxalias_dummy
  232. # endif
  233. #else
  234. # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
  235. _GL_EXTERN_C int _gl_cxxalias_dummy
  236. #endif
  237. #endif /* _GL_CXXDEFS_H */