@@ -42,10 +42,6 @@ public struct ApplyOperandConventions : Collection {
42
42
calleeArgumentConventions. count - unappliedArgumentCount
43
43
}
44
44
45
- public func isCallee( operand: Operand ) -> Bool {
46
- return operand. index == ApplyOperandConventions . calleeIndex
47
- }
48
-
49
45
public var startIndex : Int { ApplyOperandConventions . firstArgumentIndex }
50
46
51
47
public var endIndex : Int { ApplyOperandConventions . firstArgumentIndex + appliedArgumentCount }
@@ -55,45 +51,47 @@ public struct ApplyOperandConventions : Collection {
55
51
}
56
52
57
53
public subscript( _ operandIndex: Int ) -> ArgumentConvention {
58
- return calleeArgumentConventions [ calleeArgumentIndex ( ofOperandIndex: operandIndex) !]
54
+ return calleeArgumentConventions [
55
+ calleeArgumentIndex ( ofOperandIndex: operandIndex) !]
59
56
}
60
57
61
- public func convention( of operand: Operand ) -> ArgumentConvention ? {
62
- guard let argIdx = calleeArgumentIndex ( of: operand) else {
63
- return nil
64
- }
65
- return calleeArgumentConventions [ argIdx]
58
+ public subscript( result operandIndex: Int ) -> ResultInfo ? {
59
+ return calleeArgumentConventions [ result:
60
+ calleeArgumentIndex ( ofOperandIndex: operandIndex) !]
66
61
}
67
62
68
- public func originalParameter ( of operand : Operand ) -> ParameterInfo ? {
69
- guard let argIdx = calleeArgumentIndex ( of : operand ) else {
70
- return nil
71
- }
72
- let firstParamIdx = calleeArgumentConventions . firstParameterIndex
73
- guard argIdx >= firstParamIdx else {
74
- return nil
75
- }
76
- return calleeArgumentConventions . originalParameters [ argIdx - firstParamIdx ]
63
+ public subscript ( parameter operandIndex : Int ) -> ParameterInfo ? {
64
+ return calleeArgumentConventions [ parameter :
65
+ calleeArgumentIndex ( ofOperandIndex : operandIndex ) ! ]
66
+ }
67
+
68
+ public subscript ( resultDependsOn operandIndex : Int )
69
+ -> LifetimeDependenceConvention ? {
70
+ return calleeArgumentConventions [ resultDependsOn :
71
+ calleeArgumentIndex ( ofOperandIndex : operandIndex ) ! ]
77
72
}
78
73
79
74
public var firstParameterOperandIndex : Int {
80
75
return ApplyOperandConventions . firstArgumentIndex +
81
76
calleeArgumentConventions. firstParameterIndex
82
77
}
83
78
84
- // TODO: rewrite uses of this API to pass an Operand instead, and
85
- // make this private. No client should have multiple integer
86
- // indices, some of which are caller indices, and some of which are
87
- // callee indices.
79
+ // TODO: rewrite uses of this API to avoid manipulating integer
80
+ // indices, and make this private. No client should have multiple
81
+ // integer indices, some of which are caller indices, and some of
82
+ // which are callee indices.
88
83
public func calleeArgumentIndex( ofOperandIndex index: Int ) -> Int ? {
89
- let callerArgIdx = index - ApplyOperandConventions. firstArgumentIndex
90
- guard callerArgIdx >= 0 else { return nil }
91
-
84
+ let callerArgIdx = index - startIndex
85
+ if callerArgIdx < 0 {
86
+ return nil
87
+ }
92
88
let calleeArgIdx = callerArgIdx + unappliedArgumentCount
93
- guard calleeArgIdx < calleeArgumentConventions. count else { return nil }
89
+ assert ( calleeArgIdx < calleeArgumentConventions. count,
90
+ " invalid operand index " )
94
91
return calleeArgIdx
95
92
}
96
93
94
+ // TODO: this should be private.
97
95
public func calleeArgumentIndex( of operand: Operand ) -> Int ? {
98
96
calleeArgumentIndex ( ofOperandIndex: operand. index)
99
97
}
@@ -114,7 +112,21 @@ extension ApplySite {
114
112
return callee. type. isAsyncFunction
115
113
}
116
114
117
- /// Returns the subset of operands which are argument operands.
115
+ public var referencedFunction : Function ? {
116
+ if let fri = callee as? FunctionRefInst {
117
+ return fri. referencedFunction
118
+ }
119
+ return nil
120
+ }
121
+
122
+ public func hasSemanticsAttribute( _ attr: StaticString ) -> Bool {
123
+ if let callee = referencedFunction {
124
+ return callee. hasSemanticsAttribute ( attr)
125
+ }
126
+ return false
127
+ }
128
+
129
+ /// Returns the subset of operands that are argument operands.
118
130
///
119
131
/// This does not include the callee function operand.
120
132
public var argumentOperands : OperandArray {
@@ -123,6 +135,15 @@ extension ApplySite {
123
135
return operands [ offset..< ( numArgs + offset) ]
124
136
}
125
137
138
+ /// Returns the subset of operands that are parameters. This does
139
+ /// not include indirect results. This does include 'self'.
140
+ public var parameterOperands : OperandArray {
141
+ let firstParamIdx =
142
+ operandConventions. calleeArgumentConventions. firstParameterIndex
143
+ let argOpers = argumentOperands // bridged call
144
+ return argOpers [ firstParamIdx..< argOpers. count]
145
+ }
146
+
126
147
/// Returns the subset of operand values which are arguments.
127
148
///
128
149
/// This does not include the callee function operand.
@@ -132,17 +153,22 @@ extension ApplySite {
132
153
133
154
/// Indirect results including the error result.
134
155
public var indirectResultOperands : OperandArray {
135
- let offset = ApplyOperandConventions . firstArgumentIndex
136
- return operands [ offset..< operandConventions. firstParameterOperandIndex]
156
+ let ops = operandConventions
157
+ return operands [ ops. startIndex..< ops. firstParameterOperandIndex]
158
+ }
159
+
160
+ public func isIndirectResult( operand: Operand ) -> Bool {
161
+ let idx = operand. index
162
+ let ops = operandConventions
163
+ return idx >= ops. startIndex && idx < ops. firstParameterOperandIndex
137
164
}
138
165
139
166
public var substitutionMap : SubstitutionMap {
140
167
SubstitutionMap ( bridged. ApplySite_getSubstitutionMap ( ) )
141
168
}
142
169
143
170
public var calleeArgumentConventions : ArgumentConventions {
144
- ArgumentConventions ( originalFunctionConvention: originalFunctionConvention,
145
- substitutedFunctionConvention: substitutedFunctionConvention)
171
+ ArgumentConventions ( convention: functionConvention)
146
172
}
147
173
148
174
public var operandConventions : ApplyOperandConventions {
@@ -153,16 +179,39 @@ extension ApplySite {
153
179
154
180
/// Returns true if `operand` is the callee function operand and not am argument operand.
155
181
public func isCallee( operand: Operand ) -> Bool {
156
- operandConventions . isCallee ( operand: operand )
182
+ operand. index == ApplyOperandConventions . calleeIndex
157
183
}
158
184
159
185
public func convention( of operand: Operand ) -> ArgumentConvention ? {
160
- operandConventions. convention ( of: operand)
186
+ let idx = operand. index
187
+ return idx < operandConventions. startIndex ? nil : operandConventions [ idx]
188
+ }
189
+
190
+ public func result( for operand: Operand ) -> ResultInfo ? {
191
+ let idx = operand. index
192
+ return idx < operandConventions. startIndex ? nil
193
+ : operandConventions [ result: idx]
194
+ }
195
+
196
+ public func parameter( for operand: Operand ) -> ParameterInfo ? {
197
+ let idx = operand. index
198
+ return idx < operandConventions. startIndex ? nil
199
+ : operandConventions [ parameter: idx]
200
+ }
201
+
202
+ public func resultDependence( on operand: Operand )
203
+ -> LifetimeDependenceConvention ? {
204
+ let idx = operand. index
205
+ return idx < operandConventions. startIndex ? nil
206
+ : operandConventions [ resultDependsOn: idx]
207
+ }
208
+
209
+ public var hasResultDependence : Bool {
210
+ functionConvention. resultDependencies != nil
161
211
}
162
-
212
+
163
213
public var yieldConventions : YieldConventions {
164
- YieldConventions ( originalFunctionConvention: originalFunctionConvention,
165
- substitutedFunctionConvention: substitutedFunctionConvention)
214
+ YieldConventions ( convention: functionConvention)
166
215
}
167
216
168
217
public func convention( of yield: MultipleValueInstructionResult )
@@ -184,45 +233,30 @@ extension ApplySite {
184
233
/// %pa = partial_apply @callee(c, d, e)
185
234
/// %a = apply %pa (a, b)
186
235
/// ```
236
+ ///
237
+ /// TODO: delete this API and rewrite the users.
187
238
public func operand( forCalleeArgumentIndex calleeArgIdx: Int ) -> Operand ? {
188
239
let callerArgIdx = calleeArgIdx - operandConventions. unappliedArgumentCount
189
240
guard callerArgIdx >= 0 && callerArgIdx < numArguments else { return nil }
190
241
return argumentOperands [ callerArgIdx]
191
242
}
192
243
193
- public var referencedFunction : Function ? {
194
- if let fri = callee as? FunctionRefInst {
195
- return fri. referencedFunction
196
- }
197
- return nil
198
- }
199
-
200
- public func hasSemanticsAttribute( _ attr: StaticString ) -> Bool {
201
- if let callee = referencedFunction {
202
- return callee. hasSemanticsAttribute ( attr)
203
- }
204
- return false
205
- }
206
-
207
244
/// Returns the argument index of an operand.
208
245
///
209
246
/// Returns nil if 'operand' is not an argument operand. This is the case if
210
247
/// it's the callee function operand.
211
248
///
212
249
/// Warning: the returned integer can be misused as an index into
213
250
/// the wrong collection. Replace uses of this API with safer APIs.
251
+ ///
252
+ /// TODO: delete this API and rewrite the users.
214
253
public func calleeArgumentIndex( of operand: Operand ) -> Int ? {
215
254
operandConventions. calleeArgumentIndex ( of: operand)
216
255
}
217
256
}
218
257
219
258
extension ApplySite {
220
- private var originalFunctionConvention : FunctionConvention {
221
- FunctionConvention ( for: callee. type. bridged. getASTType ( ) ,
222
- in: parentFunction)
223
- }
224
-
225
- private var substitutedFunctionConvention : FunctionConvention {
259
+ private var functionConvention : FunctionConvention {
226
260
FunctionConvention ( for: bridged. ApplySite_getSubstitutedCalleeType ( ) ,
227
261
in: parentFunction)
228
262
}
@@ -238,7 +272,27 @@ extension FullApplySite {
238
272
/// The number of indirect out arguments.
239
273
///
240
274
/// 0 if the callee has a direct or no return value and 1, if it has an indirect return value.
275
+ ///
276
+ /// FIXME: This is incorrect in two cases: it does not include the
277
+ /// indirect error result, and, prior to address lowering, does not
278
+ /// include pack results.
241
279
public var numIndirectResultArguments : Int {
242
280
return bridged. FullApplySite_numIndirectResultArguments ( )
243
281
}
282
+
283
+ /// The direct result or yields produced by this apply. This does
284
+ /// not include any potential results returned by a coroutine
285
+ /// (end_apply results).
286
+ public var resultOrYields : SingleInlineArray < Value > {
287
+ var values = SingleInlineArray < Value > ( )
288
+ if let beginApply = self as? BeginApplyInst {
289
+ beginApply. yieldedValues. forEach { values. push ( $0) }
290
+ } else {
291
+ let result = singleDirectResult!
292
+ if !result. type. isEmpty ( in: parentFunction) {
293
+ values. push ( result)
294
+ }
295
+ }
296
+ return values
297
+ }
244
298
}
0 commit comments