LLVM 22.0.0git
CASID.h
Go to the documentation of this file.
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"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Error.h"
18
19namespace llvm {
20
21class raw_ostream;
22
23namespace cas {
24
25class CASID;
26
27/// Context for CAS identifiers.
29 virtual void anchor();
30
31public:
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().
39
40protected:
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.
58class CASID {
59public:
60 void dump() const;
61
63 ID.print(OS);
64 return OS;
65 }
66
67 /// Print CASID.
68 void print(raw_ostream &OS) const {
69 return getContext().printIDImpl(OS, *this);
70 }
71
72 /// Return a printable string for CASID.
73 std::string toString() const;
74
76 return arrayRefFromStringRef<uint8_t>(Hash);
77 }
78
79 friend bool operator==(const CASID &LHS, const CASID &RHS) {
80 if (LHS.Context == RHS.Context)
81 return LHS.Hash == RHS.Hash;
82
83 // EmptyKey or TombstoneKey.
84 if (!LHS.Context || !RHS.Context)
85 return false;
86
87 // CASIDs are equal when they have the same hash schema and same hash value.
88 return LHS.Context->getHashSchemaIdentifier() ==
89 RHS.Context->getHashSchemaIdentifier() &&
90 LHS.Hash == RHS.Hash;
91 }
92
93 friend bool operator!=(const CASID &LHS, const CASID &RHS) {
94 return !(LHS == RHS);
95 }
96
97 friend hash_code hash_value(const CASID &ID) {
98 ArrayRef<uint8_t> Hash = ID.getHash();
99 return hash_combine_range(Hash.begin(), Hash.end());
100 }
101
102 const CASContext &getContext() const {
103 assert(Context && "Tombstone or empty key for DenseMap?");
104 return *Context;
105 }
106
109 }
112 }
113
114 CASID() = delete;
115
116 /// Create CASID from CASContext and raw hash bytes.
117 static CASID create(const CASContext *Context, StringRef Hash) {
118 return CASID(Context, Hash);
119 }
120
121private:
122 CASID(const CASContext *Context, StringRef Hash)
123 : Context(Context), Hash(Hash) {}
124
125 const CASContext *Context;
126 SmallString<32> Hash;
127};
128
129} // namespace cas
130
131template <> struct DenseMapInfo<cas::CASID> {
133
136 }
137
138 static unsigned getHashValue(cas::CASID ID) {
139 return (unsigned)hash_value(ID);
140 }
141
142 static bool isEqual(cas::CASID LHS, cas::CASID RHS) { return LHS == RHS; }
143};
144
145} // namespace llvm
146
147#endif // LLVM_CAS_CASID_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines DenseMapInfo traits for DenseMap.
raw_pwrite_stream & OS
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:136
iterator begin() const
Definition: ArrayRef.h:135
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Context for CAS identifiers.
Definition: CASID.h:28
virtual void printIDImpl(raw_ostream &OS, const CASID &ID) const =0
Print ID to OS.
virtual StringRef getHashSchemaIdentifier() const =0
Get an identifer for the schema used by this CAS context.
virtual ~CASContext()=default
Unique identifier for a CAS object.
Definition: CASID.h:58
friend hash_code hash_value(const CASID &ID)
Definition: CASID.h:97
void dump() const
Definition: ObjectStore.cpp:23
void print(raw_ostream &OS) const
Print CASID.
Definition: CASID.h:68
static CASID getDenseMapTombstoneKey()
Definition: CASID.h:110
friend bool operator==(const CASID &LHS, const CASID &RHS)
Definition: CASID.h:79
ArrayRef< uint8_t > getHash() const
Definition: CASID.h:75
const CASContext & getContext() const
Definition: CASID.h:102
static CASID getDenseMapEmptyKey()
Definition: CASID.h:107
friend bool operator!=(const CASID &LHS, const CASID &RHS)
Definition: CASID.h:93
std::string toString() const
Return a printable string for CASID.
Definition: ObjectStore.cpp:27
static CASID create(const CASContext *Context, StringRef Hash)
Create CASID from CASContext and raw hash bytes.
Definition: CASID.h:117
friend raw_ostream & operator<<(raw_ostream &OS, const CASID &ID)
Definition: CASID.h:62
An opaque object representing a hash code.
Definition: Hashing.h:76
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
hash_code hash_value(const FixedPointSemantics &Val)
Definition: APFixedPoint.h:137
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:469
static bool isEqual(cas::CASID LHS, cas::CASID RHS)
Definition: CASID.h:142
static cas::CASID getTombstoneKey()
Definition: CASID.h:134
static unsigned getHashValue(cas::CASID ID)
Definition: CASID.h:138
static cas::CASID getEmptyKey()
Definition: CASID.h:132
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition: DenseMapInfo.h:54