Skip to content

Commit 6ad2066

Browse files
authored
Merge pull request #83731 from tshortli/availability-domain-value-decl
AST: Make the decl associated with an AvailabilityDomain a ValueDecl
2 parents dd7890e + 0bba9bb commit 6ad2066

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

include/swift/AST/AvailabilityDomain.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
namespace swift {
3434
class ASTContext;
3535
class CustomAvailabilityDomain;
36-
class Decl;
3736
class DeclContext;
3837
class FuncDecl;
3938
class ModuleDecl;
39+
class ValueDecl;
4040

4141
/// Represents a dimension of availability (e.g. macOS platform or Swift
4242
/// language mode).
@@ -153,7 +153,7 @@ class AvailabilityDomain final {
153153

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

159159
static AvailabilityDomain forCustom(const CustomAvailabilityDomain *domain) {
@@ -250,7 +250,7 @@ class AvailabilityDomain final {
250250

251251
/// Returns the decl that represents the domain, or `nullptr` if the domain
252252
/// does not have a decl.
253-
Decl *getDecl() const;
253+
ValueDecl *getDecl() const;
254254

255255
/// Returns the module that the domain belongs to, if it is a custom domain.
256256
ModuleDecl *getModule() const;
@@ -343,22 +343,22 @@ class CustomAvailabilityDomain : public llvm::FoldingSetNode {
343343
Identifier name;
344344
Kind kind;
345345
ModuleDecl *mod;
346-
Decl *decl;
346+
ValueDecl *decl;
347347
FuncDecl *predicateFunc;
348348

349349
CustomAvailabilityDomain(Identifier name, Kind kind, ModuleDecl *mod,
350-
Decl *decl, FuncDecl *predicateFunc);
350+
ValueDecl *decl, FuncDecl *predicateFunc);
351351

352352
public:
353353
static const CustomAvailabilityDomain *get(StringRef name, Kind kind,
354-
ModuleDecl *mod, Decl *decl,
354+
ModuleDecl *mod, ValueDecl *decl,
355355
FuncDecl *predicateFunc,
356356
const ASTContext &ctx);
357357

358358
Identifier getName() const { return name; }
359359
Kind getKind() const { return kind; }
360360
ModuleDecl *getModule() const { return mod; }
361-
Decl *getDecl() const { return decl; }
361+
ValueDecl *getDecl() const { return decl; }
362362

363363
/// Returns the function that should be called at runtime to determine whether
364364
/// the domain is available, or `nullptr` if the domain's availability is not

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5911,7 +5911,7 @@ const AvailabilityContext::Storage *AvailabilityContext::Storage::get(
59115911

59125912
const CustomAvailabilityDomain *
59135913
CustomAvailabilityDomain::get(StringRef name, Kind kind, ModuleDecl *mod,
5914-
Decl *decl, FuncDecl *predicateFunc,
5914+
ValueDecl *decl, FuncDecl *predicateFunc,
59155915
const ASTContext &ctx) {
59165916
auto identifier = ctx.getIdentifier(name);
59175917
llvm::FoldingSetNodeID id;

lib/AST/AvailabilityDomain.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ getCustomDomainKind(clang::FeatureAvailKind featureAvailKind) {
3939
}
4040

4141
static const CustomAvailabilityDomain *
42-
customDomainForClangDecl(Decl *decl, const ASTContext &ctx) {
42+
customDomainForClangDecl(ValueDecl *decl, const ASTContext &ctx) {
4343
auto *clangDecl = decl->getClangDecl();
4444
ASSERT(clangDecl);
4545

@@ -68,7 +68,7 @@ customDomainForClangDecl(Decl *decl, const ASTContext &ctx) {
6868
}
6969

7070
std::optional<AvailabilityDomain>
71-
AvailabilityDomain::forCustom(Decl *decl, const ASTContext &ctx) {
71+
AvailabilityDomain::forCustom(ValueDecl *decl, const ASTContext &ctx) {
7272
if (!decl)
7373
return std::nullopt;
7474

@@ -247,7 +247,7 @@ llvm::StringRef AvailabilityDomain::getNameForAttributePrinting() const {
247247
}
248248
}
249249

250-
Decl *AvailabilityDomain::getDecl() const {
250+
ValueDecl *AvailabilityDomain::getDecl() const {
251251
if (auto *customDomain = getCustomDomain())
252252
return customDomain->getDecl();
253253

@@ -369,7 +369,8 @@ bool StableAvailabilityDomainComparator::operator()(
369369
}
370370

371371
CustomAvailabilityDomain::CustomAvailabilityDomain(Identifier name, Kind kind,
372-
ModuleDecl *mod, Decl *decl,
372+
ModuleDecl *mod,
373+
ValueDecl *decl,
373374
FuncDecl *predicateFunc)
374375
: name(name), kind(kind), mod(mod), decl(decl),
375376
predicateFunc(predicateFunc) {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4098,7 +4098,8 @@ void ClangModuleUnit::lookupAvailabilityDomains(
40984098
if (varDecl->getOwningModule() != getClangModule())
40994099
return;
41004100

4101-
auto *imported = ctx.getClangModuleLoader()->importDeclDirectly(varDecl);
4101+
auto *imported = dyn_cast_or_null<ValueDecl>(
4102+
ctx.getClangModuleLoader()->importDeclDirectly(varDecl));
41024103
if (!imported)
41034104
return;
41044105

lib/Serialization/Deserialization.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5824,7 +5824,7 @@ decodeDomainKind(uint8_t kind) {
58245824

58255825
static std::optional<AvailabilityDomain>
58265826
decodeAvailabilityDomain(AvailabilityDomainKind domainKind,
5827-
PlatformKind platformKind, Decl *decl,
5827+
PlatformKind platformKind, ValueDecl *decl,
58285828
const ASTContext &ctx) {
58295829
switch (domainKind) {
58305830
case AvailabilityDomainKind::Universal:
@@ -5893,9 +5893,16 @@ DeclDeserializer::readAvailable_DECL_ATTR(SmallVectorImpl<uint64_t> &scratch,
58935893
else
58945894
kind = AvailableAttr::Kind::Default;
58955895

5896-
Decl *domainDecl = nullptr;
5896+
ValueDecl *domainDecl = nullptr;
58975897
if (domainDeclID) {
5898-
SET_OR_RETURN_ERROR(domainDecl, MF.getDeclChecked(domainDeclID));
5898+
Decl *decodedDomainDecl = nullptr;
5899+
SET_OR_RETURN_ERROR(decodedDomainDecl, MF.getDeclChecked(domainDeclID));
5900+
5901+
if (decodedDomainDecl) {
5902+
domainDecl = dyn_cast<ValueDecl>(decodedDomainDecl);
5903+
if (!domainDecl)
5904+
return llvm::make_error<InavalidAvailabilityDomainError>();
5905+
}
58995906
}
59005907

59015908
auto domain = decodeAvailabilityDomain(domainKind, platform, domainDecl, ctx);

0 commit comments

Comments
 (0)