Implement canonicalization of Value and Element (#3024)

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>
This commit is contained in:
Geoff Romer
2023-08-03 16:05:59 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins Richard Smith
parent 2c4fff25a8
commit c93a0e5e42
18 changed files with 614 additions and 113 deletions
+39
View File
@@ -0,0 +1,39 @@
// 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
#include "explorer/common/decompose.h"
#include <gtest/gtest.h>
namespace Carbon {
namespace {
struct Decomposeable : public HashFromDecompose<Decomposeable> {
template <typename F>
auto Decompose(F f) const {
return f(i, s);
}
int i = 0;
std::string s;
};
TEST(HashFromDecomposeTest, EqualValues) {
Decomposeable d1 = {.i = 42, .s = "foo"};
Decomposeable d2 = {.i = 42, .s = "foo"};
EXPECT_TRUE(d1 == d2);
EXPECT_TRUE(hash_value(d1) == hash_value(d2));
}
TEST(HashFromDecomposeTest, NonEqualValues) {
Decomposeable d1 = {.i = 42, .s = "foo"};
Decomposeable d2 = {.i = 42, .s = "bar"};
EXPECT_FALSE(d1 == d2);
EXPECT_FALSE(hash_value(d1) == hash_value(d2));
}
} // namespace
} // namespace Carbon