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