Skip to content

Serialization: Encode custom availability domains #80616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions include/swift/AST/AvailabilityDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace swift {
class ASTContext;
class CustomAvailabilityDomain;
class Decl;
class DeclContext;
class ModuleDecl;

Expand Down Expand Up @@ -148,6 +149,11 @@ class AvailabilityDomain final {
return AvailabilityDomain(Kind::Embedded);
}

/// If `decl` represents an availability domain, returns the corresponding
/// `AvailabilityDomain` value. Otherwise, returns `std::nullopt`.
static std::optional<AvailabilityDomain> forCustom(Decl *decl,
const ASTContext &ctx);

static AvailabilityDomain forCustom(const CustomAvailabilityDomain *domain) {
return AvailabilityDomain(domain);
}
Expand Down Expand Up @@ -236,6 +242,10 @@ class AvailabilityDomain final {
/// Returns the string to use when printing an `@available` attribute.
llvm::StringRef getNameForAttributePrinting() const;

/// Returns the decl that represents the domain, or `nullptr` if the domain
/// does not have a decl.
Decl *getDecl() const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the moment, does this always points to a clang VarDecl? And when would a domain not have a decl?

Copy link
Contributor Author

@tshortli tshortli Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, right now it can only reference and imported VarDecl. However, I anticipate it pointing to additional types of declarations once we have a Swift syntax for custom availability domains.


/// Returns the module that the domain belongs to, if it is a custom domain.
ModuleDecl *getModule() const;

Expand Down Expand Up @@ -307,24 +317,26 @@ class CustomAvailabilityDomain : public llvm::FoldingSetNode {
Identifier name;
Kind kind;
ModuleDecl *mod;
Decl *decl;

CustomAvailabilityDomain(Identifier name, ModuleDecl *mod, Kind kind);
CustomAvailabilityDomain(Identifier name, Kind kind, ModuleDecl *mod,
Decl *decl);

public:
static const CustomAvailabilityDomain *get(StringRef name, ModuleDecl *mod,
Kind kind, const ASTContext &ctx);
static const CustomAvailabilityDomain *get(StringRef name, Kind kind,
ModuleDecl *mod, Decl *decl,
const ASTContext &ctx);

Identifier getName() const { return name; }
Kind getKind() const { return kind; }
ModuleDecl *getModule() const { return mod; }
Decl *getDecl() const { return decl; }

/// Uniquing for `ASTContext`.
static void Profile(llvm::FoldingSetNodeID &ID, Identifier name,
ModuleDecl *mod, Kind kind);
ModuleDecl *mod);

void Profile(llvm::FoldingSetNodeID &ID) const {
Profile(ID, name, mod, kind);
}
void Profile(llvm::FoldingSetNodeID &ID) const { Profile(ID, name, mod); }
};

/// Represents either a resolved availability domain or an identifier written
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ SUPPRESSIBLE_EXPERIMENTAL_FEATURE(ExecutionAttribute, false)
ADOPTABLE_EXPERIMENTAL_FEATURE(AsyncCallerExecution, false)

/// Allow custom availability domains to be defined and referenced.
SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CustomAvailability, true)
EXPERIMENTAL_FEATURE(CustomAvailability, true)

/// Allow isolated conformances.
EXPERIMENTAL_FEATURE(IsolatedConformances, true)
Expand Down
9 changes: 5 additions & 4 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5866,11 +5866,11 @@ const AvailabilityContext::Storage *AvailabilityContext::Storage::get(
}

