Files
carbon-lang/executable_semantics/interpreter/impl_scope.h
T
210856dd57 Generic classes (#1124)
* start of generic classes

* fix regressions

* class functions in interfaces

* access to class function on interface from class parameter

* more stuff working for generic classes

* fixing bugs

* update TypeEqual for generic classes

* fixing bugs and finding new ones

* disable unqualified access to members from other members for now

* minor edits

* bug fixes

* introduce compile_time_value to use in type checker instead of constant_value

* cleanup

* put a CHECK back in

* failure test cases for the new FATAL_COMPILATION_ERROR

* change a runtime FATAL into a FATAL_COMPILATION_ERROR

* Update executable_semantics/ast/declaration.h

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/action_stack.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/resolve_names.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* responses to reviews

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* rename compile_time_value to symbolic_identity

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/type_checker.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* Update executable_semantics/interpreter/value.cpp

Co-authored-by: Jon Meow <jperkins@google.com>

* more edits from review

* Apply suggestions from code review

Co-authored-by: Geoff Romer <gromer@google.com>

* review responses

* Update executable_semantics/interpreter/interpreter.cpp

Co-authored-by: Geoff Romer <gromer@google.com>

* const impl_scope for TypeCheckChoiceDeclaration

* add some const

Co-authored-by: Jon Meow <jperkins@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
2022-03-29 13:09:41 -04:00

86 lines
3.2 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
#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 -> ErrorOr<ValueNodeView>;
void Print(llvm::raw_ostream& out) const;
private:
auto TryResolve(Nonnull<const Value*> iface_type, Nonnull<const Value*> type,
SourceLocation source_loc) const
-> ErrorOr<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_