Files
swift-mirror/lib/AST/AvailabilitySpec.cpp
Ankit Aggarwal 92d09f4e19 Extend @available to support PackageDescription
<rdar://problem/46548531> Extend @available to support PackageDescription

This introduces a new private availability kind "_PackageDescription" to
allow availability testing by an arbitary version that can be passed
using a new command-line flag "-swiftpm-manifest-version". The semantics
are exactly same as Swift version specific availability. In longer term,
it maybe possible to remove this enhancement once there is
a language-level availability support for 3rd party libraries.

Motivation:

Swift packages are configured using a Package.swift manifest file. The
manifest file uses a library called PackageDescription, which contains
various settings that can be configured for a package. The new additions
in the PackageDescription APIs are gated behind a "tools version" that
every manifest must declare. This means, packages don't automatically
get access to the new APIs. They need to update their declared tools
version in order to use the new API. This is basically similar to the
minimum deployment target version we have for our OSes.

This gating is important for allowing packages to maintain backwards
compatibility. SwiftPM currently checks for API usages at runtime in
order to implement this gating. This works reasonably well but can lead
to a poor experience with features like code-completion and module
interface generation in IDEs and editors (that use sourcekit-lsp) as
SwiftPM has no control over these features.
2018-12-08 09:38:40 +05:30

79 lines
3.0 KiB
C++

//===--- AvailabilitySpec.cpp - Swift Availability Query ASTs -------------===//
//
// 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 availability specification AST classes.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/AvailabilitySpec.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
SourceRange AvailabilitySpec::getSourceRange() const {
switch (getKind()) {
case AvailabilitySpecKind::PlatformVersionConstraint:
return cast<PlatformVersionConstraintAvailabilitySpec>(this)->getSourceRange();
case AvailabilitySpecKind::LanguageVersionConstraint:
case AvailabilitySpecKind::PackageDescriptionVersionConstraint:
return cast<PlatformAgnosticVersionConstraintAvailabilitySpec>(this)->getSourceRange();
case AvailabilitySpecKind::OtherPlatform:
return cast<OtherPlatformAvailabilitySpec>(this)->getSourceRange();
}
llvm_unreachable("bad AvailabilitySpecKind");
}
// Only allow allocation of AvailabilitySpecs using the
// allocator in ASTContext.
void *AvailabilitySpec::operator new(size_t Bytes, ASTContext &C,
unsigned Alignment) {
return C.Allocate(Bytes, Alignment);
}
SourceRange PlatformVersionConstraintAvailabilitySpec::getSourceRange() const {
return SourceRange(PlatformLoc, VersionSrcRange.End);
}
void PlatformVersionConstraintAvailabilitySpec::print(raw_ostream &OS,
unsigned Indent) const {
OS.indent(Indent) << '(' << "platform_version_constraint_availability_spec"
<< " platform='" << platformString(getPlatform()) << "'"
<< " version='" << getVersion() << "'"
<< ')';
}
SourceRange PlatformAgnosticVersionConstraintAvailabilitySpec::getSourceRange() const {
return SourceRange(PlatformAgnosticNameLoc, VersionSrcRange.End);
}
void PlatformAgnosticVersionConstraintAvailabilitySpec::print(raw_ostream &OS,
unsigned Indent) const {
OS.indent(Indent) << '('
<< "platform_agnostic_version_constraint_availability_spec"
<< " kind='"
<< (isLanguageVersionSpecific() ?
"swift" : "package_description")
<< "'"
<< " version='" << getVersion() << "'"
<< ')';
}
void OtherPlatformAvailabilitySpec::print(raw_ostream &OS, unsigned Indent) const {
OS.indent(Indent) << '(' << "other_constraint_availability_spec"
<< " "
<< ')';
}