Add support for parsing @lifetime attribute to specify lifetime dependencies on declarations

This commit is contained in:
Meghana Gupta
2024-09-01 00:36:37 -07:00
parent 854298ab3b
commit e61d87c01c
12 changed files with 200 additions and 8 deletions

View File

@@ -24,6 +24,7 @@
#include "swift/AST/DeclNameLoc.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/KnownProtocols.h"
#include "swift/AST/LifetimeDependence.h"
#include "swift/AST/MacroDeclaration.h"
#include "swift/AST/Ownership.h"
#include "swift/AST/PlatformKind.h"
@@ -2627,6 +2628,27 @@ public:
}
};
class LifetimeAttr final
: public DeclAttribute,
private llvm::TrailingObjects<LifetimeAttr, LifetimeDependenceSpecifier> {
friend TrailingObjects;
unsigned NumEntries = 0;
explicit LifetimeAttr(SourceLoc atLoc, SourceRange baseRange, bool implicit,
ArrayRef<LifetimeDependenceSpecifier> entries);
public:
static LifetimeAttr *create(ASTContext &context, SourceLoc atLoc,
SourceRange baseRange, bool implicit,
ArrayRef<LifetimeDependenceSpecifier> entries);
static bool classof(const DeclAttribute *DA) {
return DA->getKind() == DeclAttrKind::Lifetime;
}
};
/// Predicate used to filter MatchingAttributeRange.
template <typename ATTR, bool AllowInvalid> struct ToAttributeKind {
ToAttributeKind() {}

View File

@@ -506,6 +506,10 @@ SIMPLE_DECL_ATTR(unsafe, Unsafe,
ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
160)
DECL_ATTR(lifetime, Lifetime,
OnAccessor | OnConstructor | OnFunc | OnSubscript | LongAttribute | ABIBreakingToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
161)
LAST_DECL_ATTR(Unsafe)
#undef DECL_ATTR_ALIAS

View File

@@ -74,7 +74,9 @@ private:
public:
static LifetimeDependenceSpecifier getNamedLifetimeDependenceSpecifier(
SourceLoc loc, ParsedLifetimeDependenceKind kind, Identifier name) {
SourceLoc loc, Identifier name,
ParsedLifetimeDependenceKind kind =
ParsedLifetimeDependenceKind::Default) {
return {loc, SpecifierKind::Named, kind, name};
}
@@ -84,13 +86,15 @@ public:
}
static LifetimeDependenceSpecifier getOrderedLifetimeDependenceSpecifier(
SourceLoc loc, ParsedLifetimeDependenceKind kind, unsigned index) {
SourceLoc loc, unsigned index,
ParsedLifetimeDependenceKind kind =
ParsedLifetimeDependenceKind::Default) {
return {loc, SpecifierKind::Ordered, kind, index};
}
static LifetimeDependenceSpecifier
getSelfLifetimeDependenceSpecifier(SourceLoc loc,
ParsedLifetimeDependenceKind kind) {
static LifetimeDependenceSpecifier getSelfLifetimeDependenceSpecifier(
SourceLoc loc, ParsedLifetimeDependenceKind kind =
ParsedLifetimeDependenceKind::Default) {
return {loc, SpecifierKind::Self, kind, {}};
}