1 //===- TapiUniversal.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 // This file defines the Text-based Dynamic Library Stub format.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Object/TapiUniversal.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Object/TapiFile.h"
16 #include "llvm/TextAPI/TextAPIReader.h"
17
18 using namespace llvm;
19 using namespace MachO;
20 using namespace object;
21
TapiUniversal(MemoryBufferRef Source,Error & Err)22 TapiUniversal::TapiUniversal(MemoryBufferRef Source, Error &Err)
23 : Binary(ID_TapiUniversal, Source) {
24 Expected<std::unique_ptr<InterfaceFile>> Result = TextAPIReader::get(Source);
25 ErrorAsOutParameter ErrAsOuParam(Err);
26 if (!Result) {
27 Err = Result.takeError();
28 return;
29 }
30 ParsedFile = std::move(Result.get());
31
32 auto FlattenObjectInfo = [this](const auto &File,
33 std::optional<size_t> DocIdx = std::nullopt) {
34 StringRef Name = File->getInstallName();
35 for (const Architecture Arch : File->getArchitectures())
36 Libraries.emplace_back(Library({Name, Arch, DocIdx}));
37 };
38 FlattenObjectInfo(ParsedFile);
39 // Get inlined documents from tapi file.
40 size_t DocIdx = 0;
41 for (const std::shared_ptr<InterfaceFile> &File : ParsedFile->documents())
42 FlattenObjectInfo(File, DocIdx++);
43 }
44
45 TapiUniversal::~TapiUniversal() = default;
46
47 Expected<std::unique_ptr<TapiFile>>
getAsObjectFile() const48 TapiUniversal::ObjectForArch::getAsObjectFile() const {
49 const auto &InlinedDocuments = Parent->ParsedFile->documents();
50 const Library &CurrLib = Parent->Libraries[Index];
51 assert(
52 (isTopLevelLib() || (CurrLib.DocumentIdx.has_value() &&
53 (InlinedDocuments.size() > *CurrLib.DocumentIdx))) &&
54 "Index into documents exceeds the container for them");
55 InterfaceFile *IF = isTopLevelLib()
56 ? Parent->ParsedFile.get()
57 : InlinedDocuments[*CurrLib.DocumentIdx].get();
58 return std::make_unique<TapiFile>(Parent->getMemoryBufferRef(), *IF,
59 CurrLib.Arch);
60 }
61
62 Expected<std::unique_ptr<TapiUniversal>>
create(MemoryBufferRef Source)63 TapiUniversal::create(MemoryBufferRef Source) {
64 Error Err = Error::success();
65 std::unique_ptr<TapiUniversal> Ret(new TapiUniversal(Source, Err));
66 if (Err)
67 return std::move(Err);
68 return std::move(Ret);
69 }
70