Skip to content

Commit 2afa5ab

Browse files
authored
Merge pull request swiftlang#79385 from tshortli/availability-domain-or-identifier
AST: Introduce `AvailabilityDomainOrIdentifier`
2 parents 5c1a02a + 06030d6 commit 2afa5ab

File tree

9 files changed

+89
-84
lines changed

9 files changed

+89
-84
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -755,18 +755,17 @@ BridgedAvailableAttr BridgedAvailableAttr_createParsed(
755755
BridgedSourceRange cDeprecatedRange, BridgedVersionTuple cObsoleted,
756756
BridgedSourceRange cObsoletedRange);
757757

758-
SWIFT_NAME(
759-
"BridgedAvailableAttr.createParsed(_:atLoc:range:domainString:domainLoc:kind:message:"
760-
"renamed:introduced:introducedRange:deprecated:deprecatedRange:"
761-
"obsoleted:obsoletedRange:)")
762-
BridgedAvailableAttr BridgedAvailableAttr_createParsedStr(
758+
SWIFT_NAME("BridgedAvailableAttr.createParsed(_:atLoc:range:domainIdentifier:"
759+
"domainLoc:kind:message:renamed:introduced:introducedRange:"
760+
"deprecated:deprecatedRange:obsoleted:obsoletedRange:)")
761+
BridgedAvailableAttr BridgedAvailableAttr_createParsedIdentifier(
763762
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
764-
BridgedSourceRange cRange, BridgedStringRef cDomainString, BridgedSourceLoc cDomainLoc,
765-
BridgedAvailableAttrKind cKind, BridgedStringRef cMessage,
766-
BridgedStringRef cRenamed, BridgedVersionTuple cIntroduced,
767-
BridgedSourceRange cIntroducedRange, BridgedVersionTuple cDeprecated,
768-
BridgedSourceRange cDeprecatedRange, BridgedVersionTuple cObsoleted,
769-
BridgedSourceRange cObsoletedRange);
763+
BridgedSourceRange cRange, BridgedIdentifier cDomainIdentifier,
764+
BridgedSourceLoc cDomainLoc, BridgedAvailableAttrKind cKind,
765+
BridgedStringRef cMessage, BridgedStringRef cRenamed,
766+
BridgedVersionTuple cIntroduced, BridgedSourceRange cIntroducedRange,
767+
BridgedVersionTuple cDeprecated, BridgedSourceRange cDeprecatedRange,
768+
BridgedVersionTuple cObsoleted, BridgedSourceRange cObsoletedRange);
770769

