@@ -25,24 +25,6 @@ public struct FunctionConvention : CustomStringConvertible {
2525 self . hasLoweredAddresses = function. hasLoweredAddresses
2626 }
2727
28- public struct Results : Collection {
29- let bridged : BridgedResultInfoArray
30- let hasLoweredAddresses : Bool
31-
32- public var startIndex : Int { 0 }
33-
34- public var endIndex : Int { bridged. count ( ) }
35-
36- public func index( after index: Int ) -> Int {
37- return index + 1
38- }
39-
40- public subscript( _ index: Int ) -> ResultInfo {
41- return ResultInfo ( bridged: bridged. at ( index) ,
42- hasLoweredAddresses: hasLoweredAddresses)
43- }
44- }
45-
4628 /// All results including the error.
4729 public var results : Results {
4830 Results ( bridged: bridgedFunctionType. SILFunctionType_getResultsWithError ( ) ,
@@ -71,24 +53,6 @@ public struct FunctionConvention : CustomStringConvertible {
7153 : results. lazy. filter { $0. convention == . pack }
7254 }
7355
74- public struct Parameters : Collection {
75- let bridged : BridgedParameterInfoArray
76- let hasLoweredAddresses : Bool
77-
78- public var startIndex : Int { 0 }
79-
80- public var endIndex : Int { bridged. count ( ) }
81-
82- public func index( after index: Int ) -> Int {
83- return index + 1
84- }
85-
86- public subscript( _ index: Int ) -> ParameterInfo {
87- return ParameterInfo ( bridged: bridged. at ( index) ,
88- hasLoweredAddresses: hasLoweredAddresses)
89- }
90- }
91-
9256 public var parameters : Parameters {
9357 Parameters ( bridged: bridgedFunctionType. SILFunctionType_getParameters ( ) ,
9458 hasLoweredAddresses: hasLoweredAddresses)
@@ -98,34 +62,31 @@ public struct FunctionConvention : CustomStringConvertible {
9862 bridgedFunctionType. SILFunctionType_hasSelfParam ( )
9963 }
10064
101- public struct Yields : Collection {
102- let bridged : BridgedYieldInfoArray
103- let hasLoweredAddresses : Bool
104-
105- public var startIndex : Int { 0 }
106-
107- public var endIndex : Int { bridged. count ( ) }
108-
109- public func index( after index: Int ) -> Int {
110- return index + 1
111- }
112-
113- public subscript( _ index: Int ) -> ParameterInfo {
114- return ParameterInfo ( bridged: bridged. at ( index) ,
115- hasLoweredAddresses: hasLoweredAddresses)
116- }
117- }
118-
11965 public var yields : Yields {
12066 Yields ( bridged: bridgedFunctionType. SILFunctionType_getYields ( ) ,
12167 hasLoweredAddresses: hasLoweredAddresses)
12268 }
12369
70+ /// If the function result depends on any parameters, return a
71+ /// Collection of LifetimeDependenceConvention indexed on the
72+ /// function parameter.
73+ public var resultDependencies : ResultDependencies ? {
74+ let deps = bridgedFunctionType. SILFunctionType_getLifetimeDependenceInfo ( )
75+ if deps. empty ( ) {
76+ return nil
77+ }
78+ return ResultDependencies ( bridged: deps, parameterCount: parameters. count,
79+ hasSelfParameter: hasSelfParameter)
80+ }
81+
12482 public var description : String {
12583 var str = String ( taking: bridgedFunctionType. getDebugDescription ( ) )
12684 parameters. forEach { str += " \n parameter: " + $0. description }
12785 results. forEach { str += " \n result: " + $0. description }
12886 str += ( hasLoweredAddresses ? " \n [lowered_address] " : " \n [sil_opaque] " )
87+ if let deps = resultDependencies {
88+ str += " \n result dependences \( deps) "
89+ }
12990 return str
13091 }
13192}
@@ -160,6 +121,26 @@ public struct ResultInfo : CustomStringConvertible {
160121 }
161122}
162123
124+ extension FunctionConvention {
125+ public struct Results : Collection {
126+ let bridged : BridgedResultInfoArray
127+ let hasLoweredAddresses : Bool
128+
129+ public var startIndex : Int { 0 }
130+
131+ public var endIndex : Int { bridged. count ( ) }
132+
133+ public func index( after index: Int ) -> Int {
134+ return index + 1
135+ }
136+
137+ public subscript( _ index: Int ) -> ResultInfo {
138+ return ResultInfo ( bridged: bridged. at ( index) ,
139+ hasLoweredAddresses: hasLoweredAddresses)
140+ }
141+ }
142+ }
143+
163144public struct ParameterInfo : CustomStringConvertible {
164145 /// The parameter type that describes the abstract calling
165146 /// convention of the parameter.
@@ -193,6 +174,109 @@ public struct ParameterInfo : CustomStringConvertible {
193174 }
194175}
195176
177+ extension FunctionConvention {
178+ public struct Parameters : Collection {
179+ let bridged : BridgedParameterInfoArray
180+ let hasLoweredAddresses : Bool
181+
182+ public var startIndex : Int { 0 }
183+
184+ public var endIndex : Int { bridged. count ( ) }
185+
186+ public func index( after index: Int ) -> Int {
187+ return index + 1
188+ }
189+
190+ public subscript( _ index: Int ) -> ParameterInfo {
191+ return ParameterInfo ( bridged: bridged. at ( index) ,
192+ hasLoweredAddresses: hasLoweredAddresses)
193+ }
194+ }
195+ }
196+
197+ extension FunctionConvention {
198+ public struct Yields : Collection {
199+ let bridged : BridgedYieldInfoArray
200+ let hasLoweredAddresses : Bool
201+
202+ public var startIndex : Int { 0 }
203+
204+ public var endIndex : Int { bridged. count ( ) }
205+
206+ public func index( after index: Int ) -> Int {
207+ return index + 1
208+ }
209+
210+ public subscript( _ index: Int ) -> ParameterInfo {
211+ return ParameterInfo ( bridged: bridged. at ( index) ,
212+ hasLoweredAddresses: hasLoweredAddresses)
213+ }
214+ }
215+ }
216+
217+ public enum LifetimeDependenceConvention {
218+ case inherit
219+ case borrow
220+ case mutate
221+ }
222+
223+ extension FunctionConvention {
224+ /// Collection of LifetimeDependenceConvention? that parallels parameters.
225+ public struct ResultDependencies : Collection , CustomStringConvertible {
226+ let bridged : BridgedLifetimeDependenceInfo
227+ let paramCount : Int
228+ let hasSelfParam : Bool
229+
230+ init ( bridged: BridgedLifetimeDependenceInfo , parameterCount: Int ,
231+ hasSelfParameter: Bool ) {
232+ assert ( !bridged. empty ( ) )
233+ self . bridged = bridged
234+ self . paramCount = parameterCount
235+ self . hasSelfParam = hasSelfParameter
236+ }
237+
238+ public var startIndex : Int { 0 }
239+
240+ public var endIndex : Int { paramCount }
241+
242+ public func index( after index: Int ) -> Int {
243+ return index + 1
244+ }
245+
246+ public subscript( _ index: Int ) -> LifetimeDependenceConvention ? {
247+ let inherit = bridged. checkInherit ( bridgedIndex ( parameterIndex: index) )
248+ let borrow = bridged. checkBorrow ( bridgedIndex ( parameterIndex: index) )
249+ let mutate = bridged. checkMutate ( bridgedIndex ( parameterIndex: index) )
250+ if inherit {
251+ assert ( !borrow && !mutate, " mutualy exclusive lifetime specifiers " )
252+ return . inherit
253+ }
254+ if borrow {
255+ assert ( !mutate, " mutualy exclusive lifetime specifiers " )
256+ return . borrow
257+ }
258+ if mutate {
259+ return . mutate
260+ }
261+ return nil
262+ }
263+
264+ // In Sema's LifetimeDependenceInfo, 'self' is always index zero,
265+ // whether it exists or not. In SILFunctionType, 'self' is the
266+ // last parameter if it exists.
267+ private func bridgedIndex( parameterIndex: Int ) -> Int {
268+ if hasSelfParam, parameterIndex == ( paramCount - 1 ) {
269+ return 0
270+ }
271+ return parameterIndex + 1
272+ }
273+
274+ public var description : String {
275+ String ( taking: bridged. getDebugDescription ( ) )
276+ }
277+ }
278+ }
279+
196280public enum ResultConvention : CustomStringConvertible {
197281 /// This result is returned indirectly, i.e. by passing the address of an uninitialized object in memory. The callee is responsible for leaving an initialized object at this address. The callee may assume that the address does not alias any valid object.
198282 case indirect
0 commit comments