1818
1919#include < cstdint>
2020
21- namespace llvm ::sandboxir {
21+ namespace llvm {
22+ namespace sandboxir {
2223
2324class Argument ;
2425class BBIterator ;
@@ -37,10 +38,28 @@ class Context {
3738 using MoveInstrCallback =
3839 std::function<void (Instruction *, const BBIterator &)>;
3940
40- // / An ID for a registered callback. Used for deregistration. Using a 64-bit
41- // / integer so we don't have to worry about the unlikely case of overflowing
42- // / a 32-bit counter.
43- using CallbackID = uint64_t ;
41+ // / An ID for a registered callback. Used for deregistration. A dedicated type
42+ // / is employed so as to keep IDs opaque to the end user; only Context should
43+ // / deal with its underlying representation.
44+ class CallbackID {
45+ public:
46+ // Uses a 64-bit integer so we don't have to worry about the unlikely case
47+ // of overflowing a 32-bit counter.
48+ using ValTy = uint64_t ;
49+ static constexpr const ValTy InvalidVal = 0 ;
50+
51+ private:
52+ // Default initialization results in an invalid ID.
53+ ValTy Val = InvalidVal;
54+ explicit CallbackID (ValTy Val) : Val{Val} {
55+ assert (Val != InvalidVal && " newly-created ID is invalid!" );
56+ }
57+
58+ public:
59+ CallbackID () = default ;
60+ friend class Context ;
61+ friend struct DenseMapInfo <CallbackID>;
62+ };
4463
4564protected:
4665 LLVMContext &LLVMCtx;
@@ -83,7 +102,7 @@ class Context {
83102 // / A counter used for assigning callback IDs during registration. The same
84103 // / counter is used for all kinds of callbacks so we can detect mismatched
85104 // / registration/deregistration.
86- CallbackID NextCallbackID = 0 ;
105+ CallbackID::ValTy NextCallbackID = 1 ;
87106
88107 // / Remove \p V from the maps and returns the unique_ptr.
89108 std::unique_ptr<Value> detachLLVMValue (llvm::Value *V);
@@ -263,6 +282,27 @@ class Context {
263282 // TODO: Add callbacks for instructions inserted/removed if needed.
264283};
265284
266- } // namespace llvm::sandboxir
285+ } // namespace sandboxir
286+
287+ // DenseMap info for CallbackIDs
288+ template <> struct DenseMapInfo <sandboxir::Context::CallbackID> {
289+ using CallbackID = sandboxir::Context::CallbackID;
290+ using ReprInfo = DenseMapInfo<CallbackID::ValTy>;
291+
292+ static CallbackID getEmptyKey () {
293+ return CallbackID{ReprInfo::getEmptyKey ()};
294+ }
295+ static CallbackID getTombstoneKey () {
296+ return CallbackID{ReprInfo::getTombstoneKey ()};
297+ }
298+ static unsigned getHashValue (const CallbackID &ID) {
299+ return ReprInfo::getHashValue (ID.Val );
300+ }
301+ static bool isEqual (const CallbackID &LHS, const CallbackID &RHS) {
302+ return ReprInfo::isEqual (LHS.Val , RHS.Val );
303+ }
304+ };
305+
306+ } // namespace llvm
267307
268308#endif // LLVM_SANDBOXIR_CONTEXT_H
0 commit comments