TextStubCommon.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //===- TextStubCommon.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. // Implememts common Text Stub YAML mappings.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "TextStubCommon.h"
  13. #include "TextAPIContext.h"
  14. using namespace llvm::MachO;
  15. namespace llvm {
  16. namespace yaml {
  17. void ScalarTraits<FlowStringRef>::output(const FlowStringRef &Value, void *Ctx,
  18. raw_ostream &OS) {
  19. ScalarTraits<StringRef>::output(Value, Ctx, OS);
  20. }
  21. StringRef ScalarTraits<FlowStringRef>::input(StringRef Value, void *Ctx,
  22. FlowStringRef &Out) {
  23. return ScalarTraits<StringRef>::input(Value, Ctx, Out.value);
  24. }
  25. QuotingType ScalarTraits<FlowStringRef>::mustQuote(StringRef Name) {
  26. return ScalarTraits<StringRef>::mustQuote(Name);
  27. }
  28. void ScalarEnumerationTraits<ObjCConstraintType>::enumeration(
  29. IO &IO, ObjCConstraintType &Constraint) {
  30. IO.enumCase(Constraint, "none", ObjCConstraintType::None);
  31. IO.enumCase(Constraint, "retain_release", ObjCConstraintType::Retain_Release);
  32. IO.enumCase(Constraint, "retain_release_for_simulator",
  33. ObjCConstraintType::Retain_Release_For_Simulator);
  34. IO.enumCase(Constraint, "retain_release_or_gc",
  35. ObjCConstraintType::Retain_Release_Or_GC);
  36. IO.enumCase(Constraint, "gc", ObjCConstraintType::GC);
  37. }
  38. void ScalarTraits<PlatformSet>::output(const PlatformSet &Values, void *IO,
  39. raw_ostream &OS) {
  40. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);
  41. assert((!Ctx || Ctx && Ctx->FileKind != FileType::Invalid) &&
  42. "File type is not set in context");
  43. if ( Ctx && Ctx->FileKind == TBD_V3 && Values.count(PlatformKind::macOS) &&
  44. Values.count(PlatformKind::macCatalyst) ) {
  45. OS << "zippered";
  46. return;
  47. }
  48. assert(Values.size() == 1U);
  49. switch (*Values.begin()) {
  50. default:
  51. llvm_unreachable("unexpected platform");
  52. break;
  53. case PlatformKind::macOS:
  54. OS << "macosx";
  55. break;
  56. case PlatformKind::iOS:
  57. OS << "ios";
  58. break;
  59. case PlatformKind::watchOS:
  60. OS << "watchos";
  61. break;
  62. case PlatformKind::tvOS:
  63. OS << "tvos";
  64. break;
  65. case PlatformKind::bridgeOS:
  66. OS << "bridgeos";
  67. break;
  68. }
  69. }
  70. StringRef ScalarTraits<PlatformSet>::input(StringRef Scalar, void *IO,
  71. PlatformSet &Values) {
  72. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);
  73. assert((!Ctx || Ctx && Ctx->FileKind != FileType::Invalid) &&
  74. "File type is not set in context");
  75. if (Scalar == "zippered") {
  76. if (Ctx && Ctx->FileKind == FileType::TBD_V3) {
  77. Values.insert(PlatformKind::macOS);
  78. Values.insert(PlatformKind::macCatalyst);
  79. return {};
  80. }
  81. return "invalid platform";
  82. }
  83. auto Platform = StringSwitch<PlatformKind>(Scalar)
  84. .Case("unknown", PlatformKind::unknown)
  85. .Case("macosx", PlatformKind::macOS)
  86. .Case("ios", PlatformKind::iOS)
  87. .Case("watchos", PlatformKind::watchOS)
  88. .Case("tvos", PlatformKind::tvOS)
  89. .Case("bridgeos", PlatformKind::bridgeOS)
  90. .Case("iosmac", PlatformKind::macCatalyst)
  91. .Default(PlatformKind::unknown);
  92. if (Platform == PlatformKind::macCatalyst)
  93. if (Ctx && Ctx->FileKind != FileType::TBD_V3)
  94. return "invalid platform";
  95. if (Platform == PlatformKind::unknown)
  96. return "unknown platform";
  97. Values.insert(Platform);
  98. return {};
  99. }
  100. QuotingType ScalarTraits<PlatformSet>::mustQuote(StringRef) {
  101. return QuotingType::None;
  102. }
  103. void ScalarBitSetTraits<ArchitectureSet>::bitset(IO &IO,
  104. ArchitectureSet &Archs) {
  105. #define ARCHINFO(arch, type, subtype) \
  106. IO.bitSetCase(Archs, #arch, 1U << static_cast<int>(AK_##arch));
  107. #include "llvm/TextAPI/MachO/Architecture.def"
  108. #undef ARCHINFO
  109. }
  110. void ScalarTraits<Architecture>::output(const Architecture &Value, void *,
  111. raw_ostream &OS) {
  112. OS << Value;
  113. }
  114. StringRef ScalarTraits<Architecture>::input(StringRef Scalar, void *,
  115. Architecture &Value) {
  116. Value = getArchitectureFromName(Scalar);
  117. return {};
  118. }
  119. QuotingType ScalarTraits<Architecture>::mustQuote(StringRef) {
  120. return QuotingType::None;
  121. }
  122. void ScalarTraits<PackedVersion>::output(const PackedVersion &Value, void *,
  123. raw_ostream &OS) {
  124. OS << Value;
  125. }
  126. StringRef ScalarTraits<PackedVersion>::input(StringRef Scalar, void *,
  127. PackedVersion &Value) {
  128. if (!Value.parse32(Scalar))
  129. return "invalid packed version string.";
  130. return {};
  131. }
  132. QuotingType ScalarTraits<PackedVersion>::mustQuote(StringRef) {
  133. return QuotingType::None;
  134. }
  135. void ScalarTraits<SwiftVersion>::output(const SwiftVersion &Value, void *,
  136. raw_ostream &OS) {
  137. switch (Value) {
  138. case 1:
  139. OS << "1.0";
  140. break;
  141. case 2:
  142. OS << "1.1";
  143. break;
  144. case 3:
  145. OS << "2.0";
  146. break;
  147. case 4:
  148. OS << "3.0";
  149. break;
  150. default:
  151. OS << (unsigned)Value;
  152. break;
  153. }
  154. }
  155. StringRef ScalarTraits<SwiftVersion>::input(StringRef Scalar, void *,
  156. SwiftVersion &Value) {
  157. Value = StringSwitch<SwiftVersion>(Scalar)
  158. .Case("1.0", 1)
  159. .Case("1.1", 2)
  160. .Case("2.0", 3)
  161. .Case("3.0", 4)
  162. .Default(0);
  163. if (Value != SwiftVersion(0))
  164. return {};
  165. if (Scalar.getAsInteger(10, Value))
  166. return "invalid Swift ABI version.";
  167. return StringRef();
  168. }
  169. QuotingType ScalarTraits<SwiftVersion>::mustQuote(StringRef) {
  170. return QuotingType::None;
  171. }
  172. void ScalarTraits<UUID>::output(const UUID &Value, void *, raw_ostream &OS) {
  173. OS << Value.first << ": " << Value.second;
  174. }
  175. StringRef ScalarTraits<UUID>::input(StringRef Scalar, void *, UUID &Value) {
  176. auto Split = Scalar.split(':');
  177. auto Arch = Split.first.trim();
  178. auto UUID = Split.second.trim();
  179. if (UUID.empty())
  180. return "invalid uuid string pair";
  181. Value.second = UUID;
  182. Value.first = Target{getArchitectureFromName(Arch), PlatformKind::unknown};
  183. return {};
  184. }
  185. QuotingType ScalarTraits<UUID>::mustQuote(StringRef) {
  186. return QuotingType::Single;
  187. }
  188. } // end namespace yaml.
  189. } // end namespace llvm.