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