1 //===-- NonRelocatableStringpool.cpp --------------------------------------===//
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 #include "llvm/CodeGen/NonRelocatableStringpool.h"
10 #include "llvm/ADT/STLExtras.h"
11 
12 namespace llvm {
13 
14 DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
15   if (Translator)
16     S = Translator(S);
17   auto I = Strings.insert({S, DwarfStringPoolEntry()});
18   auto &Entry = I.first->second;
19   if (I.second || !Entry.isIndexed()) {
20     Entry.Index = NumEntries++;
21     Entry.Offset = CurrentEndOffset;
22     Entry.Symbol = nullptr;
23     CurrentEndOffset += S.size() + 1;
24   }
25   return DwarfStringPoolEntryRef(*I.first);
26 }
27 
28 StringRef NonRelocatableStringpool::internString(StringRef S) {
29   DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
30 
31   if (Translator)
32     S = Translator(S);
33 
34   auto InsertResult = Strings.insert({S, Entry});
35   return InsertResult.first->getKey();
36 }
37 
38 std::vector<DwarfStringPoolEntryRef>
39 NonRelocatableStringpool::getEntriesForEmission() const {
40   std::vector<DwarfStringPoolEntryRef> Result;
41   Result.reserve(Strings.size());
42   for (const auto &E : Strings)
43     if (E.getValue().isIndexed())
44       Result.emplace_back(E);
45   llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
46                         const DwarfStringPoolEntryRef B) {
47     return A.getIndex() < B.getIndex();
48   });
49   return Result;
50 }
51 
52 } // namespace llvm
53