Skip to content

Commit 3d2f2e8

Browse files
committed
[NFC] Rename/reorg LifetimeDependence utils
1 parent 52cc295 commit 3d2f2e8

File tree

12 files changed

+235
-249
lines changed

12 files changed

+235
-249
lines changed

include/swift/AST/Attr.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,19 +2630,19 @@ class RawLayoutAttr final : public DeclAttribute {
26302630

26312631
class LifetimeAttr final
26322632
: public DeclAttribute,
2633-
private llvm::TrailingObjects<LifetimeAttr, LifetimeDependenceSpecifier> {
2633+
private llvm::TrailingObjects<LifetimeAttr, LifetimeEntry> {
26342634

26352635
friend TrailingObjects;
26362636

26372637
unsigned NumEntries = 0;
26382638

26392639
explicit LifetimeAttr(SourceLoc atLoc, SourceRange baseRange, bool implicit,
2640-
ArrayRef<LifetimeDependenceSpecifier> entries);
2640+
ArrayRef<LifetimeEntry> entries);
26412641

26422642
public:
26432643
static LifetimeAttr *create(ASTContext &context, SourceLoc atLoc,
26442644
SourceRange baseRange, bool implicit,
2645-
ArrayRef<LifetimeDependenceSpecifier> entries);
2645+
ArrayRef<LifetimeEntry> entries);
26462646

26472647
static bool classof(const DeclAttribute *DA) {
26482648
return DA->getKind() == DeclAttrKind::Lifetime;

include/swift/AST/LifetimeDependence.h

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- LifetimeDependenceSpecifiers.h ------------------------*- C++ -*-===//
1+
//===--- LifetimeDependence.h ---------------------------------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -43,13 +43,12 @@ enum class ParsedLifetimeDependenceKind : uint8_t {
4343

4444
enum class LifetimeDependenceKind : uint8_t { Inherit = 0, Scope };
4545

46-
class LifetimeDependenceSpecifier {
47-
public:
48-
enum class SpecifierKind { Named, Ordered, Self, Immortal };
46+
enum class LifetimeEntryKind { Named, Ordered, Self, Immortal };
4947

48+
class LifetimeEntry {
5049
private:
5150
SourceLoc loc;
52-
SpecifierKind specifierKind;
51+
LifetimeEntryKind lifetimeEntryKind;
5352
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind;
5453
union Value {
5554
struct {
@@ -65,72 +64,72 @@ class LifetimeDependenceSpecifier {
6564
Value() {}
6665
} value;
6766

68-
LifetimeDependenceSpecifier(
69-
SourceLoc loc, SpecifierKind specifierKind,
70-
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind, Value value)
71-
: loc(loc), specifierKind(specifierKind),
67+
LifetimeEntry(SourceLoc loc, LifetimeEntryKind lifetimeEntryKind,
68+
ParsedLifetimeDependenceKind parsedLifetimeDependenceKind,
69+
Value value)
70+
: loc(loc), lifetimeEntryKind(lifetimeEntryKind),
7271
parsedLifetimeDependenceKind(parsedLifetimeDependenceKind),
7372
value(value) {}
7473

7574
public:
76-
static LifetimeDependenceSpecifier getNamedLifetimeDependenceSpecifier(
77-
SourceLoc loc, Identifier name,
78-
ParsedLifetimeDependenceKind kind =
79-
ParsedLifetimeDependenceKind::Default) {
80-
return {loc, SpecifierKind::Named, kind, name};
75+
static LifetimeEntry
76+
getNamedLifetimeEntry(SourceLoc loc, Identifier name,
77+
ParsedLifetimeDependenceKind kind =
78+
ParsedLifetimeDependenceKind::Default) {
79+
return {loc, LifetimeEntryKind::Named, kind, name};
8180
}
8281

83-
static LifetimeDependenceSpecifier
84-
getImmortalLifetimeDependenceSpecifier(SourceLoc loc) {
85-
return {loc, SpecifierKind::Immortal, {}, {}};
82+
static LifetimeEntry getImmortalLifetimeEntry(SourceLoc loc) {
83+
return {loc, LifetimeEntryKind::Immortal, {}, {}};
8684
}
8785

88-
static LifetimeDependenceSpecifier getOrderedLifetimeDependenceSpecifier(
89-
SourceLoc loc, unsigned index,
90-
ParsedLifetimeDependenceKind kind =
91-
ParsedLifetimeDependenceKind::Default) {
92-
return {loc, SpecifierKind::Ordered, kind, index};
86+
static LifetimeEntry
87+
getOrderedLifetimeEntry(SourceLoc loc, unsigned index,
88+
ParsedLifetimeDependenceKind kind =
89+
ParsedLifetimeDependenceKind::Default) {
90+
return {loc, LifetimeEntryKind::Ordered, kind, index};
9391
}
9492

95-
static LifetimeDependenceSpecifier getSelfLifetimeDependenceSpecifier(
96-
SourceLoc loc, ParsedLifetimeDependenceKind kind =
97-
ParsedLifetimeDependenceKind::Default) {
98-
return {loc, SpecifierKind::Self, kind, {}};
93+
static LifetimeEntry
94+
getSelfLifetimeEntry(SourceLoc loc,
95+
ParsedLifetimeDependenceKind kind =
96+
ParsedLifetimeDependenceKind::Default) {
97+
return {loc, LifetimeEntryKind::Self, kind, {}};
9998
}
10099

101100
SourceLoc getLoc() const { return loc; }
102101

103-
SpecifierKind getSpecifierKind() const { return specifierKind; }
102+
LifetimeEntryKind getLifetimeEntryKind() const { return lifetimeEntryKind; }
104103

105104
ParsedLifetimeDependenceKind getParsedLifetimeDependenceKind() const {
106105
return parsedLifetimeDependenceKind;
107106
}
108107

109108
Identifier getName() const {
110-
assert(specifierKind == SpecifierKind::Named);
109+
assert(lifetimeEntryKind == LifetimeEntryKind::Named);
111110
return value.Named.name;
112111
}
113112

114113
unsigned getIndex() const {
115-
assert(specifierKind == SpecifierKind::Ordered);
114+
assert(lifetimeEntryKind == LifetimeEntryKind::Ordered);
116115
return value.Ordered.index;
117116
}
118117

119118
std::string getParamString() const {
120-
switch (specifierKind) {
121-
case SpecifierKind::Named:
119+
switch (lifetimeEntryKind) {
120+
case LifetimeEntryKind::Named:
122121
return value.Named.name.str().str();
123-
case SpecifierKind::Self:
122+
case LifetimeEntryKind::Self:
124123
return "self";
125-
case SpecifierKind::Ordered:
124+
case LifetimeEntryKind::Ordered:
126125
return std::to_string(value.Ordered.index);
127-
case SpecifierKind::Immortal:
126+
case LifetimeEntryKind::Immortal:
128127
return "immortal";
129128
}
130-
llvm_unreachable("Invalid LifetimeDependenceSpecifier::SpecifierKind");
129+
llvm_unreachable("Invalid LifetimeEntryKind");
131130
}
132131

133-
std::string getLifetimeDependenceSpecifierString() const {
132+
std::string getDependsOnString() const {
134133
switch (parsedLifetimeDependenceKind) {
135134
case ParsedLifetimeDependenceKind::Default:
136135
return "dependsOn(" + getParamString() + ")";
@@ -139,8 +138,7 @@ class LifetimeDependenceSpecifier {
139138
case ParsedLifetimeDependenceKind::Inherit:
140139
return "dependsOn(inherited " + getParamString() + ")";
141140
}
142-
llvm_unreachable(
143-
"Invalid LifetimeDependenceSpecifier::ParsedLifetimeDependenceKind");
141+
llvm_unreachable("Invalid LifetimeEntry::ParsedLifetimeDependenceKind");
144142
}
145143
};
146144

@@ -155,10 +153,10 @@ class LifetimeDependenceInfo {
155153
unsigned sourceIndex,
156154
LifetimeDependenceKind kind);
157155

158-
/// Builds LifetimeDependenceInfo on a result or parameter from a swift decl
156+
/// Builds LifetimeDependenceInfo from dependsOn type modifier
159157
static std::optional<LifetimeDependenceInfo>
160-
fromTypeRepr(AbstractFunctionDecl *afd, LifetimeDependentTypeRepr *typeRepr,
161-
unsigned targetIndex);
158+
fromDependsOn(AbstractFunctionDecl *afd, TypeRepr *targetRepr,
159+
Type targetType, unsigned targetIndex);
162160

163161
/// Infer LifetimeDependenceInfo on result
164162
static std::optional<LifetimeDependenceInfo> infer(AbstractFunctionDecl *afd);
@@ -173,9 +171,9 @@ class LifetimeDependenceInfo {
173171

174172
/// Builds LifetimeDependenceInfo from SIL function type
175173
static std::optional<LifetimeDependenceInfo>
176-
fromTypeRepr(LifetimeDependentTypeRepr *lifetimeDependentRepr,
177-
unsigned targetIndex, ArrayRef<SILParameterInfo> params,
178-
DeclContext *dc);
174+
fromDependsOn(LifetimeDependentTypeRepr *lifetimeDependentRepr,
175+
unsigned targetIndex, ArrayRef<SILParameterInfo> params,
176+
DeclContext *dc);
179177

180178
public:
181179
LifetimeDependenceInfo(IndexSubset *inheritLifetimeParamIndices,

include/swift/AST/TypeRepr.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,31 +1533,28 @@ class SILBoxTypeRepr final : public TypeRepr,
15331533

15341534
class LifetimeDependentTypeRepr final
15351535
: public SpecifierTypeRepr,
1536-
private llvm::TrailingObjects<LifetimeDependentTypeRepr,
1537-
LifetimeDependenceSpecifier> {
1536+
private llvm::TrailingObjects<LifetimeDependentTypeRepr, LifetimeEntry> {
15381537
friend TrailingObjects;
15391538

15401539
size_t numTrailingObjects(OverloadToken<LifetimeDependentTypeRepr>) const {
15411540
return Bits.LifetimeDependentTypeRepr.NumDependencies;
15421541
}
15431542

15441543
public:
1545-
LifetimeDependentTypeRepr(TypeRepr *base,
1546-
ArrayRef<LifetimeDependenceSpecifier> specifiers)
1544+
LifetimeDependentTypeRepr(TypeRepr *base, ArrayRef<LifetimeEntry> specifiers)
15471545
: SpecifierTypeRepr(TypeReprKind::LifetimeDependent, base,
15481546
specifiers.front().getLoc()) {
15491547
assert(base);
15501548
Bits.LifetimeDependentTypeRepr.NumDependencies = specifiers.size();
15511549
std::uninitialized_copy(specifiers.begin(), specifiers.end(),
1552-
getTrailingObjects<LifetimeDependenceSpecifier>());
1550+
getTrailingObjects<LifetimeEntry>());
15531551
}
15541552

1555-
static LifetimeDependentTypeRepr *
1556-
create(ASTContext &C, TypeRepr *base,
1557-
ArrayRef<LifetimeDependenceSpecifier> specifiers);
1553+
static LifetimeDependentTypeRepr *create(ASTContext &C, TypeRepr *base,
1554+
ArrayRef<LifetimeEntry> specifiers);
15581555

1559-
ArrayRef<LifetimeDependenceSpecifier> getLifetimeDependencies() const {
1560-
return {getTrailingObjects<LifetimeDependenceSpecifier>(),
1556+
ArrayRef<LifetimeEntry> getLifetimeDependencies() const {
1557+
return {getTrailingObjects<LifetimeEntry>(),
15611558
Bits.LifetimeDependentTypeRepr.NumDependencies};
15621559
}
15631560

include/swift/Parse/Parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,8 +1270,8 @@ class Parser {
12701270
ConventionTypeAttr *&result,
12711271
bool justChecking);
12721272

1273-
ParserStatus parseLifetimeDependenceSpecifiers(
1274-
SmallVectorImpl<LifetimeDependenceSpecifier> &specifierList);
1273+
ParserStatus
1274+
parseLifetimeEntries(SmallVectorImpl<LifetimeEntry> &specifierList);
12751275

12761276
ParserResult<ImportDecl> parseDeclImport(ParseDeclOptions Flags,
12771277
DeclAttributes &Attributes);
@@ -1474,7 +1474,7 @@ class Parser {
14741474
SourceLoc ConstLoc;
14751475
SourceLoc SendingLoc;
14761476
SmallVector<TypeOrCustomAttr> Attributes;
1477-
SmallVector<LifetimeDependenceSpecifier> lifetimeDependenceSpecifiers;
1477+
SmallVector<LifetimeEntry> lifetimeEntries;
14781478

14791479
ParsedTypeAttributeList(ParseTypeReason reason) : ParseReason(reason) {}
14801480

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3579,7 +3579,7 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, StringRef>,
35793579
for (auto &dep : T->getLifetimeDependencies()) {
35803580
printFieldRaw(
35813581
[&](raw_ostream &out) {
3582-
out << " " << dep.getLifetimeDependenceSpecifierString() << " ";
3582+
out << " " << dep.getDependsOnString() << " ";
35833583
},
35843584
"");
35853585
}

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4111,7 +4111,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
41114111
if (auto *typeRepr = dyn_cast_or_null<LifetimeDependentTypeRepr>(
41124112
decl->getResultTypeRepr())) {
41134113
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4114-
Printer << " " << dep.getLifetimeDependenceSpecifierString() << " ";
4114+
Printer << " " << dep.getDependsOnString() << " ";
41154115
}
41164116
}
41174117
}
@@ -4348,7 +4348,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
43484348
auto *typeRepr =
43494349
cast<LifetimeDependentTypeRepr>(decl->getResultTypeRepr());
43504350
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4351-
Printer << dep.getLifetimeDependenceSpecifierString() << " ";
4351+
Printer << dep.getDependsOnString() << " ";
43524352
}
43534353
// TODO: Handle failable initializers with lifetime dependent returns
43544354
Printer << "Self";

lib/AST/Attr.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,20 +3043,18 @@ AllowFeatureSuppressionAttr *AllowFeatureSuppressionAttr::create(
30433043
}
30443044

30453045
LifetimeAttr::LifetimeAttr(SourceLoc atLoc, SourceRange baseRange,
3046-
bool implicit,
3047-
ArrayRef<LifetimeDependenceSpecifier> entries)
3046+
bool implicit, ArrayRef<LifetimeEntry> entries)
30483047
: DeclAttribute(DeclAttrKind::Lifetime, atLoc, baseRange, implicit),
30493048
NumEntries(entries.size()) {
30503049
std::copy(entries.begin(), entries.end(),
3051-
getTrailingObjects<LifetimeDependenceSpecifier>());
3050+
getTrailingObjects<LifetimeEntry>());
30523051
}
30533052

3054-
LifetimeAttr *
3055-
LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
3056-
SourceRange baseRange, bool implicit,
3057-
ArrayRef<LifetimeDependenceSpecifier> entries) {
3058-
unsigned size = totalSizeToAlloc<LifetimeDependenceSpecifier>(entries.size());
3059-
void *mem = context.Allocate(size, alignof(LifetimeDependenceSpecifier));
3053+
LifetimeAttr *LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
3054+
SourceRange baseRange, bool implicit,
3055+
ArrayRef<LifetimeEntry> entries) {
3056+
unsigned size = totalSizeToAlloc<LifetimeEntry>(entries.size());
3057+
void *mem = context.Allocate(size, alignof(LifetimeEntry));
30603058
return new (mem) LifetimeAttr(atLoc, baseRange, implicit, entries);
30613059
}
30623060

0 commit comments

Comments
 (0)