2626#include " swift/Basic/SourceLoc.h"
2727
2828#include " llvm/ADT/ArrayRef.h"
29+ #include " llvm/Support/TrailingObjects.h"
2930
3031namespace 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+
142145private:
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
154164public:
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 }
0 commit comments