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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
//===--- AvailabilityContext.cpp - Swift Availability Structures ----------===//
//
// 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 "swift/AST/AvailabilityContext.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/AvailabilityContextStorage.h"
#include "swift/AST/Decl.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
// Defined as a macro because you can't take the reference of a bitfield.
#define CONSTRAIN_BOOL(_old, _new) \
[&]() { \
if (_old || !_new) \
return false; \
_old = true; \
return true; \
}()
static bool constrainRange(AvailabilityRange &existing,
const AvailabilityRange &other) {
if (!other.isContainedIn(existing))
return false;
existing = other;
return true;
}
bool AvailabilityContext::PlatformInfo::constrainWith(
const PlatformInfo &other) {
bool isConstrained = false;
isConstrained |= constrainRange(Range, other.Range);
if (other.IsUnavailable) {
isConstrained |= constrainUnavailability(other.UnavailablePlatform);
isConstrained |=
CONSTRAIN_BOOL(IsUnavailableInEmbedded, other.IsUnavailableInEmbedded);
}
isConstrained |= CONSTRAIN_BOOL(IsDeprecated, other.IsDeprecated);
return isConstrained;
}
bool AvailabilityContext::PlatformInfo::constrainWith(const Decl *decl) {
bool isConstrained = false;
auto &ctx = decl->getASTContext();
if (auto range = AvailabilityInference::annotatedAvailableRange(decl))
isConstrained |= constrainRange(Range, *range);
if (auto *attr = decl->getAttrs().getUnavailable(ctx)) {
isConstrained |= constrainUnavailability(attr->Platform);
isConstrained |=
CONSTRAIN_BOOL(IsUnavailableInEmbedded, attr->isForEmbedded());
}
isConstrained |=
CONSTRAIN_BOOL(IsDeprecated, decl->getAttrs().isDeprecated(ctx));
return isConstrained;
}
bool AvailabilityContext::PlatformInfo::constrainUnavailability(
std::optional<PlatformKind> unavailablePlatform) {
if (!unavailablePlatform)
return false;
if (IsUnavailable) {
// Universal unavailability cannot be refined.
if (UnavailablePlatform == PlatformKind::none)
return false;
// There's nothing to do if the platforms already match.
if (UnavailablePlatform == *unavailablePlatform)
return false;
// The new platform must be more restrictive.
if (*unavailablePlatform != PlatformKind::none &&
inheritsAvailabilityFromPlatform(*unavailablePlatform,
UnavailablePlatform))
return false;
}
IsUnavailable = true;
UnavailablePlatform = *unavailablePlatform;
return true;
}
bool AvailabilityContext::PlatformInfo::isContainedIn(
const PlatformInfo &other) const {
if (!Range.isContainedIn(other.Range))
return false;
if (!IsUnavailable && other.IsUnavailable)
return false;
if (IsUnavailable && other.IsUnavailable) {
if (UnavailablePlatform != other.UnavailablePlatform &&
UnavailablePlatform != PlatformKind::none &&
inheritsAvailabilityFromPlatform(UnavailablePlatform,
other.UnavailablePlatform))
return false;
if (IsUnavailableInEmbedded && !other.IsUnavailableInEmbedded)
return false;
}
if (!IsDeprecated && other.IsDeprecated)
return false;
return true;
}
void AvailabilityContext::Storage::Profile(llvm::FoldingSetNodeID &id) const {
Platform.Profile(id);
}
AvailabilityContext AvailabilityContext::getDefault(ASTContext &ctx) {
PlatformInfo platformInfo{AvailabilityRange::forInliningTarget(ctx),
PlatformKind::none,
/*IsUnavailable*/ false,
/*IsUnavailableInEmbedded*/ false,
/*IsDeprecated*/ false};
return AvailabilityContext(Storage::get(platformInfo, ctx));
}
AvailabilityContext
AvailabilityContext::get(const AvailabilityRange &platformAvailability,
std::optional<PlatformKind> unavailablePlatform,
bool deprecated, ASTContext &ctx) {
PlatformInfo platformInfo{platformAvailability,
unavailablePlatform.has_value()
? *unavailablePlatform
: PlatformKind::none,
unavailablePlatform.has_value(),
/*IsUnavailableInEmbedded*/ false, deprecated};
return AvailabilityContext(Storage::get(platformInfo, ctx));
}
AvailabilityRange AvailabilityContext::getPlatformRange() const {
return Info->Platform.Range;
}
std::optional<PlatformKind>
AvailabilityContext::getUnavailablePlatformKind() const {
if (Info->Platform.IsUnavailable)
return Info->Platform.UnavailablePlatform;
return std::nullopt;
}
bool AvailabilityContext::isUnavailableInEmbedded() const {
return Info->Platform.IsUnavailableInEmbedded;
}
bool AvailabilityContext::isDeprecated() const {
return Info->Platform.IsDeprecated;
}
void AvailabilityContext::constrainWithContext(const AvailabilityContext &other,
ASTContext &ctx) {
PlatformInfo platformAvailability{Info->Platform};
if (platformAvailability.constrainWith(other.Info->Platform)) {
Info = Storage::get(platformAvailability, ctx);
}
}
void AvailabilityContext::constrainWithDecl(const Decl *decl) {
constrainWithDeclAndPlatformRange(decl, AvailabilityRange::alwaysAvailable());
}
void AvailabilityContext::constrainWithPlatformRange(
const AvailabilityRange &platformRange, ASTContext &ctx) {
PlatformInfo platformAvailability{Info->Platform};
if (!constrainRange(platformAvailability.Range, platformRange))
return;
Info = Storage::get(platformAvailability, ctx);
}
void AvailabilityContext::constrainWithDeclAndPlatformRange(
const Decl *decl, const AvailabilityRange &platformRange) {
PlatformInfo platformAvailability{Info->Platform};
bool isConstrained = false;
isConstrained |= platformAvailability.constrainWith(decl);
isConstrained |= constrainRange(platformAvailability.Range, platformRange);
if (!isConstrained)
return;
Info = Storage::get(platformAvailability, decl->getASTContext());
}
bool AvailabilityContext::isContainedIn(const AvailabilityContext other) const {
if (!Info->Platform.isContainedIn(other.Info->Platform))
return false;
return true;
}
static std::string
stringForAvailability(const AvailabilityRange &availability) {
if (availability.isAlwaysAvailable())
return "all";
if (availability.isKnownUnreachable())
return "none";
return availability.getVersionString();
}
void AvailabilityContext::print(llvm::raw_ostream &os) const {
os << "version=" << stringForAvailability(getPlatformRange());
if (auto unavailablePlatform = getUnavailablePlatformKind())
os << " unavailable=" << platformString(*unavailablePlatform);
if (isDeprecated())
os << " deprecated";
}
void AvailabilityContext::dump() const { print(llvm::errs()); }
|