1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/PrintStream.h>
29
30namespace WTF {
31
32// This is a boolean type that is part of an abstract value lattice. It's useful for inferring what
33// the boolean value of something is by exploring all boolean values we encounter.
34//
35// It's useful to think of a lattice as a set. The comments below also describe what the enum values
36// mean in terms of sets.
37//
38// FIXME: This would work a lot better as a class with methods. Then we could ensure that the default
39// value is always Bottom, we could have nice conversions to and from boolean, and things like the
40// leastUpperBound function could be a member function with a nicer name.
41// https://bugs.webkit.org/show_bug.cgi?id=185804
42enum class BooleanLattice : uint8_t {
43 // Bottom means that we haven't seen any boolean values yet. We don't know what boolean value we
44 // will infer yet. If we are left with Bottom after we have considered all booleans, it means
45 // that we did not see any booleans.
46 //
47 // This represents the empty set.
48 Bottom = 0,
49
50 // We definitely saw false.
51 //
52 // This represents a set that just contains false.
53 False = 1,
54
55 // We definitely saw true.
56 //
57 // This represents a set that just contains true.
58 True = 2,
59
60 // Top means that we have seen both false and true. Like Bottom, it means that we don't know what
61 // boolean value this lattice has. But unlike Bottom, which bases its lack of knowledge on not
62 // having seen any booleans, Top bases its lack of knowledge based on having seen both False and
63 // True.
64 //
65 // This represents a set that contains both false and true.
66 Top = 3
67};
68
69inline BooleanLattice leastUpperBoundOfBooleanLattices(BooleanLattice a, BooleanLattice b)
70{
71 return static_cast<BooleanLattice>(static_cast<uint8_t>(a) | static_cast<uintptr_t>(b));
72}
73
74inline void printInternal(PrintStream& out, BooleanLattice value)
75{
76 switch (value) {
77 case BooleanLattice::Bottom:
78 out.print("Bottom");
79 return;
80 case BooleanLattice::False:
81 out.print("False");
82 return;
83 case BooleanLattice::True:
84 out.print("True");
85 return;
86 case BooleanLattice::Top:
87 out.print("Top");
88 return;
89 }
90 RELEASE_ASSERT_NOT_REACHED();
91}
92
93} // namespace WTF
94
95using WTF::BooleanLattice;
96using WTF::leastUpperBoundOfBooleanLattices;
97
98