Skip to content

Commit d56ef3b

Browse files
committed
Improve the ArgumentConvention API
1 parent 043bf43 commit d56ef3b

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,31 @@ public struct ApplyOperandConventions : Collection {
5959
}
6060

6161
public func convention(of operand: Operand) -> ArgumentConvention? {
62-
guard let argIdx = calleeArgumentIndex(of: operand) else { return nil }
62+
guard let argIdx = calleeArgumentIndex(of: operand) else {
63+
return nil
64+
}
6365
return calleeArgumentConventions[argIdx]
6466
}
6567

68+
public func originalParameter(of operand: Operand) -> ParameterInfo? {
69+
guard let argIdx = calleeArgumentIndex(of: operand) else {
70+
return nil
71+
}
72+
guard argIdx >= calleeArgumentConventions.firstParameterIndex else {
73+
return nil
74+
}
75+
return calleeArgumentConventions.originalParameters[argIdx]
76+
}
77+
78+
public var firstParameterOperandIndex: Int {
79+
return ApplyOperandConventions.firstArgumentIndex +
80+
calleeArgumentConventions.firstParameterIndex
81+
}
82+
83+
// TODO: rewrite uses of this API to pass an Operand instead, and
84+
// make this private. No client should have multiple integer
85+
// indices, some of which are caller indices, and some of which are
86+
// callee indices.
6687
public func calleeArgumentIndex(ofOperandIndex index: Int) -> Int? {
6788
let callerArgIdx = index - ApplyOperandConventions.firstArgumentIndex
6889
guard callerArgIdx >= 0 else { return nil }
@@ -108,22 +129,23 @@ extension ApplySite {
108129
argumentOperands.values
109130
}
110131

111-
public var substitutionMap: SubstitutionMap {
112-
SubstitutionMap(bridged.ApplySite_getSubstitutionMap())
113-
}
114-
115-
/// Get the conventions of the callee without the applied substitutions.
116-
public var originalCalleeConvention: FunctionConvention {
117-
FunctionConvention(for: callee.type.bridged.getASTType(), in: parentFunction)
132+
/// Indirect results including the error result.
133+
public var indirectResultOperands: OperandArray {
134+
return argumentOperands[0..<operandConventions.firstParameterOperandIndex]
118135
}
119136

120-
/// Get the conventions of the callee with the applied substitutions.
121-
public var substitutedCalleeConvention: FunctionConvention {
122-
FunctionConvention(for: bridged.ApplySite_getSubstitutedCalleeType(), in: parentFunction)
137+
public var substitutionMap: SubstitutionMap {
138+
SubstitutionMap(bridged.ApplySite_getSubstitutionMap())
123139
}
124140

125141
public var calleeArgumentConventions: ArgumentConventions {
126-
ArgumentConventions(functionConvention: substitutedCalleeConvention)
142+
let originalConv = FunctionConvention(for: callee.type.bridged.getASTType(),
143+
in: parentFunction)
144+
let substConv = FunctionConvention(
145+
for: bridged.ApplySite_getSubstitutedCalleeType(),
146+
in: parentFunction)
147+
return ArgumentConventions(originalFunctionConvention: originalConv,
148+
substitutedFunctionConvention: substConv)
127149
}
128150

129151
public var operandConventions: ApplyOperandConventions {

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,34 @@ public struct TerminatorResult {
170170
/// ArgumentConventions indexed on a SIL function's argument index.
171171
/// When derived from an ApplySite, this corresponds to the callee index.
172172
public struct ArgumentConventions : Collection, CustomStringConvertible {
173-
public let functionConvention: FunctionConvention
173+
public let originalFunctionConvention: FunctionConvention
174+
public let substitutedFunctionConvention: FunctionConvention?
174175

175-
public var bridgedFunctionType: BridgedASTType { functionConvention.bridgedFunctionType }
176-
177-
/// Indirect results including the error result.
176+
/// Indirect results including the error result. Apply type
177+
/// substitution if it is available.
178178
public var indirectSILResults: LazyFilterSequence<FunctionConvention.Results> {
179-
functionConvention.indirectSILResults
179+
if let substitutedFunctionConvention {
180+
return substitutedFunctionConvention.indirectSILResults
181+
}
182+
return originalFunctionConvention.indirectSILResults
180183
}
181184

182185
/// Number of SIL arguments for the function type's results
183186
/// including the error result. Use this to avoid lazy iteration.
184187
var indirectSILResultCount: Int {
185-
functionConvention.indirectSILResultCount
188+
originalFunctionConvention.indirectSILResultCount
189+
}
190+
191+
public var originalParameters: FunctionConvention.Parameters {
192+
originalFunctionConvention.parameters
186193
}
187194

188-
public var parameters: FunctionConvention.Parameters { functionConvention.parameters }
195+
public var parameters: FunctionConvention.Parameters {
196+
if let substitutedFunctionConvention {
197+
return substitutedFunctionConvention.parameters
198+
}
199+
return originalFunctionConvention.parameters
200+
}
189201

190202
public var startIndex: Int { 0 }
191203

@@ -207,17 +219,22 @@ public struct ArgumentConventions : Collection, CustomStringConvertible {
207219

208220
/// The SIL argument index of the 'self' paramter.
209221
var selfIndex: Int? {
210-
guard functionConvention.hasSelfParameter else { return nil }
222+
guard originalFunctionConvention.hasSelfParameter else { return nil }
211223
// self is the last parameter
212224
return endIndex - 1
213225
}
214226

215227
public var description: String {
216-
var str = String(taking: bridgedFunctionType.getDebugDescription())
228+
var str = String(taking: originalFunctionConvention.bridgedFunctionType.getDebugDescription())
229+
if let substitutedFunctionConvention {
230+
str += "\n" + String(taking: substitutedFunctionConvention.bridgedFunctionType.getDebugDescription())
231+
}
217232
indirectSILResults.forEach {
218233
str += "\nindirect result: " + $0.description
219234
}
220-
parameters.forEach { str += "\n parameter: " + $0.description }
235+
parameters.forEach {
236+
str += "\n parameter: " + $0.description
237+
}
221238
return str
222239
}
223240
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
103103
}
104104

105105
public var argumentConventions: ArgumentConventions {
106-
ArgumentConventions(functionConvention: convention)
106+
ArgumentConventions(originalFunctionConvention: convention,
107+
substitutedFunctionConvention: nil)
107108
}
108109

109110
public var returnInstruction: ReturnInst? {

0 commit comments

Comments
 (0)