From 44fdcc3db877e6adee1f944cf481db836ea8e3a1 Mon Sep 17 00:00:00 2001 From: Daniel James Szelogowski Date: Fri, 12 Jan 2024 16:35:54 -0600 Subject: [PATCH] Refactor Duplicate Code Patterns in StaticScope Class (#3550) These changes are aimed at improving code readability and maintainability within the `StaticScope` class of the AST folder. - **Optimized Print and PrintID Methods:** Introduced a template function `PrintCommon` to handle common logic in `Print` and `PrintID`. This change simplifies the class interface and avoids repetition, enhancing code readability. - **Simplified TryResolveHere Method:** Streamlined the `TryResolveHere` method by simplifying the conditional logic. The refactored code is more readable and easier to understand, improving overall code quality. --------- Co-authored-by: Richard Smith --- explorer/ast/static_scope.cpp | 34 +++++++++++++++------------------- explorer/ast/static_scope.h | 3 +++ 2 files changed, 18 insertions(+), 19 deletions(-) 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;