Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:43

0001 //===-- LVStringPool.h ------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the LVStringPool class, which is used to implement a
0010 // basic string pool table.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
0015 #define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H
0016 
0017 #include "llvm/ADT/StringMap.h"
0018 #include "llvm/Support/Allocator.h"
0019 #include "llvm/Support/Debug.h"
0020 #include "llvm/Support/Format.h"
0021 #include "llvm/Support/raw_ostream.h"
0022 #include <iomanip>
0023 #include <vector>
0024 
0025 namespace llvm {
0026 namespace logicalview {
0027 
0028 class LVStringPool {
0029   static constexpr size_t BadIndex = std::numeric_limits<size_t>::max();
0030   using TableType = StringMap<size_t, BumpPtrAllocator>;
0031   using ValueType = TableType::value_type;
0032   BumpPtrAllocator Allocator;
0033   TableType StringTable;
0034   std::vector<ValueType *> Entries;
0035 
0036 public:
0037   LVStringPool() { getIndex(""); }
0038   LVStringPool(LVStringPool const &other) = delete;
0039   LVStringPool(LVStringPool &&other) = delete;
0040   ~LVStringPool() = default;
0041 
0042   bool isValidIndex(size_t Index) const { return Index != BadIndex; }
0043 
0044   // Return number of strings in the pool. The empty string is allocated
0045   // at the slot zero. We substract 1 to indicate the number of non empty
0046   // strings.
0047   size_t getSize() const { return Entries.size() - 1; }
0048 
0049   // Return the index for the specified key, otherwise 'BadIndex'.
0050   size_t findIndex(StringRef Key) const {
0051     TableType::const_iterator Iter = StringTable.find(Key);
0052     if (Iter != StringTable.end())
0053       return Iter->second;
0054     return BadIndex;
0055   }
0056 
0057   // Return an index for the specified key.
0058   size_t getIndex(StringRef Key) {
0059     size_t Index = findIndex(Key);
0060     if (isValidIndex(Index))
0061       return Index;
0062     size_t Value = Entries.size();
0063     ValueType *Entry = ValueType::create(Key, Allocator, Value);
0064     StringTable.insert(Entry);
0065     Entries.push_back(Entry);
0066     return Value;
0067   }
0068 
0069   // Given the index, return its corresponding string.
0070   StringRef getString(size_t Index) const {
0071     return (Index >= Entries.size()) ? StringRef() : Entries[Index]->getKey();
0072   }
0073 
0074   void print(raw_ostream &OS) const {
0075     if (!Entries.empty()) {
0076       OS << "\nString Pool:\n";
0077       for (const ValueType *Entry : Entries)
0078         OS << "Index: " << Entry->getValue() << ", "
0079            << "Key: '" << Entry->getKey() << "'\n";
0080     }
0081   }
0082 
0083 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0084   void dump() const { print(dbgs()); }
0085 #endif
0086 };
0087 
0088 } // namespace logicalview
0089 } // end namespace llvm
0090 
0091 #endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVSTRINGPOOL_H