mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Defer parsing of method bodies until the end of a suitable enclosing scope. (#3832)
In parse, form a list of methods that are defined inline, tracking where they start, where they end, and which other inline methods are nested within them. In check, when we reach an inline method body, skip it and add it to a worklist to be processed later. We also track when we reach the start and end of a context in which inline method bodies are deferred, so that we know when to replay the bodies. When suspending a function definition to be processed later, the `DeclNameStack` entry is moved to separate storage, including popping the corresponding scopes from the scope stack and removing the corresponding lexical names from lexical lookup. Later, when we return to the function and parse its definition, the `DeclNameStack` entry is restored. The same is done when we reach the end of a nested context that can have inline methods, so that we can reenter the nested scope before processing its members. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
d7fb1b287d
commit
f9ce0b194d
@@ -63,6 +63,36 @@ auto DeclNameStack::PopScope() -> void {
|
||||
decl_name_stack_.pop_back();
|
||||
}
|
||||
|
||||
auto DeclNameStack::Suspend() -> SuspendedName {
|
||||
CARBON_CHECK(decl_name_stack_.back().state == NameContext::State::Finished)
|
||||
<< "Missing call to FinishName before Suspend";
|
||||
SuspendedName result = {decl_name_stack_.pop_back_val(), {}};
|
||||
auto enclosing_index = result.name_context.enclosing_scope;
|
||||
auto& scope_stack = context_->scope_stack();
|
||||
while (scope_stack.PeekIndex() > enclosing_index) {
|
||||
result.scopes.push_back(scope_stack.Suspend());
|
||||
}
|
||||
CARBON_CHECK(scope_stack.PeekIndex() == enclosing_index)
|
||||
<< "Scope index " << enclosing_index
|
||||
<< " does not enclose the current scope " << scope_stack.PeekIndex();
|
||||
return result;
|
||||
}
|
||||
|
||||
auto DeclNameStack::Restore(SuspendedName sus) -> void {
|
||||
// The enclosing state must be the same when a name is restored.
|
||||
CARBON_CHECK(context_->scope_stack().PeekIndex() ==
|
||||
sus.name_context.enclosing_scope)
|
||||
<< "Name restored at the wrong position in the name stack.";
|
||||
|
||||
// clang-tidy warns that the `std::move` below has no effect. While that's
|
||||
// true, this `move` defends against `NameContext` growing more state later.
|
||||
// NOLINTNEXTLINE(performance-move-const-arg)
|
||||
decl_name_stack_.push_back(std::move(sus.name_context));
|
||||
for (auto& suspended_scope : llvm::reverse(sus.scopes)) {
|
||||
context_->scope_stack().Restore(std::move(suspended_scope));
|
||||
}
|
||||
}
|
||||
|
||||
auto DeclNameStack::LookupOrAddName(NameContext name_context,
|
||||
SemIR::InstId target_id) -> SemIR::InstId {
|
||||
switch (name_context.state) {
|
||||
|
||||
Reference in New Issue
Block a user