Skip to content

Commit 1f304e5

Browse files
committed
SIL: improve APIs for Box types
* move `isBox` from `SIL.Type` to `TypeProperties` to make it also available for AST types * add `BoxFieldsArray.isMutable(fieldIndex:)`
1 parent 094b246 commit 1f304e5

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ extension TypeProperties {
141141
public var isMetatype: Bool { rawType.bridged.isMetatypeType() }
142142
public var isExistentialMetatype: Bool { rawType.bridged.isExistentialMetatypeType() }
143143
public var isDynamicSelf: Bool { rawType.bridged.isDynamicSelf()}
144+
public var isBox: Bool { rawType.bridged.isBox() }
144145

145146
/// True if this is the type which represents an integer literal used in a type position.
146147
/// For example `N` in `struct T<let N: Int> {}`

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ extension CanonicalType {
3131
public var silType: Type? {
3232
BridgedType.createSILType(bridged).typeOrNil
3333
}
34+
35+
public func getBoxFields(in function: Function) -> BoxFieldsArray {
36+
precondition(isBox)
37+
return BoxFieldsArray(boxType: self, function: function)
38+
}
3439
}
3540

3641
extension Decl {

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
5959
return bridged.isReferenceCounted(function.bridged)
6060
}
6161

62-
public var isBox: Bool { bridged.isBox() }
63-
6462
public var isMoveOnly: Bool { bridged.isMoveOnly() }
6563

6664
/// Return true if this type conforms to Escapable.
@@ -150,7 +148,7 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
150148

151149
public func getBoxFields(in function: Function) -> BoxFieldsArray {
152150
precondition(isBox)
153-
return BoxFieldsArray(type: self, function: function)
151+
return BoxFieldsArray(boxType: canonicalType, function: function)
154152
}
155153

156154
/// Returns nil if the nominal is a resilient type because in this case the complete list
@@ -299,14 +297,18 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
299297
}
300298

301299
public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray {
302-
fileprivate let type: Type
303-
fileprivate let function: Function
300+
public let boxType: CanonicalType
301+
public let function: Function
304302

305303
public var startIndex: Int { return 0 }
306-
public var endIndex: Int { Int(type.bridged.getNumBoxFields()) }
304+
public var endIndex: Int { BridgedType.getNumBoxFields(boxType.bridged) }
307305

308306
public subscript(_ index: Int) -> Type {
309-
type.bridged.getBoxFieldType(index, function.bridged).type
307+
BridgedType.getBoxFieldType(boxType.bridged, index, function.bridged).type
308+
}
309+
310+
public func isMutable(fieldIndex: Int) -> Bool {
311+
BridgedType.getBoxFieldIsMutable(boxType.bridged, fieldIndex)
310312
}
311313
}
312314

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,6 +3148,7 @@ struct BridgedASTType {
31483148
BRIDGED_INLINE bool isBuiltinFloat() const;
31493149
BRIDGED_INLINE bool isBuiltinVector() const;
31503150
BRIDGED_INLINE bool isBuiltinFixedArray() const;
3151+
BRIDGED_INLINE bool isBox() const;
31513152
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinVectorElementType() const;
31523153
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArrayElementType() const;
31533154
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArraySizeType() const;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ bool BridgedASTType::isBuiltinFixedArray() const {
546546
return unbridged()->is<swift::BuiltinFixedArrayType>();
547547
}
548548

549+
bool BridgedASTType::isBox() const {
550+
return unbridged()->is<swift::SILBoxType>();
551+
}
552+
549553
BridgedASTType BridgedASTType::getBuiltinVectorElementType() const {
550554
return {unbridged()->castTo<swift::BuiltinVectorType>()->getElementType().getPointer()};
551555
}

include/swift/SIL/SILBridging.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ struct BridgedType {
252252
BRIDGED_INLINE bool isNonTrivialOrContainsRawPointer(BridgedFunction f) const;
253253
BRIDGED_INLINE bool isLoadable(BridgedFunction f) const;
254254
BRIDGED_INLINE bool isReferenceCounted(BridgedFunction f) const;
255-
BRIDGED_INLINE bool isBox() const;
256255
BRIDGED_INLINE bool containsNoEscapeFunction() const;
257256
BRIDGED_INLINE bool isEmpty(BridgedFunction f) const;
258257
BRIDGED_INLINE bool isMoveOnly() const;
@@ -261,8 +260,10 @@ struct BridgedType {
261260
BRIDGED_INLINE bool isMarkedAsImmortal() const;
262261
BRIDGED_INLINE bool isAddressableForDeps(BridgedFunction f) const;
263262
BRIDGED_INLINE SwiftInt getCaseIdxOfEnumType(BridgedStringRef name) const;
264-
BRIDGED_INLINE SwiftInt getNumBoxFields() const;
265-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBoxFieldType(SwiftInt idx, BridgedFunction f) const;
263+
static BRIDGED_INLINE SwiftInt getNumBoxFields(BridgedCanType boxTy);
264+
static SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBoxFieldType(BridgedCanType boxTy,
265+
SwiftInt idx, BridgedFunction f);
266+
static BRIDGED_INLINE bool getBoxFieldIsMutable(BridgedCanType boxTy, SwiftInt idx);
266267
BRIDGED_INLINE SwiftInt getNumNominalFields() const;
267268
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getFieldType(SwiftInt idx, BridgedFunction f) const;
268269
BRIDGED_INLINE SwiftInt getFieldIdxOfNominalType(BridgedStringRef name) const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,6 @@ bool BridgedType::isReferenceCounted(BridgedFunction f) const {
348348
return unbridged().isReferenceCounted(f.getFunction());
349349
}
350350

351-
bool BridgedType::isBox() const {
352-
return unbridged().is<swift::SILBoxType>();
353-
}
354-
355351
bool BridgedType::containsNoEscapeFunction() const {
356352
return unbridged().containsNoEscapeFunction();
357353
}
@@ -384,16 +380,20 @@ SwiftInt BridgedType::getCaseIdxOfEnumType(BridgedStringRef name) const {
384380
return unbridged().getCaseIdxOfEnumType(name.unbridged());
385381
}
386382

387-
SwiftInt BridgedType::getNumBoxFields() const {
388-
return unbridged().castTo<swift::SILBoxType>()->getLayout()->getFields().size();
383+
SwiftInt BridgedType::getNumBoxFields(BridgedCanType boxTy) {
384+
return boxTy.unbridged()->castTo<swift::SILBoxType>()->getLayout()->getFields().size();
389385
}
390386

391-
BridgedType BridgedType::getBoxFieldType(SwiftInt idx, BridgedFunction f) const {
387+
BridgedType BridgedType::getBoxFieldType(BridgedCanType boxTy, SwiftInt idx, BridgedFunction f) {
392388
auto *fn = f.getFunction();
393-
return swift::getSILBoxFieldType(fn->getTypeExpansionContext(), unbridged().castTo<swift::SILBoxType>(),
389+
return swift::getSILBoxFieldType(fn->getTypeExpansionContext(), boxTy.unbridged()->castTo<swift::SILBoxType>(),
394390
fn->getModule().Types, idx);
395391
}
396392

393+
bool BridgedType::getBoxFieldIsMutable(BridgedCanType boxTy, SwiftInt idx) {
394+
return boxTy.unbridged()->castTo<swift::SILBoxType>()->getLayout()->getFields()[idx].isMutable();
395+
}
396+
397397
SwiftInt BridgedType::getNumNominalFields() const {
398398
return unbridged().getNumNominalFields();
399399
}

0 commit comments

Comments
 (0)