Skip to content

Commit cd13d7d

Browse files
committed
AST: Remove AvailableAttr::RenameDecl.
The renamed decl is now stored exclusively in the split request evaluator storage, which is more efficient since most availability attributes do not specify a renamed decl.
1 parent 7789ce8 commit cd13d7d

File tree

10 files changed

+121
-136
lines changed

10 files changed

+121
-136
lines changed

include/swift/AST/Attr.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ enum class PlatformAgnosticAvailabilityKind : uint8_t {
734734
class AvailableAttr : public DeclAttribute {
735735
public:
736736
AvailableAttr(SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
737-
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
737+
StringRef Message, StringRef Rename,
738738
const llvm::VersionTuple &Introduced,
739739
SourceRange IntroducedRange,
740740
const llvm::VersionTuple &Deprecated,
@@ -752,14 +752,13 @@ class AvailableAttr : public DeclAttribute {
752752
/// This should take the form of an operator, identifier, or full function
753753
/// name, optionally with a prefixed type, similar to the syntax used for
754754
/// the `NS_SWIFT_NAME` annotation in Objective-C.
755+
///
756+
/// \c ValueDecl::getRenamedDecl() can be used to look up the declaration
757+
/// referred to by this string. Note that this attribute can have a rename
758+
/// target that was provided directly when synthesized and therefore can have
759+
/// a rename decl even when this string is empty.
755760
const StringRef Rename;
756761

757-
/// The declaration referred to by \c Rename. Note that this is only set for
758-
/// deserialized attributes or inferred attributes from ObjectiveC code.
759-
/// \c ValueDecl::getRenamedDecl should be used to find the declaration
760-
/// corresponding to \c Rename.
761-
ValueDecl *RenameDecl;
762-
763762
/// Indicates when the symbol was introduced.
764763
const std::optional<llvm::VersionTuple> Introduced;
765764

@@ -853,11 +852,6 @@ class AvailableAttr : public DeclAttribute {
853852
llvm::VersionTuple Obsoleted
854853
= llvm::VersionTuple());
855854

856-
/// Create an AvailableAttr that indicates the given \p AsyncFunc should be
857-
/// preferentially used in async contexts
858-
static AvailableAttr *
859-
createForAsyncAlternative(ASTContext &C, AbstractFunctionDecl *AsyncFunc);
860-
861855
AvailableAttr *clone(ASTContext &C, bool implicit) const;
862856
AvailableAttr *clone(ASTContext &C) const {
863857
return clone(C, isImplicit());

include/swift/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,6 +3266,11 @@ class ValueDecl : public Decl {
32663266
/// Performs a request to look up the decl that this decl has been renamed to
32673267
/// if `attr` indicates that it has been renamed.
32683268
ValueDecl *getRenamedDecl(const AvailableAttr *attr) const;
3269+
3270+
/// Directly sets the renamed decl corresponding to `attr`. This should only
3271+
/// be used when synthesizing an `AvailableAttr`, before calling
3272+
/// `getRenamedDecl()`.
3273+
void setRenamedDecl(const AvailableAttr *attr, ValueDecl *renameDecl) const;
32693274
};
32703275

32713276
/// This is a common base class for declarations which declare a type.

lib/AST/Attr.cpp

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,14 +2226,14 @@ Type RawLayoutAttr::getResolvedCountType(StructDecl *sd) const {
22262226

22272227
AvailableAttr::AvailableAttr(
22282228
SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
2229-
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
2229+
StringRef Message, StringRef Rename,
22302230
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
22312231
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
22322232
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
22332233
PlatformAgnosticAvailabilityKind PlatformAgnostic, bool Implicit,
22342234
bool IsSPI, bool IsForEmbedded)
22352235
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
2236-
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
2236+
Message(Message), Rename(Rename),
22372237
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
22382238
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
22392239
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange) {
@@ -2265,23 +2265,10 @@ AvailableAttr::createPlatformAgnostic(ASTContext &C,
22652265
assert(!Obsoleted.empty());
22662266
}
22672267
return new (C) AvailableAttr(
2268-
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename, nullptr,
2269-
NoVersion, SourceRange(),
2270-
NoVersion, SourceRange(),
2271-
Obsoleted, SourceRange(),
2272-
Kind, /* isImplicit */ false, /*SPI*/false);
2273-
}
2274-
2275-
AvailableAttr *
2276-
AvailableAttr::createForAsyncAlternative(ASTContext &C,
2277-
AbstractFunctionDecl *AsyncFunc) {
2278-
llvm::VersionTuple NoVersion;
2279-
return new (C) AvailableAttr(
2280-
SourceLoc(), SourceRange(), PlatformKind::none, "", "", AsyncFunc,
2281-
NoVersion, SourceRange(),
2282-
NoVersion, SourceRange(),
2283-
NoVersion, SourceRange(),
2284-
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true, /*SPI*/false);
2268+
SourceLoc(), SourceRange(), PlatformKind::none, Message, Rename,
2269+
/*Introduced=*/NoVersion, SourceRange(), /*Deprecated=*/NoVersion,
2270+
SourceRange(), Obsoleted, SourceRange(), Kind, /*Implicit=*/false,
2271+
/*SPI=*/false);
22852272
}
22862273

22872274
bool AvailableAttr::isActivePlatform(const ASTContext &ctx) const {
@@ -2294,19 +2281,16 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
22942281
}
22952282

22962283
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
2297-
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
2298-
implicit ? SourceRange() : getRange(),
2299-
getPlatform(), Message, Rename, RenameDecl,
2300-
Introduced ? *Introduced : llvm::VersionTuple(),
2301-
implicit ? SourceRange() : IntroducedRange,
2302-
Deprecated ? *Deprecated : llvm::VersionTuple(),
2303-
implicit ? SourceRange() : DeprecatedRange,
2304-
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2305-
implicit ? SourceRange() : ObsoletedRange,
2306-
getPlatformAgnosticAvailability(),
2307-
implicit,
2308-
isSPI(),
2309-
isForEmbedded());
2284+
return new (C) AvailableAttr(
2285+
implicit ? SourceLoc() : AtLoc, implicit ? SourceRange() : getRange(),
2286+
getPlatform(), Message, Rename,
2287+
Introduced ? *Introduced : llvm::VersionTuple(),
2288+
implicit ? SourceRange() : IntroducedRange,
2289+
Deprecated ? *Deprecated : llvm::VersionTuple(),
2290+
implicit ? SourceRange() : DeprecatedRange,
2291+
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2292+
implicit ? SourceRange() : ObsoletedRange,
2293+
getPlatformAgnosticAvailability(), implicit, isSPI(), isForEmbedded());
23102294
}
23112295

23122296
std::optional<OriginallyDefinedInAttr::ActiveVersion>

lib/AST/Availability.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,10 @@ static AvailableAttr *createAvailableAttr(PlatformKind Platform,
183183
Inferred.Obsoleted.value_or(llvm::VersionTuple());
184184

185185
return new (Context)
186-
AvailableAttr(SourceLoc(), SourceRange(), Platform,
187-
Message, Rename, RenameDecl,
188-
Introduced, /*IntroducedRange=*/SourceRange(),
189-
Deprecated, /*DeprecatedRange=*/SourceRange(),
190-
Obsoleted, /*ObsoletedRange=*/SourceRange(),
191-
Inferred.PlatformAgnostic, /*Implicit=*/true,
192-
Inferred.IsSPI);
186+
AvailableAttr(SourceLoc(), SourceRange(), Platform, Message, Rename,
187+
Introduced, SourceRange(), Deprecated, SourceRange(),
188+
Obsoleted, SourceRange(), Inferred.PlatformAgnostic,
189+
/*Implicit=*/true, Inferred.IsSPI);
193190
}
194191

195192
void AvailabilityInference::applyInferredAvailableAttrs(
@@ -246,15 +243,20 @@ void AvailabilityInference::applyInferredAvailableAttrs(
246243
}
247244

248245
DeclAttributes &Attrs = ToDecl->getAttrs();
246+
auto *ToValueDecl = dyn_cast<ValueDecl>(ToDecl);
249247

250248
// Create an availability attribute for each observed platform and add
251249
// to ToDecl.
252250
for (auto &Pair : Inferred) {
253251
auto *Attr = createAvailableAttr(Pair.first, Pair.second, Message,
254252
Rename, RenameDecl, Context);
255253

256-
if (Attr)
254+
if (Attr) {
255+
if (RenameDecl && ToValueDecl)
256+
ToValueDecl->setRenamedDecl(Attr, RenameDecl);
257+
257258
Attrs.add(Attr);
259+
}
258260
}
259261
}
260262

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4236,6 +4236,14 @@ ValueDecl *ValueDecl::getRenamedDecl(const AvailableAttr *attr) const {
42364236
RenamedDeclRequest{this, attr}, nullptr);
42374237
}
42384238

4239+
void ValueDecl::setRenamedDecl(const AvailableAttr *attr,
4240+
ValueDecl *renameDecl) const {
4241+
// This is only designed to be used with decls synthesized by ClangImporter.
4242+
assert(hasClangNode());
4243+
getASTContext().evaluator.cacheNonEmptyOutput(RenamedDeclRequest{this, attr},
4244+
std::move(renameDecl));
4245+
}
4246+
42394247
SourceLoc Decl::getAttributeInsertionLoc(bool forModifier) const {
42404248
// Some decls have a parent/child split where the introducer keyword is on the
42414249
// parent, but the attributes are on the children. If this is a child in such

lib/ClangImporter/ImportDecl.cpp

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -536,20 +536,17 @@ static void applyAvailableAttribute(Decl *decl, AvailabilityRange &info,
536536
return;
537537

538538
llvm::VersionTuple noVersion;
539-
auto AvAttr = new (C) AvailableAttr(SourceLoc(), SourceRange(),
540-
targetPlatform(C.LangOpts),
541-
/*Message=*/StringRef(),
542-
/*Rename=*/StringRef(),
543-
/*RenameDecl=*/nullptr,
544-
info.getRawMinimumVersion(),
545-
/*IntroducedRange*/SourceRange(),
546-
/*Deprecated=*/noVersion,
547-
/*DeprecatedRange*/SourceRange(),
548-
/*Obsoleted=*/noVersion,
549-
/*ObsoletedRange*/SourceRange(),
550-
PlatformAgnosticAvailabilityKind::None,
551-
/*Implicit=*/false,
552-
/*SPI*/false);
539+
auto AvAttr = new (C) AvailableAttr(
540+
SourceLoc(), SourceRange(), targetPlatform(C.LangOpts),
541+
/*Message=*/StringRef(),
542+
/*Rename=*/StringRef(), info.getRawMinimumVersion(),
543+
/*IntroducedRange=*/SourceRange(),
544+
/*Deprecated=*/noVersion,
545+
/*DeprecatedRange=*/SourceRange(),
546+
/*Obsoleted=*/noVersion,
547+
/*ObsoletedRange=*/SourceRange(), PlatformAgnosticAvailabilityKind::None,
548+
/*Implicit=*/false,
549+
/*SPI=*/false);
553550

554551
decl->getAttrs().add(AvAttr);
555552
}
@@ -1316,14 +1313,13 @@ namespace {
13161313
: llvm::VersionTuple(majorVersion);
13171314
attr = new (ctx) AvailableAttr(
13181315
SourceLoc(), SourceRange(), PlatformKind::none,
1319-
/*Message*/StringRef(), ctx.AllocateCopy(renamed.str()),
1320-
/*RenameDecl=*/nullptr,
1321-
/*Introduced*/introducedVersion, SourceRange(),
1322-
/*Deprecated*/llvm::VersionTuple(), SourceRange(),
1323-
/*Obsoleted*/llvm::VersionTuple(), SourceRange(),
1316+
/*Message=*/StringRef(), ctx.AllocateCopy(renamed.str()),
1317+
/*Introduced=*/introducedVersion, SourceRange(),
1318+
/*Deprecated=*/llvm::VersionTuple(), SourceRange(),
1319+
/*Obsoleted=*/llvm::VersionTuple(), SourceRange(),
13241320
PlatformAgnosticAvailabilityKind::SwiftVersionSpecific,
1325-
/*Implicit*/false,
1326-
/*SPI*/false);
1321+
/*Implicit=*/false,
1322+
/*SPI=*/false);
13271323
}
13281324
}
13291325

