Skip to content

Commit 548c980

Browse files
committed
AST: Add an IsForEmbedded bit to AvailableAttr.
This bit is true for attributes written as `@_unavailableInEmbedded` in source.
1 parent 177267c commit 548c980

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

include/swift/AST/Attr.h

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,12 @@ class DeclAttribute : public AttributeBase {
147147
Value : 32
148148
);
149149

150-
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 1,
150+
SWIFT_INLINE_BITFIELD(AvailableAttr, DeclAttribute, 1+1,
151151
/// Whether this attribute was spelled `@_spi_available`.
152-
IsSPI : 1
152+
IsSPI : 1,
153+
154+
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
155+
IsForEmbedded : 1
153156
);
154157

155158
SWIFT_INLINE_BITFIELD(ClangImporterSynthesizedTypeAttr, DeclAttribute, 1,
@@ -679,8 +682,6 @@ enum class PlatformAgnosticAvailabilityKind {
679682
/// Defines the @available attribute.
680683
class AvailableAttr : public DeclAttribute {
681684
public:
682-
#define INIT_VER_TUPLE(X) X(X.empty() ? std::optional<llvm::VersionTuple>() : X)
683-
684685
AvailableAttr(SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
685686
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
686687
const llvm::VersionTuple &Introduced,
@@ -689,17 +690,7 @@ class AvailableAttr : public DeclAttribute {
689690
SourceRange DeprecatedRange,
690691
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
691692
PlatformAgnosticAvailabilityKind PlatformAgnostic,
692-
bool Implicit, bool IsSPI)
693-
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
694-
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
695-
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
696-
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
697-
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
698-
PlatformAgnostic(PlatformAgnostic), Platform(Platform) {
699-
Bits.AvailableAttr.IsSPI = IsSPI;
700-
}
701-
702-
#undef INIT_VER_TUPLE
693+
bool Implicit, bool IsSPI, bool IsForEmbedded = false);
703694

704695
/// The optional message.
705696
const StringRef Message;
@@ -760,6 +751,9 @@ class AvailableAttr : public DeclAttribute {
760751
/// Whether this attribute was spelled `@_spi_available`.
761752
bool isSPI() const { return Bits.AvailableAttr.IsSPI; }
762753

754+
/// Whether this attribute was spelled `@_unavailableInEmbedded`.
755+
bool isForEmbedded() const { return Bits.AvailableAttr.IsForEmbedded; }
756+
763757
/// Returns the platform-agnostic availability.
764758
PlatformAgnosticAvailabilityKind getPlatformAgnosticAvailability() const {
765759
return PlatformAgnostic;

include/swift/Strings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ constexpr static const StringLiteral CLANG_MODULE_DEFAULT_SPI_GROUP_NAME =
8585
constexpr static const StringLiteral SPI_AVAILABLE_ATTRNAME =
8686
"_spi_available";
8787

88+
/// The attribute name for @_spi_available
89+
constexpr static const StringLiteral UNAVAILABLE_IN_EMBEDDED_ATTRNAME =
90+
"_unavailableInEmbedded";
91+
8892
/// A composition class containing a StringLiteral for the names of
8993
/// Swift builtins. The reason we use this is to ensure that we when
9094
/// necessary slice off the "Builtin." prefix from these names in a

lib/AST/Attr.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,13 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
13801380
Printer << ", unavailable)";
13811381
break;
13821382
}
1383+
if (Attr->isForEmbedded()) {
1384+
std::string atUnavailableInEmbedded =
1385+
(llvm::Twine("@") + UNAVAILABLE_IN_EMBEDDED_ATTRNAME).str();
1386+
Printer.printAttrName(atUnavailableInEmbedded);
1387+
break;
1388+
}
1389+
13831390
if (Attr->isSPI()) {
13841391
std::string atSPI = (llvm::Twine("@") + SPI_AVAILABLE_ATTRNAME).str();
13851392
Printer.printAttrName(atSPI);
@@ -2243,6 +2250,34 @@ Type RawLayoutAttr::getResolvedCountType(StructDecl *sd) const {
22432250
ErrorType::get(ctx));
22442251
}
22452252

2253+
#define INIT_VER_TUPLE(X) X(X.empty() ? std::optional<llvm::VersionTuple>() : X)
2254+
2255+
AvailableAttr::AvailableAttr(
2256+
SourceLoc AtLoc, SourceRange Range, PlatformKind Platform,
2257+
StringRef Message, StringRef Rename, ValueDecl *RenameDecl,
2258+
const llvm::VersionTuple &Introduced, SourceRange IntroducedRange,
2259+
const llvm::VersionTuple &Deprecated, SourceRange DeprecatedRange,
2260+
const llvm::VersionTuple &Obsoleted, SourceRange ObsoletedRange,
2261+
PlatformAgnosticAvailabilityKind PlatformAgnostic, bool Implicit,
2262+
bool IsSPI, bool IsForEmbedded)
2263+
: DeclAttribute(DeclAttrKind::Available, AtLoc, Range, Implicit),
2264+
Message(Message), Rename(Rename), RenameDecl(RenameDecl),
2265+
INIT_VER_TUPLE(Introduced), IntroducedRange(IntroducedRange),
2266+
INIT_VER_TUPLE(Deprecated), DeprecatedRange(DeprecatedRange),
2267+
INIT_VER_TUPLE(Obsoleted), ObsoletedRange(ObsoletedRange),
2268+
PlatformAgnostic(PlatformAgnostic), Platform(Platform) {
2269+
Bits.AvailableAttr.IsSPI = IsSPI;
2270+
2271+
if (IsForEmbedded) {
2272+
// FIXME: The IsForEmbedded bit should be removed when library availability
2273+
// conditions are implemented (rdar://138802876)
2274+
Bits.AvailableAttr.IsForEmbedded = true;
2275+
assert(Platform == PlatformKind::none);
2276+
}
2277+
}
2278+
2279+
#undef INIT_VER_TUPLE
2280+
22462281
AvailableAttr *
22472282
AvailableAttr::createPlatformAgnostic(ASTContext &C,
22482283
StringRef Message,
@@ -2294,7 +2329,8 @@ AvailableAttr *AvailableAttr::clone(ASTContext &C, bool implicit) const {
22942329
implicit ? SourceRange() : ObsoletedRange,
22952330
PlatformAgnostic,
22962331
implicit,
2297-
isSPI());
2332+
isSPI(),
2333+
isForEmbedded());
22982334
}
22992335

23002336
std::optional<OriginallyDefinedInAttr::ActiveVersion>

lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,18 +4501,16 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes,
45014501

45024502
// Rewrite @_unavailableInEmbedded into @available(*, unavailable) when in
45034503
// embedded Swift mode, or into nothing when in regular mode.
4504-
if (!DK && Tok.getText() == "_unavailableInEmbedded") {
4504+
if (!DK && Tok.getText() == UNAVAILABLE_IN_EMBEDDED_ATTRNAME) {
45054505
SourceLoc attrLoc = consumeToken();
45064506
if (Context.LangOpts.hasFeature(Feature::Embedded)) {
45074507
StringRef Message = "unavailable in embedded Swift", Renamed;
4508-
auto attr = new (Context) AvailableAttr(AtLoc, SourceRange(AtLoc, attrLoc),
4509-
PlatformKind::none,
4510-
Message, Renamed, /*RenameDecl=*/nullptr,
4511-
llvm::VersionTuple(), SourceRange(),
4512-
llvm::VersionTuple(), SourceRange(),
4513-
llvm::VersionTuple(), SourceRange(),
4514-
PlatformAgnosticAvailabilityKind::Unavailable,
4515-
/*Implicit=*/false, /*IsSPI=*/false);
4508+
auto attr = new (Context) AvailableAttr(
4509+
AtLoc, SourceRange(AtLoc, attrLoc), PlatformKind::none, Message,
4510+
Renamed, /*RenameDecl=*/nullptr, llvm::VersionTuple(), SourceRange(),
4511+
llvm::VersionTuple(), SourceRange(), llvm::VersionTuple(),
4512+
SourceRange(), PlatformAgnosticAvailabilityKind::Unavailable,
4513+
/*Implicit=*/false, /*IsSPI=*/false, /*IsForEmbedded=*/true);
45164514
Attributes.add(attr);
45174515
}
45184516
return makeParserSuccess();

0 commit comments

Comments
 (0)