Skip to content

Commit 124cd26

Browse files
committed
[NFC] Add Type::isOrHasOptimizedCOW api
1 parent a9df0bd commit 124cd26

File tree

6 files changed

+24
-0
lines changed

6 files changed

+24
-0
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ extension TypeProperties {
158158
/// True if this the nominal type `Swift.Optional`.
159159
public var isOptional: Bool { rawType.bridged.isOptional() }
160160

161+
/// True if this has optimized COW type: Array/ArraySlice/ContiguousArray
162+
public var isOrHasOptimizedCOW: Bool {rawType.bridged.isOrHasOptimizedCOW()}
163+
161164
/// True if this type is a value type (struct/enum) that defines a `deinit`.
162165
public var isValueTypeWithDeinit: Bool {
163166
if let nominal = nominal, nominal.valueTypeDestructor != nil {

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,6 +3105,7 @@ struct BridgedASTType {
31053105
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinVectorElementType() const;
31063106
BRIDGED_INLINE bool isBuiltinFixedWidthInteger(SwiftInt width) const;
31073107
BRIDGED_INLINE bool isOptional() const;
3108+
BRIDGED_INLINE bool isOrHasOptimizedCOW() const;
31083109
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getNominalOrBoundGenericNominal() const;
31093110
BRIDGED_INLINE TraitResult canBeClass() const;
31103111
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getAnyNominal() const;

include/swift/AST/ASTBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,10 @@ bool BridgedASTType::isOptional() const {
552552
return unbridged()->getCanonicalType()->isOptional();
553553
}
554554

555+
bool BridgedASTType::isOrHasOptimizedCOW() const {
556+
return unbridged()->getCanonicalType()->isOrHasOptimizedCOW();
557+
}
558+
555559
bool BridgedASTType::isUnownedStorageType() const {
556560
return unbridged()->is<swift::UnownedStorageType>();
557561
}

include/swift/AST/KnownStdlibTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ KNOWN_STDLIB_TYPE_DECL(StaticString, NominalTypeDecl, 0)
5050
KNOWN_STDLIB_TYPE_DECL(Substring, NominalTypeDecl, 0)
5151
KNOWN_STDLIB_TYPE_DECL(Array, NominalTypeDecl, 1)
5252
KNOWN_STDLIB_TYPE_DECL(ArraySlice, NominalTypeDecl, 1)
53+
KNOWN_STDLIB_TYPE_DECL(ContiguousArray, NominalTypeDecl, 1)
5354
KNOWN_STDLIB_TYPE_DECL(_ArrayBuffer, NominalTypeDecl, 1)
5455
KNOWN_STDLIB_TYPE_DECL(_ContiguousArrayBuffer, NominalTypeDecl, 1)
5556
KNOWN_STDLIB_TYPE_DECL(_ContiguousArrayStorage, ClassDecl, 1)

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,9 @@ class alignas(1 << TypeAlignInBits) TypeBase
991991
/// element type of the array.
992992
Type isArrayType();
993993

994+
/// Check if this type is or has Swift.Array recursively.
995+
bool isOrHasOptimizedCOW() const;
996+
994997
/// Determines the element type of a known
995998
/// [Autoreleasing]Unsafe[Mutable][Raw]Pointer variant, or returns null if the
996999
/// type is not a pointer.

lib/AST/Type.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,18 @@ Type TypeBase::isArrayType() {
840840
return Type();
841841
}
842842

843+
bool TypeBase::isOrHasOptimizedCOW() const {
844+
bool foundOptCOWType = false;
845+
getCanonicalType().findIf([&](Type type) -> bool {
846+
if (type->isArray() || type->isArraySlice() || type->isContiguousArray()) {
847+
foundOptCOWType = true;
848+
return true;
849+
};
850+
return true;
851+
});
852+
return foundOptCOWType;
853+
}
854+
843855
Type TypeBase::getAnyPointerElementType(PointerTypeKind &PTK) {
844856
auto &C = getASTContext();
845857
if (isUnsafeMutableRawPointer()) {

0 commit comments

Comments
 (0)