Skip to content

Commit 47ac2d2

Browse files
committed
Swift Bridging: use C++ instead of C bridging for BridgedArgument
1 parent 7789b40 commit 47ac2d2

File tree

4 files changed

+49
-100
lines changed

4 files changed

+49
-100
lines changed

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Argument : Value, Hashable {
2020
public var definingInstruction: Instruction? { nil }
2121

2222
public var parentBlock: BasicBlock {
23-
return SILArgument_getParent(bridged).block
23+
return bridged.getParent().block
2424
}
2525

2626
var bridged: BridgedArgument { BridgedArgument(obj: SwiftObject(self)) }
@@ -40,7 +40,7 @@ public class Argument : Value, Hashable {
4040

4141
final public class FunctionArgument : Argument {
4242
public var convention: ArgumentConvention {
43-
SILArgument_getConvention(bridged).convention
43+
bridged.getConvention().convention
4444
}
4545
}
4646

@@ -193,7 +193,7 @@ extension BridgedArgument {
193193
public var functionArgument: FunctionArgument { obj.getAs(FunctionArgument.self) }
194194
}
195195

196-
extension BridgedFunction.ArgumentConvention {
196+
extension BridgedArgumentConvention {
197197
var convention: ArgumentConvention {
198198
switch self {
199199
case .Indirect_In: return .indirectIn

include/swift/SIL/SILArgument.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,6 @@ class SILArgument : public ValueBase {
218218
}
219219
};
220220

221-
inline SILArgument *castToArgument(SwiftObject argument) {
222-
return static_cast<SILArgument *>(argument);
223-
}
224-
225221
class SILPhiArgument : public SILArgument {
226222
friend class SILBasicBlock;
227223

include/swift/SIL/SILBridging.h

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ struct BridgedOperandArray {
146146
SwiftInt count;
147147
};
148148

149+
// Unfortunately we need to take a detour over this enum.
150+
// Currently it's not possible to switch over `SILArgumentConvention::ConventionType`,
151+
// because it's not a class enum.
152+
enum class BridgedArgumentConvention {
153+
Indirect_In = swift::SILArgumentConvention::Indirect_In,
154+
Indirect_In_Guaranteed = swift::SILArgumentConvention::Indirect_In_Guaranteed,
155+
Indirect_Inout = swift::SILArgumentConvention::Indirect_Inout,
156+
Indirect_InoutAliasable = swift::SILArgumentConvention::Indirect_InoutAliasable,
157+
Indirect_Out = swift::SILArgumentConvention::Indirect_Out,
158+
Direct_Owned = swift::SILArgumentConvention::Direct_Owned,
159+
Direct_Unowned = swift::SILArgumentConvention::Direct_Unowned,
160+
Direct_Guaranteed = swift::SILArgumentConvention::Direct_Guaranteed,
161+
Pack_Owned = swift::SILArgumentConvention::Pack_Owned,
162+
Pack_Inout = swift::SILArgumentConvention::Pack_Inout,
163+
Pack_Guaranteed = swift::SILArgumentConvention::Pack_Guaranteed,
164+
Pack_Out = swift::SILArgumentConvention::Pack_Out
165+
};
166+
167+
inline BridgedArgumentConvention castToArgumentConvention(swift::SILArgumentConvention convention) {
168+
return static_cast<BridgedArgumentConvention>(convention.Value);
169+
}
170+
149171
struct BridgedFunction {
150172
SwiftObject obj;
151173

@@ -154,20 +176,6 @@ struct BridgedFunction {
154176
return static_cast<swift::SILFunction *>(obj);
155177
}
156178

157-
// Unfortunately we need to take a detour over this enum.
158-
// Currently it's not possible to switch over `SILArgumentConvention::ConventionType`,
159-
// because it's not a class enum.
160-
enum class ArgumentConvention {
161-
Indirect_In,
162-
Indirect_In_Guaranteed,
163-
Indirect_Inout,
164-
Indirect_InoutAliasable,
165-
Indirect_Out,
166-
Direct_Owned,
167-
Direct_Unowned,
168-
Direct_Guaranteed
169-
};
170-
171179
SWIFT_IMPORT_UNSAFE
172180
llvm::StringRef getName() const { return getFunction()->getName(); }
173181

@@ -206,11 +214,9 @@ struct BridgedFunction {
206214
return conv.getSILArgumentType(idx, getFunction()->getTypeExpansionContext());
207215
}
208216

209-
ArgumentConvention getBridged(swift::SILArgumentConvention conv) const;
210-
211-
ArgumentConvention getSILArgumentConvention(SwiftInt idx) const {
217+
BridgedArgumentConvention getSILArgumentConvention(SwiftInt idx) const {
212218
swift::SILFunctionConventions conv(getFunction()->getConventionsInContext());
213-
return getBridged(swift::SILArgumentConvention(conv.getParamInfoForSILArg(idx).getConvention()));
219+
return castToArgumentConvention(swift::SILArgumentConvention(conv.getParamInfoForSILArg(idx).getConvention()));
214220
}
215221

216222
swift::SILType getSILResultType() const {
@@ -307,8 +313,6 @@ struct BridgedGlobalVar {
307313
bool isLet() const { return getGlobal()->isLet(); }
308314
};
309315

310-
BridgedFunction::ArgumentConvention castToArgumentConvention(swift::SILArgumentConvention convention);
311-
312316
struct BridgedMultiValueResult {
313317
SwiftObject obj;
314318
};
@@ -621,7 +625,7 @@ struct BridgedInstruction {
621625
return as.getSubstitutionMap();
622626
}
623627

624-
BridgedFunction::ArgumentConvention ApplySite_getArgumentConvention(SwiftInt calleeArgIdx) const {
628+
BridgedArgumentConvention ApplySite_getArgumentConvention(SwiftInt calleeArgIdx) const {
625629
auto as = swift::ApplySite(getInst());
626630
auto conv = as.getSubstCalleeConv().getSILArgumentConvention(calleeArgIdx);
627631
return castToArgumentConvention(conv.Value);
@@ -637,13 +641,25 @@ struct BridgedInstruction {
637641
}
638642
};
639643

640-
typedef struct {
644+
struct BridgedArgument {
641645
SwiftObject obj;
642-
} BridgedArgument;
643646

644-
typedef struct {
647+
swift::SILArgument * _Nonnull getArgument() const {
648+
return static_cast<swift::SILArgument *>(obj);
649+
}
650+
651+
SWIFT_IMPORT_UNSAFE
652+
inline BridgedBasicBlock getParent() const;
653+
654+
BridgedArgumentConvention getConvention() const {
655+
auto *fArg = llvm::cast<swift::SILFunctionArgument>(getArgument());
656+
return castToArgumentConvention(fArg->getArgumentConvention());
657+
}
658+
};
659+
660+
struct OptionalBridgedArgument {
645661
OptionalSwiftObject obj;
646-
} OptionalBridgedArgument;
662+
};
647663

648664
struct OptionalBridgedBasicBlock {
649665
OptionalSwiftObject obj;
@@ -876,9 +892,6 @@ SILLocation_getAutogeneratedLocation(swift::SILDebugLocation loc);
876892
bool SILLocation_equal(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
877893
bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDebugLocation rhs);
878894

879-
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
880-
BridgedFunction::ArgumentConvention SILArgument_getConvention(BridgedArgument argument);
881-
882895
BridgedInstruction MultiValueInstResult_getParent(BridgedMultiValueResult result);
883896
SwiftInt MultiValueInstResult_getIndex(BridgedMultiValueResult result);
884897

@@ -973,6 +986,11 @@ OptionalBridgedSuccessor BridgedBasicBlock::getFirstPred() const {
973986
return {getBlock()->pred_begin().getSuccessorRef()};
974987
}
975988

989+
BridgedBasicBlock BridgedArgument::getParent() const {
990+
return {getArgument()->getParent()};
991+
}
992+
993+
976994
SWIFT_END_NULLABILITY_ANNOTATIONS
977995

978996
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -151,32 +151,6 @@ std::string BridgedFunction::getDebugDescription() const {
151151
return str;
152152
}
153153

154-
BridgedFunction::ArgumentConvention BridgedFunction::getBridged(SILArgumentConvention conv) const {
155-
switch (conv.Value) {
156-
case SILArgumentConvention::Indirect_Inout:
157-
return BridgedFunction::ArgumentConvention::Indirect_Inout;
158-
case SILArgumentConvention::Indirect_InoutAliasable:
159-
return BridgedFunction::ArgumentConvention::Indirect_InoutAliasable;
160-
case SILArgumentConvention::Indirect_In_Guaranteed:
161-
return BridgedFunction::ArgumentConvention::Indirect_In_Guaranteed;
162-
case SILArgumentConvention::Indirect_In:
163-
return BridgedFunction::ArgumentConvention::Indirect_In;
164-
case SILArgumentConvention::Indirect_Out:
165-
return BridgedFunction::ArgumentConvention::Indirect_Out;
166-
case SILArgumentConvention::Direct_Unowned:
167-
return BridgedFunction::ArgumentConvention::Direct_Unowned;
168-
case SILArgumentConvention::Direct_Owned:
169-
return BridgedFunction::ArgumentConvention::Direct_Owned;
170-
case SILArgumentConvention::Direct_Guaranteed:
171-
return BridgedFunction::ArgumentConvention::Direct_Guaranteed;
172-
case SILArgumentConvention::Pack_Inout:
173-
case SILArgumentConvention::Pack_Out:
174-
case SILArgumentConvention::Pack_Guaranteed:
175-
case SILArgumentConvention::Pack_Owned:
176-
llvm_unreachable("cannot bridge variadic generics");
177-
}
178-
}
179-
180154
//===----------------------------------------------------------------------===//
181155
// SILBasicBlock
182156
//===----------------------------------------------------------------------===//
@@ -189,45 +163,6 @@ std::string BridgedBasicBlock::getDebugDescription() const {
189163
return str;
190164
}
191165

192-
//===----------------------------------------------------------------------===//
193-
// SILArgument
194-
//===----------------------------------------------------------------------===//
195-
196-
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument) {
197-
return {castToArgument(argument)->getParent()};
198-
}
199-
200-
BridgedFunction::ArgumentConvention castToArgumentConvention(SILArgumentConvention convention) {
201-
switch (convention) {
202-
case SILArgumentConvention::Indirect_Inout:
203-
return BridgedFunction::ArgumentConvention::Indirect_Inout;
204-
case SILArgumentConvention::Indirect_InoutAliasable:
205-
return BridgedFunction::ArgumentConvention::Indirect_InoutAliasable;
206-
case SILArgumentConvention::Indirect_In_Guaranteed:
207-
return BridgedFunction::ArgumentConvention::Indirect_In_Guaranteed;
208-
case SILArgumentConvention::Indirect_In:
209-
return BridgedFunction::ArgumentConvention::Indirect_In;
210-
case SILArgumentConvention::Indirect_Out:
211-
return BridgedFunction::ArgumentConvention::Indirect_Out;
212-
case SILArgumentConvention::Direct_Unowned:
213-
return BridgedFunction::ArgumentConvention::Direct_Unowned;
214-
case SILArgumentConvention::Direct_Owned:
215-
return BridgedFunction::ArgumentConvention::Direct_Owned;
216-
case SILArgumentConvention::Direct_Guaranteed:
217-
return BridgedFunction::ArgumentConvention::Direct_Guaranteed;
218-
case SILArgumentConvention::Pack_Inout:
219-
case SILArgumentConvention::Pack_Out:
220-
case SILArgumentConvention::Pack_Guaranteed:
221-
case SILArgumentConvention::Pack_Owned:
222-
llvm_unreachable("cannot bridge variadic generics");
223-
}
224-
}
225-
226-
BridgedFunction::ArgumentConvention SILArgument_getConvention(BridgedArgument argument) {
227-
auto *arg = castToArgument<SILFunctionArgument>(argument);
228-
return castToArgumentConvention(arg->getArgumentConvention());
229-
}
230-
231166
//===----------------------------------------------------------------------===//
232167
// SILValue
233168
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)