771770
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedExecutionKind {
772771
BridgedExecutionKindConcurrent,

include/swift/AST/Attr.h

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,12 @@ class DeclAttribute : public AttributeBase {
151151
Value : 32
152152
);
153153

154-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 4+1+1+1+1+1,
154+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 4+1+1+1+1,
155155
/// An `AvailableAttr::Kind` value.
156156
Kind : 4,
157157

158158
/// State storage for `SemanticAvailableAttrRequest`.
159159
HasComputedSemanticAttr : 1,
160-
HasDomain : 1,
161160

162161
/// State storage for `RenamedDeclRequest`.
163162
HasComputedRenamedDecl : 1,
@@ -737,16 +736,8 @@ class AvailableAttr : public DeclAttribute {
737736
NoAsync,
738737
};
739738

740-
AvailableAttr(SourceLoc AtLoc, SourceRange Range, AvailabilityDomain Domain,
741-
SourceLoc DomainLoc, Kind Kind, StringRef Message,
742-
StringRef Rename, const llvm::VersionTuple &Introduced,
743-
SourceRange IntroducedRange,
744-
const llvm::VersionTuple &Deprecated,
745-
SourceRange DeprecatedRange,
746-
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
747-
bool Implicit, bool IsSPI);
748-
749-
AvailableAttr(SourceLoc AtLoc, SourceRange Range, StringRef DomainString,
739+
AvailableAttr(SourceLoc AtLoc, SourceRange Range,
740+
AvailabilityDomainOrIdentifier DomainOrIdentifier,
750741
SourceLoc DomainLoc, Kind Kind, StringRef Message,
751742
StringRef Rename, const llvm::VersionTuple &Introduced,
752743
SourceRange IntroducedRange,
@@ -758,10 +749,7 @@ class AvailableAttr : public DeclAttribute {
758749
private:
759750
friend class SemanticAvailableAttr;
760751

761-
union {
762-
AvailabilityDomain Domain;
763-
StringRef DomainString;
764-
};
752+
AvailabilityDomainOrIdentifier DomainOrIdentifier;
765753
const SourceLoc DomainLoc;
766754

767755
const StringRef Message;
@@ -777,24 +765,20 @@ class AvailableAttr : public DeclAttribute {
777765
public:
778766
/// Returns true if the `AvailabilityDomain` associated with the attribute
779767
/// has been resolved successfully.
780-
bool hasCachedDomain() const { return Bits.AvailableAttr.HasDomain; }
768+
bool hasCachedDomain() const { return DomainOrIdentifier.isDomain(); }
781769

782770
/// Returns the `AvailabilityDomain` associated with the attribute, or
783771
/// `std::nullopt` if it has either not yet been resolved or could not be
784772
/// resolved successfully.
785773
std::optional<AvailabilityDomain> getCachedDomain() const {
786-
if (hasCachedDomain())
787-
return Domain;
788-
return std::nullopt;
774+
return DomainOrIdentifier.getAsDomain();
789775
}
790776

791777
/// If the attribute does not already have a cached `AvailabilityDomain`, this
792-
/// returns the domain string that was written in source, from which an
778+
/// returns the domain identifier that was written in source, from which an
793779
/// `AvailabilityDomain` can be resolved.
794-
std::optional<StringRef> getDomainString() const {
795-
if (hasCachedDomain())
796-
return std::nullopt;
797-
return DomainString;
780+
std::optional<Identifier> getDomainIdentifier() const {
781+
return DomainOrIdentifier.getAsIdentifier();
798782
}
799783

800784
SourceLoc getDomainLoc() const { return DomainLoc; }
@@ -919,9 +903,8 @@ class AvailableAttr : public DeclAttribute {
919903
void setRawObsoleted(llvm::VersionTuple version) { Obsoleted = version; }
920904

921905
void setCachedDomain(AvailabilityDomain domain) {
922-
assert(!Bits.AvailableAttr.HasDomain);
923-
Domain = domain;
924-
Bits.AvailableAttr.HasDomain = true;
906+
assert(!DomainOrIdentifier.isDomain());
907+
DomainOrIdentifier.setDomain(domain);
925908
}
926909

927910
bool hasComputedSemanticAttr() const {

include/swift/AST/AvailabilityDomain.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,40 @@ class CustomAvailabilityDomain : public ASTAllocated<CustomAvailabilityDomain> {
277277
ModuleDecl *getModule() const { return mod; }
278278
};
279279

280+
/// Represents either a resolved availability domain or an identifier written
281+
/// in source that has not yet been resolved to a domain.
282+
class AvailabilityDomainOrIdentifier {
283+
using Storage = llvm::PointerUnion<AvailabilityDomain, Identifier>;
284+
Storage storage;
285+
286+
public:
287+
AvailabilityDomainOrIdentifier(Identifier identifier)
288+
: storage(identifier) {};
289+
AvailabilityDomainOrIdentifier(AvailabilityDomain domain)
290+
: storage(domain) {};
291+
292+
bool isDomain() const { return storage.is<AvailabilityDomain>(); }
293+
bool isIdentifier() const { return storage.is<Identifier>(); }
294+
295+
/// Overwrites the existing domain or identifier with the given domain.
296+
void setDomain(AvailabilityDomain domain) { storage = Storage(domain); }
297+
298+
/// Returns the resolved domain, or `std::nullopt` if there isn't one.
299+
std::optional<AvailabilityDomain> getAsDomain() const {
300+
if (isDomain())
301+
return storage.get<AvailabilityDomain>();
302+
return std::nullopt;
303+
}
304+
305+
/// Returns the unresolved identifier, or `std::nullopt` if the domain has
306+
/// been resolved.
307+
std::optional<Identifier> getAsIdentifier() const {
308+
if (isIdentifier())
309+
return storage.get<Identifier>();
310+
return std::nullopt;
311+
}
312+
};
313+
280314
} // end namespace swift
281315

282316
namespace llvm {

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4965,7 +4965,8 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
49654965
printField(domain->getNameForAttributePrinting(),
49664966
Label::always("domain"));
49674967
} else {
4968-
printField(*Attr->getDomainString(), Label::always("domainString"));
4968+
printField(*Attr->getDomainIdentifier(),
4969+
Label::always("domainIdentifier"));
49694970
}
49704971

49714972
switch (Attr->getKind()) {

lib/AST/Attr.cpp

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,34 +2126,19 @@ Type RawLayoutAttr::getResolvedCountType(StructDecl *sd) const {
21262126
}
21272127

21282128
AvailableAttr::AvailableAttr(
2129-
SourceLoc AtLoc, SourceRange Range, AvailabilityDomain Domain,
2130-
SourceLoc DomainLoc, Kind Kind, StringRef Message, StringRef Rename,
2129+
SourceLoc AtLoc, SourceRange Range,
2130+
AvailabilityDomainOrIdentifier DomainOrIdentifier, SourceLoc DomainLoc,
2131+
Kind Kind, StringRef Message, StringRef Rename,
21312132
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
21322133
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
21332134
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
21342135
bool Implicit, bool IsSPI)
21352136
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
2136-
Domain(Domain), DomainLoc(DomainLoc), Message(Message), Rename(Rename),
2137-
Introduced(Introduced), IntroducedRange(IntroducedRange),
2138-
Deprecated(Deprecated), DeprecatedRange(DeprecatedRange),
2139-
Obsoleted(Obsoleted), ObsoletedRange(ObsoletedRange) {
2140-
Bits.AvailableAttr.Kind = static_cast<uint8_t>(Kind);
2141-
Bits.AvailableAttr.HasDomain = true;
2142-
Bits.AvailableAttr.IsSPI = IsSPI;
2143-
}
2144-
2145-
AvailableAttr::AvailableAttr(
2146-
SourceLoc AtLoc, SourceRange Range, StringRef DomainString,
2147-
SourceLoc DomainLoc, Kind Kind, StringRef Message, StringRef Rename,
2148-
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
2149-
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
2150-
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
2151-
bool Implicit, bool IsSPI)
2152-
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
2153-
DomainString(DomainString), DomainLoc(DomainLoc), Message(Message),
2154-
Rename(Rename), Introduced(Introduced), IntroducedRange(IntroducedRange),
2155-
Deprecated(Deprecated), DeprecatedRange(DeprecatedRange),
2156-
Obsoleted(Obsoleted), ObsoletedRange(ObsoletedRange) {
2137+
DomainOrIdentifier(DomainOrIdentifier), DomainLoc(DomainLoc),
2138+
Message(Message), Rename(Rename), Introduced(Introduced),
2139+
IntroducedRange(IntroducedRange), Deprecated(Deprecated),
2140+
DeprecatedRange(DeprecatedRange), Obsoleted(Obsoleted),
2141+
ObsoletedRange(ObsoletedRange) {
21572142
Bits.AvailableAttr.Kind = static_cast<uint8_t>(Kind);
21582143
Bits.AvailableAttr.IsSPI = IsSPI;
21592144
}
@@ -2225,9 +2210,9 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
22252210
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
22262211
return new (C) AvailableAttr(
22272212
implicit ? SourceLoc() : AtLoc, implicit ? SourceRange() : getRange(),
2228-
Domain, implicit ? SourceLoc() : DomainLoc, getKind(), Message, Rename,
2229-
Introduced, implicit ? SourceRange() : IntroducedRange, Deprecated,
2230-
implicit ? SourceRange() : DeprecatedRange, Obsoleted,
2213+
DomainOrIdentifier, implicit ? SourceLoc() : DomainLoc, getKind(),
2214+
Message, Rename, Introduced, implicit ? SourceRange() : IntroducedRange,
2215+
Deprecated, implicit ? SourceRange() : DeprecatedRange, Obsoleted,
22312216
implicit ? SourceRange() : ObsoletedRange, implicit, isSPI());
22322217
}
22332218

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,18 @@ BridgedAvailableAttr BridgedAvailableAttr_createParsed(
106106
/*IsSPI=*/false);
107107
}
108108

109-
BridgedAvailableAttr BridgedAvailableAttr_createParsedStr(
109+
BridgedAvailableAttr BridgedAvailableAttr_createParsedIdentifier(
110110
BridgedASTContext cContext, BridgedSourceLoc cAtLoc,
111-
BridgedSourceRange cRange, BridgedStringRef cDomainString,
111+
BridgedSourceRange cRange, BridgedIdentifier cDomainIdentifier,
112112
BridgedSourceLoc cDomainLoc, BridgedAvailableAttrKind cKind,
113113
BridgedStringRef cMessage, BridgedStringRef cRenamed,
114114
BridgedVersionTuple cIntroduced, BridgedSourceRange cIntroducedRange,
115115
BridgedVersionTuple cDeprecated, BridgedSourceRange cDeprecatedRange,
116116
BridgedVersionTuple cObsoleted, BridgedSourceRange cObsoletedRange) {
117+
117118
return new (cContext.unbridged())
118119
AvailableAttr(cAtLoc.unbridged(), cRange.unbridged(),
119-
cDomainString.unbridged(), cDomainLoc.unbridged(),
120+
cDomainIdentifier.unbridged(), cDomainLoc.unbridged(),
120121
unbridge(cKind), cMessage.unbridged(), cRenamed.unbridged(),
121122
cIntroduced.unbridged(), cIntroducedRange.unbridged(),
122123
cDeprecated.unbridged(), cDeprecatedRange.unbridged(),

lib/ASTGen/Sources/ASTGen/Availability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ extension ASTGenVisitor {
234234
self.ctx,
235235
atLoc: atLoc,
236236
range: range,
237-
domainString: platformStr.bridged,
237+
domainIdentifier: self.ctx.getIdentifier(platformStr.bridged),
238238
domainLoc: platformLoc,
239239
kind: attrKind,
240240
message: message ?? BridgedStringRef(),

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,10 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
548548
return nullptr;
549549

550550
auto Attr = new (Context) AvailableAttr(
551-
AtLoc, SourceRange(AttrLoc, Tok.getLoc()), Platform, PlatformLoc,
552-
AttrKind, Message, Renamed, Introduced.Version, Introduced.Range,
553-
Deprecated.Version, Deprecated.Range, Obsoleted.Version, Obsoleted.Range,
551+
AtLoc, SourceRange(AttrLoc, Tok.getLoc()),
552+
Context.getIdentifier(Platform), PlatformLoc, AttrKind, Message, Renamed,
553+
Introduced.Version, Introduced.Range, Deprecated.Version,
554+
Deprecated.Range, Obsoleted.Version, Obsoleted.Range,
554555
/*Implicit=*/false, AttrName == SPI_AVAILABLE_ATTRNAME);
555556
return makeParserResult(Attr);
556557

lib/Sema/TypeCheckAttr.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8326,13 +8326,13 @@ ValueDecl *RenamedDeclRequest::evaluate(Evaluator &evaluator,
83268326
}
83278327

83288328
static std::optional<AvailabilityDomain>
8329-
getAvailabilityDomainForName(StringRef name, const DeclContext *declContext) {
8330-
if (auto builtinDomain =
8331-
AvailabilityDomain::builtinDomainForString(name, declContext))
8329+
getAvailabilityDomainForName(Identifier identifier,
8330+
const DeclContext *declContext) {
8331+
if (auto builtinDomain = AvailabilityDomain::builtinDomainForString(
8332+
identifier.str(), declContext))
83328333
return builtinDomain;
83338334

83348335
auto &ctx = declContext->getASTContext();
8335-
auto identifier = ctx.getIdentifier(name);
83368336
if (auto customDomain =
83378337
ctx.MainModule->getAvailabilityDomainForIdentifier(identifier))
83388338
return customDomain;
@@ -8359,22 +8359,23 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
83598359
auto domain = attr->getCachedDomain();
83608360

83618361
if (!domain) {
8362-
auto domainName = attr->getDomainString();
8363-
ASSERT(domainName);
8362+
auto domainIdentifier = attr->getDomainIdentifier();
8363+
ASSERT(domainIdentifier);
83648364

83658365
// Attempt to resolve the domain specified for the attribute and diagnose
83668366
// if no domain is found.
83678367
auto declContext = decl->getInnermostDeclContext();
8368-
domain = getAvailabilityDomainForName(*domainName, declContext);
8368+
domain = getAvailabilityDomainForName(*domainIdentifier, declContext);
83698369
if (!domain) {
8370-
if (auto suggestion = closestCorrectedPlatformString(*domainName)) {
8370+
auto domainString = domainIdentifier->str();
8371+
if (auto suggestion = closestCorrectedPlatformString(domainString)) {
83718372
diags
83728373
.diagnose(domainLoc, diag::attr_availability_suggest_platform,
8373-
*domainName, attrName, *suggestion)
8374+
domainString, attrName, *suggestion)
83748375
.fixItReplace(SourceRange(domainLoc), *suggestion);
83758376
} else {
83768377
diags.diagnose(attrLoc, diag::attr_availability_unknown_platform,
8377-
*domainName, attrName);
8378+
domainString, attrName);
83788379
}
83798380
return std::nullopt;
83808381
}
@@ -8384,7 +8385,7 @@ SemanticAvailableAttrRequest::evaluate(swift::Evaluator &evaluator,
83848385
!declContext->isInSwiftinterface()) {
83858386
diags.diagnose(domainLoc,
83868387
diag::attr_availability_requires_custom_availability,
8387-
*domainName, attr);
8388+
domain->getNameForAttributePrinting(), attr);
83888389
return std::nullopt;
83898390
}
83908391

0 commit comments

Comments
 (0)