action.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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::ScopeAction:
  50. out << "ScopeAction";
  51. }
  52. out << "<" << pos_ << ">";
  53. if (results_.size() > 0) {
  54. out << "(";
  55. llvm::ListSeparator sep;
  56. for (auto& result : results_) {
  57. out << sep << *result;
  58. }
  59. out << ")";
  60. }
  61. }
  62. void Action::PrintList(const Stack<Nonnull<Action*>>& ls,
  63. llvm::raw_ostream& out) {
  64. llvm::ListSeparator sep(" :: ");
  65. for (const auto& action : ls) {
  66. out << sep << *action;
  67. }
  68. }
  69. } // namespace Carbon