1c20d1f90SCyndy Ishida //===- TapiUniversal.cpp --------------------------------------------------===//
2c20d1f90SCyndy Ishida //
3c20d1f90SCyndy Ishida // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c20d1f90SCyndy Ishida // See https://llvm.org/LICENSE.txt for license information.
5c20d1f90SCyndy Ishida // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c20d1f90SCyndy Ishida //
7c20d1f90SCyndy Ishida //===----------------------------------------------------------------------===//
8c20d1f90SCyndy Ishida //
9c20d1f90SCyndy Ishida // This file defines the Text-based Dynamic Library Stub format.
10c20d1f90SCyndy Ishida //
11c20d1f90SCyndy Ishida //===----------------------------------------------------------------------===//
12c20d1f90SCyndy Ishida 
13c20d1f90SCyndy Ishida #include "llvm/Object/TapiUniversal.h"
14c20d1f90SCyndy Ishida #include "llvm/ADT/StringRef.h"
15c20d1f90SCyndy Ishida #include "llvm/Object/Error.h"
16e72c195fSserge-sans-paille #include "llvm/Object/TapiFile.h"
17e72c195fSserge-sans-paille #include "llvm/TextAPI/ArchitectureSet.h"
180116d04dSCyndy Ishida #include "llvm/TextAPI/TextAPIReader.h"
19c20d1f90SCyndy Ishida 
20c20d1f90SCyndy Ishida using namespace llvm;
21c20d1f90SCyndy Ishida using namespace MachO;
22c20d1f90SCyndy Ishida using namespace object;
23c20d1f90SCyndy Ishida 
TapiUniversal(MemoryBufferRef Source,Error & Err)24c20d1f90SCyndy Ishida TapiUniversal::TapiUniversal(MemoryBufferRef Source, Error &Err)
25c20d1f90SCyndy Ishida     : Binary(ID_TapiUniversal, Source) {
2628fefcc8SCyndy Ishida   Expected<std::unique_ptr<InterfaceFile>> Result = TextAPIReader::get(Source);
27c20d1f90SCyndy Ishida   ErrorAsOutParameter ErrAsOuParam(&Err);
28c20d1f90SCyndy Ishida   if (!Result) {
29c20d1f90SCyndy Ishida     Err = Result.takeError();
30c20d1f90SCyndy Ishida     return;
31c20d1f90SCyndy Ishida   }
32c20d1f90SCyndy Ishida   ParsedFile = std::move(Result.get());
33c20d1f90SCyndy Ishida 
3428fefcc8SCyndy Ishida   auto FlattenObjectInfo = [this](const auto &File) {
3528fefcc8SCyndy Ishida     StringRef Name = File->getInstallName();
3628fefcc8SCyndy Ishida     for (const Architecture Arch : File->getArchitectures())
3728fefcc8SCyndy Ishida       Libraries.emplace_back(Library({Name, Arch}));
3828fefcc8SCyndy Ishida   };
3928fefcc8SCyndy Ishida 
4028fefcc8SCyndy Ishida   FlattenObjectInfo(ParsedFile);
4128fefcc8SCyndy Ishida   // Get inlined documents from tapi file.
4228fefcc8SCyndy Ishida   for (const std::shared_ptr<InterfaceFile> &File : ParsedFile->documents())
4328fefcc8SCyndy Ishida     FlattenObjectInfo(File);
44c20d1f90SCyndy Ishida }
45c20d1f90SCyndy Ishida 
46c20d1f90SCyndy Ishida TapiUniversal::~TapiUniversal() = default;
47c20d1f90SCyndy Ishida 
48c20d1f90SCyndy Ishida Expected<std::unique_ptr<TapiFile>>
getAsObjectFile() const49c20d1f90SCyndy Ishida TapiUniversal::ObjectForArch::getAsObjectFile() const {
50c20d1f90SCyndy Ishida   return std::unique_ptr<TapiFile>(new TapiFile(Parent->getMemoryBufferRef(),
51*5062d78fSKazu Hirata                                                 *Parent->ParsedFile,
5228fefcc8SCyndy Ishida                                                 Parent->Libraries[Index].Arch));
53c20d1f90SCyndy Ishida }
54c20d1f90SCyndy Ishida 
55c20d1f90SCyndy Ishida Expected<std::unique_ptr<TapiUniversal>>
create(MemoryBufferRef Source)56c20d1f90SCyndy Ishida TapiUniversal::create(MemoryBufferRef Source) {
57c20d1f90SCyndy Ishida   Error Err = Error::success();
58c20d1f90SCyndy Ishida   std::unique_ptr<TapiUniversal> Ret(new TapiUniversal(Source, Err));
59c20d1f90SCyndy Ishida   if (Err)
60c55cf4afSBill Wendling     return std::move(Err);
61c55cf4afSBill Wendling   return std::move(Ret);
62c20d1f90SCyndy Ishida }
63