Skip to content

Commit 52b89a8

Browse files
authored
Merge pull request #78805 from tshortli/private-available-attr-fields
AST: Eliminate remaining direct access to AvailableAttr's fields
2 parents 90cbaec + ba822d1 commit 52b89a8

File tree

8 files changed

+97
-83
lines changed

8 files changed

+97
-83
lines changed

include/swift/AST/Attr.h

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,6 @@ enum class AvailableVersionComparison {
719719

720720
/// Defines the @available attribute.
721721
class AvailableAttr : public DeclAttribute {
722-
AvailabilityDomain Domain;
723-
724722
public:
725723
enum class Kind : uint8_t {
726724
/// The attribute does not specify `deprecated`, `unavailable`,
@@ -744,11 +742,44 @@ class AvailableAttr : public DeclAttribute {
744742
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
745743
bool Implicit, bool IsSPI);
746744

747-
/// The optional message.
745+
private:
746+
friend class SemanticAvailableAttr;
747+
748+
AvailabilityDomain Domain;
749+
748750
const StringRef Message;
751+
const StringRef Rename;
752+
753+
const std::optional<llvm::VersionTuple> Introduced;
754+
const SourceRange IntroducedRange;
755+
const std::optional<llvm::VersionTuple> Deprecated;
756+
const SourceRange DeprecatedRange;
757+
const std::optional<llvm::VersionTuple> Obsoleted;
758+
const SourceRange ObsoletedRange;
759+
760+
public:
761+
/// Returns the parsed version for `introduced:`.
762+
std::optional<llvm::VersionTuple> getRawIntroduced() const {
763+
return Introduced;
764+
}
765+
766+
/// Returns the parsed version for `deprecated:`.
767+
std::optional<llvm::VersionTuple> getRawDeprecated() const {
768+
return Deprecated;
769+
}
770+
771+
/// Returns the parsed version for `obsoleted:`.
772+
std::optional<llvm::VersionTuple> getRawObsoleted() const {
773+
return Obsoleted;
774+
}
749775

750-
/// An optional replacement string to emit in a fixit. This allows simple
751-
/// declaration renames to be applied by Xcode.
776+
/// Returns the parsed string for `message:`, which will be presented with
777+
/// diagnostics about the availability of the decl.
778+
StringRef getMessage() const { return Message; }
779+
780+
/// Returns the parsed string for `rename:`, which is an optional replacement
781+
/// string to emit in a fixit. This allows simple declaration renames to be
782+
/// applied by Xcode.
752783
///
753784
/// This should take the form of an operator, identifier, or full function
754785
/// name, optionally with a prefixed type, similar to the syntax used for
@@ -758,25 +789,7 @@ class AvailableAttr : public DeclAttribute {
758789
/// referred to by this string. Note that this attribute can have a rename
759790
/// target that was provided directly when synthesized and therefore can have
760791
/// a rename decl even when this string is empty.
761-
const StringRef Rename;
762-
763-
/// Indicates when the symbol was introduced.
764-
const std::optional<llvm::VersionTuple> Introduced;
765-
766-
/// Indicates where the Introduced version was specified.
767-
const SourceRange IntroducedRange;
768-
769-
/// Indicates when the symbol was deprecated.
770-
const std::optional<llvm::VersionTuple> Deprecated;
771-
772-
/// Indicates where the Deprecated version was specified.
773-
const SourceRange DeprecatedRange;
774-
775-
/// Indicates when the symbol was obsoleted.
776-
const std::optional<llvm::VersionTuple> Obsoleted;
777-
778-
/// Indicates where the Obsoleted version was specified.
779-
const SourceRange ObsoletedRange;
792+
StringRef getRename() const { return Rename; }
780793

781794
/// Whether this is an unconditionally unavailable entity.
782795
bool isUnconditionallyUnavailable() const;
@@ -3242,6 +3255,9 @@ class SemanticAvailableAttr final {
32423255
return attr->Introduced;
32433256
}
32443257

3258+
/// The source range of the `introduced:` component.
3259+
SourceRange getIntroducedSourceRange() const { return attr->IntroducedRange; }
3260+
32453261
/// Returns the effective range in which the declaration with this attribute
32463262
/// was introduced.
32473263
AvailabilityRange getIntroducedRange(const ASTContext &Ctx) const;

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,11 +1361,10 @@ static bool isABIPlaceholderRecursive(Decl *D) {
13611361
StringRef SDKContext::getPlatformIntroVersion(Decl *D, PlatformKind Kind) {
13621362
if (!D)
13631363
return StringRef();
1364-
for (auto semanticAttr : D->getSemanticAvailableAttrs()) {
1365-
auto attr = semanticAttr.getParsedAttr();
1366-
auto domain = semanticAttr.getDomain();
1367-
if (domain.getPlatformKind() == Kind && attr->Introduced) {
1368-
return buffer(attr->Introduced->getAsString());
1364+
for (auto attr : D->getSemanticAvailableAttrs()) {
1365+
auto domain = attr.getDomain();
1366+
if (domain.getPlatformKind() == Kind && attr.getIntroduced()) {
1367+
return buffer(attr.getIntroduced()->getAsString());
13691368
}
13701369
}
13711370
return StringRef();
@@ -1374,12 +1373,11 @@ StringRef SDKContext::getPlatformIntroVersion(Decl *D, PlatformKind Kind) {
13741373
StringRef SDKContext::getLanguageIntroVersion(Decl *D) {
13751374
if (!D)
13761375
return StringRef();
1377-
for (auto semanticAttr : D->getSemanticAvailableAttrs()) {
1378-
auto attr = semanticAttr.getParsedAttr();
1379-
auto domain = semanticAttr.getDomain();
1376+
for (auto attr : D->getSemanticAvailableAttrs()) {
1377+
auto domain = attr.getDomain();
13801378

1381-
if (domain.isSwiftLanguage() && attr->Introduced) {
1382-
return buffer(attr->Introduced->getAsString());
1379+
if (domain.isSwiftLanguage() && attr.getIntroduced()) {
1380+
return buffer(attr.getIntroduced()->getAsString());
13831381
}
13841382
}
13851383
return getLanguageIntroVersion(D->getDeclContext()->getAsDecl());

lib/AST/ASTDumper.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,22 +3928,19 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, StringRef>,
39283928
printFlag("noasync");
39293929
break;
39303930
}
3931-
if (Attr->Introduced.has_value())
3932-
printFieldRaw(
3933-
[&](auto &out) { out << Attr->Introduced.value().getAsString(); },
3934-
"introduced");
3935-
if (Attr->Deprecated.has_value())
3936-
printFieldRaw(
3937-
[&](auto &out) { out << Attr->Deprecated.value().getAsString(); },
3938-
"deprecated");
3939-
if (Attr->Obsoleted.has_value())
3940-
printFieldRaw(
3941-
[&](auto &out) { out << Attr->Obsoleted.value().getAsString(); },
3942-
"obsoleted");
3943-
if (!Attr->Message.empty())
3944-
printFieldQuoted(Attr->Message, "message");
3945-
if (!Attr->Rename.empty())
3946-
printFieldQuoted(Attr->Rename, "rename");
3931+
if (auto introduced = Attr->getRawIntroduced())
3932+
printFieldRaw([&](auto &out) { out << introduced.value().getAsString(); },
3933+
"introduced");
3934+
if (auto deprecated = Attr->getRawDeprecated())
3935+
printFieldRaw([&](auto &out) { out << deprecated.value().getAsString(); },
3936+
"deprecated");
3937+
if (auto obsoleted = Attr->getRawObsoleted())
3938+
printFieldRaw([&](auto &out) { out << obsoleted.value().getAsString(); },
3939+
"obsoleted");
3940+
if (!Attr->getMessage().empty())
3941+
printFieldQuoted(Attr->getMessage(), "message");
3942+
if (!Attr->getRename().empty())
3943+
printFieldQuoted(Attr->getRename(), "rename");
39473944
printFoot();
39483945
}
39493946
void visitBackDeployedAttr(BackDeployedAttr *Attr, StringRef label) {

lib/AST/Attr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ isShortFormAvailabilityImpliedByOther(SemanticAvailableAttr Attr,
471471
if (!inheritsAvailabilityFromPlatform(platform, otherPlatform))
472472
continue;
473473

474-
if (Attr.getParsedAttr()->Introduced == other.getParsedAttr()->Introduced)
474+
if (Attr.getIntroduced() == other.getIntroduced())
475475
return true;
476476
}
477477
return false;
@@ -495,14 +495,14 @@ static void printShortFormAvailable(const Decl *D,
495495

496496
bool isFirst = true;
497497
bool isPlatformAvailability = false;
498-
for (auto semanticAttr : Attrs) {
499-
auto *availAttr = semanticAttr.getParsedAttr();
500-
auto domain = semanticAttr.getDomain();
501-
assert(availAttr->Introduced.has_value());
498+
for (auto attr : Attrs) {
499+
auto domain = attr.getDomain();
500+
auto introduced = attr.getIntroduced();
501+
assert(introduced);
502502

503503
// Avoid omitting available attribute when we are printing module interface.
504504
if (!Options.IsForSwiftInterface &&
505-
isShortFormAvailabilityImpliedByOther(semanticAttr, Attrs))
505+
isShortFormAvailabilityImpliedByOther(attr, Attrs))
506506
continue;
507507

508508
Printer << (isFirst ? "" : ", ");
@@ -512,7 +512,7 @@ static void printShortFormAvailable(const Decl *D,
512512
isPlatformAvailability = true;
513513

514514
Printer << domain.getNameForAttributePrinting() << " "
515-
<< availAttr->Introduced.value().getAsString();
515+
<< introduced.value().getAsString();
516516
}
517517

518518
if (isPlatformAvailability)

lib/AST/Availability.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,14 @@ std::optional<SemanticAvailableAttr> Decl::getNoAsyncAttr() const {
557557

558558
bool Decl::isUnavailableInCurrentSwiftVersion() const {
559559
llvm::VersionTuple vers = getASTContext().LangOpts.EffectiveLanguageVersion;
560-
for (auto semanticAttr :
561-
getSemanticAvailableAttrs(/*includingInactive=*/false)) {
562-
if (semanticAttr.isSwiftLanguageModeSpecific()) {
563-
auto attr = semanticAttr.getParsedAttr();
564-
if (attr->Introduced.has_value() && attr->Introduced.value() > vers)
560+
for (auto attr : getSemanticAvailableAttrs(/*includingInactive=*/false)) {
561+
if (attr.isSwiftLanguageModeSpecific()) {
562+
auto introduced = attr.getIntroduced();
563+
if (introduced && *introduced > vers)
565564
return true;
566-
if (attr->Obsoleted.has_value() && attr->Obsoleted.value() <= vers)
565+
566+
auto obsoleted = attr.getObsoleted();
567+
if (obsoleted && *obsoleted <= vers)
567568
return true;
568569
}
569570
}

lib/AST/AvailabilityScope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ getAvailabilityConditionVersionSourceRange(const Decl *D, PlatformKind Platform,
307307
if (Range.isValid())
308308
return SourceRange();
309309
else
310-
Range = Attr.getParsedAttr()->IntroducedRange;
310+
Range = Attr.getIntroducedSourceRange();
311311
}
312312
}
313313
return Range;

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,11 +963,11 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current,
963963
public:
964964
static AvailabilityRange from(const ValueDecl *VD) {
965965
AvailabilityRange result;
966-
for (auto semanticAttr : VD->getSemanticAvailableAttrs()) {
967-
if (semanticAttr.isSwiftLanguageModeSpecific()) {
968-
if (auto introduced = semanticAttr.getParsedAttr()->Introduced)
966+
for (auto attr : VD->getSemanticAvailableAttrs()) {
967+
if (attr.isSwiftLanguageModeSpecific()) {
968+
if (auto introduced = attr.getIntroduced())
969969
result.introduced = introduced;
970-
if (auto obsoleted = semanticAttr.getParsedAttr()->Obsoleted)
970+
if (auto obsoleted = attr.getObsoleted())
971971
result.obsoleted = obsoleted;
972972
}
973973
}

lib/Serialization/Serialization.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,35 +3060,37 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30603060
}
30613061

30623062
case DeclAttrKind::Available: {
3063-
auto *theAttr = cast<AvailableAttr>(DA);
3064-
ENCODE_VER_TUPLE(Introduced, theAttr->Introduced)
3065-
ENCODE_VER_TUPLE(Deprecated, theAttr->Deprecated)
3066-
ENCODE_VER_TUPLE(Obsoleted, theAttr->Obsoleted)
3063+
auto theAttr = D->getSemanticAvailableAttr(cast<AvailableAttr>(DA));
3064+
assert(theAttr);
30673065

3068-
assert(theAttr->Rename.empty() || !theAttr->hasCachedRenamedDecl());
3069-
assert(theAttr->hasCachedDomain());
3070-
auto domain = theAttr->getCachedDomain();
3066+
ENCODE_VER_TUPLE(Introduced, theAttr->getIntroduced())
3067+
ENCODE_VER_TUPLE(Deprecated, theAttr->getDeprecated())
3068+
ENCODE_VER_TUPLE(Obsoleted, theAttr->getObsoleted())
3069+
3070+
assert(theAttr->getRename().empty() ||
3071+
!theAttr->getParsedAttr()->hasCachedRenamedDecl());
3072+
auto domain = theAttr->getDomain();
30713073

30723074
// FIXME: [availability] Serialize domain and kind directly.
30733075
llvm::SmallString<32> blob;
3074-
blob.append(theAttr->Message);
3075-
blob.append(theAttr->Rename);
3076+
blob.append(theAttr->getMessage());
3077+
blob.append(theAttr->getRename());
30763078
auto abbrCode = S.DeclTypeAbbrCodes[AvailableDeclAttrLayout::Code];
30773079
AvailableDeclAttrLayout::emitRecord(
30783080
S.Out, S.ScratchRecord, abbrCode,
3079-
theAttr->isImplicit(),
3081+
theAttr->getParsedAttr()->isImplicit(),
30803082
theAttr->isUnconditionallyUnavailable(),
30813083
theAttr->isUnconditionallyDeprecated(),
30823084
theAttr->isNoAsync(),
3083-
domain->isPackageDescription(),
3085+
domain.isPackageDescription(),
30843086
theAttr->isSPI(),
3085-
domain->isEmbedded(),
3087+
domain.isEmbedded(),
30863088
LIST_VER_TUPLE_PIECES(Introduced),
30873089
LIST_VER_TUPLE_PIECES(Deprecated),
30883090
LIST_VER_TUPLE_PIECES(Obsoleted),
3089-
static_cast<unsigned>(domain->getPlatformKind()),
3090-
theAttr->Message.size(),
3091-
theAttr->Rename.size(),
3091+
static_cast<unsigned>(domain.getPlatformKind()),
3092+
theAttr->getMessage().size(),
3093+
theAttr->getRename().size(),
30923094
blob);
30933095
return;
30943096
}

0 commit comments

Comments
 (0)