stack_fragment.cpp 1.3 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 "explorer/interpreter/stack_fragment.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. namespace Carbon {
  8. StackFragment::~StackFragment() {
  9. CARBON_CHECK(reversed_todo_.empty())
  10. << "All StackFragments must be empty before the Carbon program ends.";
  11. }
  12. void StackFragment::StoreReversed(
  13. std::vector<std::unique_ptr<Action>> reversed_todo) {
  14. CARBON_CHECK(reversed_todo_.empty());
  15. reversed_todo_ = std::move(reversed_todo);
  16. }
  17. void StackFragment::RestoreTo(Stack<std::unique_ptr<Action>>& todo) {
  18. while (!reversed_todo_.empty()) {
  19. todo.Push(std::move(reversed_todo_.back()));
  20. reversed_todo_.pop_back();
  21. }
  22. }
  23. void StackFragment::Clear() {
  24. // We destroy the underlying Actions explicitly to ensure they're
  25. // destroyed in the correct order.
  26. for (auto& action : reversed_todo_) {
  27. action.reset();
  28. }
  29. reversed_todo_.clear();
  30. }
  31. void StackFragment::Print(llvm::raw_ostream& out) const {
  32. out << "{";
  33. llvm::ListSeparator sep(" :: ");
  34. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  35. out << sep << *action;
  36. }
  37. out << "}";
  38. }
  39. } // namespace Carbon