action.cpp 5.0 KB

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