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