action.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "executable_semantics/interpreter/action.h"
  5. #include <iterator>
  6. #include <map>
  7. #include <optional>
  8. #include <utility>
  9. #include <vector>
  10. #include "executable_semantics/ast/declaration.h"
  11. #include "executable_semantics/ast/expression.h"
  12. #include "executable_semantics/common/arena.h"
  13. #include "executable_semantics/interpreter/stack.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/Casting.h"
  16. namespace Carbon {
  17. using llvm::cast;
  18. Scope::Scope(Scope&& other) noexcept
  19. : values_(other.values_),
  20. locals_(std::exchange(other.locals_, {})),
  21. heap_(other.heap_) {}
  22. auto Scope::operator=(Scope&& rhs) noexcept -> Scope& {
  23. values_ = rhs.values_;
  24. locals_ = std::exchange(rhs.locals_, {});
  25. heap_ = rhs.heap_;
  26. return *this;
  27. }
  28. Scope::~Scope() {
  29. for (const auto& l : locals_) {
  30. std::optional<AllocationId> a = values_.Get(l);
  31. CHECK(a.has_value());
  32. heap_->Deallocate(*a);
  33. }
  34. }
  35. void Action::Print(llvm::raw_ostream& out) const {
  36. switch (kind()) {
  37. case Action::Kind::LValAction:
  38. out << cast<LValAction>(*this).expression();
  39. break;
  40. case Action::Kind::ExpressionAction:
  41. out << cast<ExpressionAction>(*this).expression();
  42. break;
  43. case Action::Kind::PatternAction:
  44. out << cast<PatternAction>(*this).pattern();
  45. break;
  46. case Action::Kind::StatementAction:
  47. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  48. break;
  49. case Action::Kind::DeclarationAction:
  50. cast<DeclarationAction>(*this).declaration().Print(out);
  51. break;
  52. case Action::Kind::ScopeAction:
  53. out << "ScopeAction";
  54. }
  55. out << "<" << pos_ << ">";
  56. if (results_.size() > 0) {
  57. out << "(";
  58. llvm::ListSeparator sep;
  59. for (auto& result : results_) {
  60. out << sep << *result;
  61. }
  62. out << ")";
  63. }
  64. }
  65. } // namespace Carbon