stdbool.in.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2001-2003, 2006-2022 Free Software Foundation, Inc.
  2. Written by Bruno Haible <haible@clisp.cons.org>, 2001.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file 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 <https://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. #ifdef __cplusplus
  41. # define _Bool bool
  42. # define bool bool
  43. #else
  44. # if !defined __GNUC__
  45. /* If @HAVE__BOOL@:
  46. Some HP-UX cc and AIX IBM C compiler versions have compiler bugs when
  47. the built-in _Bool type is used. See
  48. https://gcc.gnu.org/ml/gcc-patches/2003-12/msg02303.html
  49. https://lists.gnu.org/r/bug-coreutils/2005-11/msg00161.html
  50. https://lists.gnu.org/r/bug-coreutils/2005-10/msg00086.html
  51. Similar bugs are likely with other compilers as well; this file
  52. wouldn't be used if <stdbool.h> was working.
  53. So we override the _Bool type.
  54. If !@HAVE__BOOL@:
  55. Need to define _Bool ourselves. As 'signed char' or as an enum type?
  56. Use of a typedef, with SunPRO C, leads to a stupid
  57. "warning: _Bool is a keyword in ISO C99".
  58. Use of an enum type, with IRIX cc, leads to a stupid
  59. "warning(1185): enumerated type mixed with another type".
  60. Even the existence of an enum type, without a typedef,
  61. "Invalid enumerator. (badenum)" with HP-UX cc on Tru64.
  62. The only benefit of the enum, debuggability, is not important
  63. with these compilers. So use 'signed char' and no enum. */
  64. # define _Bool signed char
  65. # else
  66. /* With this compiler, trust the _Bool type if the compiler has it. */
  67. # if !@HAVE__BOOL@
  68. /* For the sake of symbolic names in gdb, define true and false as
  69. enum constants, not only as macros.
  70. It is tempting to write
  71. typedef enum { false = 0, true = 1 } _Bool;
  72. so that gdb prints values of type 'bool' symbolically. But then
  73. values of type '_Bool' might promote to 'int' or 'unsigned int'
  74. (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
  75. (see ISO C 99 6.3.1.1.(2)). So add a negative value to the
  76. enum; this ensures that '_Bool' promotes to 'int'. */
  77. typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
  78. # endif
  79. # endif
  80. # define bool _Bool
  81. #endif
  82. /* The other macros must be usable in preprocessor directives. */
  83. #ifdef __cplusplus
  84. # define false false
  85. # define true true
  86. #else
  87. # define false 0
  88. # define true 1
  89. #endif
  90. #define __bool_true_false_are_defined 1
  91. #endif /* _GL_STDBOOL_H */