c++defs.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* C++ compatible function declaration macros.
  2. Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef _GL_CXXDEFS_H
  14. #define _GL_CXXDEFS_H
  15. /* Begin/end the GNULIB_NAMESPACE namespace. */
  16. #if defined __cplusplus && defined GNULIB_NAMESPACE
  17. # define _GL_BEGIN_NAMESPACE namespace GNULIB_NAMESPACE {
  18. # define _GL_END_NAMESPACE }
  19. #else
  20. # define _GL_BEGIN_NAMESPACE
  21. # define _GL_END_NAMESPACE
  22. #endif
  23. /* The three most frequent use cases of these macros are:
  24. * For providing a substitute for a function that is missing on some
  25. platforms, but is declared and works fine on the platforms on which
  26. it exists:
  27. #if @GNULIB_FOO@
  28. # if !@HAVE_FOO@
  29. _GL_FUNCDECL_SYS (foo, ...);
  30. # endif
  31. _GL_CXXALIAS_SYS (foo, ...);
  32. _GL_CXXALIASWARN (foo);
  33. #elif defined GNULIB_POSIXCHECK
  34. ...
  35. #endif
  36. * For providing a replacement for a function that exists on all platforms,
  37. but is broken/insufficient and needs to be replaced on some platforms:
  38. #if @GNULIB_FOO@
  39. # if @REPLACE_FOO@
  40. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  41. # undef foo
  42. # define foo rpl_foo
  43. # endif
  44. _GL_FUNCDECL_RPL (foo, ...);
  45. _GL_CXXALIAS_RPL (foo, ...);
  46. # else
  47. _GL_CXXALIAS_SYS (foo, ...);
  48. # endif
  49. _GL_CXXALIASWARN (foo);
  50. #elif defined GNULIB_POSIXCHECK
  51. ...
  52. #endif
  53. * For providing a replacement for a function that exists on some platforms
  54. but is broken/insufficient and needs to be replaced on some of them and
  55. is additionally either missing or undeclared on some other platforms:
  56. #if @GNULIB_FOO@
  57. # if @REPLACE_FOO@
  58. # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
  59. # undef foo
  60. # define foo rpl_foo
  61. # endif
  62. _GL_FUNCDECL_RPL (foo, ...);
  63. _GL_CXXALIAS_RPL (foo, ...);
  64. # else
  65. # if !@HAVE_FOO@ or if !@HAVE_DECL_FOO@
  66. _GL_FUNCDECL_SYS (foo, ...);
  67. # endif
  68. _GL_CXXALIAS_SYS (foo, ...);
  69. # endif
  70. _GL_CXXALIASWARN (foo);
  71. #elif defined GNULIB_POSIXCHECK
  72. ...
  73. #endif
  74. */
  75. /* _GL_EXTERN_C declaration;
  76. performs the declaration with C linkage. */
  77. #if defined __cplusplus
  78. # define _GL_EXTERN_C extern "C"
  79. #else
  80. # define _GL_EXTERN_C extern
  81. #endif
  82. /* _GL_FUNCDECL_RPL (func, rettype, parameters_and_attributes);
  83. declares a replacement function, named rpl_func, with the given prototype,
  84. consisting of return type, parameters, and attributes.
  85. Example:
  86. _GL_FUNCDECL_RPL (open, int, (const char *filename, int flags, ...)
  87. _GL_ARG_NONNULL ((1)));
  88. */
  89. #define _GL_FUNCDECL_RPL(func,rettype,parameters_and_attributes) \
  90. _GL_FUNCDECL_RPL_1 (rpl_##func, rettype, parameters_and_attributes)
  91. #define _GL_FUNCDECL_RPL_1(rpl_func,rettype,parameters_and_attributes) \
  92. _GL_EXTERN_C rettype rpl_func parameters_and_attributes
  93. /* _GL_FUNCDECL_SYS (func, rettype, parameters_and_attributes);
  94. declares the system function, named func, with the given prototype,
  95. consisting of return type, parameters, and attributes.
  96. Example:
  97. _GL_FUNCDECL_SYS (open, int, (const char *filename, int flags, ...)
  98. _GL_ARG_NONNULL ((1)));
  99. */
  100. #define _GL_FUNCDECL_SYS(func,rettype,parameters_and_attributes) \
  101. _GL_EXTERN_C rettype func parameters_and_attributes
  102. /* _GL_CXXALIAS_RPL (func, rettype, parameters);
  103. declares a C++ alias called GNULIB_NAMESPACE::func
  104. that redirects to rpl_func, if GNULIB_NAMESPACE is defined.
  105. Example:
  106. _GL_CXXALIAS_RPL (open, int, (const char *filename, int flags, ...));
  107. Wrapping rpl_func in an object with an inline conversion operator
  108. avoids a reference to rpl_func unless GNULIB_NAMESPACE::func is
  109. actually used in the program. */
  110. #define _GL_CXXALIAS_RPL(func,rettype,parameters) \
  111. _GL_CXXALIAS_RPL_1 (func, rpl_##func, rettype, parameters)
  112. #if defined __cplusplus && defined GNULIB_NAMESPACE
  113. # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
  114. namespace GNULIB_NAMESPACE \
  115. { \
  116. static const struct _gl_ ## func ## _wrapper \
  117. { \
  118. typedef rettype (*type) parameters; \
  119. \
  120. inline operator type () const \
  121. { \
  122. return ::rpl_func; \
  123. } \
  124. } func = {}; \
  125. } \
  126. _GL_EXTERN_C int _gl_cxxalias_dummy
  127. #else
  128. # define _GL_CXXALIAS_RPL_1(func,rpl_func,rettype,parameters) \
  129. _GL_EXTERN_C int _gl_cxxalias_dummy
  130. #endif
  131. /* _GL_CXXALIAS_MDA (func, rettype, parameters);
  132. is to be used when func is a Microsoft deprecated alias, on native Windows.
  133. It declares a C++ alias called GNULIB_NAMESPACE::func
  134. that redirects to _func, if GNULIB_NAMESPACE is defined.
  135. Example:
  136. _GL_CXXALIAS_MDA (open, int, (const char *filename, int flags, ...));
  137. */
  138. #define _GL_CXXALIAS_MDA(func,rettype,parameters) \
  139. _GL_CXXALIAS_RPL_1 (func, _##func, rettype, parameters)
  140. /* _GL_CXXALIAS_RPL_CAST_1 (func, rpl_func, rettype, parameters);
  141. is like _GL_CXXALIAS_RPL_1 (func, rpl_func, rettype, parameters);
  142. except that the C function rpl_func may have a slightly different
  143. declaration. A cast is used to silence the "invalid conversion" error
  144. that would otherwise occur. */
  145. #if defined __cplusplus && defined GNULIB_NAMESPACE
  146. # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
  147. namespace GNULIB_NAMESPACE \
  148. { \
  149. static const struct _gl_ ## func ## _wrapper \
  150. { \
  151. typedef rettype (*type) parameters; \
  152. \
  153. inline operator type () const \
  154. { \
  155. return reinterpret_cast<type>(::rpl_func); \
  156. } \
  157. } func = {}; \
  158. } \
  159. _GL_EXTERN_C int _gl_cxxalias_dummy
  160. #else
  161. # define _GL_CXXALIAS_RPL_CAST_1(func,rpl_func,rettype,parameters) \
  162. _GL_EXTERN_C int _gl_cxxalias_dummy
  163. #endif
  164. /* _GL_CXXALIAS_MDA_CAST (func, rettype, parameters);
  165. is like _GL_CXXALIAS_MDA (func, rettype, parameters);
  166. except that the C function func may have a slightly different declaration.
  167. A cast is used to silence the "invalid conversion" error that would
  168. otherwise occur. */
  169. #define _GL_CXXALIAS_MDA_CAST(func,rettype,parameters) \
  170. _GL_CXXALIAS_RPL_CAST_1 (func, _##func, rettype, parameters)
  171. /* _GL_CXXALIAS_SYS (func, rettype, parameters);
  172. declares a C++ alias called GNULIB_NAMESPACE::func
  173. that redirects to the system provided function func, if GNULIB_NAMESPACE
  174. is defined.
  175. Example:
  176. _GL_CXXALIAS_SYS (open, int, (const char *filename, int flags, ...));
  177. Wrapping func in an object with an inline conversion operator
  178. avoids a reference to func unless GNULIB_NAMESPACE::func is
  179. actually used in the program. */
  180. #if defined __cplusplus && defined GNULIB_NAMESPACE
  181. # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
  182. namespace GNULIB_NAMESPACE \
  183. { \
  184. static const struct _gl_ ## func ## _wrapper \
  185. { \
  186. typedef rettype (*type) parameters; \
  187. \
  188. inline operator type () const \
  189. { \
  190. return ::func; \
  191. } \
  192. } func = {}; \
  193. } \
  194. _GL_EXTERN_C int _gl_cxxalias_dummy
  195. #else
  196. # define _GL_CXXALIAS_SYS(func,rettype,parameters) \
  197. _GL_EXTERN_C int _gl_cxxalias_dummy
  198. #endif
  199. /* _GL_CXXALIAS_SYS_CAST (func, rettype, parameters);
  200. is like _GL_CXXALIAS_SYS (func, rettype, parameters);
  201. except that the C function func may have a slightly different declaration.
  202. A cast is used to silence the "invalid conversion" error that would
  203. otherwise occur. */
  204. #if defined __cplusplus && defined GNULIB_NAMESPACE
  205. # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
  206. namespace GNULIB_NAMESPACE \
  207. { \
  208. static const struct _gl_ ## func ## _wrapper \
  209. { \
  210. typedef rettype (*type) parameters; \
  211. \
  212. inline operator type () const \
  213. { \
  214. return reinterpret_cast<type>(::func); \
  215. } \
  216. } func = {}; \
  217. } \
  218. _GL_EXTERN_C int _gl_cxxalias_dummy
  219. #else
  220. # define _GL_CXXALIAS_SYS_CAST(func,rettype,parameters) \
  221. _GL_EXTERN_C int _gl_cxxalias_dummy
  222. #endif
  223. /* _GL_CXXALIAS_SYS_CAST2 (func, rettype, parameters, rettype2, parameters2);
  224. is like _GL_CXXALIAS_SYS (func, rettype, parameters);
  225. except that the C function is picked among a set of overloaded functions,
  226. namely the one with rettype2 and parameters2. Two consecutive casts
  227. are used to silence the "cannot find a match" and "invalid conversion"
  228. errors that would otherwise occur. */
  229. #if defined __cplusplus && defined GNULIB_NAMESPACE
  230. /* The outer cast must be a reinterpret_cast.
  231. The inner cast: When the function is defined as a set of overloaded
  232. functions, it works as a static_cast<>, choosing the designated variant.
  233. When the function is defined as a single variant, it works as a
  234. reinterpret_cast<>. The parenthesized cast syntax works both ways. */
  235. # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
  236. namespace GNULIB_NAMESPACE \
  237. { \
  238. static const struct _gl_ ## func ## _wrapper \
  239. { \
  240. typedef rettype (*type) parameters; \
  241. \
  242. inline operator type () const \
  243. { \
  244. return reinterpret_cast<type>((rettype2 (*) parameters2)(::func)); \
  245. } \
  246. } func = {}; \
  247. } \
  248. _GL_EXTERN_C int _gl_cxxalias_dummy
  249. #else
  250. # define _GL_CXXALIAS_SYS_CAST2(func,rettype,parameters,rettype2,parameters2) \
  251. _GL_EXTERN_C int _gl_cxxalias_dummy
  252. #endif
  253. /* _GL_CXXALIASWARN (func);
  254. causes a warning to be emitted when ::func is used but not when
  255. GNULIB_NAMESPACE::func is used. func must be defined without overloaded
  256. variants. */
  257. #if defined __cplusplus && defined GNULIB_NAMESPACE
  258. # define _GL_CXXALIASWARN(func) \
  259. _GL_CXXALIASWARN_1 (func, GNULIB_NAMESPACE)
  260. # define _GL_CXXALIASWARN_1(func,namespace) \
  261. _GL_CXXALIASWARN_2 (func, namespace)
  262. /* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
  263. we enable the warning only when not optimizing. */
  264. # if !(defined __GNUC__ && !defined __clang__ && __OPTIMIZE__)
  265. # define _GL_CXXALIASWARN_2(func,namespace) \
  266. _GL_WARN_ON_USE (func, \
  267. "The symbol ::" #func " refers to the system function. " \
  268. "Use " #namespace "::" #func " instead.")
  269. # elif __GNUC__ >= 3 && GNULIB_STRICT_CHECKING
  270. # define _GL_CXXALIASWARN_2(func,namespace) \
  271. extern __typeof__ (func) func
  272. # else
  273. # define _GL_CXXALIASWARN_2(func,namespace) \
  274. _GL_EXTERN_C int _gl_cxxalias_dummy
  275. # endif
  276. #else
  277. # define _GL_CXXALIASWARN(func) \
  278. _GL_EXTERN_C int _gl_cxxalias_dummy
  279. #endif
  280. /* _GL_CXXALIASWARN1 (func, rettype, parameters_and_attributes);
  281. causes a warning to be emitted when the given overloaded variant of ::func
  282. is used but not when GNULIB_NAMESPACE::func is used. */
  283. #if defined __cplusplus && defined GNULIB_NAMESPACE
  284. # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
  285. _GL_CXXALIASWARN1_1 (func, rettype, parameters_and_attributes, \
  286. GNULIB_NAMESPACE)
  287. # define _GL_CXXALIASWARN1_1(func,rettype,parameters_and_attributes,namespace) \
  288. _GL_CXXALIASWARN1_2 (func, rettype, parameters_and_attributes, namespace)
  289. /* To work around GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43881>,
  290. we enable the warning only when not optimizing. */
  291. # if !(defined __GNUC__ && !defined __clang__ && __OPTIMIZE__)
  292. # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
  293. _GL_WARN_ON_USE_CXX (func, rettype, rettype, parameters_and_attributes, \
  294. "The symbol ::" #func " refers to the system function. " \
  295. "Use " #namespace "::" #func " instead.")
  296. # else
  297. # define _GL_CXXALIASWARN1_2(func,rettype,parameters_and_attributes,namespace) \
  298. _GL_EXTERN_C int _gl_cxxalias_dummy
  299. # endif
  300. #else
  301. # define _GL_CXXALIASWARN1(func,rettype,parameters_and_attributes) \
  302. _GL_EXTERN_C int _gl_cxxalias_dummy
  303. #endif
  304. #endif /* _GL_CXXDEFS_H */