action.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/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 << "scope: [";
  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. out << kind_string() << " pos: " << pos_ << " ";
  128. switch (kind()) {
  129. case Action::Kind::LocationAction:
  130. out << "`" << cast<LocationAction>(*this).expression() << "`";
  131. break;
  132. case Action::Kind::ValueExpressionAction:
  133. out << "`" << cast<ValueExpressionAction>(*this).expression() << "`";
  134. break;
  135. case Action::Kind::ExpressionAction:
  136. out << "`" << cast<ExpressionAction>(*this).expression() << "`";
  137. break;
  138. case Action::Kind::WitnessAction:
  139. out << "`" << *cast<WitnessAction>(*this).witness() << "`";
  140. break;
  141. case Action::Kind::StatementAction:
  142. out << "`" << PrintAsID(cast<StatementAction>(*this).statement()) << "`";
  143. break;
  144. case Action::Kind::DeclarationAction:
  145. out << "`" << PrintAsID(cast<DeclarationAction>(*this).declaration())
  146. << "`";
  147. break;
  148. case Action::Kind::TypeInstantiationAction:
  149. out << "`" << *cast<TypeInstantiationAction>(*this).type() << "`";
  150. break;
  151. default:
  152. break;
  153. }
  154. if (!results_.empty()) {
  155. out << " results: [";
  156. llvm::ListSeparator sep;
  157. for (const auto& result : results_) {
  158. out << sep << "`" << *result << "`";
  159. }
  160. out << "] ";
  161. }
  162. if (scope_.has_value()) {
  163. out << " " << *scope_;
  164. }
  165. }
  166. auto Action::kind_string() const -> std::string_view {
  167. switch (kind()) {
  168. case Action::Kind::LocationAction:
  169. return "LocationAction";
  170. case Action::Kind::ValueExpressionAction:
  171. return "ValueExpressionAction";
  172. case Action::Kind::ExpressionAction:
  173. return "ExpressionAction";
  174. case Action::Kind::WitnessAction:
  175. return "WitnessAction";
  176. case Action::Kind::StatementAction:
  177. return "StatementAction";
  178. case Action::Kind::DeclarationAction:
  179. return "DeclarationAction";
  180. case Action::Kind::TypeInstantiationAction:
  181. return "TypeInstantiationAction";
  182. case Action::Kind::ScopeAction:
  183. return "ScopeAction";
  184. case Action::Kind::RecursiveAction:
  185. return "RecursiveAction";
  186. case Action::Kind::CleanUpAction:
  187. return "CleanUpAction";
  188. case Action::Kind::DestroyAction:
  189. return "DestroyAction";
  190. }
  191. }
  192. } // namespace Carbon