mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 17:52:58 +01:00
interfaces, impls, and constrained generics (basics) (#1073)
* 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>
This commit is contained in:
committed by
GitHub
co-authored by
josh11b
Geoff Romer
parent
8f6e42d866
commit
7cce1bd124
@@ -85,6 +85,8 @@ cc_library(
|
||||
hdrs = ["field_path.h"],
|
||||
deps = [
|
||||
"//common:ostream",
|
||||
"//executable_semantics/ast",
|
||||
"//executable_semantics/ast:static_scope",
|
||||
"@llvm-project//llvm:Support",
|
||||
],
|
||||
)
|
||||
@@ -183,8 +185,14 @@ cc_library(
|
||||
|
||||
cc_library(
|
||||
name = "type_checker",
|
||||
srcs = ["type_checker.cpp"],
|
||||
hdrs = ["type_checker.h"],
|
||||
srcs = [
|
||||
"impl_scope.cpp",
|
||||
"type_checker.cpp",
|
||||
],
|
||||
hdrs = [
|
||||
"impl_scope.h",
|
||||
"type_checker.h",
|
||||
],
|
||||
deps = [
|
||||
":action_and_value",
|
||||
":dictionary",
|
||||
|
||||
@@ -44,20 +44,20 @@ RuntimeScope::~RuntimeScope() {
|
||||
void RuntimeScope::Print(llvm::raw_ostream& out) const {
|
||||
out << "{";
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& [named_entity, value] : locals_) {
|
||||
out << sep << named_entity.name() << ": " << *value;
|
||||
for (const auto& [value_node, value] : locals_) {
|
||||
out << sep << value_node.base() << ": " << *value;
|
||||
}
|
||||
out << "}";
|
||||
}
|
||||
|
||||
void RuntimeScope::Initialize(NamedEntityView named_entity,
|
||||
void RuntimeScope::Initialize(ValueNodeView value_node,
|
||||
Nonnull<const Value*> value) {
|
||||
CHECK(!named_entity.constant_value().has_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(
|
||||
{named_entity, heap_->arena().New<LValue>(Address(allocations_.back()))});
|
||||
CHECK(success) << "Duplicate definition of " << named_entity.name();
|
||||
{value_node, heap_->arena().New<LValue>(Address(allocations_.back()))});
|
||||
CHECK(success) << "Duplicate definition of " << value_node.base();
|
||||
}
|
||||
|
||||
void RuntimeScope::Merge(RuntimeScope other) {
|
||||
@@ -65,15 +65,15 @@ void RuntimeScope::Merge(RuntimeScope other) {
|
||||
locals_.merge(other.locals_);
|
||||
CHECK(other.locals_.empty())
|
||||
<< "Duplicate definition of " << other.locals_.size()
|
||||
<< " names, including " << other.locals_.begin()->first.name();
|
||||
<< " names, including " << other.locals_.begin()->first.base();
|
||||
allocations_.insert(allocations_.end(), other.allocations_.begin(),
|
||||
other.allocations_.end());
|
||||
other.allocations_.clear();
|
||||
}
|
||||
|
||||
auto RuntimeScope::Get(NamedEntityView named_entity) const
|
||||
auto RuntimeScope::Get(ValueNodeView value_node) const
|
||||
-> std::optional<Nonnull<const LValue*>> {
|
||||
auto it = locals_.find(named_entity);
|
||||
auto it = locals_.find(value_node);
|
||||
if (it != locals_.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
|
||||
@@ -45,21 +45,21 @@ class RuntimeScope {
|
||||
void Print(llvm::raw_ostream& out) const;
|
||||
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
|
||||
|
||||
// Allocates storage for `named_entity` in `heap`, and initializes it with
|
||||
// Allocates storage for `value_node` in `heap`, and initializes it with
|
||||
// `value`.
|
||||
void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
|
||||
void Initialize(ValueNodeView value_node, Nonnull<const Value*> value);
|
||||
|
||||
// Transfers the names and allocations from `other` into *this. The two
|
||||
// scopes must not define the same name, and must be backed by the same Heap.
|
||||
void Merge(RuntimeScope other);
|
||||
|
||||
// Returns the local storage for named_entity, if it has storage local to
|
||||
// Returns the local storage for value_node, if it has storage local to
|
||||
// this scope.
|
||||
auto Get(NamedEntityView named_entity) const
|
||||
auto Get(ValueNodeView value_node) const
|
||||
-> std::optional<Nonnull<const LValue*>>;
|
||||
|
||||
private:
|
||||
std::map<NamedEntityView, Nonnull<const LValue*>> locals_;
|
||||
std::map<ValueNodeView, Nonnull<const LValue*>> locals_;
|
||||
std::vector<AllocationId> allocations_;
|
||||
Nonnull<HeapAllocationInterface*> heap_;
|
||||
};
|
||||
|
||||
@@ -36,47 +36,47 @@ void ActionStack::Start(std::unique_ptr<Action> action) {
|
||||
todo_.Push(std::move(action));
|
||||
}
|
||||
|
||||
void ActionStack::Initialize(NamedEntityView named_entity,
|
||||
void ActionStack::Initialize(ValueNodeView value_node,
|
||||
Nonnull<const Value*> value) {
|
||||
for (const std::unique_ptr<Action>& action : todo_) {
|
||||
if (action->scope().has_value()) {
|
||||
action->scope()->Initialize(named_entity, value);
|
||||
action->scope()->Initialize(value_node, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
globals_->Initialize(named_entity, value);
|
||||
globals_->Initialize(value_node, value);
|
||||
}
|
||||
|
||||
auto ActionStack::ValueOfName(NamedEntityView named_entity,
|
||||
auto ActionStack::ValueOfNode(ValueNodeView value_node,
|
||||
SourceLocation source_loc) const
|
||||
-> Nonnull<const Value*> {
|
||||
if (std::optional<Nonnull<const Value*>> constant_value =
|
||||
named_entity.constant_value();
|
||||
value_node.constant_value();
|
||||
constant_value.has_value()) {
|
||||
return *constant_value;
|
||||
}
|
||||
for (const std::unique_ptr<Action>& action : todo_) {
|
||||
// TODO: have static name resolution identify the scope of named_entity
|
||||
// TODO: have static name resolution identify the scope of value_node
|
||||
// as an AstNode, and then perform lookup _only_ on the Action associated
|
||||
// with that node. This will help keep unwanted dynamic-scoping behavior
|
||||
// from sneaking in.
|
||||
if (action->scope().has_value()) {
|
||||
std::optional<Nonnull<const Value*>> result =
|
||||
action->scope()->Get(named_entity);
|
||||
action->scope()->Get(value_node);
|
||||
if (result.has_value()) {
|
||||
return *result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (globals_.has_value()) {
|
||||
std::optional<Nonnull<const Value*>> result = globals_->Get(named_entity);
|
||||
std::optional<Nonnull<const Value*>> result = globals_->Get(value_node);
|
||||
if (result.has_value()) {
|
||||
return *result;
|
||||
}
|
||||
}
|
||||
// TODO: Move these errors to compile time and explain them more clearly.
|
||||
// TODO: Move these errors to name resolution and explain them more clearly.
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "could not find `" << named_entity.name() << "`";
|
||||
<< "could not find `" << value_node.base() << "`";
|
||||
}
|
||||
|
||||
void ActionStack::MergeScope(RuntimeScope scope) {
|
||||
|
||||
@@ -43,13 +43,13 @@ class ActionStack {
|
||||
// ScopeAction.
|
||||
auto CurrentAction() -> Action& { return *todo_.Top(); }
|
||||
|
||||
// Allocates storage for `named_entity`, and initializes it to `value`.
|
||||
void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
|
||||
// Allocates storage for `value_node`, and initializes it to `value`.
|
||||
void Initialize(ValueNodeView value_node, Nonnull<const Value*> value);
|
||||
|
||||
// Returns the value bound to `named_entity`. If `named_entity` is a local
|
||||
// Returns the value bound to `value_node`. If `value_node` is a local
|
||||
// variable, this will be an LValue.
|
||||
auto ValueOfName(NamedEntityView named_entity,
|
||||
SourceLocation source_loc) const -> Nonnull<const Value*>;
|
||||
auto ValueOfNode(ValueNodeView value_node, SourceLocation source_loc) const
|
||||
-> Nonnull<const Value*>;
|
||||
|
||||
// Merges `scope` into the innermost scope currently on the stack.
|
||||
void MergeScope(RuntimeScope scope);
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
#include <vector>
|
||||
|
||||
#include "common/ostream.h"
|
||||
#include "executable_semantics/ast/static_scope.h"
|
||||
#include "llvm/Support/Compiler.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class Witness;
|
||||
|
||||
// Given some initial Value, a FieldPath identifies a sub-Value within it,
|
||||
// in much the same way that a file path identifies a file within some
|
||||
// directory. FieldPaths are relative rather than absolute: the initial
|
||||
@@ -29,8 +32,33 @@ class FieldPath {
|
||||
// Constructs an empty FieldPath.
|
||||
FieldPath() = default;
|
||||
|
||||
// A single component of the FieldPath, which is typically the name
|
||||
// of a field. However, inside a generic, when there is a field
|
||||
// access on something of a generic type, e.g., `T`, then we also
|
||||
// need `witness`, a pointer to the witness table containing that field.
|
||||
class Component {
|
||||
public:
|
||||
explicit Component(std::string name) : name_(std::move(name)) {}
|
||||
Component(std::string name, std::optional<Nonnull<const Witness*>> witness)
|
||||
: name_(std::move(name)), witness_(witness) {}
|
||||
|
||||
auto name() const -> const std::string& { return name_; }
|
||||
|
||||
auto witness() const -> std::optional<Nonnull<const Witness*>> {
|
||||
return witness_;
|
||||
}
|
||||
|
||||
void Print(llvm::raw_ostream& out) const { out << name_; }
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::optional<Nonnull<const Witness*>> witness_;
|
||||
};
|
||||
|
||||
// Constructs a FieldPath consisting of a single step.
|
||||
explicit FieldPath(std::string name) : components_({std::move(name)}) {}
|
||||
explicit FieldPath(std::string name)
|
||||
: components_({Component(std::move(name))}) {}
|
||||
explicit FieldPath(const Component& f) : components_({f}) {}
|
||||
|
||||
FieldPath(const FieldPath&) = default;
|
||||
FieldPath(FieldPath&&) = default;
|
||||
@@ -42,11 +70,11 @@ class FieldPath {
|
||||
|
||||
// Appends `name` to the end of *this.
|
||||
auto Append(std::string name) -> void {
|
||||
components_.push_back(std::move(name));
|
||||
components_.push_back(Component(std::move(name)));
|
||||
}
|
||||
|
||||
void Print(llvm::raw_ostream& out) const {
|
||||
for (const std::string& component : components_) {
|
||||
for (const Component& component : components_) {
|
||||
out << "." << component;
|
||||
}
|
||||
}
|
||||
@@ -58,7 +86,7 @@ class FieldPath {
|
||||
// another Value, so its implementation details are tied to the implementation
|
||||
// details of Value.
|
||||
friend class Value;
|
||||
std::vector<std::string> components_;
|
||||
std::vector<Component> components_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -20,7 +20,7 @@ auto Heap::AllocateValue(Nonnull<const Value*> v) -> AllocationId {
|
||||
return a;
|
||||
}
|
||||
|
||||
auto Heap::Read(const Address& a, SourceLocation source_loc)
|
||||
auto Heap::Read(const Address& a, SourceLocation source_loc) const
|
||||
-> Nonnull<const Value*> {
|
||||
this->CheckAlive(a.allocation_, source_loc);
|
||||
return values_[a.allocation_.index_]->GetField(arena_, a.field_path_,
|
||||
@@ -34,7 +34,8 @@ void Heap::Write(const Address& a, Nonnull<const Value*> v,
|
||||
arena_, a.field_path_, v, source_loc);
|
||||
}
|
||||
|
||||
void Heap::CheckAlive(AllocationId allocation, SourceLocation source_loc) {
|
||||
void Heap::CheckAlive(AllocationId allocation,
|
||||
SourceLocation source_loc) const {
|
||||
if (!alive_[allocation.index_]) {
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "undefined behavior: access to dead value "
|
||||
|
||||
@@ -27,7 +27,7 @@ class Heap : public HeapAllocationInterface {
|
||||
|
||||
// Returns the value at the given address in the heap after
|
||||
// checking that it is alive.
|
||||
auto Read(const Address& a, SourceLocation source_loc)
|
||||
auto Read(const Address& a, SourceLocation source_loc) const
|
||||
-> Nonnull<const Value*>;
|
||||
|
||||
// Writes the given value at the address in the heap after
|
||||
@@ -50,7 +50,7 @@ class Heap : public HeapAllocationInterface {
|
||||
|
||||
private:
|
||||
// Signal an error if the allocation is no longer alive.
|
||||
void CheckAlive(AllocationId allocation, SourceLocation source_loc);
|
||||
void CheckAlive(AllocationId allocation, SourceLocation source_loc) const;
|
||||
|
||||
Nonnull<Arena*> arena_;
|
||||
std::vector<Nonnull<const Value*>> values_;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// 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/impl_scope.h"
|
||||
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/interpreter/value.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
|
||||
using llvm::cast;
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
void ImplScope::Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
|
||||
ValueNodeView impl) {
|
||||
impls_.push_back({.interface = iface, .type = type, .impl = impl});
|
||||
}
|
||||
|
||||
void ImplScope::AddParent(Nonnull<const ImplScope*> parent) {
|
||||
parent_scopes_.push_back(parent);
|
||||
}
|
||||
|
||||
auto ImplScope::Resolve(Nonnull<const Value*> iface_type,
|
||||
Nonnull<const Value*> type,
|
||||
SourceLocation source_loc) const -> ValueNodeView {
|
||||
std::optional<ValueNodeView> result =
|
||||
TryResolve(iface_type, type, source_loc);
|
||||
if (!result.has_value()) {
|
||||
FATAL_COMPILATION_ERROR(source_loc) << "could not find implementation of "
|
||||
<< *iface_type << " for " << *type;
|
||||
}
|
||||
return *result;
|
||||
}
|
||||
|
||||
auto ImplScope::TryResolve(Nonnull<const Value*> iface_type,
|
||||
Nonnull<const Value*> type,
|
||||
SourceLocation source_loc) const
|
||||
-> std::optional<ValueNodeView> {
|
||||
std::optional<ValueNodeView> result =
|
||||
ResolveHere(iface_type, type, source_loc);
|
||||
if (result.has_value()) {
|
||||
return result;
|
||||
}
|
||||
for (Nonnull<const ImplScope*> parent : parent_scopes_) {
|
||||
auto parent_result = parent->TryResolve(iface_type, type, source_loc);
|
||||
if (parent_result.has_value() && result.has_value() &&
|
||||
*parent_result != *result) {
|
||||
FATAL_COMPILATION_ERROR(source_loc)
|
||||
<< "ambiguous implementations of " << *iface_type << " for " << *type;
|
||||
}
|
||||
result = parent_result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
auto ImplScope::ResolveHere(Nonnull<const Value*> iface_type,
|
||||
Nonnull<const Value*> impl_type,
|
||||
SourceLocation source_loc) const
|
||||
-> std::optional<ValueNodeView> {
|
||||
switch (iface_type->kind()) {
|
||||
case Value::Kind::InterfaceType: {
|
||||
const auto& iface = cast<InterfaceType>(*iface_type);
|
||||
for (const Impl& impl : impls_) {
|
||||
if (TypeEqual(&iface, impl.interface) &&
|
||||
TypeEqual(impl_type, impl.type)) {
|
||||
return impl.impl;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
default:
|
||||
FATAL() << "expected an interface, not " << *iface_type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
@@ -0,0 +1,83 @@
|
||||
// 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
|
||||
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_IMPL_SCOPE_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_IMPL_SCOPE_H_
|
||||
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
class Value;
|
||||
|
||||
// The `ImplScope` class is responsible for mapping a type and
|
||||
// interface to the location of the witness table for the `impl` for
|
||||
// that type and interface. A scope may have parent scopes, whose
|
||||
// impls will also be visible in the child scope.
|
||||
//
|
||||
// There is typically one instance of `ImplScope` class per scope
|
||||
// because the impls that are visible for a given type and interface
|
||||
// can vary from scope to scope. For example, consider the `bar` and
|
||||
// `baz` methods in the following class C and nested class D.
|
||||
//
|
||||
// class C(U:! Type, T:! Type) {
|
||||
// class D(V:! Type where U is Fooable(T)) {
|
||||
// fn bar[me: Self](x: U, y : T) -> T{
|
||||
// return x.foo(y)
|
||||
// }
|
||||
// }
|
||||
// fn baz[me: Self](x: U, y : T) -> T {
|
||||
// return x.foo(y);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// The call to `x.foo` in `bar` is valid because the `U is Fooable(T)`
|
||||
// impl is visible in the body of `bar`. In contrast, the call to
|
||||
// `x.foo` in `baz` is not valid because there is no visible impl for
|
||||
// `U` and `Fooable` in that scope.
|
||||
class ImplScope {
|
||||
public:
|
||||
// Associates `iface` and `type` with the `impl` in this scope.
|
||||
void Add(Nonnull<const Value*> iface, Nonnull<const Value*> type,
|
||||
ValueNodeView impl);
|
||||
|
||||
// Make `parent` a parent of this scope.
|
||||
// REQUIRES: `parent` is not already a parent of this scope.
|
||||
void AddParent(Nonnull<const ImplScope*> parent);
|
||||
|
||||
// Returns the associated impl for the given `iface` and `type` in
|
||||
// the ancestor graph of this scope, or reports a compilation error
|
||||
// at `source_loc` there isn't exactly one matching impl.
|
||||
auto Resolve(Nonnull<const Value*> iface, Nonnull<const Value*> type,
|
||||
SourceLocation source_loc) const -> ValueNodeView;
|
||||
|
||||
private:
|
||||
auto TryResolve(Nonnull<const Value*> iface_type, Nonnull<const Value*> type,
|
||||
SourceLocation source_loc) const
|
||||
-> std::optional<ValueNodeView>;
|
||||
auto ResolveHere(Nonnull<const Value*> iface_type,
|
||||
Nonnull<const Value*> impl_type,
|
||||
SourceLocation source_loc) const
|
||||
-> std::optional<ValueNodeView>;
|
||||
|
||||
// The `Impl` struct is a key-value pair where the key is the
|
||||
// combination of a type and an interface, e.g., `List` and `Container`,
|
||||
// and the value is the result of statically resolving to the `impl`
|
||||
// for `List` as `Container`, which is an `ValueNodeView`. The generality
|
||||
// of `ValueNodeView` is needed (not just `ImplDeclaration`) because
|
||||
// inside a generic, we need to map, e.g., from `T` and `Container` to the
|
||||
// witness table that is passed into the generic.
|
||||
struct Impl {
|
||||
Nonnull<const Value*> interface;
|
||||
Nonnull<const Value*> type;
|
||||
ValueNodeView impl;
|
||||
};
|
||||
|
||||
std::vector<Impl> impls_;
|
||||
std::vector<Nonnull<const ImplScope*>> parent_scopes_;
|
||||
};
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
#endif // EXECUTABLE_SEMANTICS_AST_IMPL_SCOPE_H_
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "executable_semantics/interpreter/action_stack.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
@@ -183,8 +184,8 @@ auto PatternMatch(Nonnull<const Value*> p, Nonnull<const Value*> v,
|
||||
<< "Name bindings are not supported in this context";
|
||||
}
|
||||
const auto& placeholder = cast<BindingPlaceholderValue>(*p);
|
||||
if (placeholder.named_entity().has_value()) {
|
||||
(*bindings)->Initialize(*placeholder.named_entity(), v);
|
||||
if (placeholder.value_node().has_value()) {
|
||||
(*bindings)->Initialize(*placeholder.value_node(), v);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -275,8 +276,8 @@ void Interpreter::StepLvalue() {
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
// { {x :: C, E, F} :: S, H}
|
||||
// -> { {E(x) :: C, E, F} :: S, H}
|
||||
Nonnull<const Value*> value = todo_.ValueOfName(
|
||||
cast<IdentifierExpression>(exp).named_entity(), exp.source_loc());
|
||||
Nonnull<const Value*> value = todo_.ValueOfNode(
|
||||
cast<IdentifierExpression>(exp).value_node(), exp.source_loc());
|
||||
CHECK(isa<LValue>(value)) << *value;
|
||||
return todo_.FinishAction(value);
|
||||
}
|
||||
@@ -371,6 +372,8 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
|
||||
case Value::Kind::AutoType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::NominalClassType:
|
||||
case Value::Kind::InterfaceType:
|
||||
case Value::Kind::Witness:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::VariableType:
|
||||
@@ -380,6 +383,7 @@ auto Interpreter::Convert(Nonnull<const Value*> value,
|
||||
case Value::Kind::StringType:
|
||||
case Value::Kind::StringValue:
|
||||
case Value::Kind::TypeOfClassType:
|
||||
case Value::Kind::TypeOfInterfaceType:
|
||||
case Value::Kind::TypeOfChoiceType:
|
||||
// TODO: add `CHECK(TypeEqual(type, value->dynamic_type()))`, once we
|
||||
// have Value::dynamic_type.
|
||||
@@ -497,8 +501,18 @@ void Interpreter::StepExp() {
|
||||
} else {
|
||||
// { { v :: [].f :: C, E, F} :: S, H}
|
||||
// -> { { v_f :: C, E, F} : S, H}
|
||||
return todo_.FinishAction(act.results()[0]->GetField(
|
||||
arena_, FieldPath(access.field()), exp.source_loc()));
|
||||
std::optional<Nonnull<const Witness*>> witness = std::nullopt;
|
||||
if (access.impl().has_value()) {
|
||||
auto witness_addr =
|
||||
todo_.ValueOfNode(*access.impl(), access.source_loc());
|
||||
witness = cast<Witness>(
|
||||
heap_.Read(llvm::cast<LValue>(witness_addr)->address(),
|
||||
access.source_loc()));
|
||||
}
|
||||
FieldPath::Component field(access.field(), witness);
|
||||
Nonnull<const Value*> member = act.results()[0]->GetField(
|
||||
arena_, FieldPath(field), exp.source_loc());
|
||||
return todo_.FinishAction(member);
|
||||
}
|
||||
}
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
@@ -506,7 +520,7 @@ void Interpreter::StepExp() {
|
||||
const auto& ident = cast<IdentifierExpression>(exp);
|
||||
// { {x :: C, E, F} :: S, H} -> { {H(E(x)) :: C, E, F} :: S, H}
|
||||
Nonnull<const Value*> value =
|
||||
todo_.ValueOfName(ident.named_entity(), ident.source_loc());
|
||||
todo_.ValueOfNode(ident.value_node(), ident.source_loc());
|
||||
if (const auto* lvalue = dyn_cast<LValue>(value)) {
|
||||
value = heap_.Read(lvalue->address(), exp.source_loc());
|
||||
}
|
||||
@@ -567,6 +581,17 @@ void Interpreter::StepExp() {
|
||||
Nonnull<const Value*> converted_args = Convert(
|
||||
act.results()[1], &function.param_pattern().static_type());
|
||||
RuntimeScope function_scope(&heap_);
|
||||
// Bring the impl witness tables into scope.
|
||||
for (const auto& [impl_bind, impl_node] :
|
||||
cast<CallExpression>(exp).impls()) {
|
||||
Nonnull<const Value*> witness =
|
||||
todo_.ValueOfNode(impl_node, exp.source_loc());
|
||||
if (witness->kind() == Value::Kind::LValue) {
|
||||
const LValue& lval = cast<LValue>(*witness);
|
||||
witness = heap_.Read(lval.address(), exp.source_loc());
|
||||
}
|
||||
function_scope.Initialize(impl_bind, witness);
|
||||
}
|
||||
CHECK(PatternMatch(&function.param_pattern().value(),
|
||||
converted_args, exp.source_loc(),
|
||||
&function_scope));
|
||||
@@ -649,7 +674,7 @@ void Interpreter::StepExp() {
|
||||
// -> { fn pt -> rt :: {C, E, F} :: S, H}
|
||||
return todo_.FinishAction(arena_->New<FunctionType>(
|
||||
std::vector<Nonnull<const GenericBinding*>>(), act.results()[0],
|
||||
act.results()[1]));
|
||||
act.results()[1], std::vector<Nonnull<const ImplBinding*>>()));
|
||||
}
|
||||
}
|
||||
case ExpressionKind::ContinuationTypeLiteral: {
|
||||
@@ -963,6 +988,8 @@ void Interpreter::StepDeclaration() {
|
||||
case DeclarationKind::FunctionDeclaration:
|
||||
case DeclarationKind::ClassDeclaration:
|
||||
case DeclarationKind::ChoiceDeclaration:
|
||||
case DeclarationKind::InterfaceDeclaration:
|
||||
case DeclarationKind::ImplDeclaration:
|
||||
// These declarations have no run-time effects.
|
||||
return todo_.FinishAction();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "executable_semantics/interpreter/action_stack.h"
|
||||
#include "executable_semantics/interpreter/heap.h"
|
||||
#include "executable_semantics/interpreter/value.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
|
||||
@@ -128,7 +128,22 @@ void ResolveControlFlow(Nonnull<Declaration*> declaration) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
case DeclarationKind::InterfaceDeclaration: {
|
||||
auto& iface_decl = cast<InterfaceDeclaration>(*declaration);
|
||||
for (Nonnull<Declaration*> member : iface_decl.members()) {
|
||||
ResolveControlFlow(member);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ImplDeclaration: {
|
||||
auto& impl_decl = cast<ImplDeclaration>(*declaration);
|
||||
for (Nonnull<Declaration*> member : impl_decl.members()) {
|
||||
ResolveControlFlow(member);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ChoiceDeclaration:
|
||||
case DeclarationKind::VariableDeclaration:
|
||||
// do nothing
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,15 @@ static void AddExposedNames(const Declaration& declaration,
|
||||
static void AddExposedNames(const Declaration& declaration,
|
||||
StaticScope& enclosing_scope) {
|
||||
switch (declaration.kind()) {
|
||||
case DeclarationKind::InterfaceDeclaration: {
|
||||
auto& iface_decl = cast<InterfaceDeclaration>(declaration);
|
||||
enclosing_scope.Add(iface_decl.name(), &iface_decl);
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ImplDeclaration: {
|
||||
// Nothing to do here
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::FunctionDeclaration: {
|
||||
auto& func = cast<FunctionDeclaration>(declaration);
|
||||
enclosing_scope.Add(func.name(), &func);
|
||||
@@ -115,7 +124,7 @@ static void ResolveNames(Expression& expression,
|
||||
break;
|
||||
case ExpressionKind::IdentifierExpression: {
|
||||
auto& identifier = cast<IdentifierExpression>(expression);
|
||||
identifier.set_named_entity(
|
||||
identifier.set_value_node(
|
||||
enclosing_scope.Resolve(identifier.name(), identifier.source_loc()));
|
||||
break;
|
||||
}
|
||||
@@ -244,6 +253,31 @@ static void ResolveNames(Statement& statement, StaticScope& enclosing_scope) {
|
||||
static void ResolveNames(Declaration& declaration,
|
||||
StaticScope& enclosing_scope) {
|
||||
switch (declaration.kind()) {
|
||||
case DeclarationKind::InterfaceDeclaration: {
|
||||
auto& iface = cast<InterfaceDeclaration>(declaration);
|
||||
StaticScope iface_scope;
|
||||
iface_scope.AddParent(&enclosing_scope);
|
||||
iface_scope.Add("Self", iface.self());
|
||||
for (Nonnull<Declaration*> member : iface.members()) {
|
||||
AddExposedNames(*member, iface_scope);
|
||||
}
|
||||
for (Nonnull<Declaration*> member : iface.members()) {
|
||||
ResolveNames(*member, iface_scope);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ImplDeclaration: {
|
||||
auto& impl = cast<ImplDeclaration>(declaration);
|
||||
ResolveNames(impl.interface(), enclosing_scope);
|
||||
ResolveNames(*impl.impl_type(), enclosing_scope);
|
||||
for (Nonnull<Declaration*> member : impl.members()) {
|
||||
AddExposedNames(*member, enclosing_scope);
|
||||
}
|
||||
for (Nonnull<Declaration*> member : impl.members()) {
|
||||
ResolveNames(*member, enclosing_scope);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::FunctionDeclaration: {
|
||||
auto& function = cast<FunctionDeclaration>(declaration);
|
||||
StaticScope function_scope;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -13,6 +13,7 @@
|
||||
#include "executable_semantics/ast/statement.h"
|
||||
#include "executable_semantics/common/nonnull.h"
|
||||
#include "executable_semantics/interpreter/dictionary.h"
|
||||
#include "executable_semantics/interpreter/impl_scope.h"
|
||||
#include "executable_semantics/interpreter/interpreter.h"
|
||||
|
||||
namespace Carbon {
|
||||
@@ -31,17 +32,16 @@ class TypeChecker {
|
||||
// inside the argument type.
|
||||
// The `deduced` parameter is an accumulator, that is, it holds the
|
||||
// results so-far.
|
||||
static void ArgumentDeduction(
|
||||
SourceLocation source_loc,
|
||||
std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>& deduced,
|
||||
Nonnull<const Value*> param, Nonnull<const Value*> arg);
|
||||
static void ArgumentDeduction(SourceLocation source_loc, BindingMap& deduced,
|
||||
Nonnull<const Value*> param,
|
||||
Nonnull<const Value*> arg);
|
||||
|
||||
// Traverses the AST rooted at `e`, populating the static_type() of all nodes
|
||||
// and ensuring they follow Carbon's typing rules.
|
||||
//
|
||||
// `values` maps variable names to their compile-time values. It is not
|
||||
// directly used in this function but is passed to InterExp.
|
||||
void TypeCheckExp(Nonnull<Expression*> e);
|
||||
void TypeCheckExp(Nonnull<Expression*> e, const ImplScope& impl_scope);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at `p`.
|
||||
//
|
||||
@@ -49,31 +49,64 @@ class TypeChecker {
|
||||
// surrounding context gives us that information. Otherwise, it is
|
||||
// nullopt.
|
||||
void TypeCheckPattern(Nonnull<Pattern*> p,
|
||||
std::optional<Nonnull<const Value*>> expected);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at `d`.
|
||||
void TypeCheckDeclaration(Nonnull<Declaration*> d);
|
||||
std::optional<Nonnull<const Value*>> expected,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at `s`.
|
||||
//
|
||||
// REQUIRES: f.return_term().has_static_type() || f.return_term().is_auto(),
|
||||
// where `f` is nearest enclosing FunctionDeclaration of `s`.
|
||||
void TypeCheckStmt(Nonnull<Statement*> s);
|
||||
void TypeCheckStmt(Nonnull<Statement*> s, const ImplScope& impl_scope);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at `f`,
|
||||
// and may not traverse f->body() if `check_body` is false.
|
||||
// Establish the `static_type` and `constant_value` of the
|
||||
// declaration and all of its nested declarations. This involves the
|
||||
// compile-time interpretation of any type expressions in the
|
||||
// declaration. It does not involve type checking statements and
|
||||
// (runtime) expressions, as in the body of a function or a method.
|
||||
// Dispatches to one of the following functions.
|
||||
void DeclareDeclaration(Nonnull<Declaration*> d, ImplScope& enclosing_scope);
|
||||
|
||||
void DeclareFunctionDeclaration(Nonnull<FunctionDeclaration*> f,
|
||||
const ImplScope& enclosing_scope);
|
||||
|
||||
void DeclareClassDeclaration(Nonnull<ClassDeclaration*> class_decl,
|
||||
ImplScope& enclosing_scope);
|
||||
|
||||
void DeclareInterfaceDeclaration(Nonnull<InterfaceDeclaration*> iface_decl,
|
||||
ImplScope& enclosing_scope);
|
||||
|
||||
void DeclareImplDeclaration(Nonnull<ImplDeclaration*> impl_decl,
|
||||
ImplScope& enclosing_scope);
|
||||
|
||||
void DeclareChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice,
|
||||
const ImplScope& enclosing_scope);
|
||||
|
||||
// Checks the statements and (runtime) expressions within the
|
||||
// declaration, such as the body of a function.
|
||||
// Dispatches to one of the following functions.
|
||||
// Assumes that DeclareDeclaration has already been invoked on `d`.
|
||||
void TypeCheckDeclaration(Nonnull<Declaration*> d,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Type check the body of the function.
|
||||
void TypeCheckFunctionDeclaration(Nonnull<FunctionDeclaration*> f,
|
||||
bool check_body);
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at class_decl.
|
||||
void TypeCheckClassDeclaration(Nonnull<ClassDeclaration*> class_decl);
|
||||
// Type check all the members of the class.
|
||||
void TypeCheckClassDeclaration(Nonnull<ClassDeclaration*> class_decl,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Equivalent to TypeCheckExp, but operates on the AST rooted at choice_decl.
|
||||
void TypeCheckChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice);
|
||||
// Type check all the members of the interface.
|
||||
void TypeCheckInterfaceDeclaration(Nonnull<InterfaceDeclaration*> iface_decl,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Establish the type of the declaration without deeply checking
|
||||
// the declaration, such as checking the body of a function.
|
||||
void DeclareDeclaration(Nonnull<Declaration*> d);
|
||||
// Type check all the members of the implementation.
|
||||
void TypeCheckImplDeclaration(Nonnull<ImplDeclaration*> impl_decl,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// This currently does nothing, but perhaps that will change in the future.
|
||||
void TypeCheckChoiceDeclaration(Nonnull<ChoiceDeclaration*> choice,
|
||||
const ImplScope& impl_scope);
|
||||
|
||||
// Verifies that opt_stmt holds a statement, and it is structurally impossible
|
||||
// for control flow to leave that statement except via a `return`.
|
||||
@@ -98,7 +131,7 @@ class TypeChecker {
|
||||
void PrintConstants(llvm::raw_ostream& out);
|
||||
|
||||
Nonnull<Arena*> arena_;
|
||||
std::set<NamedEntityView> constants_;
|
||||
std::set<ValueNodeView> constants_;
|
||||
|
||||
bool trace_;
|
||||
};
|
||||
|
||||
@@ -28,8 +28,28 @@ auto StructValue::FindField(const std::string& name) const
|
||||
}
|
||||
|
||||
static auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
|
||||
const std::string& f, SourceLocation source_loc)
|
||||
-> Nonnull<const Value*> {
|
||||
const FieldPath::Component& field,
|
||||
SourceLocation source_loc) -> Nonnull<const Value*> {
|
||||
const std::string& f = field.name();
|
||||
|
||||
if (field.witness().has_value()) {
|
||||
Nonnull<const Witness*> witness = *field.witness();
|
||||
switch (witness->kind()) {
|
||||
case Value::Kind::Witness: {
|
||||
if (std::optional<Nonnull<const Declaration*>> mem_decl =
|
||||
FindMember(f, witness->declaration().members());
|
||||
mem_decl.has_value()) {
|
||||
const auto& fun_decl = cast<FunctionDeclaration>(**mem_decl);
|
||||
return arena->New<BoundMethodValue>(&fun_decl, v);
|
||||
} else {
|
||||
FATAL_COMPILATION_ERROR(source_loc)
|
||||
<< "member " << f << " not in " << *witness;
|
||||
}
|
||||
}
|
||||
default:
|
||||
FATAL() << "expected Witness, not " << *witness;
|
||||
}
|
||||
}
|
||||
switch (v->kind()) {
|
||||
case Value::Kind::StructValue: {
|
||||
std::optional<Nonnull<const Value*>> field =
|
||||
@@ -51,8 +71,8 @@ static auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
|
||||
std::optional<Nonnull<const FunctionValue*>> func =
|
||||
class_type.FindFunction(f);
|
||||
if (func == std::nullopt) {
|
||||
FATAL_RUNTIME_ERROR(source_loc) << "member " << f << " not in " << *v
|
||||
<< " or its class " << class_type;
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "member " << f << " not in " << *v << " or its " << class_type;
|
||||
} else if ((*func)->declaration().is_method()) {
|
||||
// Found a method. Turn it into a bound method.
|
||||
const FunctionValue& m = cast<FunctionValue>(**func);
|
||||
@@ -90,17 +110,18 @@ static auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
|
||||
auto Value::GetField(Nonnull<Arena*> arena, const FieldPath& path,
|
||||
SourceLocation source_loc) const -> Nonnull<const Value*> {
|
||||
Nonnull<const Value*> value(this);
|
||||
for (const std::string& field : path.components_) {
|
||||
for (const FieldPath::Component& field : path.components_) {
|
||||
value = GetMember(arena, value, field, source_loc);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static auto SetFieldImpl(Nonnull<Arena*> arena, Nonnull<const Value*> value,
|
||||
std::vector<std::string>::const_iterator path_begin,
|
||||
std::vector<std::string>::const_iterator path_end,
|
||||
Nonnull<const Value*> field_value,
|
||||
SourceLocation source_loc) -> Nonnull<const Value*> {
|
||||
static auto SetFieldImpl(
|
||||
Nonnull<Arena*> arena, Nonnull<const Value*> value,
|
||||
std::vector<FieldPath::Component>::const_iterator path_begin,
|
||||
std::vector<FieldPath::Component>::const_iterator path_end,
|
||||
Nonnull<const Value*> field_value, SourceLocation source_loc)
|
||||
-> Nonnull<const Value*> {
|
||||
if (path_begin == path_end) {
|
||||
return field_value;
|
||||
}
|
||||
@@ -109,11 +130,11 @@ static auto SetFieldImpl(Nonnull<Arena*> arena, Nonnull<const Value*> value,
|
||||
std::vector<NamedValue> elements = cast<StructValue>(*value).elements();
|
||||
auto it = std::find_if(elements.begin(), elements.end(),
|
||||
[path_begin](const NamedValue& element) {
|
||||
return element.name == *path_begin;
|
||||
return element.name == (*path_begin).name();
|
||||
});
|
||||
if (it == elements.end()) {
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "field " << *path_begin << " not in " << *value;
|
||||
<< "field " << (*path_begin).name() << " not in " << *value;
|
||||
}
|
||||
it->value = SetFieldImpl(arena, it->value, path_begin + 1, path_end,
|
||||
field_value, source_loc);
|
||||
@@ -127,10 +148,10 @@ static auto SetFieldImpl(Nonnull<Arena*> arena, Nonnull<const Value*> value,
|
||||
std::vector<Nonnull<const Value*>> elements =
|
||||
cast<TupleValue>(*value).elements();
|
||||
// TODO(geoffromer): update FieldPath to hold integers as well as strings.
|
||||
int index = std::stoi(*path_begin);
|
||||
int index = std::stoi((*path_begin).name());
|
||||
if (index < 0 || static_cast<size_t>(index) >= elements.size()) {
|
||||
FATAL_RUNTIME_ERROR(source_loc)
|
||||
<< "index " << *path_begin << " out of range in " << *value;
|
||||
FATAL_RUNTIME_ERROR(source_loc) << "index " << (*path_begin).name()
|
||||
<< " out of range in " << *value;
|
||||
}
|
||||
elements[index] = SetFieldImpl(arena, elements[index], path_begin + 1,
|
||||
path_end, field_value, source_loc);
|
||||
@@ -159,8 +180,8 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
case Value::Kind::BindingPlaceholderValue: {
|
||||
const auto& placeholder = cast<BindingPlaceholderValue>(*this);
|
||||
out << "Placeholder<";
|
||||
if (placeholder.named_entity().has_value()) {
|
||||
out << (*placeholder.named_entity()).name();
|
||||
if (placeholder.value_node().has_value()) {
|
||||
out << (*placeholder.value_node());
|
||||
} else {
|
||||
out << "_";
|
||||
}
|
||||
@@ -266,6 +287,17 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
out << "class " << class_type.declaration().name();
|
||||
break;
|
||||
}
|
||||
case Value::Kind::InterfaceType: {
|
||||
const InterfaceType& iface_type = cast<InterfaceType>(*this);
|
||||
out << "interface " << iface_type.declaration().name();
|
||||
break;
|
||||
}
|
||||
case Value::Kind::Witness: {
|
||||
const auto& witness = cast<Witness>(*this);
|
||||
out << "impl " << *witness.declaration().impl_type() << " as "
|
||||
<< witness.declaration().interface();
|
||||
break;
|
||||
}
|
||||
case Value::Kind::ChoiceType:
|
||||
out << "choice " << cast<ChoiceType>(*this).name();
|
||||
break;
|
||||
@@ -289,6 +321,14 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
<< cast<TypeOfClassType>(*this).class_type().declaration().name()
|
||||
<< ")";
|
||||
break;
|
||||
case Value::Kind::TypeOfInterfaceType:
|
||||
out << "typeof("
|
||||
<< cast<TypeOfInterfaceType>(*this)
|
||||
.interface_type()
|
||||
.declaration()
|
||||
.name()
|
||||
<< ")";
|
||||
break;
|
||||
case Value::Kind::TypeOfChoiceType:
|
||||
out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
|
||||
<< ")";
|
||||
@@ -364,6 +404,9 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
||||
case Value::Kind::NominalClassType:
|
||||
return cast<NominalClassType>(*t1).declaration().name() ==
|
||||
cast<NominalClassType>(*t2).declaration().name();
|
||||
case Value::Kind::InterfaceType:
|
||||
return cast<InterfaceType>(*t1).declaration().name() ==
|
||||
cast<InterfaceType>(*t2).declaration().name();
|
||||
case Value::Kind::ChoiceType:
|
||||
return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
|
||||
case Value::Kind::TupleValue: {
|
||||
@@ -391,13 +434,34 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
||||
case Value::Kind::TypeOfClassType:
|
||||
return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
|
||||
&cast<TypeOfClassType>(*t2).class_type());
|
||||
case Value::Kind::TypeOfInterfaceType:
|
||||
return TypeEqual(&cast<TypeOfInterfaceType>(*t1).interface_type(),
|
||||
&cast<TypeOfInterfaceType>(*t2).interface_type());
|
||||
case Value::Kind::TypeOfChoiceType:
|
||||
return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
|
||||
&cast<TypeOfChoiceType>(*t2).choice_type());
|
||||
default:
|
||||
case Value::Kind::IntValue:
|
||||
case Value::Kind::BoolValue:
|
||||
case Value::Kind::FunctionValue:
|
||||
case Value::Kind::BoundMethodValue:
|
||||
case Value::Kind::StructValue:
|
||||
case Value::Kind::NominalClassValue:
|
||||
case Value::Kind::AlternativeValue:
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
case Value::Kind::StringValue:
|
||||
case Value::Kind::PointerValue:
|
||||
case Value::Kind::LValue:
|
||||
case Value::Kind::BindingPlaceholderValue:
|
||||
case Value::Kind::ContinuationValue:
|
||||
FATAL() << "TypeEqual used to compare non-type values\n"
|
||||
<< *t1 << "\n"
|
||||
<< *t2;
|
||||
case Value::Kind::Witness:
|
||||
FATAL() << "TypeEqual: unexpected Witness";
|
||||
break;
|
||||
case Value::Kind::AutoType:
|
||||
FATAL() << "TypeEqual: unexpected AutoType";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -468,11 +532,14 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
||||
case Value::Kind::AutoType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::NominalClassType:
|
||||
case Value::Kind::InterfaceType:
|
||||
case Value::Kind::Witness:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::StringType:
|
||||
case Value::Kind::TypeOfClassType:
|
||||
case Value::Kind::TypeOfInterfaceType:
|
||||
case Value::Kind::TypeOfChoiceType:
|
||||
return TypeEqual(v1, v2);
|
||||
case Value::Kind::NominalClassValue:
|
||||
@@ -533,29 +600,21 @@ auto FieldTypes(const NominalClassType& class_type) -> std::vector<NamedValue> {
|
||||
return field_types;
|
||||
}
|
||||
|
||||
auto NominalClassType::FindMember(const std::string& name) const
|
||||
auto FindMember(const std::string& name,
|
||||
llvm::ArrayRef<Nonnull<Declaration*>> members)
|
||||
-> std::optional<Nonnull<const Declaration*>> {
|
||||
for (const auto& member : declaration().members()) {
|
||||
switch (member->kind()) {
|
||||
case DeclarationKind::FunctionDeclaration: {
|
||||
const auto& fun = cast<FunctionDeclaration>(*member);
|
||||
if (fun.name() == name) {
|
||||
return &fun;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(*member);
|
||||
if (var.binding().name() == name) {
|
||||
return &var;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
for (Nonnull<const Declaration*> member : members) {
|
||||
if (std::optional<std::string> mem_name = GetName(*member);
|
||||
mem_name.has_value()) {
|
||||
if (*mem_name == name)
|
||||
return member;
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void ImplBinding::Print(llvm::raw_ostream& out) const {
|
||||
out << "impl " << *type_var_ << " as " << *iface_;
|
||||
}
|
||||
|
||||
} // namespace Carbon
|
||||
|
||||
@@ -44,6 +44,7 @@ class Value {
|
||||
NominalClassValue,
|
||||
AlternativeValue,
|
||||
TupleValue,
|
||||
Witness,
|
||||
IntType,
|
||||
BoolType,
|
||||
TypeType,
|
||||
@@ -52,6 +53,7 @@ class Value {
|
||||
AutoType,
|
||||
StructType,
|
||||
NominalClassType,
|
||||
InterfaceType,
|
||||
ChoiceType,
|
||||
ContinuationType, // The type of a continuation.
|
||||
VariableType, // e.g., generic type parameters.
|
||||
@@ -61,6 +63,7 @@ class Value {
|
||||
StringType,
|
||||
StringValue,
|
||||
TypeOfClassType,
|
||||
TypeOfInterfaceType,
|
||||
TypeOfChoiceType,
|
||||
};
|
||||
|
||||
@@ -331,20 +334,20 @@ class BindingPlaceholderValue : public Value {
|
||||
explicit BindingPlaceholderValue() : Value(Kind::BindingPlaceholderValue) {}
|
||||
|
||||
// Represents a named placeholder.
|
||||
explicit BindingPlaceholderValue(NamedEntityView named_entity)
|
||||
explicit BindingPlaceholderValue(ValueNodeView value_node)
|
||||
: Value(Kind::BindingPlaceholderValue),
|
||||
named_entity_(std::move(named_entity)) {}
|
||||
value_node_(std::move(value_node)) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->kind() == Kind::BindingPlaceholderValue;
|
||||
}
|
||||
|
||||
auto named_entity() const -> const std::optional<NamedEntityView>& {
|
||||
return named_entity_;
|
||||
auto value_node() const -> const std::optional<ValueNodeView>& {
|
||||
return value_node_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<NamedEntityView> named_entity_;
|
||||
std::optional<ValueNodeView> value_node_;
|
||||
};
|
||||
|
||||
// The int type.
|
||||
@@ -382,11 +385,13 @@ class FunctionType : public Value {
|
||||
public:
|
||||
FunctionType(llvm::ArrayRef<Nonnull<const GenericBinding*>> deduced,
|
||||
Nonnull<const Value*> parameters,
|
||||
Nonnull<const Value*> return_type)
|
||||
Nonnull<const Value*> return_type,
|
||||
llvm::ArrayRef<Nonnull<const ImplBinding*>> impl_bindings)
|
||||
: Value(Kind::FunctionType),
|
||||
deduced_(deduced),
|
||||
parameters_(parameters),
|
||||
return_type_(return_type) {}
|
||||
return_type_(return_type),
|
||||
impl_bindings_(impl_bindings) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->kind() == Kind::FunctionType;
|
||||
@@ -397,11 +402,17 @@ class FunctionType : public Value {
|
||||
}
|
||||
auto parameters() const -> const Value& { return *parameters_; }
|
||||
auto return_type() const -> const Value& { return *return_type_; }
|
||||
// The bindings for the witness tables (impls) required by the
|
||||
// bounds on the type parameters of the generic function.
|
||||
auto impl_bindings() const -> llvm::ArrayRef<Nonnull<const ImplBinding*>> {
|
||||
return impl_bindings_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Nonnull<const GenericBinding*>> deduced_;
|
||||
Nonnull<const Value*> parameters_;
|
||||
Nonnull<const Value*> return_type_;
|
||||
std::vector<Nonnull<const ImplBinding*>> impl_bindings_;
|
||||
};
|
||||
|
||||
// A pointer type.
|
||||
@@ -463,10 +474,6 @@ class NominalClassType : public Value {
|
||||
|
||||
auto declaration() const -> const ClassDeclaration& { return *declaration_; }
|
||||
|
||||
// Return the declaration of the member with the given name.
|
||||
auto FindMember(const std::string& name) const
|
||||
-> std::optional<Nonnull<const Declaration*>>;
|
||||
|
||||
// Returns the value of the function named `name` in this class, or
|
||||
// nullopt if there is no such function.
|
||||
auto FindFunction(const std::string& name) const
|
||||
@@ -476,6 +483,46 @@ class NominalClassType : public Value {
|
||||
Nonnull<const ClassDeclaration*> declaration_;
|
||||
};
|
||||
|
||||
auto FieldTypes(const NominalClassType&) -> std::vector<NamedValue>;
|
||||
// Return the declaration of the member with the given name.
|
||||
auto FindMember(const std::string& name,
|
||||
llvm::ArrayRef<Nonnull<Declaration*>> members)
|
||||
-> std::optional<Nonnull<const Declaration*>>;
|
||||
|
||||
// An interface type.
|
||||
class InterfaceType : public Value {
|
||||
public:
|
||||
InterfaceType(Nonnull<const InterfaceDeclaration*> declaration)
|
||||
: Value(Kind::InterfaceType), declaration_(declaration) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->kind() == Kind::InterfaceType;
|
||||
}
|
||||
|
||||
auto declaration() const -> const InterfaceDeclaration& {
|
||||
return *declaration_;
|
||||
}
|
||||
|
||||
private:
|
||||
Nonnull<const InterfaceDeclaration*> declaration_;
|
||||
};
|
||||
|
||||
// The witness table for an impl.
|
||||
class Witness : public Value {
|
||||
public:
|
||||
Witness(Nonnull<const ImplDeclaration*> declaration)
|
||||
: Value(Kind::Witness), declaration_(declaration) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->kind() == Kind::Witness;
|
||||
}
|
||||
|
||||
auto declaration() const -> const ImplDeclaration& { return *declaration_; }
|
||||
|
||||
private:
|
||||
Nonnull<const ImplDeclaration*> declaration_;
|
||||
};
|
||||
|
||||
auto FieldTypes(const NominalClassType&) -> std::vector<NamedValue>;
|
||||
|
||||
// A choice type.
|
||||
@@ -627,6 +674,21 @@ class TypeOfClassType : public Value {
|
||||
Nonnull<const NominalClassType*> class_type_;
|
||||
};
|
||||
|
||||
class TypeOfInterfaceType : public Value {
|
||||
public:
|
||||
explicit TypeOfInterfaceType(Nonnull<const InterfaceType*> iface_type)
|
||||
: Value(Kind::TypeOfInterfaceType), iface_type_(iface_type) {}
|
||||
|
||||
static auto classof(const Value* value) -> bool {
|
||||
return value->kind() == Kind::TypeOfInterfaceType;
|
||||
}
|
||||
|
||||
auto interface_type() const -> const InterfaceType& { return *iface_type_; }
|
||||
|
||||
private:
|
||||
Nonnull<const InterfaceType*> iface_type_;
|
||||
};
|
||||
|
||||
// The type of an expression whose value is a choice type. Currently there is no
|
||||
// way to explicitly name such a type in Carbon code, but we are tentatively
|
||||
// using `typeof(ChoiceName)` as the debug-printing format, in anticipation of
|
||||
|
||||
Reference in New Issue
Block a user