Skip to content
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

[WIP] Better synthesis of Hashable._hash(into:) #14935

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
12 changes: 4 additions & 8 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ class ASTContext {
ProtocolDecl *getErrorDecl() const;
CanType getExceptionType() const;

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

Expand Down Expand Up @@ -471,12 +471,8 @@ class ASTContext {
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
FuncDecl *getEqualIntDecl() const;

/// Retrieve the declaration of
/// Swift._combineHashValues(Int, Int) -> Int.
FuncDecl *getCombineHashValuesDecl() const;

/// Retrieve the declaration of Swift._mixInt(Int) -> Int.
FuncDecl *getMixIntDecl() const;
/// Retrieve the declaration of Swift._hashValue<H>(for: H) -> Int.
FuncDecl *getHashValueForDecl() const;

/// Retrieve the declaration of Array.append(element:)
FuncDecl *getArrayAppendElementDecl() const;
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ IDENTIFIER(alloc)
IDENTIFIER(allocWithZone)
IDENTIFIER(allZeros)
IDENTIFIER(Any)
IDENTIFIER(appending)
IDENTIFIER(ArrayLiteralElement)
IDENTIFIER(atIndexedSubscript)
IDENTIFIER_(bridgeToObjectiveC)
Expand Down Expand Up @@ -54,14 +55,18 @@ IDENTIFIER(encoder)
IDENTIFIER(error)
IDENTIFIER(forKeyedSubscript)
IDENTIFIER(Foundation)
IDENTIFIER(for)
IDENTIFIER(forKey)
IDENTIFIER(from)
IDENTIFIER(fromRaw)
IDENTIFIER_(hash)
IDENTIFIER(hasher)
IDENTIFIER(hashValue)
IDENTIFIER(init)
IDENTIFIER(initialize)
IDENTIFIER(initStorage)
IDENTIFIER(initialValue)
IDENTIFIER(into)
IDENTIFIER(intValue)
IDENTIFIER(Key)
IDENTIFIER(KeyedDecodingContainer)
Expand Down
11 changes: 8 additions & 3 deletions include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
//
//===----------------------------------------------------------------------===//

#ifndef KNOWN_STDLIB_TYPE_DECL
/// KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
#ifndef KNOWN_STDLIB_TYPE_DECL_WITH_NAME
/// KNOWN_STDLIB_TYPE_DECL_WITH_NAME(NAME, IDSTR, DECL_CLASS, NUM_GENERIC_PARAMS)
///
/// The macro is expanded for each known standard library type. NAME is
/// bound to the unqualified name of the type. DECL_CLASS is bound to the
/// Decl subclass it is expected to be an instance of. NUM_GENERIC_PARAMS is
/// bound to the number of generic parameters the type is expected to have.
#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS)
#define KNOWN_STDLIB_TYPE_DECL_WITH_NAME(NAME, IDSTR, DECL_CLASS, NUM_GENERIC_PARAMS)
#endif

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
KNOWN_STDLIB_TYPE_DECL_WITH_NAME(NAME, #NAME, DECL_CLASS, NUM_GENERIC_PARAMS)

KNOWN_STDLIB_TYPE_DECL(Bool, NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(Int, NominalTypeDecl, 0)
Expand All @@ -50,6 +53,7 @@ KNOWN_STDLIB_TYPE_DECL(Sequence, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Dictionary, NominalTypeDecl, 2)
KNOWN_STDLIB_TYPE_DECL(AnyHashable, NominalTypeDecl, 0)
KNOWN_STDLIB_TYPE_DECL(MutableCollection, ProtocolDecl, 1)
KNOWN_STDLIB_TYPE_DECL_WITH_NAME(UnsafeHasher, "_UnsafeHasher", NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(AnyKeyPath, NominalTypeDecl, 0)
KNOWN_STDLIB_TYPE_DECL(PartialKeyPath, NominalTypeDecl, 1)
Expand Down Expand Up @@ -79,3 +83,4 @@ KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)

#undef KNOWN_STDLIB_TYPE_DECL
#undef KNOWN_STDLIB_TYPE_DECL_WITH_NAME
72 changes: 27 additions & 45 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ struct ASTContext::Implementation {
/// The AnyObject type.
CanType AnyObjectType;

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
#define KNOWN_STDLIB_TYPE_DECL_WITH_NAME(NAME, IDSTR, DECL_CLASS, NUM_GENERIC_PARAMS) \
/** The declaration of Swift.NAME. */ \
DECL_CLASS *NAME##Decl = nullptr;
#include "swift/AST/KnownStdlibTypes.def"
Expand Down Expand Up @@ -189,12 +189,9 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
/// func ==(Int, Int) -> Bool
FuncDecl *EqualIntDecl = nullptr;

/// func _combineHashValues(Int, Int) -> Int
FuncDecl *CombineHashValuesDecl = nullptr;
/// func _hashValue<H: Hashable>(for: H) -> Int
FuncDecl *HashValueForDecl = nullptr;

/// func _mixInt(Int) -> Int
FuncDecl *MixIntDecl = nullptr;

/// func append(Element) -> void
FuncDecl *ArrayAppendElementDecl = nullptr;

Expand Down Expand Up @@ -616,11 +613,11 @@ FuncDecl *ASTContext::getPlusFunctionOnString() const {
return Impl.PlusFunctionOnString;
}

#define KNOWN_STDLIB_TYPE_DECL(NAME, DECL_CLASS, NUM_GENERIC_PARAMS) \
#define KNOWN_STDLIB_TYPE_DECL_WITH_NAME(NAME, IDSTR, DECL_CLASS, NUM_GENERIC_PARAMS) \
DECL_CLASS *ASTContext::get##NAME##Decl() const { \
if (!Impl.NAME##Decl) \
Impl.NAME##Decl = dyn_cast_or_null<DECL_CLASS>( \
findStdlibType(*this, #NAME, NUM_GENERIC_PARAMS)); \
findStdlibType(*this, IDSTR, NUM_GENERIC_PARAMS)); \
return Impl.NAME##Decl; \
}
#include "swift/AST/KnownStdlibTypes.def"
Expand Down Expand Up @@ -989,44 +986,29 @@ FuncDecl *ASTContext::getGetBoolDecl(LazyResolver *resolver) const {
return decl;
}

FuncDecl *ASTContext::getCombineHashValuesDecl() const {
if (Impl.CombineHashValuesDecl)
return Impl.CombineHashValuesDecl;

auto resolver = getLazyResolver();
auto intType = getIntDecl()->getDeclaredType();

auto callback = [&](Type inputType, Type resultType) {
// Look for the signature (Int, Int) -> Int
auto tupleType = dyn_cast<TupleType>(inputType.getPointer());
assert(tupleType);
return tupleType->getNumElements() == 2 &&
tupleType->getElementType(0)->isEqual(intType) &&
tupleType->getElementType(1)->isEqual(intType) &&
resultType->isEqual(intType);
};

auto decl = lookupLibraryIntrinsicFunc(
*this, "_combineHashValues", resolver, callback);
Impl.CombineHashValuesDecl = decl;
return decl;
}

FuncDecl *ASTContext::getMixIntDecl() const {
if (Impl.MixIntDecl)
return Impl.MixIntDecl;
FuncDecl *ASTContext::getHashValueForDecl() const {
if (Impl.HashValueForDecl)
return Impl.HashValueForDecl;

auto resolver = getLazyResolver();
auto intType = getIntDecl()->getDeclaredType();

auto callback = [&](Type inputType, Type resultType) {
// Look for the signature (Int) -> Int
return inputType->isEqual(intType) && resultType->isEqual(intType);
};

auto decl = lookupLibraryIntrinsicFunc(*this, "_mixInt", resolver, callback);
Impl.MixIntDecl = decl;
return decl;
SmallVector<ValueDecl *, 1> results;
lookupInSwiftModule("_hashValue", results);
for (auto result : results) {
auto *fd = dyn_cast<FuncDecl>(result);
if (!fd)
continue;
auto paramLists = fd->getParameterLists();
if (paramLists.size() != 1 || paramLists[0]->size() != 1)
continue;
auto paramDecl = paramLists[0]->get(0);
if (paramDecl->getArgumentName() != Id_for)
continue;
auto genericParams = fd->getGenericParams();
if (!genericParams || genericParams->size() != 1)
continue;
Impl.HashValueForDecl = fd;
return fd;
}
return nullptr;
}

FuncDecl *ASTContext::getArrayAppendElementDecl() const {
Expand Down
4 changes: 2 additions & 2 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3583,8 +3583,8 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,

SILValue hashCode;

// TODO: Combine hashes of the indexes. There isn't a great hash combining
// interface in the standard library to do this yet.
// TODO: Combine hashes of the indexes using an _UnsafeHasher passed in as
// an extra parameter.
{
auto &index = indexes[0];

Expand Down
Loading