diff --git a/explorer/ast/static_scope.cpp b/explorer/ast/static_scope.cpp index 0815adf194d3..f5d6b62964e1 100644 --- a/explorer/ast/static_scope.cpp +++ b/explorer/ast/static_scope.cpp @@ -35,20 +35,21 @@ auto StaticScope::Add(std::string_view name, ValueNodeView entity, return Success(); } -void StaticScope::Print(llvm::raw_ostream& out) const { +template +void StaticScope::PrintCommon(Action action) const { if (ast_node_) { - ast_node_.value()->Print(out); + action(ast_node_.value()); } else { *trace_stream_ << "package"; } } +void StaticScope::Print(llvm::raw_ostream& out) const { + PrintCommon([&out](auto node) { node->Print(out); }); +} + void StaticScope::PrintID(llvm::raw_ostream& out) const { - if (ast_node_) { - ast_node_.value()->PrintID(out); - } else { - *trace_stream_ << "package"; - } + PrintCommon([&out](auto node) { node->PrintID(out); }); } void StaticScope::MarkDeclared(std::string_view name) { @@ -136,20 +137,15 @@ auto StaticScope::TryResolveHere(std::string_view name, } }); - if (allow_undeclared) { + if (allow_undeclared || it->second.status == NameStatus::Usable) { return {it->second.entity}; } - 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 {it->second.entity}; - } + return ProgramError(source_loc) + << "'" << name + << (it->second.status == NameStatus::KnownButNotDeclared + ? "' has not been declared yet" + : "' is not usable until after it has been completely " + "declared"); } auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view) diff --git a/explorer/ast/static_scope.h b/explorer/ast/static_scope.h index 52b575026fdb..5fa96508b6bf 100644 --- a/explorer/ast/static_scope.h +++ b/explorer/ast/static_scope.h @@ -55,6 +55,9 @@ class StaticScope { auto Add(std::string_view name, ValueNodeView entity, NameStatus status = NameStatus::Usable) -> ErrorOr; + template + void PrintCommon(Action action) const; + void Print(llvm::raw_ostream& out) const; void PrintID(llvm::raw_ostream& out) const;