Skip to content

Customize ClangDeclId instead of customizing ClangDecl hashing and equality #5725

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

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
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
17 changes: 15 additions & 2 deletions toolchain/base/value_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ class CanonicalValueStore {
Set<IdT, /*SmallSize=*/0, KeyContext> set_;
};

template <typename T>
concept HasTranslateValueToKey = requires { &T::TranslateValueToKey; };

template <typename IdT>
class CanonicalValueStore<IdT>::KeyContext
: public TranslatingKeyContext<KeyContext> {
Expand All @@ -275,8 +278,18 @@ class CanonicalValueStore<IdT>::KeyContext

// Note that it is safe to return a `const` reference here as the underlying
// object's lifetime is provided by the `ValueStore`.
auto TranslateKey(IdT id) const -> ValueStore<IdT>::ConstRefType {
return values_->Get(id);
auto TranslateKey(IdT id) const {
if constexpr (requires { IdT::TranslateValueToKey(values_->Get(id)); }) {
return IdT::TranslateValueToKey(values_->Get(id));
} else {
return values_->Get(id);
}
}

auto TranslateKey(ValueType value) const
requires HasTranslateValueToKey<IdT>
{
return IdT::TranslateValueToKey(value);
}

private:
Expand Down
14 changes: 3 additions & 11 deletions toolchain/sem_ir/clang_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ namespace Carbon::SemIR {
struct ClangDecl : public Printable<ClangDecl> {
auto Print(llvm::raw_ostream& out) const -> void;

friend auto CarbonHashtableEq(const ClangDecl& lhs, const ClangDecl& rhs)
-> bool {
return HashtableEq(lhs.decl, rhs.decl);
}

// Hashing for ClangDecl. See common/hashing.h.
friend auto CarbonHashValue(const ClangDecl& value, uint64_t seed)
-> HashCode {
return HashValue(value.decl, seed);
}

// The Clang declaration pointing to the Clang AST.
// TODO: Ensure we can easily serialize/deserialize this. Consider
// `clang::LazyDeclPtr`.
Expand All @@ -53,6 +42,9 @@ struct ClangDeclId : public IdBase<ClangDeclId> {
static constexpr llvm::StringLiteral Label = "clang_decl_id";

using ValueType = ClangDecl;
static auto TranslateValueToKey(const ValueType& value) -> clang::Decl* {
return value.decl;
}

using IdBase::IdBase;
};
Expand Down
Loading