TextStubCommon.cpp 7.1 KB

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