Skip to content

Commit 690b0c0

Browse files
committed
Introduce isType and getDeclType
Accidentally forgot a negation address review notes Address more of Jordan's feedback
1 parent fab3576 commit 690b0c0

Some content is hidden

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

56 files changed

+263
-446
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ class ASTContext final {
460460

461461
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
462462
/** Retrieve the declaration of Swift.NAME. */ \
463-
DECL_CLASS *get##NAME##Decl() const;
463+
DECL_CLASS *get##NAME##Decl() const; \
464+
\
465+
/** Retrieve the type of Swift.NAME. */ \
466+
Type get##NAME##Type() const;
464467
#include "swift/AST/KnownStdlibTypes.def"
465468

466469
/// Retrieve the declaration of Swift.Optional<T>.Some.
@@ -475,9 +478,6 @@ class ASTContext final {
475478
/// Retrieve the type Swift.AnyObject.
476479
CanType getAnyObjectType() const;
477480

478-
/// Retrieve the type Swift.Never.
479-
CanType getNeverType() const;
480-
481481
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECL_CLASS) \
482482
/** Retrieve the declaration of MODULE.NAME. */ \
483483
DECL_CLASS *get##NAME##Decl() const; \

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ PROTOCOL(OptionSet)
7070
PROTOCOL(CaseIterable)
7171
PROTOCOL(SIMDScalar)
7272
PROTOCOL(BinaryInteger)
73+
PROTOCOL(RangeReplaceableCollection)
7374

7475
PROTOCOL_(BridgedNSError)
7576
PROTOCOL_(BridgedStoredNSError)

include/swift/AST/KnownStdlibTypes.def

Lines changed: 0 additions & 3 deletions
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,6 +87,5 @@ 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

9491
#undef KNOWN_STDLIB_TYPE_DECL

include/swift/AST/Types.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,13 @@ class alignas(1 << TypeAlignInBits) TypeBase {
774774
/// These are the types of the Swift type system.
775775
bool isLegalFormalType();
776776

777-
/// Check if this type is equal to the empty tuple type.
777+
// Whether or not this is equal to ()
778778
bool isVoid();
779779

780-
/// Check if this type is equal to Swift.Bool.
781-
bool isBool();
780+
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
781+
/** Whether or not this is equal to Swift.NAME. */ \
782+
bool is##NAME();
783+
#include "swift/AST/KnownStdlibTypes.def"
782784

783785
/// Check if this type is equal to Builtin.IntN.
784786
bool isBuiltinIntegerType(unsigned bitWidth);

lib/AST/ASTContext.cpp

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
672672
for (auto Req: FD->getGenericRequirements()) {
673673
if (Req.getKind() == RequirementKind::Conformance &&
674674
Req.getSecondType()->getNominalOrBoundGenericNominal() ==
675-
getRangeReplaceableCollectionDecl()) {
675+
getProtocol(KnownProtocolKind::RangeReplaceableCollection)) {
676676
getImpl().PlusFunctionOnRangeReplaceableCollection = FD;
677677
}
678678
}
@@ -693,17 +693,13 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {
693693
if (!FD->getOperatorDecl())
694694
continue;
695695
auto ResultType = FD->getResultInterfaceType();
696-
if (ResultType->getNominalOrBoundGenericNominal() != getStringDecl())
696+
if (!ResultType->isString())
697697
continue;
698698
auto ParamList = FD->getParameters();
699699
if (ParamList->size() != 2)
700700
continue;
701-
auto CheckIfStringParam = [this](ParamDecl* Param) {
702-
auto Type = Param->getInterfaceType()->getNominalOrBoundGenericNominal();
703-
return Type == getStringDecl();
704-
};
705-
if (CheckIfStringParam(ParamList->get(0)) &&
706-
CheckIfStringParam(ParamList->get(1))) {
701+
if (ParamList->get(0)->getInterfaceType()->isString() &&
702+
ParamList->get(1)->getInterfaceType()->isString()) {
707703
getImpl().PlusFunctionOnString = FD;
708704
break;
709705
}
@@ -738,22 +734,28 @@ FuncDecl *ASTContext::getSequenceMakeIterator() const {
738734
}
739735

740736
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
741-
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
742-
if (getImpl().NAME##Decl) \
743-
return getImpl().NAME##Decl; \
744-
SmallVector<ValueDecl *, 1> results; \
745-
lookupInSwiftModule(#NAME, results); \
746-
for (auto result : results) { \
747-
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
748-
auto params = type->getGenericParams(); \
749-
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
750-
getImpl().NAME##Decl = type; \
751-
return type; \
752-
} \
737+
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
738+
if (getImpl().NAME##Decl) \
739+
return getImpl().NAME##Decl; \
740+
SmallVector<ValueDecl *, 1> results; \
741+
lookupInSwiftModule(#NAME, results); \
742+
for (auto result : results) { \
743+
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
744+
auto params = type->getGenericParams(); \
745+
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
746+
getImpl().NAME##Decl = type; \
747+
return type; \
753748
} \
754749
} \
755-
return nullptr; \
756-
}
750+
} \
751+
return nullptr; \
752+
} \
753+
\
754+
Type ASTContext::get##NAME##Type() const { \
755+
if (!get##NAME##Decl()) \
756+
return Type(); \
757+
return get##NAME##Decl()->getDeclaredInterfaceType(); \
758+
}
757759
#include "swift/AST/KnownStdlibTypes.def"
758760

759761
CanType ASTContext::getExceptionType() const {
@@ -846,13 +848,6 @@ CanType ASTContext::getAnyObjectType() const {
846848
return getImpl().AnyObjectType;
847849
}
848850

849-
CanType ASTContext::getNeverType() const {
850-
auto neverDecl = getNeverDecl();
851-
if (!neverDecl)
852-
return CanType();
853-
return neverDecl->getDeclaredType()->getCanonicalType();
854-
}
855-
856851
#define KNOWN_OBJC_TYPE_DECL(MODULE, NAME, DECLTYPE) \
857852
DECLTYPE *ASTContext::get##NAME##Decl() const { \
858853
if (!getImpl().NAME##Decl) { \
@@ -1088,19 +1083,18 @@ FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op,
10881083
if (!C.getIntDecl() || !C.getBoolDecl())
10891084
return nullptr;
10901085

1091-
auto intType = C.getIntDecl()->getDeclaredType();
10921086
auto isIntParam = [&](AnyFunctionType::Param param) {
10931087
return (!param.isVariadic() && !param.isInOut() &&
1094-
param.getPlainType()->isEqual(intType));
1088+
param.getPlainType()->isInt());
10951089
};
1096-
auto boolType = C.getBoolDecl()->getDeclaredType();
1097-
auto decl = lookupOperatorFunc(C, op, intType,
1098-
[=](FunctionType *type) {
1090+
1091+
auto decl = lookupOperatorFunc(C, op, C.getIntType(),
1092+
[=](FunctionType *type) {
10991093
// Check for the signature: (Int, Int) -> Bool
11001094
if (type->getParams().size() != 2) return false;
11011095
if (!isIntParam(type->getParams()[0]) ||
11021096
!isIntParam(type->getParams()[1])) return false;
1103-
return type->getResult()->isEqual(boolType);
1097+
return type->getResult()->isBool();
11041098
});
11051099
cached = decl;
11061100
return decl;
@@ -1155,11 +1149,7 @@ FuncDecl *ASTContext::getArrayAppendElementDecl() const {
11551149
return nullptr;
11561150

11571151
auto SelfInOutTy = SelfDecl->getInterfaceType();
1158-
BoundGenericStructType *SelfGenericStructTy =
1159-
SelfInOutTy->getAs<BoundGenericStructType>();
1160-
if (!SelfGenericStructTy)
1161-
return nullptr;
1162-
if (SelfGenericStructTy->getDecl() != getArrayDecl())
1152+
if (!SelfInOutTy->isArray())
11631153
return nullptr;
11641154

11651155
auto ParamList = FnDecl->getParameters();
@@ -1202,11 +1192,7 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
12021192
return nullptr;
12031193

12041194
auto SelfInOutTy = SelfDecl->getInterfaceType();
1205-
BoundGenericStructType *SelfGenericStructTy =
1206-
SelfInOutTy->getAs<BoundGenericStructType>();
1207-
if (!SelfGenericStructTy)
1208-
return nullptr;
1209-
if (SelfGenericStructTy->getDecl() != getArrayDecl())
1195+
if (!SelfInOutTy->isArray())
12101196
return nullptr;
12111197

12121198
auto ParamList = FnDecl->getParameters();
@@ -4371,7 +4357,7 @@ Type ASTContext::getBridgedToObjC(const DeclContext *dc, Type type,
43714357
[&](KnownProtocolKind known) -> ProtocolConformanceRef {
43724358
// Don't ascribe any behavior to Optional other than what we explicitly
43734359
// give it. We don't want things like AnyObject?? to work.
4374-
if (type->getAnyNominal() == getOptionalDecl())
4360+
if (type->isOptional())
43754361
return ProtocolConformanceRef::forInvalid();
43764362

43774363
// Find the protocol.

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,23 +3777,21 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37773777

37783778
void visitBoundGenericType(BoundGenericType *T) {
37793779
if (Options.SynthesizeSugarOnTypes) {
3780-
auto *NT = T->getDecl();
3781-
auto &Ctx = T->getASTContext();
3782-
if (NT == Ctx.getArrayDecl()) {
3780+
if (T->isArray()) {
37833781
Printer << "[";
37843782
visit(T->getGenericArgs()[0]);
37853783
Printer << "]";
37863784
return;
37873785
}
3788-
if (NT == Ctx.getDictionaryDecl()) {
3786+
if (T->isDictionary()) {
37893787
Printer << "[";
37903788
visit(T->getGenericArgs()[0]);
37913789
Printer << " : ";
37923790
visit(T->getGenericArgs()[1]);
37933791
Printer << "]";
37943792
return;
37953793
}
3796-
if (NT == Ctx.getOptionalDecl()) {
3794+
if (T->isOptional()) {
37973795
printWithParensIfNotSimple(T->getGenericArgs()[0]);
37983796
Printer << "?";
37993797
return;

lib/AST/ASTVerifier.cpp

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,7 @@ class Verifier : public ASTWalker {
10321032
case StmtConditionElement::CK_Boolean: {
10331033
auto *E = elt.getBoolean();
10341034
if (shouldVerifyChecked(E))
1035-
checkSameType(E->getType(), Ctx.getBoolDecl()->getDeclaredType(),
1036-
"condition type");
1035+
checkSameType(E->getType(), Ctx.getBoolType(), "condition type");
10371036
break;
10381037
}
10391038

@@ -1376,9 +1375,7 @@ class Verifier : public ASTWalker {
13761375
}
13771376

13781377
// Ensure we don't convert an array to a void pointer this way.
1379-
1380-
if (fromElement->getNominalOrBoundGenericNominal() == Ctx.getArrayDecl()
1381-
&& toElement->isEqual(Ctx.TheEmptyTupleType)) {
1378+
if (fromElement->isArray() && toElement->isVoid()) {
13821379
Out << "InOutToPointer is converting an array to a void pointer; "
13831380
"ArrayToPointer should be used instead:\n";
13841381
E->dump(Out);
@@ -1401,7 +1398,7 @@ class Verifier : public ASTWalker {
14011398
// The source may be optionally inout.
14021399
auto fromArray = E->getSubExpr()->getType()->getInOutObjectType();
14031400

1404-
if (fromArray->getNominalOrBoundGenericNominal() != Ctx.getArrayDecl()) {
1401+
if (!fromArray->isArray()) {
14051402
Out << "ArrayToPointer does not convert from array:\n";
14061403
E->dump(Out);
14071404
Out << "\n";
@@ -1422,8 +1419,7 @@ class Verifier : public ASTWalker {
14221419
PrettyStackTraceExpr debugStack(Ctx,
14231420
"verifying StringToPointer", E);
14241421

1425-
if (E->getSubExpr()->getType()->getNominalOrBoundGenericNominal()
1426-
!= Ctx.getStringDecl()) {
1422+
if (!E->getSubExpr()->getType()->isString()) {
14271423
Out << "StringToPointer does not convert from string:\n";
14281424
E->dump(Out);
14291425
Out << "\n";
@@ -2188,22 +2184,20 @@ class Verifier : public ASTWalker {
21882184
auto keyPathTy = E->getKeyPath()->getType();
21892185
auto resultTy = E->getType();
21902186

2191-
if (auto nom = keyPathTy->getAs<NominalType>()) {
2192-
if (nom->getDecl() == Ctx.getAnyKeyPathDecl()) {
2193-
// AnyKeyPath application is <T> rvalue T -> rvalue Any?
2194-
if (baseTy->is<LValueType>()) {
2195-
Out << "AnyKeyPath application base is not an rvalue\n";
2196-
abort();
2197-
}
2198-
auto resultObjTy = resultTy->getOptionalObjectType();
2199-
if (!resultObjTy || !resultObjTy->isAny()) {
2200-
Out << "AnyKeyPath application result must be Any?\n";
2201-
abort();
2202-
}
2203-
return;
2187+
if (keyPathTy->isAnyKeyPath()) {
2188+
// AnyKeyPath application is <T> rvalue T -> rvalue Any?
2189+
if (baseTy->is<LValueType>()) {
2190+
Out << "AnyKeyPath application base is not an rvalue\n";
2191+
abort();
22042192
}
2193+
auto resultObjTy = resultTy->getOptionalObjectType();
2194+
if (!resultObjTy || !resultObjTy->isAny()) {
2195+
Out << "AnyKeyPath application result must be Any?\n";
2196+
abort();
2197+
}
2198+
return;
22052199
} else if (auto bgt = keyPathTy->getAs<BoundGenericType>()) {
2206-
if (bgt->getDecl() == Ctx.getPartialKeyPathDecl()) {
2200+
if (keyPathTy->isPartialKeyPath()) {
22072201
// PartialKeyPath<T> application is rvalue T -> rvalue Any
22082202
if (!baseTy->isEqual(bgt->getGenericArgs()[0])) {
22092203
Out << "PartialKeyPath application base doesn't match type\n";
@@ -2214,7 +2208,7 @@ class Verifier : public ASTWalker {
22142208
abort();
22152209
}
22162210
return;
2217-
} else if (bgt->getDecl() == Ctx.getKeyPathDecl()) {
2211+
} else if (keyPathTy->isKeyPath()) {
22182212
// KeyPath<T, U> application is rvalue T -> rvalue U
22192213
if (!baseTy->isEqual(bgt->getGenericArgs()[0])) {
22202214
Out << "KeyPath application base doesn't match type\n";
@@ -2225,7 +2219,7 @@ class Verifier : public ASTWalker {
22252219
abort();
22262220
}
22272221
return;
2228-
} else if (bgt->getDecl() == Ctx.getWritableKeyPathDecl()) {
2222+
} else if (keyPathTy->isWritableKeyPath()) {
22292223
// WritableKeyPath<T, U> application is
22302224
// lvalue T -> lvalue U
22312225
// or rvalue T -> rvalue U
@@ -2247,7 +2241,7 @@ class Verifier : public ASTWalker {
22472241
abort();
22482242
}
22492243
return;
2250-
} else if (bgt->getDecl() == Ctx.getReferenceWritableKeyPathDecl()) {
2244+
} else if (keyPathTy->isReferenceWritableKeyPath()) {
22512245
// ReferenceWritableKeyPath<T, U> application is
22522246
// rvalue T -> lvalue U
22532247
// or lvalue T -> lvalue U
@@ -2873,8 +2867,7 @@ class Verifier : public ASTWalker {
28732867
// Verify that the optionality of the result type of the
28742868
// initializer matches the failability of the initializer.
28752869
if (!CD->isInvalid() &&
2876-
CD->getDeclContext()->getDeclaredInterfaceType()->getAnyNominal() !=
2877-
Ctx.getOptionalDecl()) {
2870+
!CD->getDeclContext()->getDeclaredInterfaceType()->isOptional()) {
28782871
bool resultIsOptional = (bool) CD->getResultInterfaceType()
28792872
->getOptionalObjectType();
28802873
auto declIsOptional = CD->isFailable();

lib/AST/Builtins.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,8 @@ static ValueDecl *getLinearFunctionConstructor(
12681268
static ValueDecl *getGlobalStringTablePointer(ASTContext &Context,
12691269
Identifier Id) {
12701270
// String -> Builtin.RawPointer
1271-
auto stringType = NominalType::get(Context.getStringDecl(), Type(), Context);
1272-
return getBuiltinFunction(Id, {stringType}, Context.TheRawPointerType);
1271+
return getBuiltinFunction(Id, {Context.getStringType()},
1272+
Context.TheRawPointerType);
12731273
}
12741274

12751275
static ValueDecl *getConvertStrongToUnownedUnsafe(ASTContext &ctx,

lib/AST/DiagnosticEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static bool isInterestingTypealias(Type type) {
398398
else
399399
return false;
400400

401-
if (aliasDecl == type->getASTContext().getVoidDecl())
401+
if (type->isVoid())
402402
return false;
403403

404404
// The 'Swift.AnyObject' typealias is not 'interesting'.

0 commit comments

Comments
 (0)