Path: blob/main/contrib/llvm-project/llvm/lib/TextAPI/TextStubCommon.cpp
35262 views
//===- TextStubCommon.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implements common Text Stub YAML mappings.9//10//===----------------------------------------------------------------------===//1112#include "TextStubCommon.h"13#include "TextAPIContext.h"14#include "llvm/ADT/StringSwitch.h"1516using namespace llvm::MachO;1718namespace llvm {19namespace yaml {2021void ScalarTraits<FlowStringRef>::output(const FlowStringRef &Value, void *Ctx,22raw_ostream &OS) {23ScalarTraits<StringRef>::output(Value, Ctx, OS);24}25StringRef ScalarTraits<FlowStringRef>::input(StringRef Value, void *Ctx,26FlowStringRef &Out) {27return ScalarTraits<StringRef>::input(Value, Ctx, Out.value);28}29QuotingType ScalarTraits<FlowStringRef>::mustQuote(StringRef Name) {30return ScalarTraits<StringRef>::mustQuote(Name);31}3233void ScalarEnumerationTraits<ObjCConstraintType>::enumeration(34IO &IO, ObjCConstraintType &Constraint) {35IO.enumCase(Constraint, "none", ObjCConstraintType::None);36IO.enumCase(Constraint, "retain_release", ObjCConstraintType::Retain_Release);37IO.enumCase(Constraint, "retain_release_for_simulator",38ObjCConstraintType::Retain_Release_For_Simulator);39IO.enumCase(Constraint, "retain_release_or_gc",40ObjCConstraintType::Retain_Release_Or_GC);41IO.enumCase(Constraint, "gc", ObjCConstraintType::GC);42}4344void ScalarTraits<PlatformSet>::output(const PlatformSet &Values, void *IO,45raw_ostream &OS) {4647const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);48assert((!Ctx || Ctx->FileKind != FileType::Invalid) &&49"File type is not set in context");5051if (Ctx && Ctx->FileKind == TBD_V3 && Values.count(PLATFORM_MACOS) &&52Values.count(PLATFORM_MACCATALYST)) {53OS << "zippered";54return;55}5657assert(Values.size() == 1U);58switch (*Values.begin()) {59default:60llvm_unreachable("unexpected platform");61break;62case PLATFORM_MACOS:63OS << "macosx";64break;65case PLATFORM_IOSSIMULATOR:66[[fallthrough]];67case PLATFORM_IOS:68OS << "ios";69break;70case PLATFORM_WATCHOSSIMULATOR:71[[fallthrough]];72case PLATFORM_WATCHOS:73OS << "watchos";74break;75case PLATFORM_TVOSSIMULATOR:76[[fallthrough]];77case PLATFORM_TVOS:78OS << "tvos";79break;80case PLATFORM_BRIDGEOS:81OS << "bridgeos";82break;83case PLATFORM_MACCATALYST:84OS << "maccatalyst";85break;86case PLATFORM_DRIVERKIT:87OS << "driverkit";88break;89}90}9192StringRef ScalarTraits<PlatformSet>::input(StringRef Scalar, void *IO,93PlatformSet &Values) {94const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);95assert((!Ctx || Ctx->FileKind != FileType::Invalid) &&96"File type is not set in context");9798if (Scalar == "zippered") {99if (Ctx && Ctx->FileKind == FileType::TBD_V3) {100Values.insert(PLATFORM_MACOS);101Values.insert(PLATFORM_MACCATALYST);102return {};103}104return "invalid platform";105}106107auto Platform = StringSwitch<PlatformType>(Scalar)108.Case("macosx", PLATFORM_MACOS)109.Case("ios", PLATFORM_IOS)110.Case("watchos", PLATFORM_WATCHOS)111.Case("tvos", PLATFORM_TVOS)112.Case("bridgeos", PLATFORM_BRIDGEOS)113.Case("iosmac", PLATFORM_MACCATALYST)114.Case("maccatalyst", PLATFORM_MACCATALYST)115.Case("driverkit", PLATFORM_DRIVERKIT)116.Default(PLATFORM_UNKNOWN);117118if (Platform == PLATFORM_MACCATALYST)119if (Ctx && Ctx->FileKind != FileType::TBD_V3)120return "invalid platform";121122if (Platform == PLATFORM_UNKNOWN)123return "unknown platform";124125Values.insert(Platform);126return {};127}128129QuotingType ScalarTraits<PlatformSet>::mustQuote(StringRef) {130return QuotingType::None;131}132133void ScalarBitSetTraits<ArchitectureSet>::bitset(IO &IO,134ArchitectureSet &Archs) {135#define ARCHINFO(arch, type, subtype, numbits) \136IO.bitSetCase(Archs, #arch, 1U << static_cast<int>(AK_##arch));137#include "llvm/TextAPI/Architecture.def"138#undef ARCHINFO139}140141void ScalarTraits<Architecture>::output(const Architecture &Value, void *,142raw_ostream &OS) {143OS << Value;144}145StringRef ScalarTraits<Architecture>::input(StringRef Scalar, void *,146Architecture &Value) {147Value = getArchitectureFromName(Scalar);148return {};149}150QuotingType ScalarTraits<Architecture>::mustQuote(StringRef) {151return QuotingType::None;152}153154void ScalarTraits<PackedVersion>::output(const PackedVersion &Value, void *,155raw_ostream &OS) {156OS << Value;157}158StringRef ScalarTraits<PackedVersion>::input(StringRef Scalar, void *,159PackedVersion &Value) {160if (!Value.parse32(Scalar))161return "invalid packed version string.";162return {};163}164QuotingType ScalarTraits<PackedVersion>::mustQuote(StringRef) {165return QuotingType::None;166}167168void ScalarTraits<SwiftVersion>::output(const SwiftVersion &Value, void *,169raw_ostream &OS) {170switch (Value) {171case 1:172OS << "1.0";173break;174case 2:175OS << "1.1";176break;177case 3:178OS << "2.0";179break;180case 4:181OS << "3.0";182break;183default:184OS << (unsigned)Value;185break;186}187}188StringRef ScalarTraits<SwiftVersion>::input(StringRef Scalar, void *IO,189SwiftVersion &Value) {190const auto *Ctx = reinterpret_cast<TextAPIContext *>(IO);191assert((!Ctx || Ctx->FileKind != FileType::Invalid) &&192"File type is not set in context");193194if (Ctx->FileKind == FileType::TBD_V4) {195if (Scalar.getAsInteger(10, Value))196return "invalid Swift ABI version.";197return {};198} else {199Value = StringSwitch<SwiftVersion>(Scalar)200.Case("1.0", 1)201.Case("1.1", 2)202.Case("2.0", 3)203.Case("3.0", 4)204.Default(0);205}206207if (Value != SwiftVersion(0))208return {};209210if (Scalar.getAsInteger(10, Value))211return "invalid Swift ABI version.";212213return StringRef();214}215QuotingType ScalarTraits<SwiftVersion>::mustQuote(StringRef) {216return QuotingType::None;217}218219void ScalarTraits<UUID>::output(const UUID &Value, void *, raw_ostream &OS) {}220221StringRef ScalarTraits<UUID>::input(StringRef Scalar, void *, UUID &Value) {222Value = {};223return {};224}225226QuotingType ScalarTraits<UUID>::mustQuote(StringRef) {227return QuotingType::Single;228}229230} // end namespace yaml.231} // end namespace llvm.232233234