action.cpp 5.3 KB

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