TapiUniversal.cpp 2.1 KB

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