action.cpp 7.2 KB

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