1/* "True" vs "False" vs "Unknown".
2   Copyright (C) 2019-2022 Free Software Foundation, Inc.
3   Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.  */
20
21#ifndef GCC_TRISTATE_H
22#define GCC_TRISTATE_H
23
24/* "True" vs "False" vs "Unknown".  */
25
26class tristate {
27 public:
28  enum value {
29    TS_UNKNOWN,
30    TS_TRUE,
31    TS_FALSE
32  };
33
34  tristate (enum value val) : m_value (val) {}
35  tristate (bool val) : m_value (val ? TS_TRUE : TS_FALSE) {}
36  static tristate unknown () { return tristate (TS_UNKNOWN); }
37
38  const char *as_string () const;
39
40  bool is_known () const { return m_value != TS_UNKNOWN; }
41  bool is_true () const { return m_value == TS_TRUE; }
42  bool is_false () const { return m_value == TS_FALSE; }
43
44  tristate not_ () const;
45  tristate or_ (tristate other) const;
46  tristate and_ (tristate other) const;
47
48  bool operator== (const tristate &other) const
49  {
50    return m_value == other.m_value;
51  }
52
53  bool operator!= (const tristate &other) const
54  {
55    return m_value != other.m_value;
56  }
57
58  enum value get_value () const { return m_value; }
59
60 private:
61  enum value m_value;
62};
63
64/* Overloaded boolean operators on tristates.  */
65
66inline tristate
67operator ! (tristate t)
68{
69  return t.not_ ();
70}
71
72inline tristate
73operator || (tristate a, tristate b)
74{
75  return a.or_ (b);
76}
77
78inline tristate
79operator && (tristate a, tristate b)
80{
81  return a.and_ (b);
82}
83
84#endif /* GCC_TRISTATE_H */
85