Skip to content

Commit 67dc936

Browse files
committed
[NFC] Rename/reorg LifetimeDependence utils
1 parent 5a5b81d commit 67dc936

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
@@ -2636,19 +2636,19 @@ class RawLayoutAttr final : public DeclAttribute {
26362636

26372637
class LifetimeAttr final
26382638
: public DeclAttribute,
2639-
private llvm::TrailingObjects<LifetimeAttr, LifetimeDependenceSpecifier> {
2639+
private llvm::TrailingObjects<LifetimeAttr, LifetimeEntry> {
26402640

26412641
friend TrailingObjects;
26422642

26432643
unsigned NumEntries = 0;
26442644

26452645
explicit LifetimeAttr(SourceLoc atLoc, SourceRange baseRange, bool implicit,
2646-
ArrayRef<LifetimeDependenceSpecifier> entries);
2646+
ArrayRef<LifetimeEntry> entries);
26472647

26482648
public:
26492649
static LifetimeAttr *create(ASTContext &context, SourceLoc atLoc,
26502650
SourceRange baseRange, bool implicit,
2651-
ArrayRef<LifetimeDependenceSpecifier> entries);
2651+
ArrayRef<LifetimeEntry> entries);
26522652

26532653
static bool classof(const DeclAttribute *DA) {
26542654
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
@@ -3555,7 +3555,7 @@ class PrintTypeRepr : public TypeReprVisitor<PrintTypeRepr, void, StringRef>,
35553555
for (auto &dep : T->getLifetimeDependencies()) {
35563556
printFieldRaw(
35573557
[&](raw_ostream &out) {
3558-
out << " " << dep.getLifetimeDependenceSpecifierString() << " ";
3558+
out << " " << dep.getDependsOnString() << " ";
35593559
},
35603560
"");
35613561
}

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4094,7 +4094,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
40944094
if (auto *typeRepr = dyn_cast_or_null<LifetimeDependentTypeRepr>(
40954095
decl->getResultTypeRepr())) {
40964096
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4097-
Printer << " " << dep.getLifetimeDependenceSpecifierString() << " ";
4097+
Printer << " " << dep.getDependsOnString() << " ";
40984098
}
40994099
}
41004100
}
@@ -4331,7 +4331,7 @@ void PrintAST::visitConstructorDecl(ConstructorDecl *decl) {
43314331
auto *typeRepr =
43324332
cast<LifetimeDependentTypeRepr>(decl->getResultTypeRepr());
43334333
for (auto &dep : typeRepr->getLifetimeDependencies()) {
4334-
Printer << dep.getLifetimeDependenceSpecifierString() << " ";
4334+
Printer << dep.getDependsOnString() << " ";
43354335
}
43364336
// TODO: Handle failable initializers with lifetime dependent returns
43374337
Printer << "Self";

lib/AST/Attr.cpp

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

30343034
LifetimeAttr::LifetimeAttr(SourceLoc atLoc, SourceRange baseRange,
3035-
bool implicit,
3036-
ArrayRef<LifetimeDependenceSpecifier> entries)
3035+
bool implicit, ArrayRef<LifetimeEntry> entries)
30373036
: DeclAttribute(DeclAttrKind::Lifetime, atLoc, baseRange, implicit),
30383037
NumEntries(entries.size()) {
30393038
std::copy(entries.begin(), entries.end(),
3040-
getTrailingObjects<LifetimeDependenceSpecifier>());
3039+
getTrailingObjects<LifetimeEntry>());
30413040
}
30423041

3043-
LifetimeAttr *
3044-
LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
3045-
SourceRange baseRange, bool implicit,
3046-
ArrayRef<LifetimeDependenceSpecifier> entries) {
3047-
unsigned size = totalSizeToAlloc<LifetimeDependenceSpecifier>(entries.size());
3048-
void *mem = context.Allocate(size, alignof(LifetimeDependenceSpecifier));
3042+
LifetimeAttr *LifetimeAttr::create(ASTContext &context, SourceLoc atLoc,
3043+
SourceRange baseRange, bool implicit,
3044+
ArrayRef<LifetimeEntry> entries) {
3045+
unsigned size = totalSizeToAlloc<LifetimeEntry>(entries.size());
3046+
void *mem = context.Allocate(size, alignof(LifetimeEntry));
30493047
return new (mem) LifetimeAttr(atLoc, baseRange, implicit, entries);
30503048
}
30513049

0 commit comments

Comments
 (0)