Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/formatter_chunks.h (C/C++)

Code before:
  };

  // Either a parent or content.
  struct Chunk {
    // Whether this chunk is known to be included in the output.
    bool include_in_output;

    // Either children or content.
    std::variant<llvm::SmallVector<ChunkId>, std::string> data;
  };

  // An empty `ChunkId`.
  static constexpr ChunkId None = ChunkId(-1);

  // Reserves space for at least `count` chunks.
  auto Reserve(size_t count) -> void { chunks_.reserve(count); }

  // Adds a `Chunk` that can have `children`. It can optionally start with one
  // `child_chunk`.
  auto AddParent(ChunkId child_chunk_id = None) -> ChunkId;

  // Switches from adding parents to adding content. This immediately makes
  // `out()` valid.
  auto StartContent() -> void;

  // Adds a new content `Chunk`. If the `parent_chunk` is included in
  // output, it'll also include the new chunk. Calls `format` to support adding
  // content to the new chunk.
  auto FormatChildContent(ChunkId parent_chunk_id,
                          llvm::function_ref<auto()->void> format) -> void;

  // Marks the given chunk as being included in the output if the current chunk
  // is. When `FormatChildContent` is currently active, there's a parent that
  // will determine `include_in_output for the child; otherwise, the child is
  // always included.
  //
  // For example, instructions in the file scope are added to implicitly-created
  // `Chunks` (`FormatChildContent` is not used). When
  // `AppendChildToCurrentParent` is called in that context, there's no parent
  // to limit visibility, so the child is also included.
  auto AppendChildToCurrentParent(ChunkId child_chunk_id) -> void;

  // Writes included chunks to the given stream.
  auto Write(llvm::raw_ostream& stream) -> void;

  // Returns a stream to write to the current chunk. Only valid to use after
  // `StartContent`, and may add a new chunk if one hasn't been started.
  auto out() -> llvm::raw_ostream& {
    CARBON_CHECK(content_start_id_ != None);
    if (!out_) {
      AddContent(/*include_in_output=*/true);

Review comment (suggestion):
"`parent_chunk`" is confusing because there's no such identifier in the code. I'd suggest instead something like this:

```suggestion
  // Adds a new content `Chunk`. If the given parent chunk is included in
```

or this:

```suggestion
  // Adds a new content `Chunk`. If `parent_chunk_id` is included in
```


Code after:
  // Reserves space for at least `count` chunks.
  auto Reserve(size_t count) -> void { chunks_.reserve(count); }

  // Adds a parent `Chunk` and returns its `ChunkId`. If `child_chunk_id` isn't
  // `None`, it's added as a child. Must be called before `StartContent`.
  //
  // By default the parent `Chunk` will not be included in the output, and
  // `AppendChildToCurrentParent` must be called to include it.
  auto AddParent(ChunkId child_chunk_id = None) -> ChunkId;

  // Switches from adding parents to adding content.
  auto StartContent() -> void;

  // Calls `format` to add content conditionally included when `parent_chunk_id`
  // is included. Must be called after `StartContent`.
  //
  // During a `FormatChildContent` call where the `parent_chunk_id` is not
  // already included in output, a new content `Chunk` is created, marked as a
  // child of `parent_chunk_id`, and `out` is temporarily directed to it during
  // the duration of `format. Otherwise, `out` lazily creates a content `Chunk`
  // which is always included in output.
  auto FormatChildContent(ChunkId parent_chunk_id,
                          llvm::function_ref<auto()->void> format) -> void;

  // Adds `child_chunk_id` to the children of a `FormatChildContent`'s
  // `parent_chunk_id` if called during `format`, or otherwise includes the
  // chunk in output.
  auto AppendChildToCurrentParent(ChunkId child_chunk_id) -> void;

  // Writes included chunks to the given stream.
  auto Write(llvm::raw_ostream& stream) -> void;

  // Returns a stream to write to a content `Chunk`. The returned reference is
  // only valid until the next `Chunk` starts. Must be called after
  // `StartContent`.
  //
  // See `FormatChildContent` for details of the target content `Chunk`.
  auto out() -> llvm::raw_ostream& {
    CARBON_CHECK(content_start_id_ != None);
    if (!out_) {
      AddContent(/*include_in_output=*/true);
    }
    return *out_;
  }

  auto size() -> size_t { return chunks_.size(); }

 private:
  // Either a parent or content.
  struct Chunk {
    // Whether this chunk is known to be included in the output.