Skip to content

Commit b443ff7

Browse files
committed
[NFC] Use llvm::TrailingObjects to represent variable list of lifetime sources
1 parent 6710d50 commit b443ff7

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

include/swift/AST/LifetimeDependence.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "swift/Basic/SourceLoc.h"
2727

2828
#include "llvm/ADT/ArrayRef.h"
29+
#include "llvm/Support/TrailingObjects.h"
2930

3031
namespace swift {
3132

@@ -137,22 +138,30 @@ struct LifetimeDescriptor {
137138
}
138139
};
139140

140-
// TODO: Use TrailingObjects to tail allocate sources
141-
class LifetimeEntry {
141+
class LifetimeEntry final
142+
: private llvm::TrailingObjects<LifetimeEntry, LifetimeDescriptor> {
143+
friend TrailingObjects;
144+
142145
private:
143146
SourceLoc startLoc, endLoc;
144-
ArrayRef<LifetimeDescriptor> sources;
147+
unsigned numSources;
145148
std::optional<LifetimeDescriptor> targetDescriptor;
146149

147150
LifetimeEntry(
148151
SourceLoc startLoc, SourceLoc endLoc,
149152
ArrayRef<LifetimeDescriptor> sources,
150153
std::optional<LifetimeDescriptor> targetDescriptor = std::nullopt)
151-
: startLoc(startLoc), endLoc(endLoc), sources(sources),
152-
targetDescriptor(targetDescriptor) {}
154+
: startLoc(startLoc), endLoc(endLoc), numSources(sources.size()),
155+
targetDescriptor(targetDescriptor) {
156+
std::uninitialized_copy(sources.begin(), sources.end(),
157+
getTrailingObjects<LifetimeDescriptor>());
158+
}
159+
160+
size_t numTrailingObjects(OverloadToken<LifetimeDescriptor>) const {
161+
return numSources;
162+
}
153163

154164
public:
155-
/// \p sources should be allocated on the ASTContext
156165
static LifetimeEntry *
157166
create(const ASTContext &ctx, SourceLoc startLoc, SourceLoc endLoc,
158167
ArrayRef<LifetimeDescriptor> sources,
@@ -162,14 +171,14 @@ class LifetimeEntry {
162171
SourceLoc getStartLoc() const { return startLoc; }
163172
SourceLoc getEndLoc() const { return endLoc; }
164173

165-
ArrayRef<LifetimeDescriptor> getSources() const { return sources; }
174+
ArrayRef<LifetimeDescriptor> getSources() const {
175+
return {getTrailingObjects<LifetimeDescriptor>(), numSources};
176+
}
166177

167178
std::optional<LifetimeDescriptor> getTargetDescriptor() const {
168179
return targetDescriptor;
169180
}
170181

171-
bool empty() const { return !sources.empty(); }
172-
173182
std::string getString() const {
174183
std::string result = "@lifetime(";
175184
if (targetDescriptor.has_value()) {
@@ -178,7 +187,7 @@ class LifetimeEntry {
178187
}
179188

180189
bool firstElem = true;
181-
for (auto source : sources) {
190+
for (auto source : getSources()) {
182191
if (!firstElem) {
183192
result += ", ";
184193
}

lib/AST/LifetimeDependence.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ LifetimeEntry *
2929
LifetimeEntry::create(const ASTContext &ctx, SourceLoc startLoc,
3030
SourceLoc endLoc, ArrayRef<LifetimeDescriptor> sources,
3131
std::optional<LifetimeDescriptor> targetDescriptor) {
32-
void *mem = ctx.Allocate(sizeof(LifetimeEntry), alignof(LifetimeEntry));
32+
unsigned size = totalSizeToAlloc<LifetimeDescriptor>(sources.size());
33+
void *mem = ctx.Allocate(size, alignof(LifetimeEntry));
3334
return new (mem) LifetimeEntry(startLoc, endLoc, sources, targetDescriptor);
3435
}
3536

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5210,7 +5210,7 @@ ParserResult<LifetimeEntry> Parser::parseLifetimeEntry(SourceLoc loc) {
52105210
}
52115211

52125212
auto *lifetimeEntry = LifetimeEntry::create(
5213-
Context, loc, rParenLoc, Context.AllocateCopy(sources), targetDescriptor);
5213+
Context, loc, rParenLoc, sources, targetDescriptor);
52145214
return ParserResult<LifetimeEntry>(lifetimeEntry);
52155215
}
52165216

0 commit comments

Comments
 (0)