action.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "executable_semantics/interpreter/action.h"
  5. #include <iterator>
  6. #include <map>
  7. #include <optional>
  8. #include <utility>
  9. #include <vector>
  10. #include "executable_semantics/ast/declaration.h"
  11. #include "executable_semantics/ast/expression.h"
  12. #include "executable_semantics/common/arena.h"
  13. #include "executable_semantics/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. RuntimeScope::~RuntimeScope() {
  31. for (AllocationId allocation : allocations_) {
  32. heap_->Deallocate(allocation);
  33. }
  34. }
  35. void RuntimeScope::Print(llvm::raw_ostream& out) const {
  36. out << "{";
  37. llvm::ListSeparator sep;
  38. for (const auto& [value_node, value] : locals_) {
  39. out << sep << value_node.base() << ": " << *value;
  40. }
  41. out << "}";
  42. }
  43. void RuntimeScope::Initialize(ValueNodeView value_node,
  44. Nonnull<const Value*> value) {
  45. CHECK(!value_node.constant_value().has_value());
  46. CHECK(value->kind() != Value::Kind::LValue);
  47. allocations_.push_back(heap_->AllocateValue(value));
  48. auto [it, success] = locals_.insert(
  49. {value_node, heap_->arena().New<LValue>(Address(allocations_.back()))});
  50. CHECK(success) << "Duplicate definition of " << value_node.base();
  51. }
  52. void RuntimeScope::Merge(RuntimeScope other) {
  53. CHECK(heap_ == other.heap_);
  54. locals_.merge(other.locals_);
  55. CHECK(other.locals_.empty())
  56. << "Duplicate definition of " << other.locals_.size()
  57. << " names, including " << other.locals_.begin()->first.base();
  58. allocations_.insert(allocations_.end(), other.allocations_.begin(),
  59. other.allocations_.end());
  60. other.allocations_.clear();
  61. }
  62. auto RuntimeScope::Get(ValueNodeView value_node) const
  63. -> std::optional<Nonnull<const LValue*>> {
  64. auto it = locals_.find(value_node);
  65. if (it != locals_.end()) {
  66. return it->second;
  67. } else {
  68. return std::nullopt;
  69. }
  70. }
  71. auto RuntimeScope::Capture(
  72. const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
  73. CHECK(!scopes.empty());
  74. RuntimeScope result(scopes.front()->heap_);
  75. for (Nonnull<const RuntimeScope*> scope : scopes) {
  76. CHECK(scope->heap_ == result.heap_);
  77. for (const auto& entry : scope->locals_) {
  78. // Intentionally disregards duplicates later in the vector.
  79. result.locals_.insert(entry);
  80. }
  81. }
  82. return result;
  83. }
  84. void Action::Print(llvm::raw_ostream& out) const {
  85. switch (kind()) {
  86. case Action::Kind::LValAction:
  87. out << cast<LValAction>(*this).expression();
  88. break;
  89. case Action::Kind::ExpressionAction:
  90. out << cast<ExpressionAction>(*this).expression();
  91. break;
  92. case Action::Kind::PatternAction:
  93. out << cast<PatternAction>(*this).pattern();
  94. break;
  95. case Action::Kind::StatementAction:
  96. cast<StatementAction>(*this).statement().PrintDepth(1, out);
  97. break;
  98. case Action::Kind::DeclarationAction:
  99. cast<DeclarationAction>(*this).declaration().Print(out);
  100. break;
  101. case Action::Kind::ScopeAction:
  102. out << "ScopeAction";
  103. }
  104. out << "<" << pos_ << ">";
  105. if (results_.size() > 0) {
  106. out << "(";
  107. llvm::ListSeparator sep;
  108. for (auto& result : results_) {
  109. out << sep << *result;
  110. }
  111. out << ")";
  112. }
  113. }
  114. } // namespace Carbon