26
26
#include " swift/Basic/SourceLoc.h"
27
27
28
28
#include " llvm/ADT/ArrayRef.h"
29
+ #include " llvm/Support/TrailingObjects.h"
29
30
30
31
namespace swift {
31
32
@@ -137,22 +138,30 @@ struct LifetimeDescriptor {
137
138
}
138
139
};
139
140
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
+
142
145
private:
143
146
SourceLoc startLoc, endLoc;
144
- ArrayRef<LifetimeDescriptor> sources ;
147
+ unsigned numSources ;
145
148
std::optional<LifetimeDescriptor> targetDescriptor;
146
149
147
150
LifetimeEntry (
148
151
SourceLoc startLoc, SourceLoc endLoc,
149
152
ArrayRef<LifetimeDescriptor> sources,
150
153
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
+ }
153
163
154
164
public:
155
- // / \p sources should be allocated on the ASTContext
156
165
static LifetimeEntry *
157
166
create (const ASTContext &ctx, SourceLoc startLoc, SourceLoc endLoc,
158
167
ArrayRef<LifetimeDescriptor> sources,
@@ -162,14 +171,14 @@ class LifetimeEntry {
162
171
SourceLoc getStartLoc () const { return startLoc; }
163
172
SourceLoc getEndLoc () const { return endLoc; }
164
173
165
- ArrayRef<LifetimeDescriptor> getSources () const { return sources; }
174
+ ArrayRef<LifetimeDescriptor> getSources () const {
175
+ return {getTrailingObjects<LifetimeDescriptor>(), numSources};
176
+ }
166
177
167
178
std::optional<LifetimeDescriptor> getTargetDescriptor () const {
168
179
return targetDescriptor;
169
180
}
170
181
171
- bool empty () const { return !sources.empty (); }
172
-
173
182
std::string getString () const {
174
183
std::string result = " @lifetime(" ;
175
184
if (targetDescriptor.has_value ()) {
@@ -178,7 +187,7 @@ class LifetimeEntry {
178
187
}
179
188
180
189
bool firstElem = true ;
181
- for (auto source : sources ) {
190
+ for (auto source : getSources () ) {
182
191
if (!firstElem) {
183
192
result += " , " ;
184
193
}
0 commit comments