mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user