| 1 | /* |
| 2 | * Copyright (C) 2015 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 | #if ENABLE(DFG_JIT) |
| 29 | |
| 30 | #include "Structure.h" |
| 31 | #include <wtf/HashMap.h> |
| 32 | |
| 33 | namespace JSC { namespace DFG { |
| 34 | |
| 35 | // This object is a key for finding a property's type. It's a tuple of Structure* and UniquedStringImpl*. |
| 36 | |
| 37 | class PropertyTypeKey { |
| 38 | public: |
| 39 | PropertyTypeKey() |
| 40 | : m_structure(nullptr) |
| 41 | , m_uid(nullptr) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | PropertyTypeKey(Structure* structure, UniquedStringImpl* uid) |
| 46 | : m_structure(structure) |
| 47 | , m_uid(uid) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | PropertyTypeKey(WTF::HashTableDeletedValueType) |
| 52 | : m_structure(nullptr) |
| 53 | , m_uid(deletedUID()) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | explicit operator bool() const { return m_structure && m_uid; } |
| 58 | |
| 59 | Structure* structure() const { return m_structure; } |
| 60 | UniquedStringImpl* uid() const { return m_uid; } |
| 61 | |
| 62 | bool operator==(const PropertyTypeKey& other) const |
| 63 | { |
| 64 | return m_structure == other.m_structure |
| 65 | && m_uid == other.m_uid; |
| 66 | } |
| 67 | |
| 68 | bool operator!=(const PropertyTypeKey& other) const |
| 69 | { |
| 70 | return !(*this == other); |
| 71 | } |
| 72 | |
| 73 | unsigned hash() const |
| 74 | { |
| 75 | return WTF::PtrHash<Structure*>::hash(m_structure) + WTF::PtrHash<UniquedStringImpl*>::hash(m_uid); |
| 76 | } |
| 77 | |
| 78 | bool isHashTableDeletedValue() const |
| 79 | { |
| 80 | return !m_structure && m_uid == deletedUID(); |
| 81 | } |
| 82 | |
| 83 | void dumpInContext(PrintStream& out, DumpContext* context) const |
| 84 | { |
| 85 | out.print(pointerDumpInContext(m_structure, context), "+" , m_uid); |
| 86 | } |
| 87 | |
| 88 | void dump(PrintStream& out) const |
| 89 | { |
| 90 | dumpInContext(out, nullptr); |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | static UniquedStringImpl* deletedUID() |
| 95 | { |
| 96 | return bitwise_cast<UniquedStringImpl*>(static_cast<intptr_t>(1)); |
| 97 | } |
| 98 | |
| 99 | Structure* m_structure; |
| 100 | UniquedStringImpl* m_uid; |
| 101 | }; |
| 102 | |
| 103 | struct PropertyTypeKeyHash { |
| 104 | static unsigned hash(const PropertyTypeKey& key) { return key.hash(); } |
| 105 | static bool equal(const PropertyTypeKey& a, const PropertyTypeKey& b) { return a == b; } |
| 106 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 107 | }; |
| 108 | |
| 109 | } } // namespace JSC::DFG |
| 110 | |
| 111 | namespace WTF { |
| 112 | |
| 113 | template<typename T> struct DefaultHash; |
| 114 | template<> struct DefaultHash<JSC::DFG::PropertyTypeKey> { |
| 115 | typedef JSC::DFG::PropertyTypeKeyHash Hash; |
| 116 | }; |
| 117 | |
| 118 | template<typename T> struct HashTraits; |
| 119 | template<> struct HashTraits<JSC::DFG::PropertyTypeKey> : SimpleClassHashTraits<JSC::DFG::PropertyTypeKey> { |
| 120 | static const bool emptyValueIsZero = false; |
| 121 | }; |
| 122 | |
| 123 | } // namespace WTF |
| 124 | |
| 125 | #endif // ENABLE(DFG_JIT) |
| 126 | |