action.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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::LValue);
  41. auto allocation_id = heap_->GetAllocationId(value);
  42. if (!allocation_id) {
  43. auto id = heap_->AllocateValue(value);
  44. auto [it, success] =
  45. locals_.insert({value_node, heap_->arena().New<LValue>(Address(id))});
  46. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  47. } else {
  48. auto [it, success] = locals_.insert(
  49. {value_node, heap_->arena().New<LValue>(Address(*allocation_id))});
  50. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  51. }
  52. }
  53. void RuntimeScope::Initialize(ValueNodeView value_node,
  54. Nonnull<const Value*> value) {
  55. CARBON_CHECK(!value_node.constant_value().has_value());
  56. CARBON_CHECK(value->kind() != Value::Kind::LValue);
  57. allocations_.push_back(heap_->AllocateValue(value));
  58. auto [it, success] = locals_.insert(
  59. {value_node, heap_->arena().New<LValue>(Address(allocations_.back()))});
  60. CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
  61. }
  62. void RuntimeScope::Merge(RuntimeScope other) {
  63. CARBON_CHECK(heap_ == other.heap_);
  64. for (auto& element : other.locals_) {
  65. CARBON_CHECK(locals_.count(element.first) == 0)
  66. << "Duplicate definition of" << element.first;
  67. locals_.insert(element);
  68. }
  69. allocations_.insert(allocations_.end(), other.allocations_.begin(),
  70. other.allocations_.end());
  71. other.allocations_.clear();
  72. }
  73. auto RuntimeScope::Get(ValueNodeView value_node) const
  74. -> std::optional<Nonnull<const LValue*>> {
  75. auto it = locals_.find(value_node);
  76. if (it != locals_.end()) {
  77. return it->second;
  78. } else {
  79. return std::nullopt;
  80. }
  81. }
  82. auto RuntimeScope::Capture(
  83. const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
  84. CARBON_CHECK(!scopes.empty());
  85. RuntimeScope result(scopes.front()->heap_);
  86. for (Nonnull<const RuntimeScope*> scope : scopes) {
  87. CARBON_CHECK(scope->heap_ == result.heap_);
  88. for (const auto& entry : scope->locals_) {
  89. // Intentionally disregards duplicates later in the vector.
  90. result.locals_.insert(entry);
  91. }
  92. }
  93. return result;
  94. }
  95. void Action::Print(llvm::raw_ostream& out) const {
  96. switch (kind()) {
  97. case Action::Kind::LValAction:
  98. out << cast<LValAction>(*this).expression() << " ";
  99. break;
  100. case Action::Kind::ExpressionAction:
  101. out << cast<ExpressionAction>(*this).expression() << " ";
  102. break;
  103. case Action::Kind::WitnessAction:
  104. out << *cast<WitnessAction>(*this).witness() << " ";
  105. break;
  106. case Action::Kind::StatementAction:
  107. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  108. out << " ";
  109. break;
  110. case Action::Kind::DeclarationAction:
  111. cast<DeclarationAction>(*this).declaration().Print(out);
  112. out << " ";
  113. break;
  114. case Action::Kind::ScopeAction:
  115. break;
  116. case Action::Kind::RecursiveAction:
  117. out << "recursive";
  118. break;
  119. case Action::Kind::CleanUpAction:
  120. out << "clean up";
  121. break;
  122. case Action::Kind::DestroyAction:
  123. out << "destroy";
  124. break;
  125. }
  126. out << "." << pos_ << ".";
  127. if (!results_.empty()) {
  128. out << " [[";
  129. llvm::ListSeparator sep;
  130. for (const auto& result : results_) {
  131. out << sep << *result;
  132. }
  133. out << "]]";
  134. }
  135. if (this->scope().has_value()) {
  136. out << " " << *this->scope();
  137. }
  138. }
  139. } // namespace Carbon