Factor ValueNodeView out of StaticScope. (#2562)

Also remove unused support in `StaticScope` for a scope to have multiple parents.

No functionality change intended.
This commit is contained in:
Richard Smith
2023-01-30 16:12:04 -08:00
committed by GitHub
parent e9480e7dfe
commit 47a1e99b9e
13 changed files with 205 additions and 210 deletions
+20 -34
View File
@@ -55,34 +55,24 @@ auto StaticScope::Resolve(const std::string& name,
auto StaticScope::TryResolve(const std::string& name,
SourceLocation source_loc) const
-> ErrorOr<std::optional<ValueNodeView>> {
auto it = declared_names_.find(name);
if (it != declared_names_.end()) {
switch (it->second.status) {
case NameStatus::KnownButNotDeclared:
return ProgramError(source_loc)
<< "'" << name << "' has not been declared yet";
case NameStatus::DeclaredButNotUsable:
return ProgramError(source_loc)
<< "'" << name
<< "' is not usable until after it has been completely declared";
case NameStatus::Usable:
return std::make_optional(it->second.entity);
for (const StaticScope* scope = this; scope;
scope = scope->parent_scope_.value_or(nullptr)) {
auto it = scope->declared_names_.find(name);
if (it != scope->declared_names_.end()) {
switch (it->second.status) {
case NameStatus::KnownButNotDeclared:
return ProgramError(source_loc)
<< "'" << name << "' has not been declared yet";
case NameStatus::DeclaredButNotUsable:
return ProgramError(source_loc) << "'" << name
<< "' is not usable until after it "
"has been completely declared";
case NameStatus::Usable:
return std::make_optional(it->second.entity);
}
}
}
std::optional<ValueNodeView> result;
for (Nonnull<const StaticScope*> parent : parent_scopes_) {
CARBON_ASSIGN_OR_RETURN(std::optional<ValueNodeView> parent_result,
parent->TryResolve(name, source_loc));
if (parent_result.has_value() && result.has_value() &&
*parent_result != *result) {
return ProgramError(source_loc)
<< "'" << name << "' is ambiguous between "
<< result->base().source_loc() << " and "
<< parent_result->base().source_loc();
}
result = parent_result;
}
return result;
return {std::nullopt};
}
auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
@@ -98,14 +88,10 @@ auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
}
auto StaticScope::ResolveReturned() const -> std::optional<ValueNodeView> {
if (returned_var_def_view_.has_value()) {
return returned_var_def_view_;
}
for (Nonnull<const StaticScope*> parent : parent_scopes_) {
std::optional<ValueNodeView> parent_returned_var =
parent->ResolveReturned();
if (parent_returned_var.has_value()) {
return parent_returned_var;
for (const StaticScope* scope = this; scope;
scope = scope->parent_scope_.value_or(nullptr)) {
if (scope->returned_var_def_view_.has_value()) {
return scope->returned_var_def_view_;
}
}
return std::nullopt;