Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. //===-- LVStringPool.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. // This file defines the LVStringPool class, which is used to implement a
  10. // basic string pool table.
  11. //
  12. //===----------------------------------------------------------------------===//
  13.  
  14. #ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
  15. #define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
  16.  
  17. #include "llvm/ADT/StringMap.h"
  18. #include "llvm/Support/Allocator.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/Format.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <iomanip>
  23. #include <vector>
  24.  
  25. namespace llvm {
  26. namespace logicalview {
  27.  
  28. class LVStringPool {
  29.   static constexpr size_t BadIndex = std::numeric_limits<size_t>::max();
  30.   using TableType = StringMap<size_t, BumpPtrAllocator>;
  31.   using ValueType = TableType::value_type;
  32.   BumpPtrAllocator Allocator;
  33.   TableType StringTable;
  34.   std::vector<ValueType *> Entries;
  35.  
  36. public:
  37.   LVStringPool() { getIndex(""); }
  38.   LVStringPool(LVStringPool const &other) = delete;
  39.   LVStringPool(LVStringPool &&other) = delete;
  40.   ~LVStringPool() = default;
  41.  
  42.   bool isValidIndex(size_t Index) const { return Index != BadIndex; }
  43.  
  44.   // Return number of strings in the pool. The empty string is allocated
  45.   // at the slot zero. We substract 1 to indicate the number of non empty
  46.   // strings.
  47.   size_t getSize() const { return Entries.size() - 1; }
  48.  
  49.   // Return the index for the specified key, otherwise 'BadIndex'.
  50.   size_t findIndex(StringRef Key) const {
  51.     TableType::const_iterator Iter = StringTable.find(Key);
  52.     if (Iter != StringTable.end())
  53.       return Iter->second;
  54.     return BadIndex;
  55.   }
  56.  
  57.   // Return an index for the specified key.
  58.   size_t getIndex(StringRef Key) {
  59.     size_t Index = findIndex(Key);
  60.     if (isValidIndex(Index))
  61.       return Index;
  62.     size_t Value = Entries.size();
  63.     ValueType *Entry = ValueType::create(Key, Allocator, std::move(Value));
  64.     StringTable.insert(Entry);
  65.     Entries.push_back(Entry);
  66.     return Value;
  67.   }
  68.  
  69.   // Given the index, return its corresponding string.
  70.   StringRef getString(size_t Index) const {
  71.     return (Index >= Entries.size()) ? StringRef() : Entries[Index]->getKey();
  72.   }
  73.  
  74.   void print(raw_ostream &OS) const {
  75.     if (!Entries.empty()) {
  76.       OS << "\nString Pool:\n";
  77.       for (const ValueType *Entry : Entries)
  78.         OS << "Index: " << Entry->getValue() << ", "
  79.            << "Key: '" << Entry->getKey() << "'\n";
  80.     }
  81.   }
  82.  
  83. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  84.   void dump() const { print(dbgs()); }
  85. #endif
  86. };
  87.  
  88. } // namespace logicalview
  89. } // end namespace llvm
  90.  
  91. #endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
  92.