@@ -2880,15 +2876,16 @@ namespace {
28802876
auto availability = Impl.SwiftContext.getSwift58Availability();
28812877
if (!availability.isAlwaysAvailable()) {
28822878
assert(availability.hasMinimumVersion());
2883-
auto AvAttr = new (Impl.SwiftContext) AvailableAttr(
2884-
SourceLoc(), SourceRange(),
2885-
targetPlatform(Impl.SwiftContext.LangOpts), "", "",
2886-
/*RenameDecl=*/nullptr, availability.getRawMinimumVersion(),
2887-
/*IntroducedRange=*/SourceRange(), {},
2888-
/*DeprecatedRange=*/SourceRange(), {},
2889-
/*ObsoletedRange=*/SourceRange(),
2890-
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/false,
2891-
false);
2879+
auto AvAttr = new (Impl.SwiftContext)
2880+
AvailableAttr(SourceLoc(), SourceRange(),
2881+
targetPlatform(Impl.SwiftContext.LangOpts),
2882+
/*Message=*/"", /*Rename=*/"",
2883+
availability.getRawMinimumVersion(),
2884+
/*IntroducedRange=*/SourceRange(), {},
2885+
/*DeprecatedRange=*/SourceRange(), {},
2886+
/*ObsoletedRange=*/SourceRange(),
2887+
PlatformAgnosticAvailabilityKind::None,
2888+
/*Implicit=*/false, false);
28922889
classDecl->getAttrs().add(AvAttr);
28932890
}
28942891
}
@@ -7883,12 +7880,28 @@ void addCompletionHandlerAttribute(Decl *asyncImport,
78837880
return;
78847881

78857882
for (auto *member : members) {
7883+
if (member == asyncImport)
7884+
continue;
7885+
7886+
auto afd = dyn_cast<AbstractFunctionDecl>(member);
7887+
if (!afd)
7888+
continue;
7889+
78867890
// Only add the attribute to functions that don't already have availability
7887-
if (member != asyncImport && isa<AbstractFunctionDecl>(member) &&
7888-
!member->getAttrs().hasAttribute<AvailableAttr>()) {
7889-
member->getAttrs().add(
7890-
AvailableAttr::createForAsyncAlternative(SwiftContext, asyncFunc));
7891-
}
7891+
if (afd->getAttrs().hasAttribute<AvailableAttr>())
7892+
continue;
7893+
7894+
llvm::VersionTuple NoVersion;
7895+
auto *attr = new (SwiftContext)
7896+
AvailableAttr(SourceLoc(), SourceRange(), PlatformKind::none,
7897+
/*Message=*/"", /*Rename=*/"", /*Introduced=*/NoVersion,
7898+
SourceRange(), /*Deprecated=*/NoVersion, SourceRange(),
7899+
/*Obsoleted=*/NoVersion, SourceRange(),
7900+
PlatformAgnosticAvailabilityKind::None, /*Implicit=*/true,
7901+
/*SPI=*/false);
7902+
7903+
afd->setRenamedDecl(attr, asyncFunc);
7904+
afd->getAttrs().add(attr);
78927905
}
78937906
}
78947907

