Skip to content

Commit f3ea548

Browse files
committed
[SIL] Added simple Lifetime enum.
The new enum makes keeping track of what flavor of lifetime values ought to have a little bit type-safer.
1 parent 2a48efe commit f3ea548

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

include/swift/SIL/Lifetime.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===---- Lifetime.h - How long a value should be kept alive ----*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Defines a simple type-safe wrapper type to indicate the kind of lifetime that
14+
// a value has--whether it is tied to a lexical scope or not.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#include "swift/AST/LifetimeAnnotation.h"
19+
20+
#ifndef SWIFT_SIL_LIFETIME_H
21+
#define SWIFT_SIL_LIFETIME_H
22+
23+
namespace swift {
24+
25+
/// How long a value (such as instances of a type) should be kept alive--how
26+
/// aggressively its destroys may be hoisted.
27+
///
28+
/// By default, types have lifetimes inferred from their structure, see
29+
/// TypeLowering::RecursiveProperties::isLexical. It can be overridden both on
30+
/// the type level and the value level via attributes.
31+
struct Lifetime {
32+
enum Storage : uint8_t {
33+
/// No lifetime. Applicable to values which aren't destroyed.
34+
None,
35+
/// A lifetime independent from the lexical scope of the value: its
36+
/// releases are hoisted without respect to deinit barriers.
37+
EagerMove,
38+
/// A lifetime tied to the lexical scope of the value: its releases are
39+
/// not hoisted over deinit barriers.
40+
Lexical,
41+
} value;
42+
43+
Lifetime(decltype(value) newValue) : value(newValue) {}
44+
45+
operator Storage() const { return value; }
46+
47+
bool isLexical() { return value == Lifetime::Lexical; }
48+
49+
bool isEagerMove() { return value == Lifetime::EagerMove; }
50+
51+
/// Given a lifetime for a type and the lifetime annotation on a value of that
52+
/// type, the lifetime appropriate for that value.
53+
///
54+
/// Value annotations override a type's lifetime, so the result is just the
55+
/// lifetime indicated by the annotation, if there is one; otherwise its the
56+
/// lifetime from the type.
57+
Lifetime getLifetimeForAnnotatedValue(LifetimeAnnotation annotation) const {
58+
switch (annotation) {
59+
case LifetimeAnnotation::None:
60+
return *this;
61+
case LifetimeAnnotation::EagerMove:
62+
return Lifetime::EagerMove;
63+
case LifetimeAnnotation::Lexical:
64+
return Lifetime::Lexical;
65+
}
66+
}
67+
};
68+
69+
} // end swift namespace
70+
71+
#endif

0 commit comments

Comments
 (0)