semantics_node_block_stack.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "toolchain/semantics/semantics_node_block_stack.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/semantics/semantics_node.h"
  7. namespace Carbon {
  8. auto SemanticsNodeBlockStack::Push(SemanticsNodeBlockId id) -> void {
  9. CARBON_VLOG() << name_ << " Push " << stack_.size() << "\n";
  10. CARBON_CHECK(stack_.size() < (1 << 20))
  11. << "Excessive stack size: likely infinite loop";
  12. stack_.push_back(id);
  13. }
  14. auto SemanticsNodeBlockStack::PeekForAdd() -> SemanticsNodeBlockId {
  15. CARBON_CHECK(!stack_.empty()) << "no current block";
  16. auto& back = stack_.back();
  17. if (!back.is_valid()) {
  18. back = semantics_ir_->AddNodeBlock();
  19. CARBON_VLOG() << name_ << " Add " << stack_.size() - 1 << ": " << back
  20. << "\n";
  21. }
  22. return back;
  23. }
  24. auto SemanticsNodeBlockStack::Pop() -> SemanticsNodeBlockId {
  25. CARBON_CHECK(!stack_.empty()) << "no current block";
  26. auto back = stack_.pop_back_val();
  27. CARBON_VLOG() << name_ << " Pop " << stack_.size() << ": " << back << "\n";
  28. if (!back.is_valid()) {
  29. return SemanticsNodeBlockId::Empty;
  30. }
  31. return back;
  32. }
  33. auto SemanticsNodeBlockStack::PrintForStackDump(llvm::raw_ostream& output) const
  34. -> void {
  35. output << name_ << ":\n";
  36. for (int i = 0; i < static_cast<int>(stack_.size()); ++i) {
  37. output << "\t" << i << ".\t" << stack_[i] << "\n";
  38. }
  39. }
  40. } // namespace Carbon