X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FValueMap.h;h=d23fccf3e8cc1f18f8dc31f447c42288b6ee24c6;hb=658c62862e470b59aaf25825de64d93fbaf8cb93;hp=14f21001df7bdcd3d14173418e871950b5df167a;hpb=71a5c22c2b635ea903c3ae7d2695ca649b38726f;p=oota-llvm.git diff --git a/include/llvm/ADT/ValueMap.h b/include/llvm/ADT/ValueMap.h index 14f21001df7..d23fccf3e8c 100644 --- a/include/llvm/ADT/ValueMap.h +++ b/include/llvm/ADT/ValueMap.h @@ -7,7 +7,19 @@ // //===----------------------------------------------------------------------===// // -// This file defines the ValueMap class. +// This file defines the ValueMap class. ValueMap maps Value* or any subclass +// to an arbitrary other type. It provides the DenseMap interface but updates +// itself to remain safe when keys are RAUWed or deleted. By default, when a +// key is RAUWed from V1 to V2, the old mapping V1->target is removed, and a new +// mapping V2->target is added. If V2 already existed, its old target is +// overwritten. When a key is deleted, its mapping is removed. +// +// You can override a ValueMap's Config parameter to control exactly what +// happens on RAUW and destruction and to get called back on each event. It's +// legal to call back into the ValueMap from a Config's callbacks. Config +// parameters should inherit from ValueMapConfig to get default +// implementations of all the methods ValueMap uses. See ValueMapConfig for +// documentation of the functions you can override. // //===----------------------------------------------------------------------===// @@ -17,13 +29,13 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/Support/ValueHandle.h" #include "llvm/Support/type_traits.h" -#include "llvm/System/Mutex.h" +#include "llvm/Support/Mutex.h" #include namespace llvm { -template +template class ValueMapCallbackVH; template @@ -31,6 +43,9 @@ class ValueMapIterator; template class ValueMapConstIterator; +/// This class defines the default behavior for configurable aspects of +/// ValueMap<>. User Configs should inherit from this class to be as compatible +/// as possible with future versions of ValueMap. template struct ValueMapConfig { /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's @@ -44,46 +59,34 @@ struct ValueMapConfig { struct ExtraData {}; template - static void onRAUW(const ExtraDataT &Data, KeyT Old, KeyT New) {} + static void onRAUW(const ExtraDataT & /*Data*/, KeyT /*Old*/, KeyT /*New*/) {} template - static void onDeleted(const ExtraDataT &Data, KeyT Old) {} + static void onDelete(const ExtraDataT &/*Data*/, KeyT /*Old*/) {} /// Returns a mutex that should be acquired around any changes to the map. /// This is only acquired from the CallbackVH (and held around calls to onRAUW - /// and onDeleted) and not inside other ValueMap methods. NULL means that no + /// and onDelete) and not inside other ValueMap methods. NULL means that no /// mutex is necessary. template - static sys::Mutex *getMutex(const ExtraDataT &Data) { return NULL; } + static sys::Mutex *getMutex(const ExtraDataT &/*Data*/) { return NULL; } }; -/// ValueMap maps Value* or any subclass to an arbitrary other -/// type. It provides the DenseMap interface. When the key values are -/// deleted or RAUWed, ValueMap relies on the Config to decide what to -/// do. Config parameters should inherit from ValueMapConfig to -/// get default implementations of all the methods ValueMap uses. -/// -/// By default, when a key is RAUWed from V1 to V2, the old mapping -/// V1->target is removed, and a new mapping V2->target is added. If -/// V2 already existed, its old target is overwritten. When a key is -/// deleted, its mapping is removed. You can override Config to get -/// called back on each event. -template, - typename ValueInfoT = DenseMapInfo > +/// See the file comment. +template > class ValueMap { - friend class ValueMapCallbackVH; - typedef ValueMapCallbackVH ValueMapCVH; - typedef DenseMap, - ValueInfoT> MapT; + friend class ValueMapCallbackVH; + typedef ValueMapCallbackVH ValueMapCVH; + typedef DenseMap > MapT; typedef typename Config::ExtraData ExtraData; MapT Map; ExtraData Data; + ValueMap(const ValueMap&) LLVM_DELETED_FUNCTION; + ValueMap& operator=(const ValueMap&) LLVM_DELETED_FUNCTION; public: typedef KeyT key_type; typedef ValueT mapped_type; typedef std::pair value_type; - ValueMap(const ValueMap& Other) : Map(Other.Map), Data(Other.Data) {} - explicit ValueMap(unsigned NumInitBuckets = 64) : Map(NumInitBuckets), Data() {} explicit ValueMap(const ExtraData &Data, unsigned NumInitBuckets = 64) @@ -108,20 +111,21 @@ public: /// count - Return true if the specified key is in the map. bool count(const KeyT &Val) const { - return Map.count(Wrap(Val)); + return Map.find_as(Val) != Map.end(); } iterator find(const KeyT &Val) { - return iterator(Map.find(Wrap(Val))); + return iterator(Map.find_as(Val)); } const_iterator find(const KeyT &Val) const { - return const_iterator(Map.find(Wrap(Val))); + return const_iterator(Map.find_as(Val)); } /// lookup - Return the entry for the specified key, or a default /// constructed value if no such entry exists. ValueT lookup(const KeyT &Val) const { - return Map.lookup(Wrap(Val)); + typename MapT::const_iterator I = Map.find_as(Val); + return I != Map.end() ? I->second : ValueT(); } // Inserts key,value pair into the map if the key isn't already in the map. @@ -142,9 +146,14 @@ public: bool erase(const KeyT &Val) { - return Map.erase(Wrap(Val)); + typename MapT::iterator I = Map.find_as(Val); + if (I == Map.end()) + return false; + + Map.erase(I); + return true; } - bool erase(iterator I) { + void erase(iterator I) { return Map.erase(I.base()); } @@ -156,12 +165,6 @@ public: return Map[Wrap(Key)]; } - ValueMap& operator=(const ValueMap& Other) { - Map = Other.Map; - Data = Other.Data; - return *this; - } - /// isPointerIntoBucketsArray - Return true if the specified pointer points /// somewhere into the ValueMap's array of buckets (i.e. either to a key or /// value in the ValueMap). @@ -177,6 +180,9 @@ public: } private: + // Takes a key being looked up in the map and wraps it into a + // ValueMapCallbackVH, the actual key type of the map. We use a helper + // function because ValueMapCVH is constructed with a second parameter. ValueMapCVH Wrap(KeyT key) const { // The only way the resulting CallbackVH could try to modify *this (making // the const_cast incorrect) is if it gets inserted into the map. But then @@ -186,11 +192,13 @@ private: } }; -template +// This CallbackVH updates its ValueMap when the contained Value changes, +// according to the user's preferences expressed through the Config object. +template class ValueMapCallbackVH : public CallbackVH { - friend class ValueMap; - friend class DenseMapInfo; - typedef ValueMap ValueMapT; + friend class ValueMap; + friend struct DenseMapInfo; + typedef ValueMap ValueMapT; typedef typename llvm::remove_pointer::type KeySansPointerT; ValueMapT *Map; @@ -208,7 +216,7 @@ public: sys::Mutex *M = Config::getMutex(Copy.Map->Data); if (M) M->acquire(); - Config::onDeleted(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. + Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this. Copy.Map->Map.erase(Copy); // Definitely destroys *this. if (M) M->release(); @@ -240,9 +248,9 @@ public: } }; -template -struct DenseMapInfo > { - typedef ValueMapCallbackVH VH; +template +struct DenseMapInfo > { + typedef ValueMapCallbackVH VH; typedef DenseMapInfo PointerInfo; static inline VH getEmptyKey() { @@ -254,10 +262,15 @@ struct DenseMapInfo > { static unsigned getHashValue(const VH &Val) { return PointerInfo::getHashValue(Val.Unwrap()); } + static unsigned getHashValue(const KeyT &Val) { + return PointerInfo::getHashValue(Val); + } static bool isEqual(const VH &LHS, const VH &RHS) { return LHS == RHS; } - static bool isPod() { return false; } + static bool isEqual(const KeyT &LHS, const VH &RHS) { + return LHS == RHS.getValPtr(); + } }; @@ -279,7 +292,7 @@ public: struct ValueTypeProxy { const KeyT first; ValueT& second; - ValueTypeProxy *operator->() { return this; } + ValueTypeProxy *operator->() { return this; } operator std::pair() const { return std::make_pair(first, second); } @@ -301,11 +314,11 @@ public: return I != RHS.I; } - inline ValueMapIterator& operator++() { // Preincrement + inline ValueMapIterator& operator++() { // Preincrement ++I; return *this; } - ValueMapIterator operator++(int) { // Postincrement + ValueMapIterator operator++(int) { // Postincrement ValueMapIterator tmp = *this; ++*this; return tmp; } }; @@ -329,7 +342,7 @@ public: struct ValueTypeProxy { const KeyT first; const ValueT& second; - ValueTypeProxy *operator->() { return this; } + ValueTypeProxy *operator->() { return this; } operator std::pair() const { return std::make_pair(first, second); } @@ -351,11 +364,11 @@ public: return I != RHS.I; } - inline ValueMapConstIterator& operator++() { // Preincrement + inline ValueMapConstIterator& operator++() { // Preincrement ++I; return *this; } - ValueMapConstIterator operator++(int) { // Postincrement + ValueMapConstIterator operator++(int) { // Postincrement ValueMapConstIterator tmp = *this; ++*this; return tmp; } };