action.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/action.h"
  5. #include <iterator>
  6. #include <map>
  7. #include <optional>
  8. #include <utility>
  9. #include <vector>
  10. #include "explorer/ast/declaration.h"
  11. #include "explorer/ast/expression.h"
  12. #include "explorer/common/arena.h"
  13. #include "explorer/interpreter/stack.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/Support/Casting.h"
  16. namespace Carbon {
  17. using llvm::cast;
  18. RuntimeScope::RuntimeScope(RuntimeScope&& other) noexcept
  19. : locals_(std::move(other.locals_)),
  20. // To transfer ownership of other.allocations_, we have to empty it out.
  21. allocations_(std::exchange(other.allocations_, {})),
  22. heap_(other.heap_),
  23. destructor_scope_(other.destructor_scope_) {}
  24. auto RuntimeScope::operator=(RuntimeScope&& rhs) noexcept -> RuntimeScope& {
  25. locals_ = std::move(rhs.locals_);
  26. // To transfer ownership of rhs.allocations_, we have to empty it out.
  27. allocations_ = std::exchange(rhs.allocations_, {});
  28. heap_ = rhs.heap_;
  29. destructor_scope_ = rhs.destructor_scope_;
  30. return *this;
  31. }
  32. RuntimeScope::~RuntimeScope() {
  33. for (AllocationId allocation : allocations_) {
  34. heap_->Deallocate(allocation);
  35. }
  36. }
  37. void RuntimeScope::Print(llvm::raw_ostream& out) const {
  38. out << "{";
  39. llvm::ListSeparator sep;
  40. for (const auto& [value_node, value] : locals_) {
  41. out << sep << value_node.base() << ": " << *value;
  42. }
  43. out << "}";
  44. }
  45. void RuntimeScope::Initialize(ValueNodeView value_node,
  46. Nonnull<const Value*> value) {
  47. CARBON_CHECK(!value_node.constant_value().has_value());
  48. CARBON_CHECK(value->kind() != Value::Kind::LValue);
  49. allocations_.push_back(heap_->AllocateValue(value));
  50. auto [it, success] = locals_.insert(
  51. {value_node, heap_->arena().New<LValue>(Address(allocations_.back()))});
  52. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  53. }
  54. void RuntimeScope::Merge(RuntimeScope other) {
  55. CARBON_CHECK(heap_ == other.heap_);
  56. for (auto& element : other.locals_) {
  57. CARBON_CHECK(locals_.count(element.first) == 0)
  58. << "Duplicate definition of" << element.first;
  59. locals_.insert(element);
  60. }
  61. allocations_.insert(allocations_.end(), other.allocations_.begin(),
  62. other.allocations_.end());
  63. other.allocations_.clear();
  64. }
  65. auto RuntimeScope::Get(ValueNodeView value_node) const
  66. -> std::optional<Nonnull<const LValue*>> {
  67. auto it = locals_.find(value_node);
  68. if (it != locals_.end()) {
  69. return it->second;
  70. } else {
  71. return std::nullopt;
  72. }
  73. }
  74. auto RuntimeScope::Capture(
  75. const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
  76. CARBON_CHECK(!scopes.empty());
  77. RuntimeScope result(scopes.front()->heap_);
  78. for (Nonnull<const RuntimeScope*> scope : scopes) {
  79. CARBON_CHECK(scope->heap_ == result.heap_);
  80. for (const auto& entry : scope->locals_) {
  81. // Intentionally disregards duplicates later in the vector.
  82. result.locals_.insert(entry);
  83. }
  84. }
  85. return result;
  86. }
  87. void RuntimeScope::TransitState() {
  88. if (destructor_scope_ == State::Normal) {
  89. destructor_scope_ = State::Destructor;
  90. } else if (destructor_scope_ == State::Destructor) {
  91. destructor_scope_ = State::CleanUpped;
  92. } else {
  93. destructor_scope_ = State::CleanUpped;
  94. }
  95. }
  96. void Action::Print(llvm::raw_ostream& out) const {
  97. switch (kind()) {
  98. case Action::Kind::LValAction:
  99. out << cast<LValAction>(*this).expression() << " ";
  100. break;
  101. case Action::Kind::ExpressionAction:
  102. out << cast<ExpressionAction>(*this).expression() << " ";
  103. break;
  104. case Action::Kind::WitnessAction:
  105. out << *cast<WitnessAction>(*this).witness() << " ";
  106. break;
  107. case Action::Kind::PatternAction:
  108. out << cast<PatternAction>(*this).pattern() << " ";
  109. break;
  110. case Action::Kind::StatementAction:
  111. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  112. out << " ";
  113. break;
  114. case Action::Kind::DeclarationAction:
  115. cast<DeclarationAction>(*this).declaration().Print(out);
  116. out << " ";
  117. break;
  118. case Action::Kind::ScopeAction:
  119. break;
  120. case Action::Kind::RecursiveAction:
  121. out << "recursive";
  122. break;
  123. case Action::Kind::CleanUpAction:
  124. out << "clean up";
  125. break;
  126. }
  127. out << "." << pos_ << ".";
  128. if (!results_.empty()) {
  129. out << " [[";
  130. llvm::ListSeparator sep;
  131. for (auto& result : results_) {
  132. out << sep << *result;
  133. }
  134. out << "]]";
  135. }
  136. if (this->scope().has_value()) {
  137. out << " " << *this->scope();
  138. }
  139. }
  140. } // namespace Carbon