TextStub.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. //===- llvm/TextAPI/TextStub.cpp - Text Stub --------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief Implements the text stub file reader/writer.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "TextAPIContext.h"
  15. #include "TextStubCommon.h"
  16. #include "llvm/ADT/BitmaskEnum.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/Support/Allocator.h"
  20. #include "llvm/Support/SourceMgr.h"
  21. #include "llvm/Support/YAMLTraits.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/TextAPI/MachO/Architecture.h"
  24. #include "llvm/TextAPI/MachO/ArchitectureSet.h"
  25. #include "llvm/TextAPI/MachO/InterfaceFile.h"
  26. #include "llvm/TextAPI/MachO/PackedVersion.h"
  27. #include "llvm/TextAPI/MachO/TextAPIReader.h"
  28. #include "llvm/TextAPI/MachO/TextAPIWriter.h"
  29. #include <algorithm>
  30. #include <set>
  31. // clang-format off
  32. /*
  33. YAML Format specification.
  34. The TBD v1 format only support two level address libraries and is per
  35. definition application extension safe.
  36. --- # the tag !tapi-tbd-v1 is optional and
  37. # shouldn't be emitted to support older linker.
  38. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  39. # supported by this file.
  40. platform: ios # Specifies the platform (macosx, ios, etc)
  41. install-name: /u/l/libfoo.dylib #
  42. current-version: 1.2.3 # Optional: defaults to 1.0
  43. compatibility-version: 1.0 # Optional: defaults to 1.0
  44. swift-version: 0 # Optional: defaults to 0
  45. objc-constraint: none # Optional: defaults to none
  46. exports: # List of export sections
  47. ...
  48. Each export section is defined as following:
  49. - archs: [ arm64 ] # the list of architecture slices
  50. allowed-clients: [ client ] # Optional: List of clients
  51. re-exports: [ ] # Optional: List of re-exports
  52. symbols: [ _sym ] # Optional: List of symbols
  53. objc-classes: [] # Optional: List of Objective-C classes
  54. objc-ivars: [] # Optional: List of Objective C Instance
  55. # Variables
  56. weak-def-symbols: [] # Optional: List of weak defined symbols
  57. thread-local-symbols: [] # Optional: List of thread local symbols
  58. */
  59. /*
  60. YAML Format specification.
  61. --- !tapi-tbd-v2
  62. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  63. # supported by this file.
  64. uuids: [ armv7:... ] # Optional: List of architecture and UUID pairs.
  65. platform: ios # Specifies the platform (macosx, ios, etc)
  66. flags: [] # Optional:
  67. install-name: /u/l/libfoo.dylib #
  68. current-version: 1.2.3 # Optional: defaults to 1.0
  69. compatibility-version: 1.0 # Optional: defaults to 1.0
  70. swift-version: 0 # Optional: defaults to 0
  71. objc-constraint: retain_release # Optional: defaults to retain_release
  72. parent-umbrella: # Optional:
  73. exports: # List of export sections
  74. ...
  75. undefineds: # List of undefineds sections
  76. ...
  77. Each export section is defined as following:
  78. - archs: [ arm64 ] # the list of architecture slices
  79. allowed-clients: [ client ] # Optional: List of clients
  80. re-exports: [ ] # Optional: List of re-exports
  81. symbols: [ _sym ] # Optional: List of symbols
  82. objc-classes: [] # Optional: List of Objective-C classes
  83. objc-ivars: [] # Optional: List of Objective C Instance
  84. # Variables
  85. weak-def-symbols: [] # Optional: List of weak defined symbols
  86. thread-local-symbols: [] # Optional: List of thread local symbols
  87. Each undefineds section is defined as following:
  88. - archs: [ arm64 ] # the list of architecture slices
  89. symbols: [ _sym ] # Optional: List of symbols
  90. objc-classes: [] # Optional: List of Objective-C classes
  91. objc-ivars: [] # Optional: List of Objective C Instance Variables
  92. weak-ref-symbols: [] # Optional: List of weak defined symbols
  93. */
  94. /*
  95. YAML Format specification.
  96. --- !tapi-tbd-v3
  97. archs: [ armv7, armv7s, arm64 ] # the list of architecture slices that are
  98. # supported by this file.
  99. uuids: [ armv7:... ] # Optional: List of architecture and UUID pairs.
  100. platform: ios # Specifies the platform (macosx, ios, etc)
  101. flags: [] # Optional:
  102. install-name: /u/l/libfoo.dylib #
  103. current-version: 1.2.3 # Optional: defaults to 1.0
  104. compatibility-version: 1.0 # Optional: defaults to 1.0
  105. swift-abi-version: 0 # Optional: defaults to 0
  106. objc-constraint: retain_release # Optional: defaults to retain_release
  107. parent-umbrella: # Optional:
  108. exports: # List of export sections
  109. ...
  110. undefineds: # List of undefineds sections
  111. ...
  112. Each export section is defined as following:
  113. - archs: [ arm64 ] # the list of architecture slices
  114. allowed-clients: [ client ] # Optional: List of clients
  115. re-exports: [ ] # Optional: List of re-exports
  116. symbols: [ _sym ] # Optional: List of symbols
  117. objc-classes: [] # Optional: List of Objective-C classes
  118. objc-eh-types: [] # Optional: List of Objective-C classes
  119. # with EH
  120. objc-ivars: [] # Optional: List of Objective C Instance
  121. # Variables
  122. weak-def-symbols: [] # Optional: List of weak defined symbols
  123. thread-local-symbols: [] # Optional: List of thread local symbols
  124. Each undefineds section is defined as following:
  125. - archs: [ arm64 ] # the list of architecture slices
  126. symbols: [ _sym ] # Optional: List of symbols
  127. objc-classes: [] # Optional: List of Objective-C classes
  128. objc-eh-types: [] # Optional: List of Objective-C classes
  129. # with EH
  130. objc-ivars: [] # Optional: List of Objective C Instance Variables
  131. weak-ref-symbols: [] # Optional: List of weak defined symbols
  132. */
  133. // clang-format on
  134. using namespace llvm;
  135. using namespace llvm::yaml;
  136. using namespace llvm::MachO;
  137. namespace {
  138. struct ExportSection {
  139. std::vector<Architecture> Architectures;
  140. std::vector<FlowStringRef> AllowableClients;
  141. std::vector<FlowStringRef> ReexportedLibraries;
  142. std::vector<FlowStringRef> Symbols;
  143. std::vector<FlowStringRef> Classes;
  144. std::vector<FlowStringRef> ClassEHs;
  145. std::vector<FlowStringRef> IVars;
  146. std::vector<FlowStringRef> WeakDefSymbols;
  147. std::vector<FlowStringRef> TLVSymbols;
  148. };
  149. struct UndefinedSection {
  150. std::vector<Architecture> Architectures;
  151. std::vector<FlowStringRef> Symbols;
  152. std::vector<FlowStringRef> Classes;
  153. std::vector<FlowStringRef> ClassEHs;
  154. std::vector<FlowStringRef> IVars;
  155. std::vector<FlowStringRef> WeakRefSymbols;
  156. };
  157. // clang-format off
  158. enum Flags : unsigned {
  159. None = 0U,
  160. FlatNamespace = 1U << 0,
  161. NotApplicationExtensionSafe = 1U << 1,
  162. InstallAPI = 1U << 2,
  163. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/InstallAPI),
  164. };
  165. // clang-format on
  166. } // end anonymous namespace.
  167. LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Architecture)
  168. LLVM_YAML_IS_SEQUENCE_VECTOR(ExportSection)
  169. LLVM_YAML_IS_SEQUENCE_VECTOR(UndefinedSection)
  170. namespace llvm {
  171. namespace yaml {
  172. template <> struct MappingTraits<ExportSection> {
  173. static void mapping(IO &IO, ExportSection &Section) {
  174. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  175. assert((!Ctx || Ctx && Ctx->FileType != FileType::Invalid) &&
  176. "File type is not set in YAML context");
  177. IO.mapRequired("archs", Section.Architectures);
  178. if (Ctx->FileType == FileType::TBD_V1)
  179. IO.mapOptional("allowed-clients", Section.AllowableClients);
  180. else
  181. IO.mapOptional("allowable-clients", Section.AllowableClients);
  182. IO.mapOptional("re-exports", Section.ReexportedLibraries);
  183. IO.mapOptional("symbols", Section.Symbols);
  184. IO.mapOptional("objc-classes", Section.Classes);
  185. if (Ctx->FileType == FileType::TBD_V3)
  186. IO.mapOptional("objc-eh-types", Section.ClassEHs);
  187. IO.mapOptional("objc-ivars", Section.IVars);
  188. IO.mapOptional("weak-def-symbols", Section.WeakDefSymbols);
  189. IO.mapOptional("thread-local-symbols", Section.TLVSymbols);
  190. }
  191. };
  192. template <> struct MappingTraits<UndefinedSection> {
  193. static void mapping(IO &IO, UndefinedSection &Section) {
  194. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  195. assert((!Ctx || Ctx && Ctx->FileType != FileType::Invalid) &&
  196. "File type is not set in YAML context");
  197. IO.mapRequired("archs", Section.Architectures);
  198. IO.mapOptional("symbols", Section.Symbols);
  199. IO.mapOptional("objc-classes", Section.Classes);
  200. if (Ctx->FileType == FileType::TBD_V3)
  201. IO.mapOptional("objc-eh-types", Section.ClassEHs);
  202. IO.mapOptional("objc-ivars", Section.IVars);
  203. IO.mapOptional("weak-ref-symbols", Section.WeakRefSymbols);
  204. }
  205. };
  206. template <> struct ScalarBitSetTraits<Flags> {
  207. static void bitset(IO &IO, Flags &Flags) {
  208. IO.bitSetCase(Flags, "flat_namespace", Flags::FlatNamespace);
  209. IO.bitSetCase(Flags, "not_app_extension_safe",
  210. Flags::NotApplicationExtensionSafe);
  211. IO.bitSetCase(Flags, "installapi", Flags::InstallAPI);
  212. }
  213. };
  214. template <> struct MappingTraits<const InterfaceFile *> {
  215. struct NormalizedTBD {
  216. explicit NormalizedTBD(IO &IO) {}
  217. NormalizedTBD(IO &IO, const InterfaceFile *&File) {
  218. Architectures = File->getArchitectures();
  219. UUIDs = File->uuids();
  220. Platform = File->getPlatform();
  221. InstallName = File->getInstallName();
  222. CurrentVersion = PackedVersion(File->getCurrentVersion());
  223. CompatibilityVersion = PackedVersion(File->getCompatibilityVersion());
  224. SwiftVersion = File->getSwiftABIVersion();
  225. ObjCConstraint = File->getObjCConstraint();
  226. Flags = Flags::None;
  227. if (!File->isApplicationExtensionSafe())
  228. Flags |= Flags::NotApplicationExtensionSafe;
  229. if (!File->isTwoLevelNamespace())
  230. Flags |= Flags::FlatNamespace;
  231. if (File->isInstallAPI())
  232. Flags |= Flags::InstallAPI;
  233. ParentUmbrella = File->getParentUmbrella();
  234. std::set<ArchitectureSet> ArchSet;
  235. for (const auto &Library : File->allowableClients())
  236. ArchSet.insert(Library.getArchitectures());
  237. for (const auto &Library : File->reexportedLibraries())
  238. ArchSet.insert(Library.getArchitectures());
  239. std::map<const Symbol *, ArchitectureSet> SymbolToArchSet;
  240. for (const auto *Symbol : File->exports()) {
  241. auto Architectures = Symbol->getArchitectures();
  242. SymbolToArchSet[Symbol] = Architectures;
  243. ArchSet.insert(Architectures);
  244. }
  245. for (auto Architectures : ArchSet) {
  246. ExportSection Section;
  247. Section.Architectures = Architectures;
  248. for (const auto &Library : File->allowableClients())
  249. if (Library.getArchitectures() == Architectures)
  250. Section.AllowableClients.emplace_back(Library.getInstallName());
  251. for (const auto &Library : File->reexportedLibraries())
  252. if (Library.getArchitectures() == Architectures)
  253. Section.ReexportedLibraries.emplace_back(Library.getInstallName());
  254. for (const auto &SymArch : SymbolToArchSet) {
  255. if (SymArch.second != Architectures)
  256. continue;
  257. const auto *Symbol = SymArch.first;
  258. switch (Symbol->getKind()) {
  259. case SymbolKind::GlobalSymbol:
  260. if (Symbol->isWeakDefined())
  261. Section.WeakDefSymbols.emplace_back(Symbol->getName());
  262. else if (Symbol->isThreadLocalValue())
  263. Section.TLVSymbols.emplace_back(Symbol->getName());
  264. else
  265. Section.Symbols.emplace_back(Symbol->getName());
  266. break;
  267. case SymbolKind::ObjectiveCClass:
  268. if (File->getFileType() != FileType::TBD_V3)
  269. Section.Classes.emplace_back(
  270. copyString("_" + Symbol->getName().str()));
  271. else
  272. Section.Classes.emplace_back(Symbol->getName());
  273. break;
  274. case SymbolKind::ObjectiveCClassEHType:
  275. if (File->getFileType() != FileType::TBD_V3)
  276. Section.Symbols.emplace_back(
  277. copyString("_OBJC_EHTYPE_$_" + Symbol->getName().str()));
  278. else
  279. Section.ClassEHs.emplace_back(Symbol->getName());
  280. break;
  281. case SymbolKind::ObjectiveCInstanceVariable:
  282. if (File->getFileType() != FileType::TBD_V3)
  283. Section.IVars.emplace_back(
  284. copyString("_" + Symbol->getName().str()));
  285. else
  286. Section.IVars.emplace_back(Symbol->getName());
  287. break;
  288. }
  289. }
  290. llvm::sort(Section.Symbols.begin(), Section.Symbols.end());
  291. llvm::sort(Section.Classes.begin(), Section.Classes.end());
  292. llvm::sort(Section.ClassEHs.begin(), Section.ClassEHs.end());
  293. llvm::sort(Section.IVars.begin(), Section.IVars.end());
  294. llvm::sort(Section.WeakDefSymbols.begin(),
  295. Section.WeakDefSymbols.end());
  296. llvm::sort(Section.TLVSymbols.begin(), Section.TLVSymbols.end());
  297. Exports.emplace_back(std::move(Section));
  298. }
  299. ArchSet.clear();
  300. SymbolToArchSet.clear();
  301. for (const auto *Symbol : File->undefineds()) {
  302. auto Architectures = Symbol->getArchitectures();
  303. SymbolToArchSet[Symbol] = Architectures;
  304. ArchSet.insert(Architectures);
  305. }
  306. for (auto Architectures : ArchSet) {
  307. UndefinedSection Section;
  308. Section.Architectures = Architectures;
  309. for (const auto &SymArch : SymbolToArchSet) {
  310. if (SymArch.second != Architectures)
  311. continue;
  312. const auto *Symbol = SymArch.first;
  313. switch (Symbol->getKind()) {
  314. case SymbolKind::GlobalSymbol:
  315. if (Symbol->isWeakReferenced())
  316. Section.WeakRefSymbols.emplace_back(Symbol->getName());
  317. else
  318. Section.Symbols.emplace_back(Symbol->getName());
  319. break;
  320. case SymbolKind::ObjectiveCClass:
  321. if (File->getFileType() != FileType::TBD_V3)
  322. Section.Classes.emplace_back(
  323. copyString("_" + Symbol->getName().str()));
  324. else
  325. Section.Classes.emplace_back(Symbol->getName());
  326. break;
  327. case SymbolKind::ObjectiveCClassEHType:
  328. if (File->getFileType() != FileType::TBD_V3)
  329. Section.Symbols.emplace_back(
  330. copyString("_OBJC_EHTYPE_$_" + Symbol->getName().str()));
  331. else
  332. Section.ClassEHs.emplace_back(Symbol->getName());
  333. break;
  334. case SymbolKind::ObjectiveCInstanceVariable:
  335. if (File->getFileType() != FileType::TBD_V3)
  336. Section.IVars.emplace_back(
  337. copyString("_" + Symbol->getName().str()));
  338. else
  339. Section.IVars.emplace_back(Symbol->getName());
  340. break;
  341. }
  342. }
  343. llvm::sort(Section.Symbols.begin(), Section.Symbols.end());
  344. llvm::sort(Section.Classes.begin(), Section.Classes.end());
  345. llvm::sort(Section.ClassEHs.begin(), Section.ClassEHs.end());
  346. llvm::sort(Section.IVars.begin(), Section.IVars.end());
  347. llvm::sort(Section.WeakRefSymbols.begin(),
  348. Section.WeakRefSymbols.end());
  349. Undefineds.emplace_back(std::move(Section));
  350. }
  351. }
  352. const InterfaceFile *denormalize(IO &IO) {
  353. auto Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  354. assert(Ctx);
  355. auto *File = new InterfaceFile;
  356. File->setPath(Ctx->Path);
  357. File->setFileType(Ctx->FileType);
  358. for (auto &ID : UUIDs)
  359. File->addUUID(ID.first, ID.second);
  360. File->setPlatform(Platform);
  361. File->setArchitectures(Architectures);
  362. File->setInstallName(InstallName);
  363. File->setCurrentVersion(CurrentVersion);
  364. File->setCompatibilityVersion(CompatibilityVersion);
  365. File->setSwiftABIVersion(SwiftVersion);
  366. File->setObjCConstraint(ObjCConstraint);
  367. File->setParentUmbrella(ParentUmbrella);
  368. if (Ctx->FileType == FileType::TBD_V1) {
  369. File->setTwoLevelNamespace();
  370. File->setApplicationExtensionSafe();
  371. } else {
  372. File->setTwoLevelNamespace(!(Flags & Flags::FlatNamespace));
  373. File->setApplicationExtensionSafe(
  374. !(Flags & Flags::NotApplicationExtensionSafe));
  375. File->setInstallAPI(Flags & Flags::InstallAPI);
  376. }
  377. for (const auto &Section : Exports) {
  378. for (const auto &Library : Section.AllowableClients)
  379. File->addAllowableClient(Library, Section.Architectures);
  380. for (const auto &Library : Section.ReexportedLibraries)
  381. File->addReexportedLibrary(Library, Section.Architectures);
  382. for (const auto &Symbol : Section.Symbols) {
  383. if (Ctx->FileType != FileType::TBD_V3 &&
  384. Symbol.value.startswith("_OBJC_EHTYPE_$_"))
  385. File->addSymbol(SymbolKind::ObjectiveCClassEHType,
  386. Symbol.value.drop_front(15), Section.Architectures);
  387. else
  388. File->addSymbol(SymbolKind::GlobalSymbol, Symbol,
  389. Section.Architectures);
  390. }
  391. for (auto &Symbol : Section.Classes) {
  392. auto Name = Symbol.value;
  393. if (Ctx->FileType != FileType::TBD_V3)
  394. Name = Name.drop_front();
  395. File->addSymbol(SymbolKind::ObjectiveCClass, Name,
  396. Section.Architectures);
  397. }
  398. for (auto &Symbol : Section.ClassEHs)
  399. File->addSymbol(SymbolKind::ObjectiveCClassEHType, Symbol,
  400. Section.Architectures);
  401. for (auto &Symbol : Section.IVars) {
  402. auto Name = Symbol.value;
  403. if (Ctx->FileType != FileType::TBD_V3)
  404. Name = Name.drop_front();
  405. File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, Name,
  406. Section.Architectures);
  407. }
  408. for (auto &Symbol : Section.WeakDefSymbols)
  409. File->addSymbol(SymbolKind::GlobalSymbol, Symbol,
  410. Section.Architectures, SymbolFlags::WeakDefined);
  411. for (auto &Symbol : Section.TLVSymbols)
  412. File->addSymbol(SymbolKind::GlobalSymbol, Symbol,
  413. Section.Architectures, SymbolFlags::ThreadLocalValue);
  414. }
  415. for (const auto &Section : Undefineds) {
  416. for (auto &Symbol : Section.Symbols) {
  417. if (Ctx->FileType != FileType::TBD_V3 &&
  418. Symbol.value.startswith("_OBJC_EHTYPE_$_"))
  419. File->addSymbol(SymbolKind::ObjectiveCClassEHType,
  420. Symbol.value.drop_front(15), Section.Architectures,
  421. SymbolFlags::Undefined);
  422. else
  423. File->addSymbol(SymbolKind::GlobalSymbol, Symbol,
  424. Section.Architectures, SymbolFlags::Undefined);
  425. }
  426. for (auto &Symbol : Section.Classes) {
  427. auto Name = Symbol.value;
  428. if (Ctx->FileType != FileType::TBD_V3)
  429. Name = Name.drop_front();
  430. File->addSymbol(SymbolKind::ObjectiveCClass, Name,
  431. Section.Architectures, SymbolFlags::Undefined);
  432. }
  433. for (auto &Symbol : Section.ClassEHs)
  434. File->addSymbol(SymbolKind::ObjectiveCClassEHType, Symbol,
  435. Section.Architectures, SymbolFlags::Undefined);
  436. for (auto &Symbol : Section.IVars) {
  437. auto Name = Symbol.value;
  438. if (Ctx->FileType != FileType::TBD_V3)
  439. Name = Name.drop_front();
  440. File->addSymbol(SymbolKind::ObjectiveCInstanceVariable, Name,
  441. Section.Architectures, SymbolFlags::Undefined);
  442. }
  443. for (auto &Symbol : Section.WeakRefSymbols)
  444. File->addSymbol(SymbolKind::GlobalSymbol, Symbol,
  445. Section.Architectures,
  446. SymbolFlags::Undefined | SymbolFlags::WeakReferenced);
  447. }
  448. return File;
  449. }
  450. llvm::BumpPtrAllocator Allocator;
  451. StringRef copyString(StringRef String) {
  452. if (String.empty())
  453. return {};
  454. void *Ptr = Allocator.Allocate(String.size(), 1);
  455. memcpy(Ptr, String.data(), String.size());
  456. return StringRef(reinterpret_cast<const char *>(Ptr), String.size());
  457. }
  458. std::vector<Architecture> Architectures;
  459. std::vector<UUID> UUIDs;
  460. Platform Platform{Platform::unknown};
  461. StringRef InstallName;
  462. PackedVersion CurrentVersion;
  463. PackedVersion CompatibilityVersion;
  464. SwiftVersion SwiftVersion;
  465. ObjCConstraint ObjCConstraint;
  466. Flags Flags{Flags::None};
  467. StringRef ParentUmbrella;
  468. std::vector<ExportSection> Exports;
  469. std::vector<UndefinedSection> Undefineds;
  470. };
  471. static void mapping(IO &IO, const InterfaceFile *&File) {
  472. auto *Ctx = reinterpret_cast<TextAPIContext *>(IO.getContext());
  473. assert((!Ctx || !IO.outputting() ||
  474. Ctx && Ctx->FileType != FileType::Invalid) &&
  475. "File type is not set in YAML context");
  476. MappingNormalization<NormalizedTBD, const InterfaceFile *> Keys(IO, File);
  477. // prope file type when reading.
  478. if (!IO.outputting()) {
  479. if (IO.mapTag("!tapi-tbd-v2", false))
  480. Ctx->FileType = FileType::TBD_V2;
  481. else if (IO.mapTag("!tapi-tbd-v3", false))
  482. Ctx->FileType = FileType::TBD_V2;
  483. else if (IO.mapTag("!tapi-tbd-v1", false) ||
  484. IO.mapTag("tag:yaml.org,2002:map", false))
  485. Ctx->FileType = FileType::TBD_V1;
  486. else {
  487. IO.setError("unsupported file type");
  488. return;
  489. }
  490. }
  491. // Set file tyoe when writing.
  492. if (IO.outputting()) {
  493. switch (Ctx->FileType) {
  494. default:
  495. llvm_unreachable("unexpected file type");
  496. case FileType::TBD_V1:
  497. // Don't write the tag into the .tbd file for TBD v1.
  498. break;
  499. case FileType::TBD_V2:
  500. IO.mapTag("!tapi-tbd-v2", true);
  501. break;
  502. case FileType::TBD_V3:
  503. IO.mapTag("!tapi-tbd-v3", true);
  504. break;
  505. }
  506. }
  507. IO.mapRequired("archs", Keys->Architectures);
  508. if (Ctx->FileType != FileType::TBD_V1)
  509. IO.mapOptional("uuids", Keys->UUIDs);
  510. IO.mapRequired("platform", Keys->Platform);
  511. if (Ctx->FileType != FileType::TBD_V1)
  512. IO.mapOptional("flags", Keys->Flags, Flags::None);
  513. IO.mapRequired("install-name", Keys->InstallName);
  514. IO.mapOptional("current-version", Keys->CurrentVersion,
  515. PackedVersion(1, 0, 0));
  516. IO.mapOptional("compatibility-version", Keys->CompatibilityVersion,
  517. PackedVersion(1, 0, 0));
  518. if (Ctx->FileType != FileType::TBD_V3)
  519. IO.mapOptional("swift-version", Keys->SwiftVersion, SwiftVersion(0));
  520. else
  521. IO.mapOptional("swift-abi-version", Keys->SwiftVersion, SwiftVersion(0));
  522. IO.mapOptional("objc-constraint", Keys->ObjCConstraint,
  523. (Ctx->FileType == FileType::TBD_V1)
  524. ? ObjCConstraint::None
  525. : ObjCConstraint::Retain_Release);
  526. if (Ctx->FileType != FileType::TBD_V1)
  527. IO.mapOptional("parent-umbrella", Keys->ParentUmbrella, StringRef());
  528. IO.mapOptional("exports", Keys->Exports);
  529. if (Ctx->FileType != FileType::TBD_V1)
  530. IO.mapOptional("undefineds", Keys->Undefineds);
  531. }
  532. };
  533. template <>
  534. struct DocumentListTraits<std::vector<const MachO::InterfaceFile *>> {
  535. static size_t size(IO &IO, std::vector<const MachO::InterfaceFile *> &Seq) {
  536. return Seq.size();
  537. }
  538. static const InterfaceFile *&
  539. element(IO &IO, std::vector<const InterfaceFile *> &Seq, size_t Index) {
  540. if (Index >= Seq.size())
  541. Seq.resize(Index + 1);
  542. return Seq[Index];
  543. }
  544. };
  545. } // end namespace yaml.
  546. namespace MachO {
  547. static void DiagHandler(const SMDiagnostic &Diag, void *Context) {
  548. auto *File = static_cast<TextAPIContext *>(Context);
  549. SmallString<1024> Message;
  550. raw_svector_ostream S(Message);
  551. SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), File->Path,
  552. Diag.getLineNo(), Diag.getColumnNo(), Diag.getKind(),
  553. Diag.getMessage(), Diag.getLineContents(),
  554. Diag.getRanges(), Diag.getFixIts());
  555. NewDiag.print(nullptr, S);
  556. File->ErrorMessage = ("malformed file\n" + Message).str();
  557. }
  558. Expected<std::unique_ptr<InterfaceFile>>
  559. TextAPIReader::get(std::unique_ptr<MemoryBuffer> InputBuffer) {
  560. TextAPIContext Ctx;
  561. Ctx.Path = InputBuffer->getBufferIdentifier();
  562. yaml::Input YAMLIn(InputBuffer->getBuffer(), &Ctx, DiagHandler, &Ctx);
  563. // Fill vector with interface file objects created by parsing the YAML file.
  564. std::vector<const InterfaceFile *> Files;
  565. YAMLIn >> Files;
  566. if (YAMLIn.error())
  567. return make_error<StringError>(Ctx.ErrorMessage, YAMLIn.error());
  568. auto *File = const_cast<InterfaceFile *>(Files.front());
  569. return std::unique_ptr<InterfaceFile>(File);
  570. }
  571. Error TextAPIWriter::writeToStream(raw_ostream &OS, const InterfaceFile &File) {
  572. TextAPIContext Ctx;
  573. Ctx.Path = File.getPath();
  574. Ctx.FileType = File.getFileType();
  575. llvm::yaml::Output YAMLOut(OS, &Ctx, /*WrapColumn=*/80);
  576. std::vector<const InterfaceFile *> Files;
  577. Files.emplace_back(&File);
  578. // Stream out yaml.
  579. YAMLOut << Files;
  580. return Error::success();
  581. }
  582. } // end namespace MachO.
  583. } // end namespace llvm.