xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/LogicalView/Core/LVStringPool.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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:
LVStringPool()37   LVStringPool() { getIndex(""); }
38   LVStringPool(LVStringPool const &other) = delete;
39   LVStringPool(LVStringPool &&other) = delete;
40   ~LVStringPool() = default;
41 
isValidIndex(size_t Index)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.
getSize()47   size_t getSize() const { return Entries.size() - 1; }
48 
49   // Return the index for the specified key, otherwise 'BadIndex'.
findIndex(StringRef Key)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.
getIndex(StringRef 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, Value);
64     StringTable.insert(Entry);
65     Entries.push_back(Entry);
66     return Value;
67   }
68 
69   // Given the index, return its corresponding string.
getString(size_t Index)70   StringRef getString(size_t Index) const {
71     return (Index >= Entries.size()) ? StringRef() : Entries[Index]->getKey();
72   }
73 
print(raw_ostream & OS)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)
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