Skip to content

Commit eb3f800

Browse files
committed
[SIL] Added SILType::getLifetime.
The function describes the lifetime that variables of that type have by default (unless otherwise annotated). This is done by looking at leaf nodes until one that is not eager move is found, at which point the lifetime is known to be lexical. Otherwise, the lifetime is eager move (i.e. if every leaf is eager move).
1 parent 6f58233 commit eb3f800

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

include/swift/SIL/SILType.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020

2121
#include "swift/AST/SILLayout.h"
2222
#include "swift/AST/Types.h"
23+
#include "swift/SIL/Lifetime.h"
24+
#include "llvm/ADT/Hashing.h"
2325
#include "llvm/ADT/PointerIntPair.h"
2426
#include "llvm/Support/ErrorHandling.h"
25-
#include "llvm/ADT/Hashing.h"
2627

2728
namespace swift {
2829

@@ -351,6 +352,15 @@ class SILType {
351352
return getASTType().hasReferenceSemantics();
352353
}
353354

355+
/// The lifetime of values of this type (which are not otherwise annotated).
356+
///
357+
/// Trivial types are ::None.
358+
/// Non-trivial types are ::Lexical by default.
359+
/// Non-trivial types which are annotated @_eagerMove are ::EagerMove.
360+
/// Aggregates which consist entirely of ::EagerMove fields are ::EagerMove.
361+
/// All other types are ::Lexical.
362+
Lifetime getLifetime(const SILFunction &F) const;
363+
354364
/// Returns true if the referenced type is any sort of class-reference type,
355365
/// meaning anything with reference semantics that is not a function type.
356366
bool isAnyClassReferenceType() const {

lib/SIL/IR/SILType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ bool SILType::isNoReturnFunction(SILModule &M,
161161
return false;
162162
}
163163

164+
Lifetime SILType::getLifetime(const SILFunction &F) const {
165+
auto contextType = hasTypeParameter() ? F.mapTypeIntoContext(*this) : *this;
166+
const auto &lowering = F.getTypeLowering(contextType);
167+
auto properties = lowering.getRecursiveProperties();
168+
if (properties.isTrivial())
169+
return Lifetime::None;
170+
return properties.isLexical() ? Lifetime::Lexical : Lifetime::EagerMove;
171+
}
172+
164173
std::string SILType::getMangledName() const {
165174
Mangle::ASTMangler mangler;
166175
return mangler.mangleTypeWithoutPrefix(getRawASTType());

0 commit comments

Comments
 (0)