mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-18 06:00:27 +00:00
See arena.h for discussion of what canonicalization means in this context. This is primarily intended to support implementing a memo table of template instantiations to resolve #2951, but could be useful for other purposes as well. Additional changes: - Pass `VTable` constructor parameters by pointer, to avoid the need to define `operator==` and `hash_value` for it. - Clean up the recurring pattern of allocating identical `NamedElement`s on the stack and heap. Instead we always allocate it on the heap and pass it by pointer. - Add `Print()` and `Dump()` to `Bindings` as a debugging convenience. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com> Co-authored-by: Richard Smith <richard@metafoo.co.uk>
55 lines
1.7 KiB
C++
55 lines
1.7 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// Utilities for types that support the `Decompose` API.
|
|
|
|
#ifndef CARBON_EXPLORER_COMMON_DECOMPOSE_H_
|
|
#define CARBON_EXPLORER_COMMON_DECOMPOSE_H_
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/Hashing.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// CRTP base class which extends `Base` to support hashing and equality
|
|
// comparison. `Base` must support the `Decompose` API.
|
|
template <typename Base>
|
|
class HashFromDecompose {
|
|
public:
|
|
friend auto operator==(const HashFromDecompose& lhs,
|
|
const HashFromDecompose& rhs) -> bool {
|
|
return static_cast<const Base*>(&lhs)->Decompose(
|
|
[&](auto&&... lhs_elements) {
|
|
return static_cast<const Base*>(&rhs)->Decompose(
|
|
[&](auto&&... rhs_elements) {
|
|
return ((lhs_elements == rhs_elements) && ...);
|
|
});
|
|
});
|
|
}
|
|
|
|
friend auto hash_value(const HashFromDecompose& self) -> llvm::hash_code {
|
|
return static_cast<const Base*>(&self)->Decompose(
|
|
[&](auto&&... lhs_elements) {
|
|
return llvm::hash_combine(WrapForHash(lhs_elements)...);
|
|
});
|
|
}
|
|
|
|
private:
|
|
// Wraps T in a form that supports `hash_value`. Used for adapting types that
|
|
// we can't extend directly.
|
|
template <typename T>
|
|
static auto WrapForHash(const T& t) -> const T& {
|
|
return t;
|
|
}
|
|
|
|
template <typename T>
|
|
static auto WrapForHash(const std::vector<T>& vec) -> llvm::ArrayRef<T> {
|
|
return vec;
|
|
}
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_EXPLORER_COMMON_DECOMPOSE_H_
|