@@ -8696,18 +8709,11 @@ void ClangImporter::Implementation::importAttributes(
86968709
if (!replacement.empty())
86978710
swiftReplacement = getSwiftNameFromClangName(replacement);
86988711

8699-
auto AvAttr = new (C) AvailableAttr(SourceLoc(), SourceRange(),
8700-
platformK.value(),
8701-
message, swiftReplacement,
8702-
/*RenameDecl=*/nullptr,
8703-
introduced,
8704-
/*IntroducedRange=*/SourceRange(),
8705-
deprecated,
8706-
/*DeprecatedRange=*/SourceRange(),
8707-
obsoleted,
8708-
/*ObsoletedRange=*/SourceRange(),
8709-
PlatformAgnostic, /*Implicit=*/false,
8710-
EnableClangSPI && IsSPI);
8712+
auto AvAttr = new (C) AvailableAttr(
8713+
SourceLoc(), SourceRange(), platformK.value(), message,
8714+
swiftReplacement, introduced, SourceRange(), deprecated,
8715+
SourceRange(), obsoleted, SourceRange(), PlatformAgnostic,
8716+
/*Implicit=*/false, EnableClangSPI && IsSPI);
87118717

87128718
MappedDecl->getAttrs().add(AvAttr);
87138719
}

lib/Parse/ParseDecl.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -632,16 +632,11 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
632632
canonicalizePlatformVersion(*PlatformKind, Obsoleted.Version);
633633
}
634634

