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