diff options
Diffstat (limited to 'include/llvm/ADT')
-rw-r--r-- | include/llvm/ADT/DenseMap.h | 4 | ||||
-rw-r--r-- | include/llvm/ADT/EquivalenceClasses.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/FoldingSet.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/ImmutableList.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/ImmutableSet.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/SmallPtrSet.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/SparseBitVector.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/StringExtras.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/StringSwitch.h | 83 | ||||
-rw-r--r-- | include/llvm/ADT/Twine.h | 2 | ||||
-rw-r--r-- | include/llvm/ADT/ValueMap.h | 54 |
11 files changed, 125 insertions, 32 deletions
diff --git a/include/llvm/ADT/DenseMap.h b/include/llvm/ADT/DenseMap.h index 0ed2d5a..52354b7 100644 --- a/include/llvm/ADT/DenseMap.h +++ b/include/llvm/ADT/DenseMap.h @@ -454,12 +454,12 @@ public: return Ptr != RHS.Ptr; } - inline DenseMapIterator& operator++() { // Preincrement + inline DenseMapIterator& operator++() { // Preincrement ++Ptr; AdvancePastEmptyBuckets(); return *this; } - DenseMapIterator operator++(int) { // Postincrement + DenseMapIterator operator++(int) { // Postincrement DenseMapIterator tmp = *this; ++*this; return tmp; } diff --git a/include/llvm/ADT/EquivalenceClasses.h b/include/llvm/ADT/EquivalenceClasses.h index ac9dd4d..f5f3d49 100644 --- a/include/llvm/ADT/EquivalenceClasses.h +++ b/include/llvm/ADT/EquivalenceClasses.h @@ -15,7 +15,7 @@ #ifndef LLVM_ADT_EQUIVALENCECLASSES_H #define LLVM_ADT_EQUIVALENCECLASSES_H -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include <set> namespace llvm { diff --git a/include/llvm/ADT/FoldingSet.h b/include/llvm/ADT/FoldingSet.h index 26090ce..81dc469 100644 --- a/include/llvm/ADT/FoldingSet.h +++ b/include/llvm/ADT/FoldingSet.h @@ -16,7 +16,7 @@ #ifndef LLVM_ADT_FOLDINGSET_H #define LLVM_ADT_FOLDINGSET_H -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" diff --git a/include/llvm/ADT/ImmutableList.h b/include/llvm/ADT/ImmutableList.h index a7f5819..5f8cb57 100644 --- a/include/llvm/ADT/ImmutableList.h +++ b/include/llvm/ADT/ImmutableList.h @@ -16,7 +16,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/ADT/FoldingSet.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include <cassert> namespace llvm { diff --git a/include/llvm/ADT/ImmutableSet.h b/include/llvm/ADT/ImmutableSet.h index 8627909..16b4403 100644 --- a/include/llvm/ADT/ImmutableSet.h +++ b/include/llvm/ADT/ImmutableSet.h @@ -16,7 +16,7 @@ #include "llvm/Support/Allocator.h" #include "llvm/ADT/FoldingSet.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include <cassert> #include <functional> diff --git a/include/llvm/ADT/SmallPtrSet.h b/include/llvm/ADT/SmallPtrSet.h index 7d00e9a..c29fc9f 100644 --- a/include/llvm/ADT/SmallPtrSet.h +++ b/include/llvm/ADT/SmallPtrSet.h @@ -18,7 +18,7 @@ #include <cassert> #include <cstring> #include <iterator> -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include "llvm/Support/PointerLikeTypeTraits.h" namespace llvm { diff --git a/include/llvm/ADT/SparseBitVector.h b/include/llvm/ADT/SparseBitVector.h index b7a6873..6c813ec 100644 --- a/include/llvm/ADT/SparseBitVector.h +++ b/include/llvm/ADT/SparseBitVector.h @@ -17,7 +17,7 @@ #include "llvm/ADT/ilist.h" #include "llvm/ADT/ilist_node.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/raw_ostream.h" #include <cassert> diff --git a/include/llvm/ADT/StringExtras.h b/include/llvm/ADT/StringExtras.h index 899823d..85936c0 100644 --- a/include/llvm/ADT/StringExtras.h +++ b/include/llvm/ADT/StringExtras.h @@ -14,7 +14,7 @@ #ifndef LLVM_ADT_STRINGEXTRAS_H #define LLVM_ADT_STRINGEXTRAS_H -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/StringRef.h" #include <cctype> diff --git a/include/llvm/ADT/StringSwitch.h b/include/llvm/ADT/StringSwitch.h new file mode 100644 index 0000000..48a52de --- /dev/null +++ b/include/llvm/ADT/StringSwitch.h @@ -0,0 +1,83 @@ +//===--- StringSwitch.h - Switch-on-literal-string Construct --------------===/ +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +//===----------------------------------------------------------------------===/ +// +// This file implements the StringSwitch template, which mimics a switch() +// statements whose cases are string literals. +// +//===----------------------------------------------------------------------===/ +#ifndef LLVM_ADT_STRINGSWITCH_H +#define LLVM_ADT_STRINGSWITCH_H + +#include "llvm/ADT/StringRef.h" +#include <cassert> +#include <cstring> + +namespace llvm { + +/// \brief A switch()-like statement whose cases are string literals. +/// +/// The StringSwitch class is a simple form of a switch() statement that +/// determines whether the given string matches one of the given string +/// literals. The template type parameter \p T is the type of the value that +/// will be returned from the string-switch expression. For example, +/// the following code switches on the name of a color in \c argv[i]: +/// +/// \code +/// Color color = StringSwitch<Color>(argv[i]) +/// .Case("red", Red) +/// .Case("orange", Orange) +/// .Case("yellow", Yellow) +/// .Case("green", Green) +/// .Case("blue", Blue) +/// .Case("indigo", Indigo) +/// .Case("violet", Violet) +/// .Default(UnknownColor); +/// \endcode +template<typename T> +class StringSwitch { + /// \brief The string we are matching. + StringRef Str; + + /// \brief The result of this switch statement, once known. + T Result; + + /// \brief Set true when the result of this switch is already known; in this + /// case, Result is valid. + bool ResultKnown; + +public: + explicit StringSwitch(StringRef Str) + : Str(Str), ResultKnown(false) { } + + template<unsigned N> + StringSwitch& Case(const char (&S)[N], const T& Value) { + if (!ResultKnown && N-1 == Str.size() && + (std::memcmp(S, Str.data(), N-1) == 0)) { + Result = Value; + ResultKnown = true; + } + + return *this; + } + + T Default(const T& Value) { + if (ResultKnown) + return Result; + + return Value; + } + + operator T() { + assert(ResultKnown && "Fell off the end of a string-switch"); + return Result; + } +}; + +} // end namespace llvm + +#endif // LLVM_ADT_STRINGSWITCH_H diff --git a/include/llvm/ADT/Twine.h b/include/llvm/ADT/Twine.h index 88fde0a..f4722db 100644 --- a/include/llvm/ADT/Twine.h +++ b/include/llvm/ADT/Twine.h @@ -11,7 +11,7 @@ #define LLVM_ADT_TWINE_H #include "llvm/ADT/StringRef.h" -#include "llvm/Support/DataTypes.h" +#include "llvm/System/DataTypes.h" #include <cassert> #include <string> diff --git a/include/llvm/ADT/ValueMap.h b/include/llvm/ADT/ValueMap.h index 14f2100..b043c38 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<KeyT> to get default +// implementations of all the methods ValueMap uses. See ValueMapConfig for +// documentation of the functions you can override. // //===----------------------------------------------------------------------===// @@ -31,6 +43,9 @@ class ValueMapIterator; template<typename DenseMapT, typename KeyT> 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<typename KeyT> struct ValueMapConfig { /// If FollowRAUW is true, the ValueMap will update mappings on RAUW. If it's @@ -46,27 +61,17 @@ struct ValueMapConfig { template<typename ExtraDataT> static void onRAUW(const ExtraDataT &Data, KeyT Old, KeyT New) {} template<typename ExtraDataT> - 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<typename ExtraDataT> 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<KeyT> 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. +/// See the file comment. template<typename KeyT, typename ValueT, typename Config = ValueMapConfig<KeyT>, typename ValueInfoT = DenseMapInfo<ValueT> > class ValueMap { @@ -177,6 +182,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,10 +194,12 @@ private: } }; +// This CallbackVH updates its ValueMap when the contained Value changes, +// according to the user's preferences expressed through the Config object. template<typename KeyT, typename ValueT, typename Config, typename ValueInfoT> class ValueMapCallbackVH : public CallbackVH { friend class ValueMap<KeyT, ValueT, Config, ValueInfoT>; - friend class DenseMapInfo<ValueMapCallbackVH>; + friend struct DenseMapInfo<ValueMapCallbackVH>; typedef ValueMap<KeyT, ValueT, Config, ValueInfoT> ValueMapT; typedef typename llvm::remove_pointer<KeyT>::type KeySansPointerT; @@ -208,7 +218,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(); @@ -279,7 +289,7 @@ public: struct ValueTypeProxy { const KeyT first; ValueT& second; - ValueTypeProxy *operator->() { return this; } + ValueTypeProxy *operator->() { return this; } operator std::pair<KeyT, ValueT>() const { return std::make_pair(first, second); } @@ -301,11 +311,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 +339,7 @@ public: struct ValueTypeProxy { const KeyT first; const ValueT& second; - ValueTypeProxy *operator->() { return this; } + ValueTypeProxy *operator->() { return this; } operator std::pair<KeyT, ValueT>() const { return std::make_pair(first, second); } @@ -351,11 +361,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; } }; |