Skip to content

Commit c0c0b80

Browse files
committed
Swift Bridging: use C++ instead of C bridging for BridgedGlobalVar
1 parent ae7770d commit c0c0b80

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

SwiftCompilerSources/Sources/SIL/GlobalVariable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import SILBridging
1515

1616
final public class GlobalVariable : CustomStringConvertible, HasShortDescription, Hashable {
1717
public var name: StringRef {
18-
return StringRef(bridged: SILGlobalVariable_getName(bridged))
18+
return StringRef(bridged: bridged.getName())
1919
}
2020

2121
public var description: String {
22-
let stdString = SILGlobalVariable_debugDescription(bridged)
22+
let stdString = bridged.getDebugDescription()
2323
return String(_cxxString: stdString)
2424
}
2525

2626
public var shortDescription: String { name.string }
2727

28-
public var isLet: Bool { SILGlobalVariable_isLet(bridged) != 0 }
28+
public var isLet: Bool { bridged.isLet() }
2929

3030
// TODO: initializer instructions
3131

include/swift/SIL/SILBridging.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,21 @@ struct OptionalBridgedFunction {
285285
OptionalSwiftObject obj;
286286
};
287287

288-
typedef struct {
288+
struct BridgedGlobalVar {
289289
SwiftObject obj;
290-
} BridgedGlobalVar;
290+
291+
SWIFT_IMPORT_UNSAFE
292+
swift::SILGlobalVariable * _Nonnull getGlobal() const {
293+
return static_cast<swift::SILGlobalVariable *>(obj);
294+
}
295+
296+
std::string getDebugDescription() const;
297+
298+
SWIFT_IMPORT_UNSAFE
299+
llvm::StringRef getName() const { return getGlobal()->getName(); }
300+
301+
bool isLet() const { return getGlobal()->isLet(); }
302+
};
291303

292304
struct BridgedBasicBlock {
293305
SwiftObject obj;
@@ -458,10 +470,6 @@ void PassContext_eraseInstruction(BridgedPassContext passContext,
458470
void PassContext_eraseBlock(BridgedPassContext passContext,
459471
BridgedBasicBlock block);
460472

461-
llvm::StringRef SILGlobalVariable_getName(BridgedGlobalVar global);
462-
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
463-
SwiftInt SILGlobalVariable_isLet(BridgedGlobalVar global);
464-
465473
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
466474
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
467475
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);

include/swift/SIL/SILBridgingUtils.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ template <class A = SILArgument> A *castToArgument(BridgedArgument argument) {
5151
return cast<A>(static_cast<SILArgument *>(argument.obj));
5252
}
5353

54-
inline SILGlobalVariable *castToGlobal(BridgedGlobalVar global) {
55-
return static_cast<SILGlobalVariable *>(global.obj);
56-
}
57-
5854
inline ValueOwnershipKind castToOwnership(BridgedValue::Ownership ownership) {
5955
switch (ownership) {
6056
case BridgedValue::Ownership::Unowned: return OwnershipKind::Unowned;

lib/SIL/Utils/SILBridging.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -363,22 +363,14 @@ bool SILLocation_hasSameSourceLocation(swift::SILDebugLocation lhs, swift::SILDe
363363
// SILGlobalVariable
364364
//===----------------------------------------------------------------------===//
365365

366-
llvm::StringRef SILGlobalVariable_getName(BridgedGlobalVar global) {
367-
return castToGlobal(global)->getName();
368-
}
369-
370-
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global) {
366+
std::string BridgedGlobalVar::getDebugDescription() const {
371367
std::string str;
372368
llvm::raw_string_ostream os(str);
373-
castToGlobal(global)->print(os);
369+
getGlobal()->print(os);
374370
str.pop_back(); // Remove trailing newline.
375371
return str;
376372
}
377373

378-
SwiftInt SILGlobalVariable_isLet(BridgedGlobalVar global) {
379-
return castToGlobal(global)->isLet();
380-
}
381-
382374
//===----------------------------------------------------------------------===//
383375
// SILVTable
384376
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)