Files
carbon-lang/toolchain/sem_ir/copy_on_write_block.h
T
Jon Ross-Perkins 6b5eb1a101 Id::Invalid -> Id::None (#4834)
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.
2025-01-22 23:15:00 +00:00

78 lines
2.9 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_SEM_IR_COPY_ON_WRITE_BLOCK_H_
#define CARBON_TOOLCHAIN_SEM_IR_COPY_ON_WRITE_BLOCK_H_
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// A handle to a new block that may be modified, with copy-on-write semantics.
//
// The constructor is given the ID of an existing block that provides the
// initial contents of the new block. The new block is lazily allocated; if no
// modifications have been made, the `id()` function will return the original
// block ID.
//
// This is intended to avoid an unnecessary block allocation in the case where
// the new block ends up being exactly the same as the original block.
template <typename BlockIdType, auto (SemIR::File::*ValueStore)()>
class CopyOnWriteBlock {
public:
struct UninitializedBlock {
size_t size;
};
// Constructs the block. `source_id` is used as the initial value of the
// block.
explicit CopyOnWriteBlock(SemIR::File& file, BlockIdType source_id)
: file_(file), source_id_(source_id) {}
// Constructs the block, treating the original block as an uninitialized block
// with `size` elements.
explicit CopyOnWriteBlock(SemIR::File& file, UninitializedBlock uninit)
: file_(file),
source_id_(BlockIdType::None),
id_((file_.*ValueStore)().AddUninitialized(uninit.size)) {}
// Gets a block ID containing the resulting elements. Note that further
// modifications may or may not allocate a new ID, so this should only be
// called once all modifications have been performed.
auto id() const -> BlockIdType { return id_; }
// Gets a canonical block ID containing the resulting elements. This assumes
// the original block ID, if specified, was also canonical.
auto GetCanonical() const -> BlockIdType {
return id_ == source_id_ ? id_ : (file_.*ValueStore)().MakeCanonical(id_);
}
// Sets the element at index `i` within the block. Lazily allocates a new
// block when the value changes for the first time.
auto Set(int i, typename BlockIdType::ElementType value) -> void {
if (source_id_.has_value() && (file_.*ValueStore)().Get(id_)[i] == value) {
return;
}
if (id_ == source_id_) {
id_ = (file_.*ValueStore)().Add((file_.*ValueStore)().Get(source_id_));
}
(file_.*ValueStore)().GetMutable(id_)[i] = value;
}
private:
SemIR::File& file_;
BlockIdType source_id_;
BlockIdType id_ = source_id_;
};
using CopyOnWriteInstBlock = CopyOnWriteBlock<InstBlockId, &File::inst_blocks>;
using CopyOnWriteStructTypeFieldsBlock =
CopyOnWriteBlock<StructTypeFieldsId, &File::struct_type_fields>;
using CopyOnWriteTypeBlock = CopyOnWriteBlock<TypeBlockId, &File::type_blocks>;
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_COPY_ON_WRITE_BLOCK_H_