Skip to content

Commit 7b4225c

Browse files
committed
Bridge YieldInfo
1 parent d56ef3b commit 7b4225c

File tree

5 files changed

+134
-17
lines changed

5 files changed

+134
-17
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,8 @@ extension ApplySite {
139139
}
140140

141141
public var calleeArgumentConventions: ArgumentConventions {
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)
142+
ArgumentConventions(originalFunctionConvention: originalFunctionConvention,
143+
substitutedFunctionConvention: substitutedFunctionConvention)
149144
}
150145

151146
public var operandConventions: ApplyOperandConventions {
@@ -154,14 +149,6 @@ extension ApplySite {
154149
unappliedArgumentCount: bridged.PartialApply_getCalleeArgIndexOfFirstAppliedArg())
155150
}
156151

157-
/// Returns the argument index of an operand.
158-
///
159-
/// Returns nil if 'operand' is not an argument operand. This is the case if
160-
/// it's the callee function operand.
161-
public func calleeArgumentIndex(of operand: Operand) -> Int? {
162-
operandConventions.calleeArgumentIndex(of: operand)
163-
}
164-
165152
/// Returns true if `operand` is the callee function operand and not am argument operand.
166153
public func isCallee(operand: Operand) -> Bool {
167154
operandConventions.isCallee(operand: operand)
@@ -170,6 +157,17 @@ extension ApplySite {
170157
public func convention(of operand: Operand) -> ArgumentConvention? {
171158
operandConventions.convention(of: operand)
172159
}
160+
161+
public var yieldConventions: YieldConventions {
162+
YieldConventions(originalFunctionConvention: originalFunctionConvention,
163+
substitutedFunctionConvention: substitutedFunctionConvention)
164+
}
165+
166+
public func convention(of yield: MultipleValueInstructionResult)
167+
-> ArgumentConvention {
168+
assert(yield.definingInstruction == self)
169+
return yieldConventions[yield.index]
170+
}
173171

174172
/// Converts an argument index of a callee to the corresponding apply operand.
175173
///
@@ -203,6 +201,29 @@ extension ApplySite {
203201
}
204202
return false
205203
}
204+
205+
/// Returns the argument index of an operand.
206+
///
207+
/// Returns nil if 'operand' is not an argument operand. This is the case if
208+
/// it's the callee function operand.
209+
///
210+
/// Warning: the returned integer can be misused as an index into
211+
/// the wrong collection. Replace uses of this API with safer APIs.
212+
public func calleeArgumentIndex(of operand: Operand) -> Int? {
213+
operandConventions.calleeArgumentIndex(of: operand)
214+
}
215+
}
216+
217+
extension ApplySite {
218+
private var originalFunctionConvention: FunctionConvention {
219+
FunctionConvention(for: callee.type.bridged.getASTType(),
220+
in: parentFunction)
221+
}
222+
223+
private var substitutedFunctionConvention: FunctionConvention {
224+
FunctionConvention(for: bridged.ApplySite_getSubstitutedCalleeType(),
225+
in: parentFunction)
226+
}
206227
}
207228

208229
public protocol FullApplySite : ApplySite {

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,41 @@ public struct ArgumentConventions : Collection, CustomStringConvertible {
239239
}
240240
}
241241

