1 //===- TextStubCommon.cpp -------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implememts common Text Stub YAML mappings.
11 //===----------------------------------------------------------------------===//
13 #include "TextStubCommon.h"
14 #include "TextAPIContext.h"
15 #include "llvm/ADT/StringSwitch.h"
17 using namespace llvm::MachO
;
22 void ScalarTraits
<FlowStringRef
>::output(const FlowStringRef
&Value
, void *Ctx
,
24 ScalarTraits
<StringRef
>::output(Value
, Ctx
, OS
);
26 StringRef ScalarTraits
<FlowStringRef
>::input(StringRef Value
, void *Ctx
,
28 return ScalarTraits
<StringRef
>::input(Value
, Ctx
, Out
.value
);
30 QuotingType ScalarTraits
<FlowStringRef
>::mustQuote(StringRef Name
) {
31 return ScalarTraits
<StringRef
>::mustQuote(Name
);
34 void ScalarEnumerationTraits
<ObjCConstraintType
>::enumeration(
35 IO
&IO
, ObjCConstraintType
&Constraint
) {
36 IO
.enumCase(Constraint
, "none", ObjCConstraintType::None
);
37 IO
.enumCase(Constraint
, "retain_release", ObjCConstraintType::Retain_Release
);
38 IO
.enumCase(Constraint
, "retain_release_for_simulator",
39 ObjCConstraintType::Retain_Release_For_Simulator
);
40 IO
.enumCase(Constraint
, "retain_release_or_gc",
41 ObjCConstraintType::Retain_Release_Or_GC
);
42 IO
.enumCase(Constraint
, "gc", ObjCConstraintType::GC
);
45 void ScalarTraits
<PlatformSet
>::output(const PlatformSet
&Values
, void *IO
,
48 const auto *Ctx
= reinterpret_cast<TextAPIContext
*>(IO
);
49 assert((!Ctx
|| Ctx
->FileKind
!= FileType::Invalid
) &&
50 "File type is not set in context");
52 if (Ctx
&& Ctx
->FileKind
== TBD_V3
&& Values
.count(PlatformKind::macOS
) &&
53 Values
.count(PlatformKind::macCatalyst
)) {
58 assert(Values
.size() == 1U);
59 switch (*Values
.begin()) {
61 llvm_unreachable("unexpected platform");
63 case PlatformKind::macOS
:
66 case PlatformKind::iOSSimulator
:
68 case PlatformKind::iOS
:
71 case PlatformKind::watchOSSimulator
:
73 case PlatformKind::watchOS
:
76 case PlatformKind::tvOSSimulator
:
78 case PlatformKind::tvOS
:
81 case PlatformKind::bridgeOS
:
84 case PlatformKind::macCatalyst
:
87 case PlatformKind::driverKit
:
93 StringRef ScalarTraits
<PlatformSet
>::input(StringRef Scalar
, void *IO
,
94 PlatformSet
&Values
) {
95 const auto *Ctx
= reinterpret_cast<TextAPIContext
*>(IO
);
96 assert((!Ctx
|| Ctx
->FileKind
!= FileType::Invalid
) &&
97 "File type is not set in context");
99 if (Scalar
== "zippered") {
100 if (Ctx
&& Ctx
->FileKind
== FileType::TBD_V3
) {
101 Values
.insert(PlatformKind::macOS
);
102 Values
.insert(PlatformKind::macCatalyst
);
105 return "invalid platform";
108 auto Platform
= StringSwitch
<PlatformKind
>(Scalar
)
109 .Case("unknown", PlatformKind::unknown
)
110 .Case("macosx", PlatformKind::macOS
)
111 .Case("ios", PlatformKind::iOS
)
112 .Case("watchos", PlatformKind::watchOS
)
113 .Case("tvos", PlatformKind::tvOS
)
114 .Case("bridgeos", PlatformKind::bridgeOS
)
115 .Case("iosmac", PlatformKind::macCatalyst
)
116 .Default(PlatformKind::unknown
);
118 if (Platform
== PlatformKind::macCatalyst
)
119 if (Ctx
&& Ctx
->FileKind
!= FileType::TBD_V3
)
120 return "invalid platform";
122 if (Platform
== PlatformKind::unknown
)
123 return "unknown platform";
125 Values
.insert(Platform
);
129 QuotingType ScalarTraits
<PlatformSet
>::mustQuote(StringRef
) {
130 return QuotingType::None
;
133 void ScalarBitSetTraits
<ArchitectureSet
>::bitset(IO
&IO
,
134 ArchitectureSet
&Archs
) {
135 #define ARCHINFO(arch, type, subtype, numbits) \
136 IO.bitSetCase(Archs, #arch, 1U << static_cast<int>(AK_##arch));
137 #include "llvm/TextAPI/Architecture.def"
141 void ScalarTraits
<Architecture
>::output(const Architecture
&Value
, void *,
145 StringRef ScalarTraits
<Architecture
>::input(StringRef Scalar
, void *,
146 Architecture
&Value
) {
147 Value
= getArchitectureFromName(Scalar
);
150 QuotingType ScalarTraits
<Architecture
>::mustQuote(StringRef
) {
151 return QuotingType::None
;
154 void ScalarTraits
<PackedVersion
>::output(const PackedVersion
&Value
, void *,
158 StringRef ScalarTraits
<PackedVersion
>::input(StringRef Scalar
, void *,
159 PackedVersion
&Value
) {
160 if (!Value
.parse32(Scalar
))
161 return "invalid packed version string.";
164 QuotingType ScalarTraits
<PackedVersion
>::mustQuote(StringRef
) {
165 return QuotingType::None
;
168 void ScalarTraits
<SwiftVersion
>::output(const SwiftVersion
&Value
, void *,
184 OS
<< (unsigned)Value
;
188 StringRef ScalarTraits
<SwiftVersion
>::input(StringRef Scalar
, void *IO
,
189 SwiftVersion
&Value
) {
190 const auto *Ctx
= reinterpret_cast<TextAPIContext
*>(IO
);
191 assert((!Ctx
|| Ctx
->FileKind
!= FileType::Invalid
) &&
192 "File type is not set in context");
194 if (Ctx
->FileKind
== FileType::TBD_V4
) {
195 if (Scalar
.getAsInteger(10, Value
))
196 return "invalid Swift ABI version.";
199 Value
= StringSwitch
<SwiftVersion
>(Scalar
)
207 if (Value
!= SwiftVersion(0))
210 if (Scalar
.getAsInteger(10, Value
))
211 return "invalid Swift ABI version.";
215 QuotingType ScalarTraits
<SwiftVersion
>::mustQuote(StringRef
) {
216 return QuotingType::None
;
219 void ScalarTraits
<UUID
>::output(const UUID
&Value
, void *, raw_ostream
&OS
) {
220 OS
<< Value
.first
<< ": " << Value
.second
;
222 StringRef ScalarTraits
<UUID
>::input(StringRef Scalar
, void *, UUID
&Value
) {
223 auto Split
= Scalar
.split(':');
224 auto Arch
= Split
.first
.trim();
225 auto UUID
= Split
.second
.trim();
227 return "invalid uuid string pair";
228 Value
.second
= std::string(UUID
);
229 Value
.first
= Target
{getArchitectureFromName(Arch
), PlatformKind::unknown
};
233 QuotingType ScalarTraits
<UUID
>::mustQuote(StringRef
) {
234 return QuotingType::Single
;
237 } // end namespace yaml.
238 } // end namespace llvm.