Skip to content

Commit 1396d6b

Browse files
committed
Bridge YieldInfo
1 parent 9770ce4 commit 1396d6b

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
@@ -140,13 +140,8 @@ extension ApplySite {
140140
}
141141

142142
public var calleeArgumentConventions: ArgumentConventions {
143-
let originalConv = FunctionConvention(for: callee.type.bridged.getASTType(),
144-
in: parentFunction)
145-
let substConv = FunctionConvention(
146-
for: bridged.ApplySite_getSubstitutedCalleeType(),
147-
in: parentFunction)
148-
return ArgumentConventions(originalFunctionConvention: originalConv,
149-
substitutedFunctionConvention: substConv)
143+
ArgumentConventions(originalFunctionConvention: originalFunctionConvention,
144+
substitutedFunctionConvention: substitutedFunctionConvention)
150145
}
151146

152147
public var operandConventions: ApplyOperandConventions {
@@ -155,14 +150,6 @@ extension ApplySite {
155150
unappliedArgumentCount: bridged.PartialApply_getCalleeArgIndexOfFirstAppliedArg())
156151
}
157152

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

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

209230
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)