Files
carbon-lang/toolchain/semantics/semantics_node_block_stack.h
T
Jon Ross-Perkins b9df8ca765 Manual cleanup of toolchain clang-tidy/clangd warnings. (#3157)
A lot of this is more boring "remove unused header", plus some other
minor cleanups. I think the most significant changes were:

- yaml_test_helpers.cpp is doing a switch on an unsigned int, comparing
to enum values.
-
[bugprone-switch-missing-default-case](https://clang.llvm.org/extra/clang-tidy/checks/bugprone/switch-missing-default-case.html)
is unhappy with EnumBase, but correctly identified
yaml_test_helpers.cpp, so I'm opting to address it rather than disabling
it even though it needs NOLINT in several locations as a result, in
addition to what I think are some low-value `default` cases. I'd be fine
going the other way with this too and disabling it globally (I could see
it being noisier in the explorer).
- MarkInitializerFor swaps the argument names between the .h and .cpp. I
think the .cpp had the order as intended.
- There's a new-ish
[performance-enum-size](https://clang.llvm.org/extra/clang-tidy/checks/performance/enum-size.html)
which I'm basically treating as "add int8_t to enums".

My main motivation here is to just clean up as many of these as I can so
that I stop seeing them in vscode.
2023-08-25 22:45:57 +00:00

90 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_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_
#include "llvm/ADT/SmallVector.h"
#include "toolchain/semantics/semantics_ir.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon::Check {
// Wraps the stack of node blocks for Context.
//
// All pushes and pops will be vlogged.
class NodeBlockStack {
public:
explicit NodeBlockStack(llvm::StringLiteral name, SemIR::File& semantics_ir,
llvm::raw_ostream* vlog_stream)
: name_(name), semantics_ir_(&semantics_ir), vlog_stream_(vlog_stream) {}
// Pushes an existing node block.
auto Push(SemIR::NodeBlockId id) -> void;
// Pushes a new node block. It will be invalid unless PeekForAdd is called in
// order to support lazy allocation.
auto Push() -> void { Push(SemIR::NodeBlockId::Invalid); }
// Pushes a new unreachable code block.
auto PushUnreachable() -> void { Push(SemIR::NodeBlockId::Unreachable); }
// Allocates and pushes a new node block.
auto PushForAdd() -> SemIR::NodeBlockId {
Push();
return PeekForAdd();
}
// Peeks at the top node block. This does not trigger lazy allocation, so the
// returned node block may be invalid.
auto Peek() -> SemIR::NodeBlockId {
CARBON_CHECK(!stack_.empty()) << "no current block";
return stack_.back();
}
// Returns the top node block, allocating one if it's still invalid.
auto PeekForAdd() -> SemIR::NodeBlockId;
// Pops the top node block. This will always return a valid node block;
// SemIR::NodeBlockId::Empty is returned if one wasn't allocated.
auto Pop() -> SemIR::NodeBlockId;
// Pops the top node block, ensuring that it is lazily allocated if it's
// empty. For use when more nodes will be added to the block later.
auto PopForAdd() -> SemIR::NodeBlockId {
PeekForAdd();
return Pop();
}
// Returns whether the current block is statically reachable.
auto is_current_block_reachable() -> bool {
return Peek() != SemIR::NodeBlockId::Unreachable;
}
// Prints the stack for a stack dump.
auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
auto empty() const -> bool { return stack_.empty(); }
auto size() const -> size_t { return stack_.size(); }
private:
// A name for debugging.
llvm::StringLiteral name_;
// The underlying SemIR::File instance. Always non-null.
SemIR::File* semantics_ir_;
// Whether to print verbose output.
llvm::raw_ostream* vlog_stream_;
// The actual stack.
// PushEntry and PopEntry control modification in order to centralize
// vlogging.
llvm::SmallVector<SemIR::NodeBlockId> stack_;
};
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_NODE_BLOCK_STACK_H_