1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
//===--- AvailabilityMixin.cpp - Symbol Graph Symbol Availability ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "AvailabilityMixin.h"
#include "JSON.h"
using namespace swift;
using namespace symbolgraphgen;
namespace {
StringRef getDomain(const AvailableAttr &AvAttr) {
switch (AvAttr.getPlatformAgnosticAvailability()) {
// SPM- and Swift-specific availability.
case PlatformAgnosticAvailabilityKind::PackageDescriptionVersionSpecific:
return { "SwiftPM" };
case PlatformAgnosticAvailabilityKind::SwiftVersionSpecific:
case PlatformAgnosticAvailabilityKind::UnavailableInSwift:
return { "Swift" };
// Although these are in the agnostic kinds, they are actually a signal
// that there is either platform-specific or completely platform-agnostic.
// They'll be handled below.
case PlatformAgnosticAvailabilityKind::Deprecated:
case PlatformAgnosticAvailabilityKind::Unavailable:
case PlatformAgnosticAvailabilityKind::None:
case PlatformAgnosticAvailabilityKind::NoAsync:
break;
}
// Platform-specific availability.
switch (AvAttr.Platform) {
case swift::PlatformKind::iOS:
return { "iOS" };
case swift::PlatformKind::macCatalyst:
return { "macCatalyst" };
case swift::PlatformKind::macOS:
return { "macOS" };
case swift::PlatformKind::tvOS:
return { "tvOS" };
case swift::PlatformKind::watchOS:
return { "watchOS" };
case swift::PlatformKind::visionOS:
return { "visionOS" };
case swift::PlatformKind::iOSApplicationExtension:
return { "iOSAppExtension" };
case swift::PlatformKind::macCatalystApplicationExtension:
return { "macCatalystAppExtension" };
case swift::PlatformKind::macOSApplicationExtension:
return { "macOSAppExtension" };
case swift::PlatformKind::tvOSApplicationExtension:
return { "tvOSAppExtension" };
case swift::PlatformKind::watchOSApplicationExtension:
return { "watchOSAppExtension" };
case swift::PlatformKind::visionOSApplicationExtension:
return { "visionOSAppExtension" };
case swift::PlatformKind::OpenBSD:
return { "OpenBSD" };
case swift::PlatformKind::Windows:
return { "Windows" };
case swift::PlatformKind::none:
return { "*" };
}
llvm_unreachable("invalid platform kind");
}
} // end anonymous namespace
Availability::Availability(const AvailableAttr &AvAttr)
: Domain(getDomain(AvAttr)),
Introduced(AvAttr.Introduced),
Deprecated(AvAttr.Deprecated),
Obsoleted(AvAttr.Obsoleted),
Message(AvAttr.Message),
Renamed(AvAttr.Rename),
IsUnconditionallyDeprecated(AvAttr.isUnconditionallyDeprecated()),
IsUnconditionallyUnavailable(AvAttr.isUnconditionallyUnavailable()) {
assert(!Domain.empty());
}
void
Availability::updateFromDuplicate(const Availability &Other) {
assert(Domain == Other.Domain);
// The highest `introduced` version always wins
// regardless of the order in which they appeared in the source.
if (!Introduced) {
Introduced = Other.Introduced;
} else if (Other.Introduced && *Other.Introduced > *Introduced) {
Introduced = Other.Introduced;
}
// The `deprecated` version that appears last in the source always wins,
// allowing even to erase a previous deprecated.
Deprecated = Other.Deprecated;
// Same for `deprecated` with no version.
IsUnconditionallyDeprecated = Other.IsUnconditionallyDeprecated;
// Same for `unavailable` with no version.
IsUnconditionallyUnavailable = Other.IsUnconditionallyUnavailable;
// Same for `obsoleted`.
Obsoleted = Other.Obsoleted;
// The `message` that appears last in the source always wins.
Message = Other.Message;
// Same for `renamed`.
Renamed = Other.Renamed;
}
void
Availability::updateFromParent(const Availability &Parent) {
assert(Domain == Parent.Domain);
// Allow filling, but never replace a child's existing `introduced`
// availability because it can never be less available than the parent anyway.
//
// e.g. you cannot write this:
// @available(macos, introduced: 10.15)
// struct S {
// @available(macos, introduced: 10.14)
// func foo() {}
// }
//
// So the child's `introduced` availability will always
// be >= the parent's.
if (!Introduced) {
Introduced = Parent.Introduced;
}
// Allow filling from the parent.
// For replacement, we will consider a parent's
// earlier deprecation to supersede a child's later deprecation.
if (!Deprecated) {
Deprecated = Parent.Deprecated;
} else if (Parent.Deprecated && *Parent.Deprecated < *Deprecated) {
Deprecated = Parent.Deprecated;
}
// The above regarding `deprecated` also will apply to `obsoleted`.
if (!Obsoleted) {
Obsoleted = Parent.Obsoleted;
} else if (Parent.Obsoleted && *Parent.Obsoleted < *Obsoleted) {
Obsoleted = Parent.Obsoleted;
}
// Never replace or fill a child's `message` with a parent's because
// there may be context at the parent that doesn't apply at the child,
// i.e. it might not always make sense.
// Never replace or fill a child's `renamed` field because it
// doesn't make sense. Just because a parent is renamed doesn't
// mean its child is renamed to the same thing.
// If a parent is unconditionally deprecated, then so are all
// of its children.
IsUnconditionallyDeprecated |= Parent.IsUnconditionallyDeprecated;
// If a parent is unconditionally unavailable, then so are all
// of its children.
IsUnconditionallyUnavailable |= Parent.IsUnconditionallyUnavailable;
}
void Availability::serialize(llvm::json::OStream &OS) const {
OS.object([&](){
OS.attribute("domain", Domain);
if (Introduced) {
AttributeRAII IntroducedAttribute("introduced", OS);
symbolgraphgen::serialize(*Introduced, OS);
}
if (Deprecated) {
AttributeRAII DeprecatedAttribute("deprecated", OS);
symbolgraphgen::serialize(*Deprecated, OS);
}
if (Obsoleted) {
AttributeRAII ObsoletedAttribute("obsoleted", OS);
symbolgraphgen::serialize(*Obsoleted, OS);
}
if (!Message.empty()) {
OS.attribute("message", Message);
}
if (!Renamed.empty()) {
OS.attribute("renamed", Renamed);
}
if (IsUnconditionallyDeprecated) {
OS.attribute("isUnconditionallyDeprecated", true);
}
if (IsUnconditionallyUnavailable) {
OS.attribute("isUnconditionallyUnavailable", true);
}
}); // end availability object
}
bool Availability::empty() const {
return !Introduced.has_value() &&
!Deprecated.has_value() &&
!Obsoleted.has_value() &&
!IsUnconditionallyDeprecated &&
!IsUnconditionallyUnavailable;
}
|