Skip to content

Commit 444c35c

Browse files
authored
Merge pull request #36953 from Azoy/isType-isDeclType
[NFC] Introduce isDecl and getDeclType
2 parents c8ba622 + 9ed732f commit 444c35c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+392
-570
lines changed

Diff for: include/swift/AST/ASTContext.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,10 @@ class ASTContext final {
482482

483483
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
484484
/** Retrieve the declaration of Swift.NAME. */ \
485-
DECL_CLASS *get##NAME##Decl() const;
485+
DECL_CLASS *get##NAME##Decl() const; \
486+
\
487+
/** Retrieve the type of Swift.NAME. */ \
488+
Type get##NAME##Type() const;
486489
#include "swift/AST/KnownStdlibTypes.def"
487490

488491
/// Retrieve the declaration of Swift.Optional<T>.Some.
@@ -491,15 +494,18 @@ class ASTContext final {
491494
/// Retrieve the declaration of Swift.Optional<T>.None.
492495
EnumElementDecl *getOptionalNoneDecl() const;
493496

497+
/// Retrieve the declaration of Swift.Void.
498+
TypeAliasDecl *getVoidDecl() const;
499+
500+
/// Retrieve the type of Swift.Void.
501+
Type getVoidType() const;
502+
494503
/// Retrieve the declaration of the "pointee" property of a pointer type.
495504
VarDecl *getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const;
496505

497506
/// Retrieve the type Swift.AnyObject.
498507
CanType getAnyObjectType() const;
499508

500-
/// Retrieve the type Swift.Never.
501-
CanType getNeverType() const;
502-
503509
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
504510
/** Retrieve the declaration of MODULE.NAME. */ \
505511
DECL_CLASS *get##NAME##Decl() const; \

Diff for: include/swift/AST/KnownProtocols.def

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ PROTOCOL(OptionSet)
7171
PROTOCOL(CaseIterable)
7272
PROTOCOL(SIMDScalar)
7373
PROTOCOL(BinaryInteger)
74+
PROTOCOL(RangeReplaceableCollection)
7475

7576
PROTOCOL_(BridgedNSError)
7677
PROTOCOL_(BridgedStoredNSError)

Diff for: include/swift/AST/KnownStdlibTypes.def

-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
2626
#endif
2727

28-
KNOWN_STDLIB_TYPE_DECL(Void, TypeAliasDecl, 0)
29-
3028
KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)
3129

3230
KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)
@@ -89,7 +87,6 @@ KNOWN_STDLIB_TYPE_DECL(Encoder, ProtocolDecl, 1)
8987
KNOWN_STDLIB_TYPE_DECL(Decoder, ProtocolDecl, 1)
9088
KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
9189
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
92-
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)
9390
KNOWN_STDLIB_TYPE_DECL(EncodingError, NominalTypeDecl, 0)
9491
KNOWN_STDLIB_TYPE_DECL(DecodingError, NominalTypeDecl, 0)
9592

