mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
* interfaces, impls, and constrained generics (basics) * separate type checking into declare vs. type check, removing redundancy * external impls * added impl scopes to handle generics calling generics * cleanup * more cleanup * Update executable_semantics/testdata/interface/external_impl_point_vector.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/generic_call_generic.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/tuple_vector_add_scale.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * Update executable_semantics/testdata/interface/vector_point_add_scale.carbon Co-authored-by: josh11b <josh11b@users.noreply.github.com> * change ImplementationDeclaration to ImplDeclaration * remove impl_type_value * split NamedEntity into two * changed GetName to be a free function * adding comments * more edits to respond to review * introduce ImplBinding, remove punning on GenericBinding * new test case and some minor edits * refactor GetMember and GetField to move impl logic to interpreter * remove commennt * change EntityView to ImplBinding in FieldAccess... * move ImplBinding * review response * added example to impl_scope.h * minor edits * Update executable_semantics/interpreter/field_path.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/interpreter/value.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/interpreter/interpreter.cpp Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/expression.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/expression.h Co-authored-by: Geoff Romer <gromer@google.com> * Update executable_semantics/ast/generic_binding.h Co-authored-by: Geoff Romer <gromer@google.com> * more edits from review * review response * Update executable_semantics/ast/static_scope.h Co-authored-by: Geoff Romer <gromer@google.com> * remove ImplType, renamed node_view to value_node Co-authored-by: josh11b <josh11b@users.noreply.github.com> Co-authored-by: Geoff Romer <gromer@google.com>
130 lines
3.9 KiB
C++
130 lines
3.9 KiB
C++
// 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 "executable_semantics/interpreter/action.h"
|
|
|
|
#include <iterator>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "executable_semantics/ast/declaration.h"
|
|
#include "executable_semantics/ast/expression.h"
|
|
#include "executable_semantics/common/arena.h"
|
|
#include "executable_semantics/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) {
|
|
CHECK(!value_node.constant_value().has_value());
|
|
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()))});
|
|
CHECK(success) << "Duplicate definition of " << value_node.base();
|
|
}
|
|
|
|
void RuntimeScope::Merge(RuntimeScope other) {
|
|
CHECK(heap_ == other.heap_);
|
|
locals_.merge(other.locals_);
|
|
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 {
|
|
CHECK(!scopes.empty());
|
|
RuntimeScope result(scopes.front()->heap_);
|
|
for (Nonnull<const RuntimeScope*> scope : scopes) {
|
|
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);
|
|
break;
|
|
case Action::Kind::DeclarationAction:
|
|
cast<DeclarationAction>(*this).declaration().Print(out);
|
|
break;
|
|
case Action::Kind::ScopeAction:
|
|
out << "ScopeAction";
|
|
}
|
|
out << "<" << pos_ << ">";
|
|
if (results_.size() > 0) {
|
|
out << "(";
|
|
llvm::ListSeparator sep;
|
|
for (auto& result : results_) {
|
|
out << sep << *result;
|
|
}
|
|
out << ")";
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon
|