forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.cpp
More file actions
137 lines (122 loc) · 4.11 KB
/
Copy pathaction.cpp
File metadata and controls
137 lines (122 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/interpreter/action.h"
#include <iterator>
#include <map>
#include <optional>
#include <utility>
#include <vector>
#include "explorer/ast/declaration.h"
#include "explorer/ast/expression.h"
#include "explorer/common/arena.h"
#include "explorer/interpreter/stack.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"
namespace Carbon {
using llvm::cast;
RuntimeScope::RuntimeScope(RuntimeScope&& other) noexcept
: locals_(std::move(other.locals_)),
// To transfer ownership of other.allocations_, we have to empty it out.
allocations_(std::exchange(other.allocations_, {})),
heap_(other.heap_) {}
auto RuntimeScope::operator=(RuntimeScope&& rhs) noexcept -> RuntimeScope& {
locals_ = std::move(rhs.locals_);
// To transfer ownership of rhs.allocations_, we have to empty it out.
allocations_ = std::exchange(rhs.allocations_, {});
heap_ = rhs.heap_;
return *this;
}
RuntimeScope::~RuntimeScope() {
for (AllocationId allocation : allocations_) {
heap_->Deallocate(allocation);
}
}
void RuntimeScope::Print(llvm::raw_ostream& out) const {
out << "{";
llvm::ListSeparator sep;
for (const auto& [value_node, value] : locals_) {
out << sep << value_node.base() << ": " << *value;
}
out << "}";
}
void RuntimeScope::Initialize(ValueNodeView value_node,
Nonnull<const Value*> value) {
CARBON_CHECK(!value_node.constant_value().has_value());
CARBON_CHECK(value->kind() != Value::Kind::LValue);
allocations_.push_back(heap_->AllocateValue(value));
auto [it, success] = locals_.insert(
{value_node, heap_->arena().New<LValue>(Address(allocations_.back()))});
CARBON_CHECK(success) << "Duplicate definition of " << value_node.base();
}
void RuntimeScope::Merge(RuntimeScope other) {
CARBON_CHECK(heap_ == other.heap_);
locals_.merge(other.locals_);
CARBON_CHECK(other.locals_.empty())
<< "Duplicate definition of " << other.locals_.size()
<< " names, including " << other.locals_.begin()->first.base();
allocations_.insert(allocations_.end(), other.allocations_.begin(),
other.allocations_.end());
other.allocations_.clear();
}
auto RuntimeScope::Get(ValueNodeView value_node) const
-> std::optional<Nonnull<const LValue*>> {
auto it = locals_.find(value_node);
if (it != locals_.end()) {
return it->second;
} else {
return std::nullopt;
}
}
auto RuntimeScope::Capture(
const std::vector<Nonnull<const RuntimeScope*>>& scopes) -> RuntimeScope {
CARBON_CHECK(!scopes.empty());
RuntimeScope result(scopes.front()->heap_);
for (Nonnull<const RuntimeScope*> scope : scopes) {
CARBON_CHECK(scope->heap_ == result.heap_);
for (const auto& entry : scope->locals_) {
// Intentionally disregards duplicates later in the vector.
result.locals_.insert(entry);
}
}
return result;
}
void Action::Print(llvm::raw_ostream& out) const {
switch (kind()) {
case Action::Kind::LValAction:
out << cast<LValAction>(*this).expression() << " ";
break;
case Action::Kind::ExpressionAction:
out << cast<ExpressionAction>(*this).expression() << " ";
break;
case Action::Kind::PatternAction:
out << cast<PatternAction>(*this).pattern() << " ";
break;
case Action::Kind::StatementAction:
cast<StatementAction>(*this).statement().PrintDepth(1, out);
out << " ";
break;
case Action::Kind::DeclarationAction:
cast<DeclarationAction>(*this).declaration().Print(out);
out << " ";
break;
case Action::Kind::ScopeAction:
break;
case Action::Kind::RecursiveAction:
out << "recursive";
break;
}
out << "." << pos_ << ".";
if (!results_.empty()) {
out << " [[";
llvm::ListSeparator sep;
for (auto& result : results_) {
out << sep << *result;
}
out << "]]";
}
if (this->scope().has_value()) {
out << " " << *this->scope();
}
}
} // namespace Carbon