TextStubCommon.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. [[fallthrough]];
  59. case PLATFORM_IOS:
  60. OS << "ios";
  61. break;
  62. case PLATFORM_WATCHOSSIMULATOR:
  63. [[fallthrough]];
  64. case PLATFORM_WATCHOS:
  65. OS << "watchos";
  66. break;
  67. case PLATFORM_TVOSSIMULATOR:
  68. [[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. .Case("driverkit", PLATFORM_DRIVERKIT)
  104. .Default(PLATFORM_UNKNOWN);
  105. if (Platform == PLATFORM_MACCATALYST)
  106. if (Ctx && Ctx->FileKind != FileType::TBD_V3)
  107. return "invalid platform";
  108. if (Platform == PLATFORM_UNKNOWN)
  109. return "unknown platform";
  110. Values.insert(Platform);
  111. return {};
  112. }
  113. QuotingType ScalarTraits<PlatformSet>::mustQuote(StringRef) {
  114. return QuotingType::None;
  115. }
  116. void ScalarBitSetTraits<ArchitectureSet>::bitset(IO &IO,
  117. ArchitectureSet &Archs) {
  118. #define ARCHINFO(arch, type, subtype, numbits) \
  119. IO.bitSetCase(Archs, #arch, 1U << static_cast<int>(AK_##arch));
  120. #include "llvm/TextAPI/Architecture.def"
  121. #undef ARCHINFO
  122. }
  123. void ScalarTraits<Architecture>::output(const Architecture &Value, void *,
  124. raw_ostream &OS) {
  125. OS << Value;
  126. }
  127. StringRef ScalarTraits<Architecture>::input(StringRef Scalar, void *,
  128. Architecture &Value) {
  129. Value = getArchitectureFromName(Scalar);
  130. return {};
  131. }
  132. QuotingType ScalarTraits<Architecture>::mustQuote(StringRef) {
  133. return QuotingType::None;
  134. }
  135. void ScalarTraits<PackedVersion>::output(const PackedVersion &Value, void *,
  136. raw_ostream &OS) {
  137. OS << Value;
  138. }
  139. StringRef ScalarTraits<PackedVersion>::input(StringRef Scalar, void *,
  140. PackedVersion &Value) {
  141. if (!Value.parse32(Scalar))
  142. return "invalid packed version string.";
  143. return {};
  144. }
  145. QuotingType ScalarTraits<PackedVersion>::mustQuote(StringRef) {
  146. return QuotingType::None;
  147. }
  148. void ScalarTraits<SwiftVersion>::output(const SwiftVersion &Value, void *,
  149. raw_ostream &OS) {
  150. switch (Value) {
  151. case 1:
  152. OS << "1.0";
  153. break;
  154. case 2:
  155. OS << "1.1";
  156. break;
  157. case 3:
  158. OS << "2.0";
  159. break;
  160. case 4:
  161. OS << "3.0";
  162. break;
  163. default:
  164. OS << (unsigned)Value;
  165. break;
  166. }
  167. }
  168. StringRef ScalarTraits<SwiftVersion>::input(StringRef Scalar, void *IO,
  169. SwiftVersion &Value) {
  170. const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);
  171. assert((!Ctx || Ctx->FileKind != FileType::Invalid) &&
  172. "File type is not set in context");
  173. if (Ctx->FileKind == FileType::TBD_V4) {
  174. if (Scalar.getAsInteger(10, Value))
  175. return "invalid Swift ABI version.";
  176. return {};
  177. } else {
  178. Value = StringSwitch<SwiftVersion>(Scalar)
  179. .Case("1.0", 1)
  180. .Case("1.1", 2)
  181. .Case("2.0", 3)
  182. .Case("3.0", 4)
  183. .Default(0);
  184. }
  185. if (Value != SwiftVersion(0))
  186. return {};
  187. if (Scalar.getAsInteger(10, Value))
  188. return "invalid Swift ABI version.";
  189. return StringRef();
  190. }
  191. QuotingType ScalarTraits<SwiftVersion>::mustQuote(StringRef) {
  192. return QuotingType::None;
  193. }
  194. void ScalarTraits<UUID>::output(const UUID &Value, void *, raw_ostream &OS) {
  195. OS << Value.first << ": " << Value.second;
  196. }
  197. StringRef ScalarTraits<UUID>::input(StringRef Scalar, void *, UUID &Value) {
  198. auto Split = Scalar.split(':');
  199. auto Arch = Split.first.trim();
  200. auto UUID = Split.second.trim();
  201. if (UUID.empty())
  202. return "invalid uuid string pair";
  203. Value.second = std::string(UUID);
  204. Value.first = Target{getArchitectureFromName(Arch), PLATFORM_UNKNOWN};
  205. return {};
  206. }
  207. QuotingType ScalarTraits<UUID>::mustQuote(StringRef) {
  208. return QuotingType::Single;
  209. }
  210. } // end namespace yaml.
  211. } // end namespace llvm.