stdbool.in.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 2001-2003, 2006-2014 Free Software Foundation, Inc.
  2. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. 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
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _GL_STDBOOL_H
  14. #define _GL_STDBOOL_H
  15. /* ISO C 99 <stdbool.h> for platforms that lack it. */
  16. /* Usage suggestions:
  17. Programs that use <stdbool.h> should be aware of some limitations
  18. and standards compliance issues.
  19. Standards compliance:
  20. - <stdbool.h> must be #included before 'bool', 'false', 'true'
  21. can be used.
  22. - You cannot assume that sizeof (bool) == 1.
  23. - Programs should not undefine the macros bool, true, and false,
  24. as C99 lists that as an "obsolescent feature".
  25. Limitations of this substitute, when used in a C89 environment:
  26. - <stdbool.h> must be #included before the '_Bool' type can be used.
  27. - You cannot assume that _Bool is a typedef; it might be a macro.
  28. - Bit-fields of type 'bool' are not supported. Portable code
  29. should use 'unsigned int foo : 1;' rather than 'bool foo : 1;'.
  30. - In C99, casts and automatic conversions to '_Bool' or 'bool' are
  31. performed in such a way that every nonzero value gets converted
  32. to 'true', and zero gets converted to 'false'. This doesn't work
  33. with this substitute. With this substitute, only the values 0 and 1
  34. give the expected result when converted to _Bool' or 'bool'.
  35. - C99 allows the use of (_Bool)0.0 in constant expressions, but
  36. this substitute cannot always provide this property.
  37. Also, it is suggested that programs use 'bool' rather than '_Bool';
  38. this isn't required, but 'bool' is more common. */
  39. /* 7.16. Boolean type and values */
  40. /* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same
  41. definitions below, but temporarily we have to #undef them. */
  42. #if defined __BEOS__ && !defined __HAIKU__
  43. # include <OS.h> /* defines bool but not _Bool */
  44. # undef false
  45. # undef true
  46. #endif
  47. #ifdef __cplusplus
  48. # define _Bool bool
  49. # define bool bool
  50. #else
  51. # if defined __BEOS__ && !defined __HAIKU__
  52. /* A compiler known to have 'bool'. */
  53. /* If the compiler already has both 'bool' and '_Bool', we can assume they
  54. are the same types. */
  55. # if !@HAVE__BOOL@
  56. typedef bool _Bool;
  57. # endif
  58. # else
  59. # if !defined __GNUC__
  60. /* If @HAVE__BOOL@:
  61. Some HP-UX cc and AIX IBM C compiler versions have compiler bugs when
  62. the built-in _Bool type is used. See
  63. http://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
  64. http://lists.gnu.org/archive/html/bug-coreutils/2005-11/msg00161.html
  65. http://lists.gnu.org/archive/html/bug-coreutils/2005-10/msg00086.html
  66. Similar bugs are likely with other compilers as well; this file
  67. wouldn't be used if <stdbool.h> was working.
  68. So we override the _Bool type.
  69. If !@HAVE__BOOL@:
  70. Need to define _Bool ourselves. As 'signed char' or as an enum type?
  71. Use of a typedef, with SunPRO C, leads to a stupid
  72. "warning: _Bool is a keyword in ISO C99".
  73. Use of an enum type, with IRIX cc, leads to a stupid
  74. "warning(1185): enumerated type mixed with another type".
  75. Even the existence of an enum type, without a typedef,
  76. "Invalid enumerator. (badenum)" with HP-UX cc on Tru64.
  77. The only benefit of the enum, debuggability, is not important
  78. with these compilers. So use 'signed char' and no enum. */
  79. # define _Bool signed char
  80. # else
  81. /* With this compiler, trust the _Bool type if the compiler has it. */
  82. # if !@HAVE__BOOL@
  83. /* For the sake of symbolic names in gdb, define true and false as
  84. enum constants, not only as macros.
  85. It is tempting to write
  86. typedef enum { false = 0, true = 1 } _Bool;
  87. so that gdb prints values of type 'bool' symbolically. But then
  88. values of type '_Bool' might promote to 'int' or 'unsigned int'
  89. (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
  90. (see ISO C 99 6.3.1.1.(2)). So add a negative value to the
  91. enum; this ensures that '_Bool' promotes to 'int'. */
  92. typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
  93. # endif
  94. # endif
  95. # endif
  96. # define bool _Bool
  97. #endif
  98. /* The other macros must be usable in preprocessor directives. */
  99. #ifdef __cplusplus
  100. # define false false
  101. # define true true
  102. #else
  103. # define false 0
  104. # define true 1
  105. #endif
  106. #define __bool_true_false_are_defined 1
  107. #endif /* _GL_STDBOOL_H */