mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
High level, replacing `Id::Invalid` with `Id::None` and `Id::is_valid` with `Id::has_value` for clarity, as discussed [here](https://discord.com/channels/655572317891461132/655578254970716160/1331664574545395794). The `IntId` refactoring is needed together with `AnyIdBase` because it's also used with `ValueStore`. Note, trying to be careful not to rewrite `EnumBase::InvalidIndex`, or `is_valid` in general (e.g., `IdKind::is_valid`). I've tried to sequence commits here: 1. Automatic replacements: - `((?:Id|Index)(?: |::|\(|Base(?:\(|::)))Invalid((?:Index)?\W)` -> `$1None$2` - `<invalid>` -> `<none>` - `InvalidNodeId` -> `NoneNodeId` - `/\*invalid\*/` -> `/*none*/` - `id((?:_|\(\))(?:\.|->))is_valid` -> `id$1has_value` 2. Manual edits: - In `int.h` and `int_test.cpp` - `IntT` has `is_value`, which I'm renaming to `is_embedded_value`. - Manual edits to comments in this file. - `AnyIdBase` and `IdBase` - Declaration of `is_valid` -> `has_value`, `InvalidIndex` -> `NoneIndex`. - In `ids.h` and `ids.cpp` - `is_valid` -> `has_value` - `// An explicitly invalid ID.` -> `// An ID with no value.`; similar for index - Various math on `InvalidIndex` -> `NoneIndex` - Various mentions of "valid" in comments - In `value_store.h`, for `IdT::Invalid`, plus one comment - In `impl.h` and `tokenized_buffer.h`, we had different initialization of `::None` values (versus `ids.h` syntax) that I fixed manually. - Spot checks to compile - Particularly where `is_valid` replacements didn't catch spots due to different naming. 3. Autoupdate tests 4. verbose.carbon (NOAUTOUPDATE) 5. Comment spot checks Note there are probably other mentions of "Invalid" that should be swept up, but I'd like to argue for merging and separating out remaining cleanup since this is so sweeping (and likely to hit merge conflicts from churn). We'll probably have lingering mentions of "invalid" for a bit regardless, just because there are uses of "invalid" in non-Id APIs.
203 lines
6.6 KiB
C++
203 lines
6.6 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
|
|
#define CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
|
|
|
|
#include <type_traits>
|
|
|
|
#include "common/check.h"
|
|
#include "common/hashtable_key_context.h"
|
|
#include "common/ostream.h"
|
|
#include "common/set.h"
|
|
#include "llvm/ADT/Sequence.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
#include "toolchain/base/mem_usage.h"
|
|
#include "toolchain/base/yaml.h"
|
|
|
|
namespace Carbon {
|
|
|
|
namespace Internal {
|
|
|
|
// Used as a parent class for non-printable types. This is just for
|
|
// std::conditional, not as an API.
|
|
class ValueStoreNotPrintable {};
|
|
} // namespace Internal
|
|
|
|
// A simple wrapper for accumulating values, providing IDs to later retrieve the
|
|
// value. This does not do deduplication.
|
|
//
|
|
// IdT::ValueType must represent the type being indexed.
|
|
template <typename IdT>
|
|
class ValueStore
|
|
: public std::conditional<
|
|
std::is_base_of_v<Printable<typename IdT::ValueType>,
|
|
typename IdT::ValueType>,
|
|
Yaml::Printable<ValueStore<IdT>>, Internal::ValueStoreNotPrintable> {
|
|
public:
|
|
using ValueType = typename IdT::ValueType;
|
|
|
|
// Typically we want to use `ValueType&` and `const ValueType& to avoid
|
|
// copies, but when the value type is a `StringRef`, we assume external
|
|
// storage for the string data and both our value type and ref type will be
|
|
// `StringRef`. This will preclude mutation of the string data.
|
|
using RefType = std::conditional_t<std::same_as<llvm::StringRef, ValueType>,
|
|
llvm::StringRef, ValueType&>;
|
|
using ConstRefType =
|
|
std::conditional_t<std::same_as<llvm::StringRef, ValueType>,
|
|
llvm::StringRef, const ValueType&>;
|
|
|
|
// Stores the value and returns an ID to reference it.
|
|
auto Add(ValueType value) -> IdT {
|
|
IdT id(values_.size());
|
|
// This routine is especially hot and the check here relatively expensive
|
|
// for the value provided, so only do this in debug builds to make tracking
|
|
// down issues easier.
|
|
CARBON_DCHECK(id.index >= 0, "Id overflow");
|
|
values_.push_back(std::move(value));
|
|
return id;
|
|
}
|
|
|
|
// Adds a default constructed value and returns an ID to reference it.
|
|
auto AddDefaultValue() -> IdT {
|
|
IdT id(values_.size());
|
|
values_.resize(id.index + 1);
|
|
return id;
|
|
}
|
|
|
|
// Returns a mutable value for an ID.
|
|
auto Get(IdT id) -> RefType {
|
|
CARBON_DCHECK(id.index >= 0, "{0}", id);
|
|
return values_[id.index];
|
|
}
|
|
|
|
// Returns the value for an ID.
|
|
auto Get(IdT id) const -> ConstRefType {
|
|
CARBON_DCHECK(id.index >= 0, "{0}", id);
|
|
return values_[id.index];
|
|
}
|
|
|
|
// Reserves space.
|
|
auto Reserve(size_t size) -> void { values_.reserve(size); }
|
|
|
|
// These are to support printable structures, and are not guaranteed.
|
|
auto OutputYaml() const -> Yaml::OutputMapping {
|
|
return Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
|
|
for (auto i : llvm::seq(values_.size())) {
|
|
IdT id(i);
|
|
map.Add(PrintToString(id), Yaml::OutputScalar(Get(id)));
|
|
}
|
|
});
|
|
}
|
|
|
|
// Collects memory usage of the values.
|
|
auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
|
|
-> void {
|
|
mem_usage.Collect(label.str(), values_);
|
|
}
|
|
|
|
auto array_ref() const -> llvm::ArrayRef<ValueType> { return values_; }
|
|
auto size() const -> size_t { return values_.size(); }
|
|
|
|
private:
|
|
// Set inline size to 0 because these will typically be too large for the
|
|
// stack, while this does make File smaller.
|
|
llvm::SmallVector<std::decay_t<ValueType>, 0> values_;
|
|
};
|
|
|
|
// A wrapper for accumulating immutable values with deduplication, providing IDs
|
|
// to later retrieve the value.
|
|
//
|
|
// IdT::ValueType must represent the type being indexed.
|
|
template <typename IdT>
|
|
class CanonicalValueStore {
|
|
public:
|
|
using ValueType = typename IdT::ValueType;
|
|
using RefType = typename ValueStore<IdT>::RefType;
|
|
using ConstRefType = typename ValueStore<IdT>::ConstRefType;
|
|
|
|
// Stores a canonical copy of the value and returns an ID to reference it.
|
|
auto Add(ValueType value) -> IdT;
|
|
|
|
// Returns the value for an ID.
|
|
auto Get(IdT id) const -> ConstRefType { return values_.Get(id); }
|
|
|
|
// Looks up the canonical ID for a value, or returns `None` if not in the
|
|
// store.
|
|
auto Lookup(ValueType value) const -> IdT;
|
|
|
|
// Reserves space.
|
|
auto Reserve(size_t size) -> void;
|
|
|
|
// These are to support printable structures, and are not guaranteed.
|
|
auto OutputYaml() const -> Yaml::OutputMapping {
|
|
return values_.OutputYaml();
|
|
}
|
|
|
|
auto array_ref() const -> llvm::ArrayRef<ValueType> {
|
|
return values_.array_ref();
|
|
}
|
|
auto size() const -> size_t { return values_.size(); }
|
|
|
|
// Collects memory usage of the values and deduplication set.
|
|
auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
|
|
-> void {
|
|
mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
|
|
auto bytes =
|
|
set_.ComputeMetrics(KeyContext(values_.array_ref())).storage_bytes;
|
|
mem_usage.Add(MemUsage::ConcatLabel(label, "set_"), bytes, bytes);
|
|
}
|
|
|
|
private:
|
|
class KeyContext;
|
|
|
|
ValueStore<IdT> values_;
|
|
Set<IdT, /*SmallSize=*/0, KeyContext> set_;
|
|
};
|
|
|
|
template <typename IdT>
|
|
class CanonicalValueStore<IdT>::KeyContext
|
|
: public TranslatingKeyContext<KeyContext> {
|
|
public:
|
|
explicit KeyContext(llvm::ArrayRef<ValueType> values) : values_(values) {}
|
|
|
|
// Note that it is safe to return a `const` reference here as the underlying
|
|
// object's lifetime is provided by the `store_`.
|
|
auto TranslateKey(IdT id) const -> const ValueType& {
|
|
return values_[id.index];
|
|
}
|
|
|
|
private:
|
|
llvm::ArrayRef<ValueType> values_;
|
|
};
|
|
|
|
template <typename IdT>
|
|
auto CanonicalValueStore<IdT>::Add(ValueType value) -> IdT {
|
|
auto make_key = [&] { return IdT(values_.Add(std::move(value))); };
|
|
return set_.Insert(value, make_key, KeyContext(values_.array_ref())).key();
|
|
}
|
|
|
|
template <typename IdT>
|
|
auto CanonicalValueStore<IdT>::Lookup(ValueType value) const -> IdT {
|
|
if (auto result = set_.Lookup(value, KeyContext(values_.array_ref()))) {
|
|
return result.key();
|
|
}
|
|
return IdT::None;
|
|
}
|
|
|
|
template <typename IdT>
|
|
auto CanonicalValueStore<IdT>::Reserve(size_t size) -> void {
|
|
// Compute the resulting new insert count using the size of values -- the
|
|
// set doesn't have a fast to compute current size.
|
|
if (size > values_.size()) {
|
|
set_.GrowForInsertCount(size - values_.size(),
|
|
KeyContext(values_.array_ref()));
|
|
}
|
|
values_.Reserve(size);
|
|
}
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_BASE_VALUE_STORE_H_
|