Skip to content

[NFC] Introduce isDecl and getDeclType #36953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@ class ASTContext final {

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
/** Retrieve the declaration of Swift.NAME. */ \
DECL_CLASS *get##NAME##Decl() const;
DECL_CLASS *get##NAME##Decl() const; \
\
/** Retrieve the type of Swift.NAME. */ \
Type get##NAME##Type() const;
#include "swift/AST/KnownStdlibTypes.def"

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

/// Retrieve the declaration of Swift.Void.
TypeAliasDecl *getVoidDecl() const;

/// Retrieve the type of Swift.Void.
Type getVoidType() const;

/// Retrieve the declaration of the "pointee" property of a pointer type.
VarDecl *getPointerPointeePropertyDecl(PointerTypeKind ptrKind) const;

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

/// Retrieve the type Swift.Never.
CanType getNeverType() const;

#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
/** Retrieve the declaration of MODULE.NAME. */ \
DECL_CLASS *get##NAME##Decl() const; \
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ PROTOCOL(OptionSet)
PROTOCOL(CaseIterable)
PROTOCOL(SIMDScalar)
PROTOCOL(BinaryInteger)
PROTOCOL(RangeReplaceableCollection)

PROTOCOL_(BridgedNSError)
PROTOCOL_(BridgedStoredNSError)
Expand Down
3 changes: 0 additions & 3 deletions include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
#endif

KNOWN_STDLIB_TYPE_DECL(Void, TypeAliasDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)
Expand Down Expand Up @@ -89,7 +87,6 @@ KNOWN_STDLIB_TYPE_DECL(Encoder, ProtocolDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Decoder, ProtocolDecl, 1)
KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)
KNOWN_STDLIB_TYPE_DECL(EncodingError, NominalTypeDecl, 0)
KNOWN_STDLIB_TYPE_DECL(DecodingError, NominalTypeDecl, 0)

Expand Down
9 changes: 4 additions & 5 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,10 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// Check if this type is equal to the empty tuple type.
bool isVoid();

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

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

/// Check if this is a Double type from standard library.
bool isDoubleType();

