|
| 1 | +//===- llvm/CAS/CASID.h -----------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef LLVM_CAS_CASID_H |
| 10 | +#define LLVM_CAS_CASID_H |
| 11 | + |
| 12 | +#include "llvm/ADT/ArrayRef.h" |
| 13 | +#include "llvm/ADT/DenseMapInfo.h" |
| 14 | +#include "llvm/ADT/SmallString.h" |
| 15 | +#include "llvm/ADT/StringExtras.h" |
| 16 | +#include "llvm/ADT/StringRef.h" |
| 17 | +#include "llvm/Support/Error.h" |
| 18 | + |
| 19 | +namespace llvm { |
| 20 | + |
| 21 | +class raw_ostream; |
| 22 | + |
| 23 | +namespace cas { |
| 24 | + |
| 25 | +class CASID; |
| 26 | + |
| 27 | +/// Context for CAS identifiers. |
| 28 | +class CASContext { |
| 29 | + virtual void anchor(); |
| 30 | + |
| 31 | +public: |
| 32 | + virtual ~CASContext() = default; |
| 33 | + |
| 34 | + /// Get an identifer for the schema used by this CAS context. Two CAS |
| 35 | + /// instances should return \c true for this identifier if and only if their |
| 36 | + /// CASIDs are safe to compare by hash. This is used by \a |
| 37 | + /// CASID::equalsImpl(). |
| 38 | + virtual StringRef getHashSchemaIdentifier() const = 0; |
| 39 | + |
| 40 | +protected: |
| 41 | + /// Print \p ID to \p OS. |
| 42 | + virtual void printIDImpl(raw_ostream &OS, const CASID &ID) const = 0; |
| 43 | + |
| 44 | + friend class CASID; |
| 45 | +}; |
| 46 | + |
| 47 | +/// Unique identifier for a CAS object. |
| 48 | +/// |
| 49 | +/// Locally, stores an internal CAS identifier that's specific to a single CAS |
| 50 | +/// instance. It's guaranteed not to change across the view of that CAS, but |
| 51 | +/// might change between runs. |
| 52 | +/// |
| 53 | +/// It also has \a CASIDContext pointer to allow comparison of these |
| 54 | +/// identifiers. If two CASIDs are from the same CASIDContext, they can be |
| 55 | +/// compared directly. If they are, then \a |
| 56 | +/// CASIDContext::getHashSchemaIdentifier() is compared to see if they can be |
| 57 | +/// compared by hash, in which case the result of \a getHash() is compared. |
| 58 | +class CASID { |
| 59 | +public: |
| 60 | + void dump() const; |
| 61 | + void print(raw_ostream &OS) const { |
| 62 | + return getContext().printIDImpl(OS, *this); |
| 63 | + } |
| 64 | + friend raw_ostream &operator<<(raw_ostream &OS, const CASID &ID) { |
| 65 | + ID.print(OS); |
| 66 | + return OS; |
| 67 | + } |
| 68 | + std::string toString() const; |
| 69 | + |
| 70 | + ArrayRef<uint8_t> getHash() const { |
| 71 | + return arrayRefFromStringRef<uint8_t>(Hash); |
| 72 | + } |
| 73 | + |
| 74 | + friend bool operator==(const CASID &LHS, const CASID &RHS) { |
| 75 | + if (LHS.Context == RHS.Context) |
| 76 | + return LHS.Hash == RHS.Hash; |
| 77 | + |
| 78 | + // EmptyKey or TombstoneKey. |
| 79 | + if (!LHS.Context || !RHS.Context) |
| 80 | + return false; |
| 81 | + |
| 82 | + // CASIDs are equal when they have the same hash schema and same hash value. |
| 83 | + return LHS.Context->getHashSchemaIdentifier() == |
| 84 | + RHS.Context->getHashSchemaIdentifier() && |
| 85 | + LHS.Hash == RHS.Hash; |
| 86 | + } |
| 87 | + |
| 88 | + friend bool operator!=(const CASID &LHS, const CASID &RHS) { |
| 89 | + return !(LHS == RHS); |
| 90 | + } |
| 91 | + |
| 92 | + friend hash_code hash_value(const CASID &ID) { |
| 93 | + ArrayRef<uint8_t> Hash = ID.getHash(); |
| 94 | + return hash_combine_range(Hash.begin(), Hash.end()); |
| 95 | + } |
| 96 | + |
| 97 | + const CASContext &getContext() const { |
| 98 | + assert(Context && "Tombstone or empty key for DenseMap?"); |
| 99 | + return *Context; |
| 100 | + } |
| 101 | + |
| 102 | + static CASID getDenseMapEmptyKey() { |
| 103 | + return CASID(nullptr, DenseMapInfo<StringRef>::getEmptyKey()); |
| 104 | + } |
| 105 | + static CASID getDenseMapTombstoneKey() { |
| 106 | + return CASID(nullptr, DenseMapInfo<StringRef>::getTombstoneKey()); |
| 107 | + } |
| 108 | + |
| 109 | + CASID() = delete; |
| 110 | + |
| 111 | + static CASID create(const CASContext *Context, StringRef Hash) { |
| 112 | + return CASID(Context, Hash); |
| 113 | + } |
| 114 | + |
| 115 | +private: |
| 116 | + CASID(const CASContext *Context, StringRef Hash) |
| 117 | + : Context(Context), Hash(Hash) {} |
| 118 | + |
| 119 | + const CASContext *Context; |
| 120 | + SmallString<32> Hash; |
| 121 | +}; |
| 122 | + |
| 123 | +/// This is used to workaround the issue of MSVC needing default-constructible |
| 124 | +/// types for \c std::promise/future. |
| 125 | +template <typename T> struct AsyncValue { |
| 126 | + Expected<std::optional<T>> take() { return std::move(Value); } |
| 127 | + |
| 128 | + AsyncValue() : Value(std::nullopt) {} |
| 129 | + AsyncValue(Error &&E) : Value(std::move(E)) {} |
| 130 | + AsyncValue(T &&V) : Value(std::move(V)) {} |
| 131 | + AsyncValue(std::nullopt_t) : Value(std::nullopt) {} |
| 132 | + AsyncValue(Expected<std::optional<T>> &&Obj) : Value(std::move(Obj)) {} |
| 133 | + |
| 134 | +private: |
| 135 | + Expected<std::optional<T>> Value; |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace cas |
| 139 | + |
| 140 | +template <> struct DenseMapInfo<cas::CASID> { |
| 141 | + static cas::CASID getEmptyKey() { return cas::CASID::getDenseMapEmptyKey(); } |
| 142 | + |
| 143 | + static cas::CASID getTombstoneKey() { |
| 144 | + return cas::CASID::getDenseMapTombstoneKey(); |
| 145 | + } |
| 146 | + |
| 147 | + static unsigned getHashValue(cas::CASID ID) { |
| 148 | + return (unsigned)hash_value(ID); |
| 149 | + } |
| 150 | + |
| 151 | + static bool isEqual(cas::CASID LHS, cas::CASID RHS) { return LHS == RHS; } |
| 152 | +}; |
| 153 | + |
| 154 | +} // namespace llvm |
| 155 | + |
| 156 | +#endif // LLVM_CAS_CASID_H |
0 commit comments