diff --git a/src/Scope.cc b/src/Scope.cc index 8db868a571..d6c0c343e1 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -14,7 +14,7 @@ typedef PList scope_list; static scope_list scopes; static Scope* top_scope; - +static IntrusivePtr nil_id; Scope::Scope(IntrusivePtr id, attr_list* al) : scope_id(std::move(id)) @@ -57,6 +57,30 @@ Scope::~Scope() } } +const IntrusivePtr& Scope::Find(std::string_view name) const + { + auto entry = local.find(name); + + if ( entry != local.end() ) + return entry->second; + + return nil_id; + } + +IntrusivePtr Scope::Remove(std::string_view name) + { + auto entry = local.find(name); + + if ( entry != local.end() ) + { + auto id = std::move(entry->second); + local.erase(entry); + return id; + } + + return nullptr; + } + ID* Scope::GenerateTemporary(const char* name) { return new ID(name, SCOPE_FUNCTION, false); @@ -148,8 +172,7 @@ const IntrusivePtr& lookup_ID(const char* name, const char* curr_module, return global_scope()->Find(globalname); } - static IntrusivePtr nil; - return nil; + return nil_id; } IntrusivePtr install_ID(const char* name, const char* module_name, diff --git a/src/Scope.h b/src/Scope.h index c83d706b42..5201d1b5e0 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -4,6 +4,7 @@ #include #include +#include #include #include "Obj.h" @@ -21,42 +22,17 @@ public: explicit Scope(IntrusivePtr id, attr_list* al); ~Scope() override; - template - const IntrusivePtr& Find(N&& name) const - { - static IntrusivePtr nil; - const auto& entry = local.find(std::forward(name)); - - if ( entry != local.end() ) - return entry->second; - - return nil; - } + const IntrusivePtr& Find(std::string_view name) const; template [[deprecated("Remove in v4.1. Use Find().")]] ID* Lookup(N&& name) const - { - return Find(name).get(); - } + { return Find(name).get(); } template void Insert(N&& name, I&& id) { local[std::forward(name)] = std::forward(id); } - template - IntrusivePtr Remove(N&& name) - { - const auto& entry = local.find(std::forward(name)); - - if ( entry != local.end() ) - { - auto id = std::move(entry->second); - local.erase(entry); - return id; - } - - return nullptr; - } + IntrusivePtr Remove(std::string_view name); ID* ScopeID() const { return scope_id.get(); } attr_list* Attrs() const { return attrs; } @@ -82,7 +58,7 @@ protected: IntrusivePtr scope_id; attr_list* attrs; IntrusivePtr return_type; - std::map> local; + std::map, std::less<>> local; id_list* inits; };