Skip to content

Commit e469c16

Browse files
committed
Swift Bridging: remove BridgedType and directly use the C++ SILType instead
1 parent 598644f commit e469c16

File tree

9 files changed

+260
-337
lines changed

9 files changed

+260
-337
lines changed

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,40 @@ import Basic
1414
import SILBridging
1515

1616
public struct Type : CustomStringConvertible, NoReflectionChildren {
17-
public let bridged: BridgedType
17+
public let bridged: swift.SILType
1818

19-
public var isAddress: Bool { SILType_isAddress(bridged) != 0 }
19+
public var isAddress: Bool { bridged.isAddress() }
2020
public var isObject: Bool { !isAddress }
2121

2222
public func isTrivial(in function: Function) -> Bool {
23-
return SILType_isTrivial(bridged, function.bridged) != 0
23+
return bridged.isTrivial(function.bridged.getFunction())
2424
}
2525

2626
/// Returns true if the type is a trivial type and is and does not contain a Builtin.RawPointer.
2727
public func isTrivialNonPointer(in function: Function) -> Bool {
28-
return SILType_isNonTrivialOrContainsRawPointer(bridged, function.bridged) == 0
28+
return !bridged.isNonTrivialOrContainsRawPointer(function.bridged.getFunction())
2929
}
3030

3131
public func isReferenceCounted(in function: Function) -> Bool {
32-
return SILType_isReferenceCounted(bridged, function.bridged) != 0
32+
return bridged.isReferenceCounted(function.bridged.getFunction())
3333
}
3434

35-
public var hasArchetype: Bool { SILType_hasArchetype(bridged) }
35+
public var hasArchetype: Bool { bridged.hasArchetype() }
3636

37-
public var isNominal: Bool { SILType_isNominal(bridged) != 0 }
38-
public var isClass: Bool { SILType_isClass(bridged) != 0 }
39-
public var isStruct: Bool { SILType_isStruct(bridged) != 0 }
40-
public var isTuple: Bool { SILType_isTuple(bridged) != 0 }
41-
public var isEnum: Bool { SILType_isEnum(bridged) != 0 }
42-
public var isFunction: Bool { SILType_isFunction(bridged) }
43-
public var isMetatype: Bool { SILType_isMetatype(bridged) }
37+
public var isNominal: Bool { bridged.getNominalOrBoundGenericNominal() != nil }
38+
public var isClass: Bool { bridged.getClassOrBoundGenericClass() != nil }
39+
public var isStruct: Bool { bridged.getStructOrBoundGenericStruct() != nil }
40+
public var isTuple: Bool { bridged.isTuple() }
41+
public var isEnum: Bool { bridged.getEnumOrBoundGenericEnum() != nil }
42+
public var isFunction: Bool { bridged.isFunction() }
43+
public var isMetatype: Bool { bridged.isMetatype() }
4444

4545
/// Can only be used if the type is in fact a nominal type (`isNominal` is true).
46-
public var nominal: Decl { Decl(bridged: SILType_getNominal(bridged)) }
46+
public var nominal: NominalTypeDecl {
47+
NominalTypeDecl(bridged: BridgedNominalTypeDecl(decl: bridged.getNominalOrBoundGenericNominal()))
48+
}
4749

48-
public var isOrContainsObjectiveCClass: Bool { SILType_isOrContainsObjectiveCClass(bridged) }
50+
public var isOrContainsObjectiveCClass: Bool { bridged.isOrContainsObjectiveCClass() }
4951

5052
public var tupleElements: TupleElementArray { TupleElementArray(type: self) }
5153

@@ -54,28 +56,28 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
5456
}
5557

5658
public func instanceTypeOfMetatype(in function: Function) -> Type {
57-
SILType_instanceTypeOfMetatype(bridged, function.bridged).type
59+
bridged.getInstanceTypeOfMetatype(function.bridged.getFunction()).type
5860
}
5961

60-
public var isCalleeConsumedFunction: Bool { SILType_isCalleeConsumedFunction(bridged) }
62+
public var isCalleeConsumedFunction: Bool { bridged.isCalleeConsumedFunction() }
6163