242+
public struct YieldConventions : Collection, CustomStringConvertible {
243+
public let originalFunctionConvention: FunctionConvention
244+
public let substitutedFunctionConvention: FunctionConvention?
245+
246+
public var yields: FunctionConvention.Yields {
247+
if let substitutedFunctionConvention {
248+
return substitutedFunctionConvention.yields
249+
}
250+
return originalFunctionConvention.yields
251+
}
252+
253+
public var startIndex: Int { 0 }
254+
255+
public var endIndex: Int { yields.count }
256+
257+
public func index(after index: Int) -> Int {
258+
return index + 1
259+
}
260+
261+
public subscript(_ index: Int) -> ArgumentConvention {
262+
return yields[index].convention
263+
}
264+
265+
public var description: String {
266+
var str = String(taking: originalFunctionConvention.bridgedFunctionType.getDebugDescription())
267+
if let substitutedFunctionConvention {
268+
str += "\n" + String(taking: substitutedFunctionConvention.bridgedFunctionType.getDebugDescription())
269+
}
270+
yields.forEach {
271+
str += "\n yield: " + $0.description
272+
}
273+
return str
274+
}
275+
}
276+
242277
public enum ArgumentConvention : CustomStringConvertible {
243278
/// This argument is passed indirectly, i.e. by directly passing the address
244279
/// of an object in memory. The callee is responsible for destroying the

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ public struct FunctionConvention : CustomStringConvertible {
9898
bridgedFunctionType.SILFunctionType_hasSelfParam()
9999
}
100100

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+
119+
public var yields: Yields {
120+
Yields(bridged: bridgedFunctionType.SILFunctionType_getYields(),
121+
hasLoweredAddresses: hasLoweredAddresses)
122+
}
123+
101124
public var description: String {
102125
var str = String(taking: bridgedFunctionType.getDebugDescription())
103126
parameters.forEach { str += "\nparameter: " + $0.description }
@@ -145,8 +168,9 @@ public struct ParameterInfo : CustomStringConvertible {
145168
public let hasLoweredAddresses: Bool
146169

147170
/// Is this parameter passed indirectly in SIL? Most formally
148-
/// indirect results can be passed directly in SIL. This depends on
149-
/// whether the calling function has lowered addresses.
171+
/// indirect results can be passed directly in SIL (opaque values
172+
/// mode). This depends on whether the calling function has lowered
173+
/// addresses.
150174
public var isSILIndirect: Bool {
151175
switch convention {
152176
case .indirectIn, .indirectInGuaranteed:

include/swift/SIL/SILBridging.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ struct BridgedParameterInfoArray {
181181
BridgedParameterInfo at(SwiftInt parameterIndex) const;
182182
};
183183

184+
struct BridgedYieldInfoArray {
185+
BridgedArrayRef yieldInfoArray;
186+
187+
#ifdef USED_IN_CPP_SOURCE
188+
BridgedYieldInfoArray(llvm::ArrayRef<swift::SILYieldInfo> yields)
189+
: yieldInfoArray(yields) {}
190+
191+
llvm::ArrayRef<swift::SILYieldInfo> unbridged() const {
192+
return yieldInfoArray.unbridged<swift::SILYieldInfo>();
193+
}
194+
#endif
195+
196+
BRIDGED_INLINE SwiftInt count() const;
197+
198+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
199+
BridgedParameterInfo at(SwiftInt resultIndex) const;
200+
};
201+
184202
// Temporary access to the AST type within SIL until ASTBridging provides it.
185203
struct BridgedASTType {
186204
swift::TypeBase * _Nullable type;
@@ -215,6 +233,9 @@ struct BridgedASTType {
215233
BridgedParameterInfoArray SILFunctionType_getParameters() const;
216234

217235
BRIDGED_INLINE bool SILFunctionType_hasSelfParam() const;
236+
237+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
238+
BridgedYieldInfoArray SILFunctionType_getYields() const;
218239
};
219240

220241
struct BridgedType {

include/swift/SIL/SILBridgingImpl.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ BridgedResultInfo BridgedResultInfoArray::at(SwiftInt resultIndex) const {
5353
return BridgedResultInfo(unbridged()[resultIndex]);
5454
}
5555

56+
//===----------------------------------------------------------------------===//
57+
// BridgedYieldInfo
58+
//===----------------------------------------------------------------------===//
59+
60+
SwiftInt BridgedYieldInfoArray::count() const {
61+
return unbridged().size();
62+
}
63+
64+
BridgedParameterInfo BridgedYieldInfoArray::at(SwiftInt resultIndex) const {
65+
return BridgedParameterInfo(unbridged()[resultIndex]);
66+
}
67+
5668
//===----------------------------------------------------------------------===//
5769
// BridgedParameterInfo
5870
//===----------------------------------------------------------------------===//
@@ -110,6 +122,10 @@ bool BridgedASTType::SILFunctionType_hasSelfParam() const {
110122
return unbridged()->castTo<swift::SILFunctionType>()->hasSelfParam();
111123
}
112124

125+
BridgedYieldInfoArray BridgedASTType::SILFunctionType_getYields() const {
126+
return unbridged()->castTo<swift::SILFunctionType>()->getYields();
127+
}
128+
113129
//===----------------------------------------------------------------------===//
114130
// BridgedType
115131
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)