blob: b8392a112526d6cdf0b49998c4df5c427ef5dcb0 [file] [log] [blame]
Jonas Devliegheree993fb52018-03-01 10:05:54 +00001//===- NonRelocatableStringpool.cpp - A simple stringpool ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "NonRelocatableStringpool.h"
11
12namespace llvm {
13namespace dsymutil {
14
15DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
16 if (S.empty() && !Strings.empty())
17 return EmptyString;
18
Jonas Devlieghere20767d12019-01-07 23:27:25 +000019 if (Translator)
20 S = Translator(S);
Jonas Devliegheree993fb52018-03-01 10:05:54 +000021 auto I = Strings.insert({S, DwarfStringPoolEntry()});
22 auto &Entry = I.first->second;
Pavel Labath23332c52018-08-07 09:54:52 +000023 if (I.second || !Entry.isIndexed()) {
Jonas Devliegheree993fb52018-03-01 10:05:54 +000024 Entry.Index = NumEntries++;
25 Entry.Offset = CurrentEndOffset;
26 Entry.Symbol = nullptr;
27 CurrentEndOffset += S.size() + 1;
28 }
Pavel Labath23332c52018-08-07 09:54:52 +000029 return DwarfStringPoolEntryRef(*I.first, true);
Jonas Devliegheree993fb52018-03-01 10:05:54 +000030}
31
32StringRef NonRelocatableStringpool::internString(StringRef S) {
Pavel Labath23332c52018-08-07 09:54:52 +000033 DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
Jonas Devlieghere20767d12019-01-07 23:27:25 +000034
35 if (Translator)
36 S = Translator(S);
37
Jonas Devliegheree993fb52018-03-01 10:05:54 +000038 auto InsertResult = Strings.insert({S, Entry});
39 return InsertResult.first->getKey();
40}
41
42std::vector<DwarfStringPoolEntryRef>
Pavel Labath23332c52018-08-07 09:54:52 +000043NonRelocatableStringpool::getEntriesForEmission() const {
Jonas Devliegheree993fb52018-03-01 10:05:54 +000044 std::vector<DwarfStringPoolEntryRef> Result;
45 Result.reserve(Strings.size());
46 for (const auto &E : Strings)
Pavel Labath23332c52018-08-07 09:54:52 +000047 if (E.getValue().isIndexed())
48 Result.emplace_back(E, true);
Fangrui Songc4662642018-09-30 22:31:29 +000049 llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
50 const DwarfStringPoolEntryRef B) {
51 return A.getIndex() < B.getIndex();
52 });
Jonas Devliegheree993fb52018-03-01 10:05:54 +000053 return Result;
54}
55
56} // namespace dsymutil
57} // namespace llvm