region_stack.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_
  5. #define CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_
  6. #include <utility>
  7. #include "common/array_stack.h"
  8. #include "toolchain/check/diagnostic_helpers.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // Provides a stack of single-entry regions being built.
  12. class RegionStack {
  13. public:
  14. // A callback for Context::TODO.
  15. using TodoFn = std::function<auto(SemIR::LocId, std::string)->void>;
  16. explicit RegionStack(TodoFn todo_fn) : todo_fn_(std::move(todo_fn)) {}
  17. // Mark the start of a new single-entry region with the given entry block.
  18. auto PushRegion(SemIR::InstBlockId entry_block_id) -> void {
  19. stack_.PushArray();
  20. stack_.AppendToTop(entry_block_id);
  21. }
  22. // Mark the start of a new empty, unreachable region.
  23. auto PushUnreachableRegion() -> void { stack_.PushArray(); }
  24. // Add `block_id` to the most recently pushed single-entry region. To preserve
  25. // the single-entry property, `block_id` must not be directly reachable from
  26. // any block outside the region. To ensure the region's blocks are in lexical
  27. // order, this should be called when the first parse node associated with this
  28. // block is handled, or as close as possible.
  29. auto AddToRegion(SemIR::InstBlockId block_id, SemIR::LocId loc_id) -> void {
  30. if (stack_.empty()) {
  31. todo_fn_(loc_id,
  32. "Control flow expressions are currently only supported inside "
  33. "functions.");
  34. return;
  35. }
  36. if (block_id == SemIR::InstBlockId::Unreachable) {
  37. return;
  38. }
  39. stack_.AppendToTop(block_id);
  40. }
  41. // Adds multiple blocks at once. The caller is responsible for validating that
  42. // each block is reachable.
  43. auto AddToRegion(llvm::ArrayRef<SemIR::InstBlockId> block_ids) -> void {
  44. stack_.AppendToTop(block_ids);
  45. }
  46. // Complete creation of the most recently pushed single-entry region, and
  47. // return a list of its blocks.
  48. auto PopRegion() -> llvm::SmallVector<SemIR::InstBlockId> {
  49. llvm::SmallVector<SemIR::InstBlockId> result(stack_.PeekArray());
  50. stack_.PopArray();
  51. return result;
  52. }
  53. // Pops a region, and does not return its contents.
  54. auto PopAndDiscardRegion() -> void { stack_.PopArray(); }
  55. // Returns the top-most region.
  56. auto PeekRegion() -> llvm::ArrayRef<SemIR::InstBlockId> {
  57. return stack_.PeekArray();
  58. }
  59. // Runs verification that the processing cleanly finished.
  60. auto VerifyOnFinish() const -> void {
  61. CARBON_CHECK(stack_.empty(), "region_stack still has {0} entries",
  62. stack_.all_values_size());
  63. }
  64. // Returns true if any regions have been added.
  65. auto empty() -> bool { return stack_.empty(); }
  66. private:
  67. TodoFn todo_fn_;
  68. ArrayStack<SemIR::InstBlockId> stack_;
  69. };
  70. } // namespace Carbon::Check
  71. #endif // CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_