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/Error.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/TextAPI/MachO/TextAPIReader.h"
18
19using namespace llvm;
20using namespace MachO;
21using namespace object;
22
23TapiUniversal::TapiUniversal(MemoryBufferRef Source, Error &Err)
24    : Binary(ID_TapiUniversal, Source) {
25  Expected<std::unique_ptr<InterfaceFile>> Result = TextAPIReader::get(Source);
26  ErrorAsOutParameter ErrAsOuParam(&Err);
27  if (!Result) {
28    Err = Result.takeError();
29    return;
30  }
31  ParsedFile = std::move(Result.get());
32
33  auto FlattenObjectInfo = [this](const auto &File) {
34    StringRef Name = File->getInstallName();
35    for (const Architecture Arch : File->getArchitectures())
36      Libraries.emplace_back(Library({Name, Arch}));
37  };
38
39  FlattenObjectInfo(ParsedFile);
40  // Get inlined documents from tapi file.
41  for (const std::shared_ptr<InterfaceFile> &File : ParsedFile->documents())
42    FlattenObjectInfo(File);
43}
44
45TapiUniversal::~TapiUniversal() = default;
46
47Expected<std::unique_ptr<TapiFile>>
48TapiUniversal::ObjectForArch::getAsObjectFile() const {
49  return std::unique_ptr<TapiFile>(new TapiFile(Parent->getMemoryBufferRef(),
50                                                *Parent->ParsedFile.get(),
51                                                Parent->Libraries[Index].Arch));
52}
53
54Expected<std::unique_ptr<TapiUniversal>>
55TapiUniversal::create(MemoryBufferRef Source) {
56  Error Err = Error::success();
57  std::unique_ptr<TapiUniversal> Ret(new TapiUniversal(Source, Err));
58  if (Err)
59    return std::move(Err);
60  return std::move(Ret);
61}
62