Skip to content

Commit 899f145

Browse files
committed
[AST] Add Unsafe[...]Buffer types as known decls
1 parent 3574c51 commit 899f145

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,6 +3220,14 @@ static inline bool isRawPointerKind(PointerTypeKind PTK) {
32203220
llvm_unreachable("Unhandled PointerTypeKind in switch.");
32213221
}
32223222

3223+
// Kinds of buffer pointer types.
3224+
enum BufferPointerTypeKind : unsigned {
3225+
BPTK_UnsafeMutableRawBufferPointer,
3226+
BPTK_UnsafeRawBufferPointer,
3227+
BPTK_UnsafeMutableBufferPointer,
3228+
BPTK_UnsafeBufferPointer,
3229+
};
3230+
32233231
enum KeyPathTypeKind : unsigned char {
32243232
KPTK_AnyKeyPath,
32253233
KPTK_PartialKeyPath,

include/swift/AST/KnownStdlibTypes.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ KNOWN_STDLIB_TYPE_DECL(UnsafePointer, NominalTypeDecl, 1)
7676
KNOWN_STDLIB_TYPE_DECL(OpaquePointer, NominalTypeDecl, 0)
7777
KNOWN_STDLIB_TYPE_DECL(AutoreleasingUnsafeMutablePointer, NominalTypeDecl, 1)
7878

79+
KNOWN_STDLIB_TYPE_DECL(UnsafeBufferPointer, NominalTypeDecl, 1)
80+
KNOWN_STDLIB_TYPE_DECL(UnsafeMutableBufferPointer, NominalTypeDecl, 1)
81+
KNOWN_STDLIB_TYPE_DECL(UnsafeRawBufferPointer, NominalTypeDecl, 0)
82+
KNOWN_STDLIB_TYPE_DECL(UnsafeMutableRawBufferPointer, NominalTypeDecl, 0)
83+
7984
KNOWN_STDLIB_TYPE_DECL(Unmanaged, NominalTypeDecl, 1)
8085

8186
KNOWN_STDLIB_TYPE_DECL(Never, NominalTypeDecl, 0)

include/swift/AST/Types.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum class AllocationArena;
4949
class ArchetypeType;
5050
class AssociatedTypeDecl;
5151
class ASTContext;
52+
enum BufferPointerTypeKind : unsigned;
5253
class ClassDecl;
5354
class DependentMemberType;
5455
class GenericTypeParamDecl;
@@ -713,6 +714,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
713714
/// current type.
714715
Type wrapInPointer(PointerTypeKind kind);
715716

717+
/// Determines the element type of a known Unsafe[Mutable][Raw]BufferPointer
718+
/// variant, or returns null if the type is not a buffer pointer.
719+
Type getAnyBufferPointerElementType(BufferPointerTypeKind &BPTK);
720+
Type getAnyBufferPointerElementType() {
721+
BufferPointerTypeKind Ignore;
722+
return getAnyBufferPointerElementType(Ignore);
723+
}
724+
716725
/// Determine whether the given type is "specialized", meaning that
717726
/// it involves generic types for which generic arguments have been provided.
718727
/// For example, the types Vector<Int> and Vector<Int>.Element are both

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,10 @@ bool ASTContext::hasPointerArgumentIntrinsics() const {
13841384
&& getUnsafeMutablePointerDecl()
13851385
&& getUnsafePointerDecl()
13861386
&& (!LangOpts.EnableObjCInterop || getAutoreleasingUnsafeMutablePointerDecl())
1387+
&& getUnsafeBufferPointerDecl()
1388+
&& getUnsafeMutableBufferPointerDecl()
1389+
&& getUnsafeRawBufferPointerDecl()
1390+
&& getUnsafeMutableRawBufferPointerDecl()
13871391
&& getConvertPointerToPointerArgument()
13881392
&& getConvertMutableArrayToPointerArgument()
13891393
&& getConvertConstArrayToPointerArgument()

lib/AST/Type.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,30 @@ Type TypeBase::wrapInPointer(PointerTypeKind kind) {
645645
return BoundGenericType::get(pointerDecl, /*parent*/nullptr, Type(this));
646646
}
647647

648+
Type TypeBase::getAnyBufferPointerElementType(BufferPointerTypeKind &BPTK) {
649+
auto &C = getASTContext();
650+
if (auto nominalTy = getAs<NominalType>()) {
651+
if (nominalTy->getDecl() == C.getUnsafeMutableRawBufferPointerDecl()) {
652+
BPTK = BPTK_UnsafeMutableRawBufferPointer;
653+
} else if (nominalTy->getDecl() == C.getUnsafeRawBufferPointerDecl()) {
654+
BPTK = BPTK_UnsafeRawBufferPointer;
655+
} else {
656+
return Type();
657+
}
658+
return C.TheEmptyTupleType;
659+
}
660+
if (auto boundTy = getAs<BoundGenericType>()) {
661+
if (boundTy->getDecl() == C.getUnsafeMutableBufferPointerDecl()) {
662+
BPTK = BPTK_UnsafeMutableBufferPointer;
663+
} else if (boundTy->getDecl() == C.getUnsafeBufferPointerDecl()) {
664+
BPTK = BPTK_UnsafeBufferPointer;
665+
} else {
666+
return Type();
667+
}
668+
return boundTy->getGenericArgs()[0];
669+
}
670+
return Type();
671+
}
648672

649673
Type TypeBase::lookThroughAllOptionalTypes() {
650674
Type type(this);

0 commit comments

Comments
 (0)