62-
public var isMarkedAsImmortal: Bool { SILType_isMarkedAsImmortal(bridged) }
64+
public var isMarkedAsImmortal: Bool { bridged.isMarkedAsImmortal() }
6365

6466
public func getIndexOfEnumCase(withName name: String) -> Int? {
6567
let idx = name._withStringRef {
66-
SILType_getCaseIdxOfEnumType(bridged, $0)
68+
bridged.getCaseIdxOfEnumType($0)
6769
}
6870
return idx >= 0 ? idx : nil
6971
}
7072

7173
public var description: String {
72-
String(_cxxString: SILType_debugDescription(bridged))
74+
String(_cxxString: bridged.getDebugDescription())
7375
}
7476
}
7577

7678
extension Type: Equatable {
7779
public static func ==(lhs: Type, rhs: Type) -> Bool {
78-
lhs.bridged.typePtr == rhs.bridged.typePtr
80+
lhs.bridged == rhs.bridged
7981
}
8082
}
8183

@@ -84,44 +86,44 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
8486
fileprivate let function: Function
8587

8688
public var startIndex: Int { return 0 }
87-
public var endIndex: Int { SILType_getNumNominalFields(type.bridged) }
89+
public var endIndex: Int { Int(type.bridged.getNumNominalFields()) }
8890

8991
public subscript(_ index: Int) -> Type {
90-
SILType_getNominalFieldType(type.bridged, index, function.bridged).type
92+
type.bridged.getFieldType(index, function.bridged.getFunction()).type
9193
}
9294

9395
public func getIndexOfField(withName name: String) -> Int? {
9496
let idx = name._withStringRef {
95-
SILType_getFieldIdxOfNominalType(type.bridged, $0)
97+
type.bridged.getFieldIdxOfNominalType($0)
9698
}
9799
return idx >= 0 ? idx : nil
98100
}
99101

100102
public func getNameOfField(withIndex idx: Int) -> StringRef {
101-
StringRef(bridged: SILType_getNominalFieldName(type.bridged, idx))
103+
StringRef(bridged: type.bridged.getFieldName(idx))
102104
}
103105
}
104106

105107
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
106108
fileprivate let type: Type
107109

108110
public var startIndex: Int { return 0 }
109-
public var endIndex: Int { SILType_getNumTupleElements(type.bridged) }
111+
public var endIndex: Int { Int(type.bridged.getNumTupleElements()) }
110112

111113
public subscript(_ index: Int) -> Type {
112-
SILType_getTupleElementType(type.bridged, index).type
114+
type.bridged.getTupleElementType(index).type
113115
}
114116
}
115117

