action.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "common/check.h"
  11. #include "common/error.h"
  12. #include "explorer/ast/declaration.h"
  13. #include "explorer/ast/expression.h"
  14. #include "explorer/ast/value.h"
  15. #include "explorer/common/arena.h"
  16. #include "explorer/common/source_location.h"
  17. #include "explorer/interpreter/stack.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/Support/Casting.h"
  20. namespace Carbon {
  21. using llvm::cast;
  22. RuntimeScope::RuntimeScope(RuntimeScope&& other) noexcept
  23. : locals_(std::move(other.locals_)),
  24. bound_values_(std::move(other.bound_values_)),
  25. // To transfer ownership of other.allocations_, we have to empty it out.
  26. allocations_(std::exchange(other.allocations_, {})),
  27. heap_(other.heap_) {}
  28. auto RuntimeScope::operator=(RuntimeScope&& rhs) noexcept -> RuntimeScope& {
  29. locals_ = std::move(rhs.locals_);
  30. bound_values_ = std::move(rhs.bound_values_);
  31. // To transfer ownership of rhs.allocations_, we have to empty it out.
  32. allocations_ = std::exchange(rhs.allocations_, {});
  33. heap_ = rhs.heap_;
  34. return *this;
  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, Address address) {
  45. CARBON_CHECK(!value_node.constant_value().has_value());
  46. bool success =
  47. locals_.insert({value_node, heap_->arena().New<LocationValue>(address)})
  48. .second;
  49. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  50. }
  51. void RuntimeScope::BindAndPin(ValueNodeView value_node, Address address) {
  52. Bind(value_node, address);
  53. bool success = bound_values_.insert(&value_node.base()).second;
  54. CARBON_CHECK(success) << "Duplicate pinned node for " << value_node.base();
  55. heap_->BindValueToReference(value_node, address);
  56. }
  57. void RuntimeScope::BindLifetimeToScope(Address address) {
  58. CARBON_CHECK(address.element_path_.IsEmpty())
  59. << "Cannot extend lifetime of a specific sub-element";
  60. allocations_.push_back(address.allocation_);
  61. }
  62. void RuntimeScope::BindValue(ValueNodeView value_node,
  63. Nonnull<const Value*> value) {
  64. CARBON_CHECK(!value_node.constant_value().has_value());
  65. CARBON_CHECK(value->kind() != Value::Kind::LocationValue);
  66. bool success = locals_.insert({value_node, value}).second;
  67. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  68. }
  69. auto RuntimeScope::Initialize(ValueNodeView value_node,
  70. Nonnull<const Value*> value)
  71. -> Nonnull<const LocationValue*> {
  72. CARBON_CHECK(!value_node.constant_value().has_value());
  73. CARBON_CHECK(value->kind() != Value::Kind::LocationValue);
  74. allocations_.push_back(heap_->AllocateValue(value));
  75. const auto* location =
  76. heap_->arena().New<LocationValue>(Address(allocations_.back()));
  77. bool success = locals_.insert({value_node, location}).second;
  78. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  79. return location;
  80. }
  81. void RuntimeScope::Merge(RuntimeScope other) {
  82. CARBON_CHECK(heap_ == other.heap_);
  83. for (auto& element : other.locals_) {
  84. bool success = locals_.insert(element).second;
  85. CARBON_CHECK(success) << "Duplicate definition of " << element.first;
  86. }
  87. for (const auto* element : other.bound_values_) {
  88. bool success = bound_values_.insert(element).second;
  89. CARBON_CHECK(success) << "Duplicate bound value.";
  90. }
  91. allocations_.insert(allocations_.end(), other.allocations_.begin(),
  92. other.allocations_.end());
  93. other.allocations_.clear();
  94. }
  95. auto RuntimeScope::Get(ValueNodeView value_node,
  96. SourceLocation source_loc) const
  97. -> ErrorOr<std::optional<Nonnull<const Value*>>> {
  98. auto it = locals_.find(value_node);
  99. if (it == locals_.end()) {
  100. return {std::nullopt};
  101. }
  102. if (bound_values_.contains(&value_node.base())) {
  103. // Check if the bound value is still alive.
  104. CARBON_CHECK(it->second->kind() == Value::Kind::LocationValue);
  105. if (!heap_->is_bound_value_alive(
  106. value_node, cast<LocationValue>(it->second)->address())) {
  107. return ProgramError(source_loc)
  108. << "Reference has changed since this value was bound.";
  109. }
  110. }
  111. return {it->second};
  112. }
  113. auto RuntimeScope::Capture(
  114. const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
  115. CARBON_CHECK(!scopes.empty());
  116. RuntimeScope result(scopes.front()->heap_);
  117. for (Nonnull<const RuntimeScope*> scope : scopes) {
  118. CARBON_CHECK(scope->heap_ == result.heap_);
  119. for (const auto& entry : scope->locals_) {
  120. // Intentionally disregards duplicates later in the vector.
  121. result.locals_.insert(entry);
  122. }
  123. }
  124. return result;
  125. }
  126. void Action::Print(llvm::raw_ostream& out) const {
  127. switch (kind()) {
  128. case Action::Kind::LocationAction:
  129. out << cast<LocationAction>(*this).expression() << " ";
  130. break;
  131. case Action::Kind::ValueExpressionAction:
  132. out << cast<ValueExpressionAction>(*this).expression() << " ";
  133. break;
  134. case Action::Kind::ExpressionAction:
  135. out << cast<ExpressionAction>(*this).expression() << " ";
  136. break;
  137. case Action::Kind::WitnessAction:
  138. out << *cast<WitnessAction>(*this).witness() << " ";
  139. break;
  140. case Action::Kind::StatementAction:
  141. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  142. out << " ";
  143. break;
  144. case Action::Kind::DeclarationAction:
  145. cast<DeclarationAction>(*this).declaration().Print(out);
  146. out << " ";
  147. break;
  148. case Action::Kind::TypeInstantiationAction:
  149. cast<TypeInstantiationAction>(*this).type()->Print(out);
  150. out << " ";
  151. break;
  152. case Action::Kind::ScopeAction:
  153. break;
  154. case Action::Kind::RecursiveAction:
  155. out << "recursive";
  156. break;
  157. case Action::Kind::CleanUpAction:
  158. out << "clean up";
  159. break;
  160. case Action::Kind::DestroyAction:
  161. out << "destroy";
  162. break;
  163. }
  164. out << "." << pos_ << ".";
  165. if (!results_.empty()) {
  166. out << " [[";
  167. llvm::ListSeparator sep;
  168. for (const auto& result : results_) {
  169. out << sep << *result;
  170. }
  171. out << "]]";
  172. }
  173. if (scope_.has_value()) {
  174. out << " " << *scope_;
  175. }
  176. }
  177. } // namespace Carbon