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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
//===--- AvailabilityScope.cpp - Swift Availability Scopes ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements the AvailabilityScope class.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/AvailabilityScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/SourceManager.h"
using namespace swift;
AvailabilityScope::AvailabilityScope(ASTContext &Ctx, IntroNode Node,
AvailabilityScope *Parent,
SourceRange SrcRange,
const AvailabilityContext Info)
: Node(Node), SrcRange(SrcRange), AvailabilityInfo(Info) {
if (Parent) {
assert(SrcRange.isValid());
Parent->addChild(this, Ctx);
assert(Info.isContainedIn(Parent->getAvailabilityContext()));
}
Ctx.addDestructorCleanup(Children);
}
AvailabilityScope *
AvailabilityScope::createForSourceFile(SourceFile *SF,
const AvailabilityContext Info) {
assert(SF);
ASTContext &Ctx = SF->getASTContext();
SourceRange range;
AvailabilityScope *parentContext = nullptr;
switch (SF->Kind) {
case SourceFileKind::MacroExpansion:
case SourceFileKind::DefaultArgument: {
// Look up the parent context in the enclosing file that this file's
// root context should be nested under.
auto enclosingSF = SF->getEnclosingSourceFile();
if (!enclosingSF)
break;
if (auto parentScope = enclosingSF->getAvailabilityScope()) {
auto charRange = Ctx.SourceMgr.getRangeForBuffer(SF->getBufferID());
range = SourceRange(charRange.getStart(), charRange.getEnd());
auto originalNode = SF->getNodeInEnclosingSourceFile();
parentContext = parentScope->findMostRefinedSubContext(
originalNode.getStartLoc(), Ctx);
}
break;
}
case SourceFileKind::Library:
case SourceFileKind::Main:
case SourceFileKind::Interface:
break;
case SourceFileKind::SIL:
llvm_unreachable("unexpected SourceFileKind");
}
return new (Ctx) AvailabilityScope(
Ctx, SF, parentContext, range,
parentContext ? parentContext->getAvailabilityContext() : Info);
}
AvailabilityScope *AvailabilityScope::createForDecl(
ASTContext &Ctx, Decl *D, AvailabilityScope *Parent,
const AvailabilityContext Info, SourceRange SrcRange) {
assert(D);
assert(Parent);
return new (Ctx) AvailabilityScope(Ctx, D, Parent, SrcRange, Info);
}
AvailabilityScope *AvailabilityScope::createForDeclImplicit(
ASTContext &Ctx, Decl *D, AvailabilityScope *Parent,
const AvailabilityContext Info, SourceRange SrcRange) {
assert(D);
assert(Parent);
return new (Ctx) AvailabilityScope(Ctx, IntroNode(D, Reason::DeclImplicit),
Parent, SrcRange, Info);
}
AvailabilityScope *
AvailabilityScope::createForIfStmtThen(ASTContext &Ctx, IfStmt *S,
AvailabilityScope *Parent,
const AvailabilityContext Info) {
assert(S);
assert(Parent);
return new (Ctx) AvailabilityScope(Ctx, IntroNode(S, /*IsThen=*/true), Parent,
S->getThenStmt()->getSourceRange(), Info);
}
AvailabilityScope *
AvailabilityScope::createForIfStmtElse(ASTContext &Ctx, IfStmt *S,
AvailabilityScope *Parent,
const AvailabilityContext Info) {
assert(S);
assert(Parent);
return new (Ctx)
AvailabilityScope(Ctx, IntroNode(S, /*IsThen=*/false), Parent,
S->getElseStmt()->getSourceRange(), Info);
}
AvailabilityScope *AvailabilityScope::createForConditionFollowingQuery(
ASTContext &Ctx, PoundAvailableInfo *PAI,
const StmtConditionElement &LastElement, AvailabilityScope *Parent,
const AvailabilityContext Info) {
assert(PAI);
assert(Parent);
SourceRange Range(PAI->getEndLoc(), LastElement.getEndLoc());
return new (Ctx) AvailabilityScope(Ctx, PAI, Parent, Range, Info);
}
AvailabilityScope *AvailabilityScope::createForGuardStmtFallthrough(
ASTContext &Ctx, GuardStmt *RS, BraceStmt *ContainingBraceStmt,
AvailabilityScope *Parent, const AvailabilityContext Info) {
assert(RS);
assert(ContainingBraceStmt);
assert(Parent);
SourceRange Range(RS->getEndLoc(), ContainingBraceStmt->getEndLoc());
return new (Ctx) AvailabilityScope(Ctx, IntroNode(RS, /*IsFallthrough=*/true),
Parent, Range, Info);
}
AvailabilityScope *
AvailabilityScope::createForGuardStmtElse(ASTContext &Ctx, GuardStmt *RS,
AvailabilityScope *Parent,
const AvailabilityContext Info) {
assert(RS);
assert(Parent);
return new (Ctx)
AvailabilityScope(Ctx, IntroNode(RS, /*IsFallthrough=*/false), Parent,
RS->getBody()->getSourceRange(), Info);
}
AvailabilityScope *
AvailabilityScope::createForWhileStmtBody(ASTContext &Ctx, WhileStmt *S,
AvailabilityScope *Parent,
const AvailabilityContext Info) {
assert(S);
assert(Parent);
return new (Ctx)
AvailabilityScope(Ctx, S, Parent, S->getBody()->getSourceRange(), Info);
}
void AvailabilityScope::addChild(AvailabilityScope *Child, ASTContext &Ctx) {
assert(Child->getSourceRange().isValid());
// Handle the first child.
if (Children.empty()) {
Children.push_back(Child);
return;
}
// Handle a child that is ordered after the existing children (this should be
// the common case).
auto &srcMgr = Ctx.SourceMgr;
if (srcMgr.isBefore(Children.back()->getSourceRange().Start,
Child->getSourceRange().Start)) {
Children.push_back(Child);
return;
}
// Insert the child amongst the existing sorted children.
auto iter = std::upper_bound(
Children.begin(), Children.end(), Child,
[&srcMgr](AvailabilityScope *lhs, AvailabilityScope *rhs) {
return srcMgr.isBefore(lhs->getSourceRange().Start,
rhs->getSourceRange().Start);
});
Children.insert(iter, Child);
}
AvailabilityScope *
AvailabilityScope::findMostRefinedSubContext(SourceLoc Loc, ASTContext &Ctx) {
assert(Loc.isValid());
if (SrcRange.isValid() && !Ctx.SourceMgr.containsTokenLoc(SrcRange, Loc))
return nullptr;
(void)evaluateOrDefault(Ctx.evaluator,
ExpandChildAvailabilityScopesRequest{this}, {});
assert(!getNeedsExpansion());
// Do a binary search to find the first child with a source range that
// ends after the given location.
auto iter = std::lower_bound(Children.begin(), Children.end(), Loc,
[&Ctx](AvailabilityScope *scope, SourceLoc loc) {
return Ctx.SourceMgr.isBefore(
scope->getSourceRange().End, loc);
});
// Check whether the matching child or any of its descendants contain
// the given location.
if (iter != Children.end()) {
if (auto found = (*iter)->findMostRefinedSubContext(Loc, Ctx))
return found;
}
// The location is in this context's range but not in any child's, so this
// context must be the innermost context.
return this;
}
void AvailabilityScope::dump(SourceManager &SrcMgr) const {
dump(llvm::errs(), SrcMgr);
}
void AvailabilityScope::dump(raw_ostream &OS, SourceManager &SrcMgr) const {
print(OS, SrcMgr, 0);
OS << '\n';
}
SourceLoc AvailabilityScope::getIntroductionLoc() const {
switch (getReason()) {
case Reason::Decl:
case Reason::DeclImplicit:
return Node.getAsDecl()->getLoc();
case Reason::IfStmtThenBranch:
case Reason::IfStmtElseBranch:
return Node.getAsIfStmt()->getIfLoc();
case Reason::ConditionFollowingAvailabilityQuery:
return Node.getAsPoundAvailableInfo()->getStartLoc();
case Reason::GuardStmtFallthrough:
case Reason::GuardStmtElseBranch:
return Node.getAsGuardStmt()->getGuardLoc();
case Reason::WhileStmtBody:
return Node.getAsWhileStmt()->getStartLoc();
case Reason::Root:
return SourceLoc();
}
llvm_unreachable("Unhandled Reason in switch.");
}
static SourceRange
getAvailabilityConditionVersionSourceRange(const PoundAvailableInfo *PAI,
PlatformKind Platform,
const llvm::VersionTuple &Version) {
SourceRange Range;
for (auto *S : PAI->getQueries()) {
if (auto *V = dyn_cast<PlatformVersionConstraintAvailabilitySpec>(S)) {
if (V->getPlatform() == Platform && V->getVersion() == Version) {
// More than one: return invalid range, no unique choice.
if (Range.isValid())
return SourceRange();
else
Range = V->getVersionSrcRange();
}
}
}
return Range;
}
static SourceRange getAvailabilityConditionVersionSourceRange(
const MutableArrayRef<StmtConditionElement> &Conds, PlatformKind Platform,
const llvm::VersionTuple &Version) {
SourceRange Range;
for (auto const &C : Conds) {
if (C.getKind() == StmtConditionElement::CK_Availability) {
SourceRange R = getAvailabilityConditionVersionSourceRange(
C.getAvailability(), Platform, Version);
// More than one: return invalid range.
if (Range.isValid())
return SourceRange();
else
Range = R;
}
}
return Range;
}
static SourceRange
getAvailabilityConditionVersionSourceRange(const DeclAttributes &DeclAttrs,
PlatformKind Platform,
const llvm::VersionTuple &Version) {
SourceRange Range;
for (auto *Attr : DeclAttrs) {
if (auto *AA = dyn_cast<AvailableAttr>(Attr)) {
if (AA->Introduced.has_value() && AA->Introduced.value() == Version &&
AA->Platform == Platform) {
// More than one: return invalid range.
if (Range.isValid())
return SourceRange();
else
Range = AA->IntroducedRange;
}
}
}
return Range;
}
SourceRange AvailabilityScope::getAvailabilityConditionVersionSourceRange(
PlatformKind Platform, const llvm::VersionTuple &Version) const {
switch (getReason()) {
case Reason::Decl:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsDecl()->getAttrs(), Platform, Version);
case Reason::IfStmtThenBranch:
case Reason::IfStmtElseBranch:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsIfStmt()->getCond(), Platform, Version);
case Reason::ConditionFollowingAvailabilityQuery:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsPoundAvailableInfo(), Platform, Version);
case Reason::GuardStmtFallthrough:
case Reason::GuardStmtElseBranch:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsGuardStmt()->getCond(), Platform, Version);
case Reason::WhileStmtBody:
return ::getAvailabilityConditionVersionSourceRange(
Node.getAsWhileStmt()->getCond(), Platform, Version);
case Reason::Root:
case Reason::DeclImplicit:
return SourceRange();
}
llvm_unreachable("Unhandled Reason in switch.");
}
std::optional<const AvailabilityRange>
AvailabilityScope::getExplicitAvailabilityRange() const {
switch (getReason()) {
case Reason::Root:
return std::nullopt;
case Reason::Decl: {
auto decl = Node.getAsDecl();
if (auto attr = AvailabilityInference::attrForAnnotatedAvailableRange(decl))
return AvailabilityInference::availableRange(attr, decl->getASTContext());
return std::nullopt;
}
case Reason::DeclImplicit:
return std::nullopt;
case Reason::IfStmtThenBranch:
case Reason::IfStmtElseBranch:
case Reason::ConditionFollowingAvailabilityQuery:
case Reason::GuardStmtFallthrough:
case Reason::GuardStmtElseBranch:
case Reason::WhileStmtBody:
// Availability is inherently explicit for all of these nodes.
return getPlatformAvailabilityRange();
}
llvm_unreachable("Unhandled Reason in switch.");
}
static std::string
stringForAvailability(const AvailabilityRange &availability) {
if (availability.isAlwaysAvailable())
return "all";
if (availability.isKnownUnreachable())
return "none";
return availability.getVersionString();
}
void AvailabilityScope::print(raw_ostream &OS, SourceManager &SrcMgr,
unsigned Indent) const {
OS.indent(Indent);
OS << "(" << getReasonName(getReason());
OS << " ";
AvailabilityInfo.print(OS);
if (getReason() == Reason::Decl || getReason() == Reason::DeclImplicit) {
Decl *D = Node.getAsDecl();
OS << " decl=";
if (auto VD = dyn_cast<ValueDecl>(D)) {
OS << VD->getName();
} else if (auto *ED = dyn_cast<ExtensionDecl>(D)) {
OS << "extension." << ED->getExtendedType().getString();
} else if (isa<TopLevelCodeDecl>(D)) {
OS << "<top-level-code>";
} else if (auto PBD = dyn_cast<PatternBindingDecl>(D)) {
if (auto VD = PBD->getAnchoringVarDecl(0)) {
OS << VD->getName();
}
} else if (auto ECD = dyn_cast<EnumCaseDecl>(D)) {
if (auto EED = ECD->getFirstElement()) {
OS << EED->getName();
}
}
}
auto R = getSourceRange();
if (R.isValid()) {
OS << " src_range=";
if (getReason() != Reason::Root) {
R.print(OS, SrcMgr, /*PrintText=*/false);
} else if (auto info = SrcMgr.getGeneratedSourceInfo(
Node.getAsSourceFile()->getBufferID())) {
info->originalSourceRange.print(OS, SrcMgr, /*PrintText=*/false);
} else {
OS << "<unknown>";
}
}
if (getReason() == Reason::Root) {
if (auto info = SrcMgr.getGeneratedSourceInfo(
Node.getAsSourceFile()->getBufferID())) {
OS << " generated_kind=" << GeneratedSourceInfo::kindToString(info->kind);
} else {
OS << " file=" << Node.getAsSourceFile()->getFilename().str();
}
}
if (auto explicitAvailability = getExplicitAvailabilityRange())
OS << " explicit_version=" << stringForAvailability(*explicitAvailability);
for (AvailabilityScope *Child : Children) {
OS << '\n';
Child->print(OS, SrcMgr, Indent + 2);
}
OS.indent(Indent);
OS << ")";
}
AvailabilityScope::Reason AvailabilityScope::getReason() const {
return Node.getReason();
}
StringRef AvailabilityScope::getReasonName(Reason R) {
switch (R) {
case Reason::Root:
return "root";
case Reason::Decl:
return "decl";
case Reason::DeclImplicit:
return "decl_implicit";
case Reason::IfStmtThenBranch:
return "if_then";
case Reason::IfStmtElseBranch:
return "if_else";
case Reason::ConditionFollowingAvailabilityQuery:
return "condition_following_availability";
case Reason::GuardStmtFallthrough:
return "guard_fallthrough";
case Reason::GuardStmtElseBranch:
return "guard_else";
case Reason::WhileStmtBody:
return "while_body";
}
llvm_unreachable("Unhandled Reason in switch.");
}
void swift::simple_display(llvm::raw_ostream &out,
const AvailabilityScope *scope) {
out << "Scope @" << scope;
}
std::optional<evaluator::SideEffect>
ExpandChildAvailabilityScopesRequest::getCachedResult() const {
auto *scope = std::get<0>(getStorage());
if (scope->getNeedsExpansion())
return std::nullopt;
return evaluator::SideEffect();
}
void ExpandChildAvailabilityScopesRequest::cacheResult(
evaluator::SideEffect sideEffect) const {
auto *scope = std::get<0>(getStorage());
scope->setNeedsExpansion(false);
}
/// Emit an error message, dump each context with its corresponding label, and
/// abort.
static void verificationError(
ASTContext &ctx, llvm::StringRef msg,
std::initializer_list<std::pair<const char *, const AvailabilityScope *>>
labelsAndNodes) {
llvm::errs() << msg << "\n";
for (auto pair : labelsAndNodes) {
auto label = std::get<0>(pair);
auto scope = std::get<1>(pair);
llvm::errs() << label << ":\n";
scope->print(llvm::errs(), ctx.SourceMgr);
llvm::errs() << "\n";
}
abort();
}
void AvailabilityScope::verify(const AvailabilityScope *parent,
ASTContext &ctx) const {
// Verify the children first.
for (auto child : Children) {
child->verify(this, ctx);
}
// Verify that the children are in sorted order and that their source ranges
// do not overlap.
auto &srcMgr = ctx.SourceMgr;
if (Children.size() > 1) {
auto const *previous = Children.front();
for (auto const *current : ArrayRef(Children).drop_front()) {
if (!srcMgr.isAtOrBefore(previous->getSourceRange().Start,
current->getSourceRange().Start))
verificationError(
ctx, "out of order children",
{{"child 1", previous}, {"child 2", current}, {"parent", this}});
if (srcMgr.containsLoc(previous->getSourceRange(),
current->getSourceRange().Start))
verificationError(
ctx, "overlapping children",
{{"child 1", previous}, {"child 2", current}, {"parent", this}});
previous = current;
}
}
// Only root nodes are allowed to have no parent.
if (!parent) {
if (getReason() != Reason::Root)
verificationError(ctx, "interior node without parent", {{"node", this}});
return;
}
// All nodes with a parent must have a valid source range.
if (!SrcRange.isValid())
verificationError(ctx, "invalid source range", {{"node", this}});
// Child nodes must be contained by their parents in all dimensions (source
// range, introduction version, etc).
if (getReason() != Reason::Root) {
auto parentRange = parent->SrcRange;
if (parentRange.isValid() &&
!(srcMgr.isAtOrBefore(parentRange.Start, SrcRange.Start) &&
srcMgr.isAtOrBefore(SrcRange.End, parentRange.End)))
verificationError(ctx, "child source range not contained",
{{"child", this}, {"parent", parent}});
}
if (!getAvailabilityContext().isContainedIn(parent->getAvailabilityContext()))
verificationError(ctx, "child availability range not contained",
{{"child", this}, {"parent", parent}});
}
void AvailabilityScope::verify(ASTContext &ctx) {
verify(nullptr, ctx);
}
|