@@ -56,13 +56,30 @@ struct DenseMapInfo {
5656 // static bool isEqual(const T &LHS, const T &RHS);
5757};
5858
59- template <typename T> struct DenseMapInfo <T *> {
59+ // Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
60+ // that are aligned to alignof(T) bytes, but try to avoid requiring T to be
61+ // complete. This allows clients to instantiate DenseMap<T*, ...> with forward
62+ // declared key types. Assume that no pointer key type requires more than 4096
63+ // bytes of alignment.
64+ template <typename T>
65+ struct DenseMapInfo <T*> {
66+ // The following should hold, but it would require T to be complete:
67+ // static_assert(alignof(T) <= (1 << Log2MaxAlign),
68+ // "DenseMap does not support pointer keys requiring more than "
69+ // "Log2MaxAlign bits of alignment");
70+ static constexpr uintptr_t Log2MaxAlign = 12 ;
71+
6072 static inline T* getEmptyKey () {
61- // We assume that raw pointers do not carry alignment requirements.
62- return reinterpret_cast <T *>(-1 );
73+ uintptr_t Val = static_cast <uintptr_t >(-1 );
74+ Val <<= Log2MaxAlign;
75+ return reinterpret_cast <T*>(Val);
6376 }
6477
65- static inline T *getTombstoneKey () { return reinterpret_cast <T *>(-2 ); }
78+ static inline T* getTombstoneKey () {
79+ uintptr_t Val = static_cast <uintptr_t >(-2 );
80+ Val <<= Log2MaxAlign;
81+ return reinterpret_cast <T*>(Val);
82+ }
6683
6784 static unsigned getHashValue (const T *PtrVal) {
6885 return (unsigned ((uintptr_t )PtrVal) >> 4 ) ^
0 commit comments