Use static name resolution in Interpreter (#1022)

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
Geoff Romer
2022-01-21 09:48:11 -08:00
committed by GitHub
co-authored by Jon Meow
parent d28e75629e
commit c311c8849c
13 changed files with 304 additions and 221 deletions
+30 -10
View File
@@ -19,13 +19,21 @@ namespace Carbon {
class ActionStack {
public:
// Constructs an empty ActionStack
explicit ActionStack(Scope globals) : globals_(std::move(globals)) {}
ActionStack() = default;
void Print(llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
// Returns an Env containing the currently-defined global variables.
auto GlobalEnv() const -> Env { return globals_.values(); }
// TODO: consider unifying with Print.
void PrintScopes(llvm::raw_ostream& out) const;
// Sets the heap that variables will be allocated on. Cannot be called at
// run time, or when IsEmpty() is false, and marks the start of run time.
void SetHeap(Nonnull<HeapAllocationInterface*> heap) {
CHECK(todo_.IsEmpty());
CHECK(!globals_.has_value());
globals_ = RuntimeScope(heap);
}
// Starts execution with `action` at the top of the stack. Cannot be called
// when IsEmpty() is false.
@@ -38,13 +46,25 @@ class ActionStack {
// ScopeAction.
auto CurrentAction() -> Action& { return *todo_.Top(); }
// The scope that should be used to resolve name lookups in the current
// action.
auto CurrentScope() const -> Scope&;
// Allocates storage for `named_entity`, and initializes it to `value`.
void Initialize(NamedEntityView named_entity, Nonnull<const Value*> value);
// Returns the value bound to `named_entity`. If `named_entity` is a local
// variable, this will be an LValue.
auto ValueOfName(NamedEntityView named_entity,
SourceLocation source_loc) const -> Nonnull<const Value*>;
// Merges `scope` into the innermost scope currently on the stack.
void MergeScope(RuntimeScope scope);
// Initializes `fragment` so that, when resumed, it begins execution of
// `body`.
void InitializeFragment(ContinuationValue::StackFragment& fragment,
Nonnull<const Statement*> body);
// The result produced by the `action` argument of the most recent
// `Start` call. *this must be empty, signifying that the action has been
// fully executed.
// Start call. Cannot be called if IsEmpty() is false, or if `action`
// was an action that doesn't produce results.
auto result() const -> Nonnull<const Value*> { return *result_; }
// The following methods, called "transition methods", update the state of
@@ -65,7 +85,7 @@ class ActionStack {
// Advances the current action one step, and push `child` onto the stack.
// If `scope` is specified, `child` will be executed in that scope.
void Spawn(std::unique_ptr<Action> child);
void Spawn(std::unique_ptr<Action> child, Scope scope);
void Spawn(std::unique_ptr<Action> child, RuntimeScope scope);
// Advances the current action one step.
void RunAgain();
@@ -100,7 +120,7 @@ class ActionStack {
// TODO: consider defining a non-nullable unique_ptr-like type to use here.
Stack<std::unique_ptr<Action>> todo_;
std::optional<Nonnull<const Value*>> result_;
mutable Scope globals_;
std::optional<RuntimeScope> globals_;
};
} // namespace Carbon