const CustomAvailabilityDomain *
CustomAvailabilityDomain::get(StringRef name, ModuleDecl *mod, Kind kind,
const ASTContext &ctx) {
CustomAvailabilityDomain::get(StringRef name, Kind kind, ModuleDecl *mod,
Decl *decl, const ASTContext &ctx) {
auto identifier = ctx.getIdentifier(name);
llvm::FoldingSetNodeID id;
CustomAvailabilityDomain::Profile(id, identifier, mod, kind);
CustomAvailabilityDomain::Profile(id, identifier, mod);

auto &foldingSet = ctx.getImpl().CustomAvailabilityDomains;
void *insertPos;
Expand All @@ -5880,7 +5880,8 @@ CustomAvailabilityDomain::get(StringRef name, ModuleDecl *mod, Kind kind,

void *mem = ctx.Allocate(sizeof(CustomAvailabilityDomain),
alignof(CustomAvailabilityDomain));
auto *newNode = ::new (mem) CustomAvailabilityDomain(identifier, mod, kind);
auto *newNode =
::new (mem) CustomAvailabilityDomain(identifier, kind, mod, decl);
foldingSet.InsertNode(newNode, insertPos);

return newNode;
Expand Down
8 changes: 0 additions & 8 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3302,14 +3302,6 @@ suppressingFeatureAddressableTypes(PrintOptions &options,
action();
}

static void
suppressingFeatureCustomAvailability(PrintOptions &options,
llvm::function_ref<void()> action) {
// FIXME: [availability] Save and restore a bit controlling whether
// @available attributes for custom domains are printed.
action();
}

static void
suppressingFeatureExecutionAttribute(PrintOptions &options,
llvm::function_ref<void()> action) {
Expand Down
68 changes: 62 additions & 6 deletions lib/AST/AvailabilityDomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,61 @@
#include "swift/AST/Module.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Assertions.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "llvm/ADT/StringSwitch.h"

using namespace swift;

CustomAvailabilityDomain::Kind
getCustomDomainKind(clang::FeatureAvailKind featureAvailKind) {
switch (featureAvailKind) {
case clang::FeatureAvailKind::None:
llvm_unreachable("unexpected kind");
case clang::FeatureAvailKind::Available:
return CustomAvailabilityDomain::Kind::Enabled;
case clang::FeatureAvailKind::Unavailable:
return CustomAvailabilityDomain::Kind::Disabled;
case clang::FeatureAvailKind::Dynamic:
return CustomAvailabilityDomain::Kind::Dynamic;
}
}

static const CustomAvailabilityDomain *
customDomainForClangDecl(Decl *decl, const ASTContext &ctx) {
auto *clangDecl = decl->getClangDecl();
ASSERT(clangDecl);

auto featureInfo = clangDecl->getASTContext().getFeatureAvailInfo(
const_cast<clang::Decl *>(clangDecl));

// Ensure the decl actually represents an availability domain.
if (featureInfo.first.empty())
return nullptr;

if (featureInfo.second.Kind == clang::FeatureAvailKind::None)
return nullptr;

return CustomAvailabilityDomain::get(
featureInfo.first, getCustomDomainKind(featureInfo.second.Kind),
decl->getModuleContext(), decl, ctx);
}

std::optional<AvailabilityDomain>
AvailabilityDomain::forCustom(Decl *decl, const ASTContext &ctx) {
if (!decl)
return std::nullopt;

if (decl->hasClangNode()) {
if (auto *customDomain = customDomainForClangDecl(decl, ctx))
return AvailabilityDomain::forCustom(customDomain);
} else {
// FIXME: [availability] Handle Swift availability domains decls.
}

return std::nullopt;
}

std::optional<AvailabilityDomain>
AvailabilityDomain::builtinDomainForString(StringRef string,
const DeclContext *declContext) {
Expand Down Expand Up @@ -166,6 +217,13 @@ llvm::StringRef AvailabilityDomain::getNameForAttributePrinting() const {
}
}

Decl *AvailabilityDomain::getDecl() const {
if (auto *customDomain = getCustomDomain())
return customDomain->getDecl();

return nullptr;
}

ModuleDecl *AvailabilityDomain::getModule() const {
if (auto customDomain = getCustomDomain())
return customDomain->getModule();
Expand Down Expand Up @@ -268,19 +326,17 @@ bool StableAvailabilityDomainComparator::operator()(
}
}

CustomAvailabilityDomain::CustomAvailabilityDomain(Identifier name,
ModuleDecl *mod, Kind kind)
: name(name), kind(kind), mod(mod) {
CustomAvailabilityDomain::CustomAvailabilityDomain(Identifier name, Kind kind,
ModuleDecl *mod, Decl *decl)
: name(name), kind(kind), mod(mod), decl(decl) {
ASSERT(!name.empty());
ASSERT(mod);
}

void CustomAvailabilityDomain::Profile(llvm::FoldingSetNodeID &ID,
Identifier name, ModuleDecl *mod,
Kind kind) {
Identifier name, ModuleDecl *mod) {
ID.AddPointer(name.getAsOpaquePointer());
ID.AddPointer(mod);
ID.AddInteger(static_cast<unsigned>(kind));
}

std::optional<AvailabilityDomain>
Expand Down
6 changes: 4 additions & 2 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,10 @@ static bool usesFeatureCoroutineAccessors(Decl *decl) {
}

static bool usesFeatureCustomAvailability(Decl *decl) {
// FIXME: [availability] Check whether @available attributes for custom
// domains are attached to the decl.
for (auto attr : decl->getSemanticAvailableAttrs()) {
if (attr.getDomain().isCustom())
return true;
}
return false;
}

Expand Down
36 changes: 15 additions & 21 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4114,35 +4114,29 @@ void ClangModuleUnit::lookupObjCMethods(

void ClangModuleUnit::lookupAvailabilityDomains(
Identifier identifier, SmallVectorImpl<AvailabilityDomain> &results) const {
auto lookupTable = owner.findLookupTable(clangModule);
if (!lookupTable)
auto domainName = identifier.str();
auto &ctx = getASTContext();
auto &clangASTContext = getClangASTContext();

auto domainInfo = clangASTContext.getFeatureAvailInfo(domainName);
if (domainInfo.Kind == clang::FeatureAvailKind::None)
return;

auto varDecl = lookupTable->lookupAvailabilityDomainDecl(identifier.str());
auto *varDecl = dyn_cast_or_null<clang::VarDecl>(domainInfo.Decl);
if (!varDecl)
return;

auto featureInfo = getClangASTContext().getFeatureAvailInfo(varDecl);
if (featureInfo.first.empty())
// The decl that was found may belong to a different Clang module.
if (varDecl->getOwningModule() != getClangModule())
return;

auto getDomainKind = [](clang::FeatureAvailKind featureAvailKind) {
switch (featureAvailKind) {
case clang::FeatureAvailKind::None:
llvm_unreachable("unexpected kind");
case clang::FeatureAvailKind::Available:
return CustomAvailabilityDomain::Kind::Enabled;
case clang::FeatureAvailKind::Unavailable:
return CustomAvailabilityDomain::Kind::Disabled;
case clang::FeatureAvailKind::Dynamic:
return CustomAvailabilityDomain::Kind::Dynamic;
}
};
auto *imported = ctx.getClangModuleLoader()->importDeclDirectly(varDecl);
if (!imported)
return;

auto domain = AvailabilityDomain::forCustom(CustomAvailabilityDomain::get(
featureInfo.first, getParentModule(),
getDomainKind(featureInfo.second.Kind), getASTContext()));
results.push_back(domain);
auto customDomain = AvailabilityDomain::forCustom(imported, ctx);
ASSERT(customDomain);
results.push_back(*customDomain);
}

void ClangModuleUnit::collectLinkLibraries(
Expand Down
Loading