Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:16

0001 //===- StringTableBuilder.h - String table building utility -----*- 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 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
0010 #define LLVM_MC_STRINGTABLEBUILDER_H
0011 
0012 #include "llvm/ADT/CachedHashString.h"
0013 #include "llvm/ADT/DenseMap.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/Support/Alignment.h"
0016 #include <cstddef>
0017 #include <cstdint>
0018 
0019 namespace llvm {
0020 
0021 class raw_ostream;
0022 
0023 /// Utility for building string tables with deduplicated suffixes.
0024 class StringTableBuilder {
0025 public:
0026   enum Kind {
0027     ELF,
0028     WinCOFF,
0029     MachO,
0030     MachO64,
0031     MachOLinked,
0032     MachO64Linked,
0033     RAW,
0034     DWARF,
0035     XCOFF,
0036     DXContainer
0037   };
0038 
0039 private:
0040   DenseMap<CachedHashStringRef, size_t> StringIndexMap;
0041   size_t Size = 0;
0042   Kind K;
0043   Align Alignment;
0044   bool Finalized = false;
0045 
0046   void finalizeStringTable(bool Optimize);
0047   void initSize();
0048 
0049 public:
0050   StringTableBuilder(Kind K, Align Alignment = Align(1));
0051   ~StringTableBuilder();
0052 
0053   /// Add a string to the builder. Returns the position of S in the
0054   /// table. The position will be changed if finalize is used.
0055   /// Can only be used before the table is finalized.
0056   size_t add(CachedHashStringRef S);
0057   size_t add(StringRef S) { return add(CachedHashStringRef(S)); }
0058 
0059   /// Analyze the strings and build the final table. No more strings can
0060   /// be added after this point.
0061   void finalize();
0062 
0063   /// Finalize the string table without reording it. In this mode, offsets
0064   /// returned by add will still be valid.
0065   void finalizeInOrder();
0066 
0067   /// Get the offest of a string in the string table. Can only be used
0068   /// after the table is finalized.
0069   size_t getOffset(CachedHashStringRef S) const;
0070   size_t getOffset(StringRef S) const {
0071     return getOffset(CachedHashStringRef(S));
0072   }
0073 
0074   /// Check if a string is contained in the string table. Since this class
0075   /// doesn't store the string values, this function can be used to check if
0076   /// storage needs to be done prior to adding the string.
0077   bool contains(StringRef S) const { return contains(CachedHashStringRef(S)); }
0078   bool contains(CachedHashStringRef S) const { return StringIndexMap.count(S); }
0079 
0080   size_t getSize() const { return Size; }
0081   void clear();
0082 
0083   void write(raw_ostream &OS) const;
0084   void write(uint8_t *Buf) const;
0085 
0086   bool isFinalized() const { return Finalized; }
0087 };
0088 
0089 } // end namespace llvm
0090 
0091 #endif // LLVM_MC_STRINGTABLEBUILDER_H