Diff for: include/swift/AST/Types.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,10 @@ class alignas(1 << TypeAlignInBits) TypeBase {
792792
/// Check if this type is equal to the empty tuple type.
793793
bool isVoid();
794794

795-
/// Check if this type is equal to Swift.Bool.
796-
bool isBool();
795+
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
796+
/** Check if this type is equal to Swift.NAME. */ \
797+
bool is##NAME();
798+
#include "swift/AST/KnownStdlibTypes.def"
797799

798800
/// Check if this type is equal to Builtin.IntN.
799801
bool isBuiltinIntegerType(unsigned bitWidth);
@@ -805,9 +807,6 @@ class alignas(1 << TypeAlignInBits) TypeBase {
805807
/// on macOS or Foundation on Linux.
806808
bool isCGFloatType();
807809

808-
/// Check if this is a Double type from standard library.
809-
bool isDoubleType();
810-
811810
/// Check if this is either an Array, Set or Dictionary collection type defined
812811
/// at the top level of the Swift module
813812
bool isKnownStdlibCollectionType();

Diff for: include/swift/Sema/ConstraintSystem.h

-3
Original file line numberDiff line numberDiff line change
@@ -3793,9 +3793,6 @@ class ConstraintSystem {
37933793
/// element type of the set.
37943794
static Optional<Type> isSetType(Type t);
37953795

3796-
/// Determine if the type in question is AnyHashable.
3797-
static bool isAnyHashableType(Type t);
3798-
37993796
/// Call Expr::isTypeReference on the given expression, using a
38003797
/// custom accessor for the type on the expression that reads the
38013798
/// type from the ConstraintSystem expression type map.

Diff for: lib/AST/ASTContext.cpp

+62-46
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ struct ASTContext::Implementation {
218218
/// The declaration of Swift.Optional<T>.None.
219219
EnumElementDecl *OptionalNoneDecl = nullptr;
220220

221+
/// The declaration of Swift.Void.
222+
TypeAliasDecl *VoidDecl = nullptr;
223+
221224
/// The declaration of Swift.UnsafeMutableRawPointer.memory.
222225
VarDecl *UnsafeMutableRawPointerMemoryDecl = nullptr;
223226

@@ -712,7 +715,8 @@ FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
712715
continue;
713716
for (auto Req: FD->getGenericRequirements()) {
714717
if (Req.getKind() == RequirementKind::Conformance &&
715-
Req.getProtocolDecl() == getRangeReplaceableCollectionDecl()) {
718+
Req.getProtocolDecl() ==
719+
getProtocol(KnownProtocolKind::RangeReplaceableCollection)) {
716720
getImpl().PlusFunctionOnRangeReplaceableCollection = FD;
717721
}
718722
}
@@ -733,17 +737,13 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {
733737
if (!FD->getOperatorDecl())
734738
continue;
735739
auto ResultType = FD->getResultInterfaceType();
736-
if (ResultType->getNominalOrBoundGenericNominal() != getStringDecl())
740+
if (!ResultType->isString())
737741
continue;
738742
auto ParamList = FD->getParameters();
739743
if (ParamList->size() != 2)
740744
continue;
741-
auto CheckIfStringParam = [this](ParamDecl* Param) {
742-
auto Type = Param->getInterfaceType()->getNominalOrBoundGenericNominal();
743-
return Type == getStringDecl();
744-
};
745-
if (CheckIfStringParam(ParamList->get(0)) &&
746-
CheckIfStringParam(ParamList->get(1))) {
745+
if (ParamList->get(0)->getInterfaceType()->isString() &&
746+
ParamList->get(1)->getInterfaceType()->isString()) {
747747
getImpl().PlusFunctionOnString = FD;
748748
break;
749749
}
@@ -803,22 +803,28 @@ FuncDecl *ASTContext::getAsyncSequenceMakeAsyncIterator() const {
803803
}
804804

805805
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
806-
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
807-
if (getImpl().NAME##Decl) \
808-
return getImpl().NAME##Decl; \
809-
SmallVector<ValueDecl *, 1> results; \
810-
lookupInSwiftModule(#NAME, results); \
811-
for (auto result : results) { \
812-
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
813-
auto params = type->getGenericParams(); \
814-
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
815-
getImpl().NAME##Decl = type; \
816-
return type; \
817-
} \
806+
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
807+
if (getImpl().NAME##Decl) \
808+
return getImpl().NAME##Decl; \
809+
SmallVector<ValueDecl *, 1> results; \
810+
lookupInSwiftModule(#NAME, results); \
811+
for (auto result : results) { \
812+
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
813+
auto params = type->getGenericParams(); \
814+
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
815+
getImpl().NAME##Decl = type; \
816+
return type; \
818817
} \
819818
} \
820-
return nullptr; \
821-
}
819+
} \
820+
return nullptr; \
821+
} \
822+
\
823+
Type ASTContext::get##NAME##Type() const { \
824+
if (!get##NAME##Decl()) \
825+
return Type(); \
826+
return get##NAME##Decl()->getDeclaredInterfaceType(); \
827+
}
822828
#include "swift/AST/KnownStdlibTypes.def"
823829

824830
CanType ASTContext::getExceptionType() const {
@@ -846,6 +852,30 @@ EnumElementDecl *ASTContext::getOptionalNoneDecl() const {
846852
return getImpl().OptionalNoneDecl;
847853
}
848854

855+
TypeAliasDecl *ASTContext::getVoidDecl() const {
856+
if (getImpl().VoidDecl) {
857+
return getImpl().VoidDecl;
858+
}
859+
860+
SmallVector<ValueDecl *, 1> results;
861+
lookupInSwiftModule("Void", results);
862+
for (auto result : results) {
863+
if (auto typealias = dyn_cast<TypeAliasDecl>(result)) {
864+
getImpl().VoidDecl = typealias;
865+
return typealias;
866+
}
867+
}
868+
869+
return nullptr;
870+
}
871+
872+
Type ASTContext::getVoidType() const {
873+
auto decl = getVoidDecl();
874+
if (!decl)
875+
return Type();
876+
return decl->getDeclaredInterfaceType();
877+
}
878+
849879
static VarDecl *getPointeeProperty(VarDecl *&cache,
850880
NominalTypeDecl *(ASTContext::*getNominal)() const,
851881
const ASTContext &ctx) {
@@ -911,13 +941,6 @@ CanType ASTContext::getAnyObjectType() const {
911941
return getImpl().AnyObjectType;
912942
}
913943

914-
CanType ASTContext::getNeverType() const {
915-
auto neverDecl = getNeverDecl();
916-
if (!neverDecl)
917-
return CanType();
918-
return neverDecl->getDeclaredInterfaceType()->getCanonicalType();
919-
}
920-
921944
#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECLTYPE, GENERIC_ARGS) \
922945
DECLTYPE *ASTContext::get##NAME##Decl() const { \
923946
if (!getImpl().NAME##Decl) { \
@@ -1159,19 +1182,18 @@ FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op,
11591182
if (!C.getIntDecl() || !C.getBoolDecl())
11601183
return nullptr;
11611184

1162-
auto intType = C.getIntDecl()->getDeclaredInterfaceType();
11631185
auto isIntParam = [&](AnyFunctionType::Param param) {
11641186
return (!param.isVariadic() && !param.isInOut() &&
1165-
param.getPlainType()->isEqual(intType));
1187+
param.getPlainType()->isInt());
11661188
};
1167-
auto boolType = C.getBoolDecl()->getDeclaredInterfaceType();
1168-
auto decl = lookupOperatorFunc(C, op, intType,
1169-
[=](FunctionType *type) {
1189+
1190+
auto decl = lookupOperatorFunc(C, op, C.getIntType(),
1191+
[=](FunctionType *type) {
11701192
// Check for the signature: (Int, Int) -> Bool
11711193
if (type->getParams().size() != 2) return false;
11721194
if (!isIntParam(type->getParams()[0]) ||
11731195
!isIntParam(type->getParams()[1])) return false;
1174-
return type->getResult()->isEqual(boolType);
1196+
return type->getResult()->isBool();
11751197
});
11761198
cached = decl;
11771199
return decl;
@@ -1226,11 +1248,8 @@ FuncDecl *ASTContext::getArrayAppendElementDecl() const {
12261248
return nullptr;
12271249

12281250
auto SelfInOutTy = SelfDecl->getInterfaceType();
1229-
BoundGenericStructType *SelfGenericStructTy =
1230-
SelfInOutTy->getAs<BoundGenericStructType>();
1231-
if (!SelfGenericStructTy)
1232-
return nullptr;
1233-
if (SelfGenericStructTy->getDecl() != getArrayDecl())
1251+
1252+
if (!SelfInOutTy->isArray())
12341253
return nullptr;
12351254

12361255
auto ParamList = FnDecl->getParameters();
@@ -1273,11 +1292,8 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
12731292
return nullptr;
12741293

12751294
auto SelfInOutTy = SelfDecl->getInterfaceType();
1276-
BoundGenericStructType *SelfGenericStructTy =
1277-
SelfInOutTy->getAs<BoundGenericStructType>();
1278-
if (!SelfGenericStructTy)
1279-
return nullptr;
1280-
if (SelfGenericStructTy->getDecl() != getArrayDecl())
1295+
1296+
if (!SelfInOutTy->isArray())
12811297
return nullptr;
12821298

12831299
auto ParamList = FnDecl->getParameters();
@@ -4631,7 +4647,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
46314647
[&](KnownProtocolKind known) -> ProtocolConformanceRef {
46324648
// Don't ascribe any behavior to Optional other than what we explicitly
46334649
// give it. We don't want things like AnyObject?? to work.
4634-
if (type->getAnyNominal() == getOptionalDecl())
4650+
if (type->isOptional())
46354651
return ProtocolConformanceRef::forInvalid();
46364652

46374653
// Find the protocol.

Diff for: lib/AST/ASTPrinter.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -4517,23 +4517,21 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
45174517

45184518
void visitBoundGenericType(BoundGenericType *T) {
45194519
if (Options.SynthesizeSugarOnTypes) {
4520-
auto *NT = T->getDecl();
4521-
auto &Ctx = T->getASTContext();
4522-
if (NT == Ctx.getArrayDecl()) {
4520+
if (T->isArray()) {
45234521
Printer << "[";
45244522
visit(T->getGenericArgs()[0]);
45254523
Printer << "]";
45264524
return;
45274525
}
4528-
if (NT == Ctx.getDictionaryDecl()) {
4526+
if (T->isDictionary()) {
45294527
Printer << "[";
45304528
visit(T->getGenericArgs()[0]);
45314529
Printer << " : ";
45324530
visit(T->getGenericArgs()[1]);
45334531
Printer << "]";
45344532
return;
45354533
}
4536-
if (NT == Ctx.getOptionalDecl()) {
4534+
if (T->isOptional()) {
45374535
printWithParensIfNotSimple(T->getGenericArgs()[0]);
45384536
Printer << "?";
45394537
return;

0 commit comments

Comments
 (0)