116-
extension BridgedType {
118+
extension swift.SILType {
117119
var type: Type { Type(bridged: self) }
118120
}
119121

120122
// TODO: use an AST type for this once we have it
121-
public struct Decl : Equatable {
122-
let bridged: BridgedDecl
123+
public struct NominalTypeDecl : Equatable {
124+
let bridged: BridgedNominalTypeDecl
123125

124-
public static func ==(lhs: Decl, rhs: Decl) -> Bool {
125-
lhs.bridged == rhs.bridged
126+
public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
127+
lhs.bridged.decl == rhs.bridged.decl
126128
}
127129
}

include/swift/SIL/SILBridging.h

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/BasicBridging.h"
1717
#include "swift/Basic/BridgedSwiftObject.h"
1818
#include "swift/AST/Builtins.h"
19+
#include "swift/AST/Decl.h"
1920
#include "swift/AST/SubstitutionMap.h"
2021
#include "swift/SIL/SILInstruction.h"
2122
#include "swift/SIL/SILLocation.h"
@@ -43,10 +44,6 @@ typedef struct {
4344
const void * _Nonnull opaqueCtxt;
4445
} BridgedPassContext;
4546

46-
typedef struct {
47-
void * _Nullable typePtr;
48-
} BridgedType;
49-
5047
struct BridgedValue {
5148
SwiftObject obj;
5249

@@ -74,7 +71,7 @@ struct BridgedValue {
7471
inline OptionalBridgedOperand getFirstUse() const;
7572

7673
SWIFT_IMPORT_UNSAFE
77-
BridgedType getType() const { return {getSILValue()->getType().getOpaqueValue()}; }
74+
swift::SILType getType() const { return getSILValue()->getType(); }
7875

7976
Ownership getOwnership() const {
8077
switch (getSILValue()->getOwnershipKind()) {
@@ -130,9 +127,14 @@ struct BridgedOperandArray {
130127
SwiftInt count;
131128
};
132129

133-
typedef struct {
130+
struct BridgedFunction {
134131
SwiftObject obj;
135-
} BridgedFunction;
132+
133+
SWIFT_IMPORT_UNSAFE
134+
swift::SILFunction * _Nonnull getFunction() const {
135+
return static_cast<swift::SILFunction *>(obj);
136+
}
137+
};
136138

137139
typedef struct {
138140
OptionalSwiftObject obj;
@@ -328,7 +330,9 @@ typedef enum {
328330

329331
// AST bridging
330332

331-
typedef void * _Nonnull BridgedDecl;
333+
struct BridgedNominalTypeDecl {
334+
swift::NominalTypeDecl * _Nonnull decl;
335+
};
332336

333337
struct BridgedEffectInfo {
334338
SwiftInt argumentIndex;
@@ -386,9 +390,9 @@ SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
386390
SwiftInt SILFunction_numParameterArguments(BridgedFunction function);
387391
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
388392
SwiftInt SILFunction_getNumSILArguments(BridgedFunction function);
389-
BridgedType SILFunction_getSILArgumentType(BridgedFunction function, SwiftInt idx);
393+
swift::SILType SILFunction_getSILArgumentType(BridgedFunction function, SwiftInt idx);
390394
BridgedArgumentConvention SILFunction_getSILArgumentConvention(BridgedFunction function, SwiftInt idx);
391-
BridgedType SILFunction_getSILResultType(BridgedFunction function);
395+
swift::SILType SILFunction_getSILResultType(BridgedFunction function);
392396
SwiftInt SILFunction_isSwift51RuntimeAvailable(BridgedFunction function);
393397
SwiftInt SILFunction_isPossiblyUsedExternally(BridgedFunction function);
394398
SwiftInt SILFunction_isAvailableExternally(BridgedFunction function);
@@ -412,7 +416,7 @@ OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
412416
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
413417
BridgedArgument SILBasicBlock_getArgument(BridgedBasicBlock block, SwiftInt index);
414418
BridgedArgument SILBasicBlock_addBlockArgument(BridgedBasicBlock block,
415-
BridgedType type,
419+
swift::SILType type,
416420
BridgedValue::Ownership ownership);
417421
void SILBasicBlock_eraseArgument(BridgedBasicBlock block, SwiftInt index);
418422
void SILBasicBlock_moveAllInstructionsToBegin(BridgedBasicBlock block, BridgedBasicBlock dest);
@@ -422,35 +426,6 @@ OptionalBridgedSuccessor SILBasicBlock_getFirstPred(BridgedBasicBlock block);
422426

423427
std::string SILNode_debugDescription(BridgedNode node);
424428

425-
std::string SILType_debugDescription(BridgedType);
426-
SwiftInt SILType_isAddress(BridgedType);
427-
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
428-
SwiftInt SILType_isReferenceCounted(BridgedType type, BridgedFunction);
429-
bool SILType_hasArchetype(BridgedType type);
430-
SwiftInt SILType_isNonTrivialOrContainsRawPointer(BridgedType, BridgedFunction);
431-
SwiftInt SILType_isNominal(BridgedType type);
432-
SwiftInt SILType_isClass(BridgedType type);
433-
SwiftInt SILType_isStruct(BridgedType type);
434-
SwiftInt SILType_isTuple(BridgedType type);
435-
SwiftInt SILType_isEnum(BridgedType type);
436-
bool SILType_isFunction(BridgedType type);
437-
bool SILType_isMetatype(BridgedType type);
438-
BridgedType SILType_instanceTypeOfMetatype(BridgedType type, BridgedFunction function);
439-
BridgedDecl SILType_getNominal(BridgedType type);
440-
bool SILType_isOrContainsObjectiveCClass(BridgedType type);
441-
bool SILType_isCalleeConsumedFunction(BridgedType type);
442-
bool SILType_isMarkedAsImmortal(BridgedType type);
443-
SwiftInt SILType_getNumTupleElements(BridgedType type);
444-
BridgedType SILType_getTupleElementType(BridgedType type, SwiftInt elementIdx);
445-
SwiftInt SILType_getNumNominalFields(BridgedType type);
446-
BridgedType SILType_getNominalFieldType(BridgedType type, SwiftInt index,
447-
BridgedFunction function);
448-
SwiftInt SILType_getFieldIdxOfNominalType(BridgedType type,
449-
llvm::StringRef fieldName);
450-
llvm::StringRef SILType_getNominalFieldName(BridgedType type, SwiftInt index);
451-
SwiftInt SILType_getCaseIdxOfEnumType(BridgedType type,
452-
llvm::StringRef caseName);
453-
454429
std::string SILLocation_debugDescription(swift::SILDebugLocation loc);
455430
swift::SILDebugLocation
456431
SILLocation_getAutogeneratedLocation(swift::SILDebugLocation loc);
@@ -547,21 +522,21 @@ SwiftInt FullApplySite_numIndirectResultArguments(BridgedInstruction inst);
547522

548523
BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
549524
BridgedBuilder builder, llvm::StringRef name,
550-
BridgedType operandType, BridgedType resultType,
525+
swift::SILType operandType, swift::SILType resultType,
551526
BridgedValueArray arguments);
552527
BridgedInstruction SILBuilder_createCondFail(BridgedBuilder builder,
553528
BridgedValue condition, llvm::StringRef message);
554529
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedBuilder builder,
555-
BridgedType type, SwiftInt value);
530+
swift::SILType type, SwiftInt value);
556531
BridgedInstruction SILBuilder_createAllocStack(BridgedBuilder builder,
557-
BridgedType type, SwiftInt hasDynamicLifetime, SwiftInt isLexical,
532+
swift::SILType type, SwiftInt hasDynamicLifetime, SwiftInt isLexical,
558533
SwiftInt wasMoved);
559534
BridgedInstruction SILBuilder_createDeallocStack(BridgedBuilder builder,
560535
BridgedValue operand);
561536
BridgedInstruction SILBuilder_createDeallocStackRef(BridgedBuilder builder,
562537
BridgedValue operand);
563538
BridgedInstruction SILBuilder_createUncheckedRefCast(BridgedBuilder builder,
564-
BridgedValue op, BridgedType type);
539+
BridgedValue op, swift::SILType type);
565540
BridgedInstruction SILBuilder_createSetDeallocating(BridgedBuilder builder,
566541
BridgedValue op, bool isAtomic);
567542
BridgedInstruction SILBuilder_createFunctionRef(BridgedBuilder builder,
@@ -583,7 +558,7 @@ BridgedInstruction SILBuilder_createSwitchEnumInst(BridgedBuilder builder,
583558
const void * _Nullable enumCases, SwiftInt numEnumCases);
584559
BridgedInstruction SILBuilder_createUncheckedEnumData(BridgedBuilder builder,
585560
BridgedValue enumVal, SwiftInt caseIdx,
586-
BridgedType resultType);
561+
swift::SILType resultType);
587562
BridgedInstruction SILBuilder_createBranch(
588563
BridgedBuilder builder, BridgedBasicBlock destBlock,
589564
BridgedValueArray arguments);

include/swift/SIL/SILBridgingUtils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,10 @@
2525

2626
namespace swift {
2727

28-
inline SILType getSILType(BridgedType ty) {
29-
return SILType::getFromOpaqueValue(ty.typePtr);
30-
}
31-
3228
inline SILNode *castToSILNode(BridgedNode node) {
3329
return static_cast<SILNode *>(node.obj);
3430
}
3531

36-
inline SILType castToSILType(BridgedType type) {
37-
return SILType::getFromOpaqueValue(type.typePtr);
38-
}
39-
4032
template <class I = SILInstruction> I *castToInst(BridgedInstruction inst) {
4133
return cast<I>(static_cast<SILNode *>(inst.obj)->castToInstruction());
4234
}

0 commit comments

Comments
 (0)