@@ -16,9 +16,60 @@ import SILBridging
16
16
public protocol Value : AnyObject , CustomStringConvertible {
17
17
var uses : UseList { get }
18
18
var type : Type { get }
19
+ var ownership : Ownership { get }
19
20
var definingInstruction : Instruction ? { get }
20
21
}
21
22
23
+ public enum Ownership {
24
+ /// A Value with `unowned` ownership kind is an independent value that
25
+ /// has a lifetime that is only guaranteed to last until the next program
26
+ /// visible side-effect. To maintain the lifetime of an unowned value, it
27
+ /// must be converted to an owned representation via a copy_value.
28
+ ///
29
+ /// Unowned ownership kind occurs mainly along method/function boundaries in
30
+ /// between Swift and Objective-C code.
31
+ case unowned
32
+
33
+ /// A Value with `owned` ownership kind is an independent value that has
34
+ /// an ownership independent of any other ownership imbued within it. The
35
+ /// Value must be paired with a consuming operation that ends the SSA
36
+ /// value's lifetime exactly once along all paths through the program.
37
+ case owned
38
+
39
+ /// A Value with `guaranteed` ownership kind is an independent value that
40
+ /// is guaranteed to be live over a specific region of the program. This
41
+ /// region can come in several forms:
42
+ ///
43
+ /// 1. @guaranteed function argument. This guarantees that a value will
44
+ /// outlive a function.
45
+ ///
46
+ /// 2. A shared borrow region. This is a region denoted by a
47
+ /// begin_borrow/load_borrow instruction and an end_borrow instruction. The
48
+ /// SSA value must not be destroyed or taken inside the borrowed region.
49
+ ///
50
+ /// Any value with guaranteed ownership must be paired with an end_borrow
51
+ /// instruction exactly once along any path through the program.
52
+ case guaranteed
53
+
54
+ /// A Value with `none` ownership kind is an independent value outside of
55
+ /// the ownership system. It is used to model values that are statically
56
+ /// determined to be trivial. This includes trivially typed values as well
57
+ /// as trivial cases of non-trivial enums. Naturally `none` can be merged with
58
+ /// any ownership, allowing us to naturally model merge and branch
59
+ /// points in the SSA graph, where more information about the value is
60
+ /// statically available on some control flow paths.
61
+ case none
62
+
63
+ public var _bridged : BridgedOwnership {
64
+ switch self {
65
+ case . unowned: return Ownership_Unowned
66
+ case . owned: return Ownership_Owned
67
+ case . guaranteed: return Ownership_Guaranteed
68
+ case . none: return Ownership_None
69
+ }
70
+ }
71
+ }
72
+
22
73
extension Value {
23
74
public var description : String {
24
75
var s = SILNode_debugDescription ( bridgedNode)
@@ -55,6 +106,7 @@ extension Value {
55
106
}
56
107
return nil
57
108
}
109
+ public var ownership : Ownership { SILValue_getOwnership ( bridged) . ownership }
58
110
59
111
public var hashable : HashableValue { ObjectIdentifier ( self ) }
60
112
@@ -111,3 +163,16 @@ final class PlaceholderValue : Value {
111
163
extension OptionalBridgedValue {
112
164
var value : Value ? { obj. getAs ( AnyObject . self) as? Value }
113
165
}
166
+
167
+ extension BridgedOwnership {
168
+ var ownership : Ownership {
169
+ switch self {
170
+ case Ownership_Unowned: return . unowned
171
+ case Ownership_Owned: return . owned
172
+ case Ownership_Guaranteed: return . guaranteed
173
+ case Ownership_None: return . none
174
+ default :
175
+ fatalError ( " unsupported ownership " )
176
+ }
177
+ }
178
+ }
0 commit comments