TapiUniversal.cpp 2.1 KB

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