Refactor ValueStore to use custom iterators and ranges (#7506)

Replace the llvm::map_range based anonymous ranges returned by values()
and enumerate() with custom range and iterator types. This avoids
exposing complex template return types and improves encapsulation. But
most importantly, it is *much* cheaper to compile.

This change:
- Defines ValueStoreIterator, ValueStoreEnumerateIterator, and
ValueStoreRange templates in the Internal namespace.
- Exposes them via aliases in ValueStore: Iterator, ConstIterator,
Range, MutableRange, and EnumerateRange.
- Supports safe implicit conversion from Iterator to ConstIterator.
- Supports operator-> even when the value type is returned by value
(e.g., llvm::StringRef) by conditionally const-qualifying the pointer
type.
- Ensures C++20 comparison consistency with custom operator<=> and
operator== definitions.
- Restricts construction of these iterators and ranges to ValueStore
methods by using private constructors and friend declarations.

Assisted-by: Antigravity with Gemini
This commit is contained in:
Chandler Carruth
2026-07-15 17:06:52 +00:00
committed by GitHub
parent 168420f805
commit 14e8bffe24
3 changed files with 213 additions and 41 deletions
+23
View File
@@ -9,6 +9,7 @@
#include <string>
#include "llvm/ADT/StringRef.h"
#include "toolchain/base/value_ids.h"
namespace Carbon::Testing {
@@ -44,5 +45,27 @@ TEST(ValueStore, Real) {
EXPECT_THAT(real2.is_decimal, Eq(real2_copy.is_decimal));
}
TEST(ValueStore, StringRefIteratorArrow) {
ValueStore<IdentifierId, llvm::StringRef> store;
store.Add("hello");
// Non-const values() returns MutableRange which uses Iterator
auto mutable_range = store.values();
auto it = mutable_range.begin();
// This calls Iterator::operator->
EXPECT_EQ(it->size(), 5);
}
TEST(ValueStore, IteratorConversion) {
ValueStore<IdentifierId, llvm::StringRef> store;
store.Add("hello");
auto mutable_range = store.values();
ValueStore<IdentifierId, llvm::StringRef>::Iterator it =
mutable_range.begin();
ValueStore<IdentifierId, llvm::StringRef>::ConstIterator cit = it;
EXPECT_EQ(*cit, "hello");
}
} // namespace
} // namespace Carbon::Testing