Skip to content

Commit 40b3fcc

Browse files
authored
Merge pull request #78743 from tshortli/embedded-swift-availability-domain
AST: Model Embedded Swift availability with a domain
2 parents a48f498 + 691f6fc commit 40b3fcc

File tree

8 files changed

+40
-37
lines changed

8 files changed

+40
-37
lines changed

include/swift/AST/Attr.h

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

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

@@ -160,10 +160,7 @@ class DeclAttribute : public AttributeBase {
160160
HasRenamedDecl : 1,
161161

162162
/// Whether this attribute was spelled `@_spi_available`.
163-
IsSPI : 1,
164-
165-
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
166-
IsForEmbedded : 1
163+
IsSPI : 1
167164
);
168165

169166
SWIFT_INLINE_BITFIELD(ClangImporterSynthesizedTypeAttr, DeclAttribute, 1,
@@ -741,7 +738,7 @@ class AvailableAttr : public DeclAttribute {
741738
const llvm::VersionTuple &Deprecated,
742739
SourceRange DeprecatedRange,
743740
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
744-
bool Implicit, bool IsSPI, bool IsForEmbedded = false);
741+
bool Implicit, bool IsSPI);
745742

746743
/// The optional message.
747744
const StringRef Message;
@@ -789,9 +786,6 @@ class AvailableAttr : public DeclAttribute {
789786
/// Whether this attribute was spelled `@_spi_available`.
790787
bool isSPI() const { return Bits.AvailableAttr.IsSPI; }
791788

792-
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
793-
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
794-
795789
/// Returns the `AvailabilityDomain` associated with the attribute, or
796790
/// `std::nullopt` if it has either not yet been resolved or could not be
797791
/// resolved successfully.
@@ -3277,8 +3271,8 @@ class SemanticAvailableAttr final {
32773271
return getDomain().isPackageDescription() && isVersionSpecific();
32783272
}
32793273

3280-
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
3281-
bool isEmbeddedSpecific() const { return attr->isForEmbedded(); }
3274+
/// Whether this attribute an attribute that is specific to Embedded Swift.
3275+
bool isEmbeddedSpecific() const { return getDomain().isEmbedded(); }
32823276

32833277
/// Returns the active version from the AST context corresponding to
32843278
/// the available kind. For example, this will return the effective language

include/swift/AST/AvailabilityDomain.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class AvailabilityDomain final {
4141
/// Represents PackageDescription availability.
4242
PackageDescription,
4343

44+
/// Represents Embedded Swift availability.
45+
Embedded,
46+
4447
/// Represents availability for a specific operating system platform.
4548
Platform,
4649
};
@@ -132,6 +135,10 @@ class AvailabilityDomain final {
132135
return AvailabilityDomain(Kind::PackageDescription);
133136
}
134137

138+
static AvailabilityDomain forEmbedded() {
139+
return AvailabilityDomain(Kind::Embedded);
140+
}
141+
135142
Kind getKind() const {
136143
if (auto inlineDomain = getInlineDomain())
137144
return inlineDomain->getKind();
@@ -149,6 +156,8 @@ class AvailabilityDomain final {
149156
return getKind() == Kind::PackageDescription;
150157
}
151158

159+
bool isEmbedded() const { return getKind() == Kind::Embedded; }
160+
152161
/// Returns the platform kind for this domain if applicable.
153162
PlatformKind getPlatformKind() const {
154163
if (auto inlineDomain = getInlineDomain())
@@ -184,6 +193,7 @@ class AvailabilityDomain final {
184193
case Kind::Universal:
185194
case Kind::SwiftLanguage:
186195
case Kind::PackageDescription:
196+
case Kind::Embedded:
187197
// These availability domains are singletons.
188198
return false;
189199
case Kind::Platform:

lib/AST/Attr.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
11871187
Printer << ", unavailable)";
11881188
break;
11891189
}
1190-
if (Attr->getParsedAttr()->isForEmbedded()) {
1190+
if (Attr->isEmbeddedSpecific()) {
11911191
std::string atUnavailableInEmbedded =
11921192
(llvm::Twine("@") + UNAVAILABLE_IN_EMBEDDED_ATTRNAME).str();
11931193
Printer.printAttrName(atUnavailableInEmbedded);
@@ -2094,7 +2094,7 @@ AvailableAttr::AvailableAttr(
20942094
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
20952095
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
20962096
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
2097-
bool Implicit, bool IsSPI, bool IsForEmbedded)
2097+
bool Implicit, bool IsSPI)
20982098
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
20992099
Domain(Domain), Message(Message), Rename(Rename),
21002100
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
@@ -2104,13 +2104,6 @@ AvailableAttr::AvailableAttr(
21042104
Bits.AvailableAttr.HasComputedRenamedDecl = false;
21052105
Bits.AvailableAttr.HasRenamedDecl = false;
21062106
Bits.AvailableAttr.IsSPI = IsSPI;
2107-
2108-
if (IsForEmbedded) {
2109-
// FIXME: [availability] The IsForEmbedded bit should be removed when
2110-
// it can be represented with AvailabilityDomain (rdar://138802876)
2111-
Bits.AvailableAttr.IsForEmbedded = true;
2112-
assert(Domain.isUniversal());
2113-
}
21142107
}
21152108

21162109
#undef INIT_VER_TUPLE
@@ -2180,16 +2173,15 @@ bool BackDeployedAttr::isActivePlatform(const ASTContext &ctx,
21802173
}
21812174

21822175
AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
2183-
return new (C) AvailableAttr(implicit ? SourceLoc() : AtLoc,
2184-
implicit ? SourceRange() : getRange(), Domain,
2185-
getKind(), Message, Rename,
2186-
Introduced ? *Introduced : llvm::VersionTuple(),
2187-
implicit ? SourceRange() : IntroducedRange,
2188-
Deprecated ? *Deprecated : llvm::VersionTuple(),
2189-
implicit ? SourceRange() : DeprecatedRange,
2190-
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2191-
implicit ? SourceRange() : ObsoletedRange,
2192-
implicit, isSPI(), isForEmbedded());
2176+
return new (C) AvailableAttr(
2177+
implicit ? SourceLoc() : AtLoc, implicit ? SourceRange() : getRange(),
2178+
Domain, getKind(), Message, Rename,
2179+
Introduced ? *Introduced : llvm::VersionTuple(),
2180+
implicit ? SourceRange() : IntroducedRange,
2181+
Deprecated ? *Deprecated : llvm::VersionTuple(),
2182+
implicit ? SourceRange() : DeprecatedRange,
2183+
Obsoleted ? *Obsoleted : llvm::VersionTuple(),
2184+
implicit ? SourceRange() : ObsoletedRange, implicit, isSPI());
21932185
}
21942186

21952187
std::optional<OriginallyDefinedInAttr::ActiveVersion>

lib/AST/AvailabilityDomain.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bool AvailabilityDomain::isActive(const ASTContext &ctx) const {
2121
case Kind::Universal:
2222
case Kind::SwiftLanguage:
2323
case Kind::PackageDescription:
24+
case Kind::Embedded:
2425
return true;
2526
case Kind::Platform:
2627
return isPlatformActive(getPlatformKind(), ctx.LangOpts);
@@ -35,6 +36,8 @@ llvm::StringRef AvailabilityDomain::getNameForDiagnostics() const {
3536
return "Swift";
3637
case Kind::PackageDescription:
3738
return "PackageDescription";
39+
case Kind::Embedded:
40+
return "Embedded Swift";
3841
case Kind::Platform:
3942
return swift::prettyPlatformString(getPlatformKind());
4043
}
@@ -48,6 +51,8 @@ llvm::StringRef AvailabilityDomain::getNameForAttributePrinting() const {
4851
return "swift";
4952
case Kind::PackageDescription:
5053
return "_PackageDescription";
54+
case Kind::Embedded:
55+
return "Embedded";
5156
case Kind::Platform:
5257
return swift::platformString(getPlatformKind());
5358
}

lib/Parse/ParseDecl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,12 +4392,11 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
43924392
if (Context.LangOpts.hasFeature(Feature::Embedded)) {
43934393
StringRef Message = "unavailable in embedded Swift", Renamed;
43944394
auto attr = new (Context) AvailableAttr(
4395-
AtLoc, SourceRange(AtLoc, attrLoc),
4396-
AvailabilityDomain::forUniversal(), AvailableAttr::Kind::Unavailable,
4397-
Message, Renamed, llvm::VersionTuple(), SourceRange(),
4395+
AtLoc, SourceRange(AtLoc, attrLoc), AvailabilityDomain::forEmbedded(),
4396+
AvailableAttr::Kind::Unavailable, Message, Renamed,
43984397
llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4399-
SourceRange(),
4400-
/*Implicit=*/false, /*IsSPI=*/false, /*IsForEmbedded=*/true);
4398+
SourceRange(), llvm::VersionTuple(), SourceRange(),
4399+
/*Implicit=*/false, /*IsSPI=*/false);
44014400
Attributes.add(attr);
44024401
}
44034402
return makeParserSuccess();

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,7 @@ class UnavailabilityDiagnosticInfo {
30283028
bool shouldHideDomainNameInUnversionedDiagnostics() const {
30293029
switch (getDomain().getKind()) {
30303030
case AvailabilityDomain::Kind::Universal:
3031+
case AvailabilityDomain::Kind::Embedded:
30313032
return true;
30323033
case AvailabilityDomain::Kind::Platform:
30333034
return false;

lib/Serialization/Deserialization.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5636,14 +5636,16 @@ DeclDeserializer::readAvailable_DECL_ATTR(SmallVectorImpl<uint64_t> &scratch,
56365636
domain = isPackageDescriptionVersionSpecific
56375637
? AvailabilityDomain::forPackageDescription()
56385638
: AvailabilityDomain::forSwiftLanguage();
5639+
} else if (isForEmbedded) {
5640+
domain = AvailabilityDomain::forEmbedded();
56395641
} else {
56405642
domain = AvailabilityDomain::forUniversal();
56415643
}
56425644

56435645
auto attr = new (ctx)
56445646
AvailableAttr(SourceLoc(), SourceRange(), domain, kind, message, rename,
56455647
Introduced, SourceRange(), Deprecated, SourceRange(),
5646-
Obsoleted, SourceRange(), isImplicit, isSPI, isForEmbedded);
5648+
Obsoleted, SourceRange(), isImplicit, isSPI);
56475649
return attr;
56485650
}
56495651

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3074,7 +3074,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30743074
theAttr->isNoAsync(),
30753075
domain->isPackageDescription(),
30763076
theAttr->isSPI(),
3077-
theAttr->isForEmbedded(),
3077+
domain->isEmbedded(),
30783078
LIST_VER_TUPLE_PIECES(Introduced),
30793079
LIST_VER_TUPLE_PIECES(Deprecated),
30803080
LIST_VER_TUPLE_PIECES(Obsoleted),

0 commit comments

Comments
 (0)