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