OSDN Git Service

Cleanup: llvm::bsearch -> llvm::partition_point after r364719
[android-x86/external-llvm.git] / lib / TextAPI / MachO / InterfaceFile.cpp
1 //===- InterfaceFile.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 // Implements the Interface File.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/TextAPI/MachO/InterfaceFile.h"
14 #include <iomanip>
15 #include <sstream>
16
17 namespace llvm {
18 namespace MachO {
19 namespace detail {
20 template <typename C>
21 typename C::iterator addEntry(C &Container, StringRef InstallName) {
22   auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
23     return O.getInstallName() < InstallName;
24   });
25   if (I != Container.end() && I->getInstallName() == InstallName)
26     return I;
27
28   return Container.emplace(I, InstallName);
29 }
30 } // end namespace detail.
31
32 void InterfaceFile::addAllowableClient(StringRef Name,
33                                        ArchitectureSet Architectures) {
34   auto Client = detail::addEntry(AllowableClients, Name);
35   Client->addArchitectures(Architectures);
36 }
37
38 void InterfaceFile::addReexportedLibrary(StringRef InstallName,
39                                          ArchitectureSet Architectures) {
40   auto Lib = detail::addEntry(ReexportedLibraries, InstallName);
41   Lib->addArchitectures(Architectures);
42 }
43
44 void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) {
45   auto I = partition_point(UUIDs,
46                            [=](const std::pair<Architecture, std::string> &O) {
47                              return O.first < Arch;
48                            });
49
50   if (I != UUIDs.end() && Arch == I->first) {
51     I->second = UUID;
52     return;
53   }
54
55   UUIDs.emplace(I, Arch, UUID);
56   return;
57 }
58
59 void InterfaceFile::addUUID(Architecture Arch, uint8_t UUID[16]) {
60   std::stringstream Stream;
61   for (unsigned i = 0; i < 16; ++i) {
62     if (i == 4 || i == 6 || i == 8 || i == 10)
63       Stream << '-';
64     Stream << std::setfill('0') << std::setw(2) << std::uppercase << std::hex
65            << static_cast<int>(UUID[i]);
66   }
67   addUUID(Arch, Stream.str());
68 }
69
70 void InterfaceFile::addSymbol(SymbolKind Kind, StringRef Name,
71                               ArchitectureSet Archs, SymbolFlags Flags) {
72   Name = copyString(Name);
73   auto result = Symbols.try_emplace(SymbolsMapKey{Kind, Name}, nullptr);
74   if (result.second)
75     result.first->second = new (Allocator) Symbol{Kind, Name, Archs, Flags};
76   else
77     result.first->second->addArchitectures(Archs);
78 }
79
80 } // end namespace MachO.
81 } // end namespace llvm.