Skip to content

Add support for availability to @_specialize #39596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,8 @@ class SynthesizedProtocolAttr : public DeclAttribute {
/// type list.
class SpecializeAttr final
: public DeclAttribute,
private llvm::TrailingObjects<SpecializeAttr, Identifier> {
private llvm::TrailingObjects<SpecializeAttr, Identifier,
AvailableAttr *> {
friend class SpecializeAttrTargetDeclRequest;
friend TrailingObjects;

Expand All @@ -1321,35 +1322,44 @@ class SpecializeAttr final
LazyMemberLoader *resolver = nullptr;
uint64_t resolverContextData;
size_t numSPIGroups;
size_t numAvailableAttrs;

SpecializeAttr(SourceLoc atLoc, SourceRange Range,
TrailingWhereClause *clause, bool exported,
SpecializationKind kind, GenericSignature specializedSignature,
DeclNameRef targetFunctionName,
ArrayRef<Identifier> spiGroups);
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs);

public:
static SpecializeAttr *create(ASTContext &Ctx, SourceLoc atLoc,
SourceRange Range, TrailingWhereClause *clause,
bool exported, SpecializationKind kind,
DeclNameRef targetFunctionName,
ArrayRef<Identifier> spiGroups,
GenericSignature specializedSignature
= nullptr);
static SpecializeAttr *
create(ASTContext &Ctx, SourceLoc atLoc, SourceRange Range,
TrailingWhereClause *clause, bool exported, SpecializationKind kind,
DeclNameRef targetFunctionName, ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
GenericSignature specializedSignature = nullptr);

static SpecializeAttr *create(ASTContext &ctx, bool exported,
SpecializationKind kind,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
GenericSignature specializedSignature,
DeclNameRef replacedFunction);

static SpecializeAttr *create(ASTContext &ctx, bool exported,
SpecializationKind kind,
ArrayRef<Identifier> spiGroups,
ArrayRef<AvailableAttr *> availabilityAttrs,
GenericSignature specializedSignature,
DeclNameRef replacedFunction,
LazyMemberLoader *resolver, uint64_t data);

size_t numTrailingObjects(OverloadToken<Identifier>) const {
return numSPIGroups;
}

size_t numTrailingObjects(OverloadToken<AvailableAttr *>) const {
return numAvailableAttrs;
}
/// Name of SPIs declared by the attribute.
///
/// Note: A single SPI name per attribute is currently supported but this
Expand All @@ -1359,6 +1369,11 @@ class SpecializeAttr final
numSPIGroups };
}

ArrayRef<AvailableAttr *> getAvailabeAttrs() const {
return {this->template getTrailingObjects<AvailableAttr *>(),
numAvailableAttrs};
}

TrailingWhereClause *getTrailingWhereClause() const;

GenericSignature getSpecializedSignature() const {
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/Availability.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ class AvailabilityInference {
static Optional<AvailabilityContext> annotatedAvailableRange(const Decl *D,
ASTContext &C);

static AvailabilityContext
annotatedAvailableRangeForAttr(const SpecializeAttr* attr, ASTContext &ctx);

};

} // end namespace swift
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,9 @@ ERROR(attr_expected_lparen,none,
ERROR(attr_expected_rparen,none,
"expected ')' in '%0' %select{attribute|modifier}1", (StringRef, bool))

ERROR(attr_expected_semi,none,
"expected ';' in '%0' %select{attribute|modifier}1", (StringRef, bool))

ERROR(attr_expected_comma,none,
"expected ',' in '%0' %select{attribute|modifier}1", (StringRef, bool))

Expand Down
10 changes: 8 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ class Parser {
/// \p Attr is where to store the parsed attribute
bool parseSpecializeAttribute(
swift::tok ClosingBrace, SourceLoc AtLoc, SourceLoc Loc,
SpecializeAttr *&Attr,
SpecializeAttr *&Attr, AvailabilityContext *SILAvailability,
llvm::function_ref<bool(Parser &)> parseSILTargetName =
[](Parser &) { return false; },
llvm::function_ref<bool(Parser &)> parseSILSIPModule =
Expand All @@ -1060,7 +1060,9 @@ class Parser {
swift::tok ClosingBrace, bool &DiscardAttribute, Optional<bool> &Exported,
Optional<SpecializeAttr::SpecializationKind> &Kind,
TrailingWhereClause *&TrailingWhereClause, DeclNameRef &targetFunction,
AvailabilityContext *SILAvailability,
SmallVectorImpl<Identifier> &spiGroups,
SmallVectorImpl<AvailableAttr *> &availableAttrs,
llvm::function_ref<bool(Parser &)> parseSILTargetName,
llvm::function_ref<bool(Parser &)> parseSILSIPModule);

Expand Down Expand Up @@ -1850,7 +1852,11 @@ class Parser {
parsePlatformVersionConstraintSpec();
ParserResult<PlatformAgnosticVersionConstraintAvailabilitySpec>
parsePlatformAgnosticVersionConstraintSpec();

bool
parseAvailability(bool parseAsPartOfSpecializeAttr, StringRef AttrName,
bool &DiscardAttribute, SourceRange &attrRange,
SourceLoc AtLoc, SourceLoc Loc,
llvm::function_ref<void(AvailableAttr *)> addAttribute);
//===--------------------------------------------------------------------===//
// Code completion second pass.

Expand Down
11 changes: 9 additions & 2 deletions include/swift/SIL/SILFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class SILSpecializeAttr final {
GenericSignature specializedSignature,
bool exported, SpecializationKind kind,
SILFunction *target, Identifier spiGroup,
const ModuleDecl *spiModule);
const ModuleDecl *spiModule,
AvailabilityContext availability);

bool isExported() const {
return exported;
Expand Down Expand Up @@ -110,20 +111,26 @@ class SILSpecializeAttr final {
return spiModule;
}

AvailabilityContext getAvailability() const {
return availability;
}

void print(llvm::raw_ostream &OS) const;

private:
SpecializationKind kind;
bool exported;
GenericSignature specializedSignature;
Identifier spiGroup;
AvailabilityContext availability;
const ModuleDecl *spiModule = nullptr;
SILFunction *F = nullptr;
SILFunction *targetFunction = nullptr;

SILSpecializeAttr(bool exported, SpecializationKind kind,
GenericSignature specializedSignature, SILFunction *target,
Identifier spiGroup, const ModuleDecl *spiModule);
Identifier spiGroup, const ModuleDecl *spiModule,
AvailabilityContext availability);
};

/// SILFunction - A function body that has been lowered to SIL. This consists of
Expand Down
Loading