/// Check if this is either an Array, Set or Dictionary collection type defined
/// at the top level of the Swift module
bool isKnownStdlibCollectionType();
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3793,9 +3793,6 @@ class ConstraintSystem {
/// element type of the set.
static Optional<Type> isSetType(Type t);

/// Determine if the type in question is AnyHashable.
static bool isAnyHashableType(Type t);

/// Call Expr::isTypeReference on the given expression, using a
/// custom accessor for the type on the expression that reads the
/// type from the ConstraintSystem expression type map.
Expand Down
108 changes: 62 additions & 46 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ struct ASTContext::Implementation {
/// The declaration of Swift.Optional<T>.None.
EnumElementDecl *OptionalNoneDecl = nullptr;

/// The declaration of Swift.Void.
TypeAliasDecl *VoidDecl = nullptr;

/// The declaration of Swift.UnsafeMutableRawPointer.memory.
VarDecl *UnsafeMutableRawPointerMemoryDecl = nullptr;

Expand Down Expand Up @@ -712,7 +715,8 @@ FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
continue;
for (auto Req: FD->getGenericRequirements()) {
if (Req.getKind() == RequirementKind::Conformance &&
Req.getProtocolDecl() == getRangeReplaceableCollectionDecl()) {
Req.getProtocolDecl() ==
getProtocol(KnownProtocolKind::RangeReplaceableCollection)) {
getImpl().PlusFunctionOnRangeReplaceableCollection = FD;
}
}
Expand All @@ -733,17 +737,13 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {
if (!FD->getOperatorDecl())
continue;
auto ResultType = FD->getResultInterfaceType();
if (ResultType->getNominalOrBoundGenericNominal() != getStringDecl())
if (!ResultType->isString())
continue;
auto ParamList = FD->getParameters();
if (ParamList->size() != 2)
continue;
auto CheckIfStringParam = [this](ParamDecl* Param) {
auto Type = Param->getInterfaceType()->getNominalOrBoundGenericNominal();
return Type == getStringDecl();
};
if (CheckIfStringParam(ParamList->get(0)) &&
CheckIfStringParam(ParamList->get(1))) {
if (ParamList->get(0)->getInterfaceType()->isString() &&
ParamList->get(1)->getInterfaceType()->isString()) {
getImpl().PlusFunctionOnString = FD;
break;
}
Expand Down Expand Up @@ -803,22 +803,28 @@ FuncDecl *ASTContext::getAsyncSequenceMakeAsyncIterator() const {
}

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
if (getImpl().NAME##Decl) \
return getImpl().NAME##Decl; \
SmallVector<ValueDecl *, 1> results; \
lookupInSwiftModule(#NAME, results); \
for (auto result : results) { \
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
auto params = type->getGenericParams(); \
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
getImpl().NAME##Decl = type; \
return type; \
} \
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
if (getImpl().NAME##Decl) \
return getImpl().NAME##Decl; \
SmallVector<ValueDecl *, 1> results; \
lookupInSwiftModule(#NAME, results); \
for (auto result : results) { \
if (auto type = dyn_cast<DECL_CLASS>(result)) { \
auto params = type->getGenericParams(); \
if (NUM_GENERIC_PARAMS == (params == nullptr ? 0 : params->size())) { \
getImpl().NAME##Decl = type; \
return type; \
} \
} \
return nullptr; \
}
} \
return nullptr; \
} \
\
Type ASTContext::get##NAME##Type() const { \
if (!get##NAME##Decl()) \
return Type(); \
return get##NAME##Decl()->getDeclaredInterfaceType(); \
}
#include "swift/AST/KnownStdlibTypes.def"

CanType ASTContext::getExceptionType() const {
Expand Down Expand Up @@ -846,6 +852,30 @@ EnumElementDecl *ASTContext::getOptionalNoneDecl() const {
return getImpl().OptionalNoneDecl;
}

TypeAliasDecl *ASTContext::getVoidDecl() const {
if (getImpl().VoidDecl) {
return getImpl().VoidDecl;
}

SmallVector<ValueDecl *, 1> results;
lookupInSwiftModule("Void", results);
for (auto result : results) {
if (auto typealias = dyn_cast<TypeAliasDecl>(result)) {
getImpl().VoidDecl = typealias;
return typealias;
}
}

return nullptr;
}

Type ASTContext::getVoidType() const {
auto decl = getVoidDecl();
if (!decl)
return Type();
return decl->getDeclaredInterfaceType();
}

static VarDecl *getPointeeProperty(VarDecl *&cache,
NominalTypeDecl *(ASTContext::*getNominal)() const,
const ASTContext &ctx) {
Expand Down Expand Up @@ -911,13 +941,6 @@ CanType ASTContext::getAnyObjectType() const {
return getImpl().AnyObjectType;
}

CanType ASTContext::getNeverType() const {
auto neverDecl = getNeverDecl();
if (!neverDecl)
return CanType();
return neverDecl->getDeclaredInterfaceType()->getCanonicalType();
}

#define KNOWN_SDK_TYPE_DECL(MODULE, NAME, DECLTYPE, GENERIC_ARGS) \
DECLTYPE *ASTContext::get##NAME##Decl() const { \
if (!getImpl().NAME##Decl) { \
Expand Down Expand Up @@ -1159,19 +1182,18 @@ FuncDecl *getBinaryComparisonOperatorIntDecl(const ASTContext &C, StringRef op,
if (!C.getIntDecl() || !C.getBoolDecl())
return nullptr;

auto intType = C.getIntDecl()->getDeclaredInterfaceType();
auto isIntParam = [&](AnyFunctionType::Param param) {
return (!param.isVariadic() && !param.isInOut() &&
param.getPlainType()->isEqual(intType));
param.getPlainType()->isInt());
};
auto boolType = C.getBoolDecl()->getDeclaredInterfaceType();
auto decl = lookupOperatorFunc(C, op, intType,
[=](FunctionType *type) {

auto decl = lookupOperatorFunc(C, op, C.getIntType(),
[=](FunctionType *type) {
// Check for the signature: (Int, Int) -> Bool
if (type->getParams().size() != 2) return false;
if (!isIntParam(type->getParams()[0]) ||
!isIntParam(type->getParams()[1])) return false;
return type->getResult()->isEqual(boolType);
return type->getResult()->isBool();
});
cached = decl;
return decl;
Expand Down Expand Up @@ -1226,11 +1248,8 @@ FuncDecl *ASTContext::getArrayAppendElementDecl() const {
return nullptr;

auto SelfInOutTy = SelfDecl->getInterfaceType();
BoundGenericStructType *SelfGenericStructTy =
SelfInOutTy->getAs<BoundGenericStructType>();
if (!SelfGenericStructTy)
return nullptr;
if (SelfGenericStructTy->getDecl() != getArrayDecl())

if (!SelfInOutTy->isArray())
return nullptr;

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

auto SelfInOutTy = SelfDecl->getInterfaceType();
BoundGenericStructType *SelfGenericStructTy =
SelfInOutTy->getAs<BoundGenericStructType>();
if (!SelfGenericStructTy)
return nullptr;
if (SelfGenericStructTy->getDecl() != getArrayDecl())

if (!SelfInOutTy->isArray())
return nullptr;

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

// Find the protocol.
Expand Down
8 changes: 3 additions & 5 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4517,23 +4517,21 @@ class TypePrinter : public TypeVisitor<TypePrinter> {

void visitBoundGenericType(BoundGenericType *T) {
if (Options.SynthesizeSugarOnTypes) {
auto *NT = T->getDecl();
auto &Ctx = T->getASTContext();
if (NT == Ctx.getArrayDecl()) {
if (T->isArray()) {
Printer << "[";
visit(T->getGenericArgs()[0]);
Printer << "]";
return;
}
if (NT == Ctx.getDictionaryDecl()) {
if (T->isDictionary()) {
Printer << "[";
visit(T->getGenericArgs()[0]);
Printer << " : ";
visit(T->getGenericArgs()[1]);
Printer << "]";
return;
}
if (NT == Ctx.getOptionalDecl()) {
if (T->isOptional()) {
printWithParensIfNotSimple(T->getGenericArgs()[0]);
Printer << "?";
return;
Expand Down
Loading