635-
auto Attr = new (Context)
636-
AvailableAttr(AtLoc, SourceRange(AttrLoc, Tok.getLoc()),
637-
PlatformKind.value(),
638-
Message, Renamed, /*RenameDecl=*/nullptr,
639-
Introduced.Version, Introduced.Range,
640-
Deprecated.Version, Deprecated.Range,
641-
Obsoleted.Version, Obsoleted.Range,
642-
PlatformAgnostic,
643-
/*Implicit=*/false,
644-
AttrName == SPI_AVAILABLE_ATTRNAME);
635+
auto Attr = new (Context) AvailableAttr(
636+
AtLoc, SourceRange(AttrLoc, Tok.getLoc()), PlatformKind.value(), Message,
637+
Renamed, Introduced.Version, Introduced.Range, Deprecated.Version,
638+
Deprecated.Range, Obsoleted.Version, Obsoleted.Range, PlatformAgnostic,
639+
/*Implicit=*/false, AttrName == SPI_AVAILABLE_ATTRNAME);
645640
return makeParserResult(Attr);
646641

647642
}
@@ -927,7 +922,6 @@ bool Parser::parseAvailability(
927922
AtLoc, attrRange, Platform,
928923
/*Message=*/StringRef(),
929924
/*Rename=*/StringRef(),
930-
/*RenameDecl=*/nullptr,
931925
/*Introduced=*/Version,
932926
/*IntroducedRange=*/VersionRange,
933927
/*Deprecated=*/llvm::VersionTuple(),
@@ -4384,9 +4378,9 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
43844378
StringRef Message = "unavailable in embedded Swift", Renamed;
43854379
auto attr = new (Context) AvailableAttr(
43864380
AtLoc, SourceRange(AtLoc, attrLoc), PlatformKind::none, Message,
4387-
Renamed, /*RenameDecl=*/nullptr, llvm::VersionTuple(), SourceRange(),
4388-
llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4389-
SourceRange(), PlatformAgnosticAvailabilityKind::Unavailable,
4381+
Renamed, llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4382+
SourceRange(), llvm::VersionTuple(), SourceRange(),
4383+
PlatformAgnosticAvailabilityKind::Unavailable,
43904384
/*Implicit=*/false, /*IsSPI=*/false, /*IsForEmbedded=*/true);
43914385
Attributes.add(attr);
43924386
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7871,9 +7871,6 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
78717871
if (!attached || !attr)
78727872
return nullptr;
78737873

7874-
if (attr->RenameDecl)
7875-
return attr->RenameDecl;
7876-
78777874
if (attr->Rename.empty())
78787875
return nullptr;
78797876

0 commit comments

Comments
 (0)