action.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. void RuntimeScope::Print(llvm::raw_ostream& out) const {
  31. out << "{";
  32. llvm::ListSeparator sep;
  33. for (const auto& [value_node, value] : locals_) {
  34. out << sep << value_node.base() << ": " << *value;
  35. }
  36. out << "}";
  37. }
  38. void RuntimeScope::Bind(ValueNodeView value_node, Nonnull<const Value*> value) {
  39. CARBON_CHECK(!value_node.constant_value().has_value());
  40. CARBON_CHECK(value->kind() != Value::Kind::LocationValue);
  41. auto allocation_id = heap_->GetAllocationId(value);
  42. if (!allocation_id) {
  43. auto id = heap_->AllocateValue(value);
  44. auto [it, success] = locals_.insert(
  45. {value_node, heap_->arena().New<LocationValue>(Address(id))});
  46. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  47. } else {
  48. auto [it, success] = locals_.insert(
  49. {value_node,
  50. heap_->arena().New<LocationValue>(Address(*allocation_id))});
  51. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  52. }
  53. }
  54. void RuntimeScope::Initialize(ValueNodeView value_node,
  55. Nonnull<const Value*> value) {
  56. CARBON_CHECK(!value_node.constant_value().has_value());
  57. CARBON_CHECK(value->kind() != Value::Kind::LocationValue);
  58. allocations_.push_back(heap_->AllocateValue(value));
  59. auto [it, success] = locals_.insert(
  60. {value_node,
  61. heap_->arena().New<LocationValue>(Address(allocations_.back()))});
  62. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  63. }
  64. void RuntimeScope::Merge(RuntimeScope other) {
  65. CARBON_CHECK(heap_ == other.heap_);
  66. for (auto& element : other.locals_) {
  67. CARBON_CHECK(locals_.count(element.first) == 0)
  68. << "Duplicate definition of" << element.first;
  69. locals_.insert(element);
  70. }
  71. allocations_.insert(allocations_.end(), other.allocations_.begin(),
  72. other.allocations_.end());
  73. other.allocations_.clear();
  74. }
  75. auto RuntimeScope::Get(ValueNodeView value_node) const
  76. -> std::optional<Nonnull<const LocationValue*>> {
  77. auto it = locals_.find(value_node);
  78. if (it != locals_.end()) {
  79. return it->second;
  80. } else {
  81. return std::nullopt;
  82. }
  83. }
  84. auto RuntimeScope::Capture(
  85. const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
  86. CARBON_CHECK(!scopes.empty());
  87. RuntimeScope result(scopes.front()->heap_);
  88. for (Nonnull<const RuntimeScope*> scope : scopes) {
  89. CARBON_CHECK(scope->heap_ == result.heap_);
  90. for (const auto& entry : scope->locals_) {
  91. // Intentionally disregards duplicates later in the vector.
  92. result.locals_.insert(entry);
  93. }
  94. }
  95. return result;
  96. }
  97. void Action::Print(llvm::raw_ostream& out) const {
  98. switch (kind()) {
  99. case Action::Kind::LocationAction:
  100. out << cast<LocationAction>(*this).expression() << " ";
  101. break;
  102. case Action::Kind::ExpressionAction:
  103. out << cast<ExpressionAction>(*this).expression() << " ";
  104. break;
  105. case Action::Kind::WitnessAction:
  106. out << *cast<WitnessAction>(*this).witness() << " ";
  107. break;
  108. case Action::Kind::StatementAction:
  109. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  110. out << " ";
  111. break;
  112. case Action::Kind::DeclarationAction:
  113. cast<DeclarationAction>(*this).declaration().Print(out);
  114. out << " ";
  115. break;
  116. case Action::Kind::TypeInstantiationAction:
  117. cast<TypeInstantiationAction>(*this).type()->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