Scope: store IntrusivePtr in local

This commit is contained in:
Max Kellermann 2020-03-06 13:03:41 +01:00
parent 6e0d331267
commit 785ff57d11
6 changed files with 16 additions and 28 deletions

View file

@ -62,7 +62,7 @@ void lookup_global_symbols_regex(const string& orig_regex, vector<ID*>& matches,
ID* nextid;
for ( const auto& sym : syms )
{
ID* nextid = sym.second;
ID* nextid = sym.second.get();
if ( ! func_only || nextid->Type()->Tag() == TYPE_FUNC )
if ( ! regexec (&re, nextid->Name(), 0, 0, 0) )
matches.push_back(nextid);

View file

@ -40,9 +40,6 @@ Scope::Scope(IntrusivePtr<ID> id, attr_list* al)
Scope::~Scope()
{
for ( const auto& entry : local )
Unref(entry.second);
if ( attrs )
{
for ( const auto& attr : *attrs )
@ -101,7 +98,7 @@ void Scope::Describe(ODesc* d) const
for ( const auto& entry : local )
{
ID* id = entry.second;
ID* id = entry.second.get();
id->Describe(d);
d->NL();
}
@ -111,7 +108,7 @@ TraversalCode Scope::Traverse(TraversalCallback* cb) const
{
for ( const auto& entry : local )
{
ID* id = entry.second;
ID* id = entry.second.get();
TraversalCode tc = id->Traverse(cb);
HANDLE_TC_STMT_PRE(tc);
}
@ -176,11 +173,11 @@ IntrusivePtr<ID> install_ID(const char* name, const char* module_name,
auto id = make_intrusive<ID>(full_name.data(), scope, is_export);
if ( SCOPE_FUNCTION != scope )
global_scope()->Insert(std::move(full_name), IntrusivePtr{id}.release());
global_scope()->Insert(std::move(full_name), id);
else
{
id->SetOffset(top_scope->Length());
top_scope->Insert(std::move(full_name), IntrusivePtr{id}.release());
top_scope->Insert(std::move(full_name), id);
}
return id;

View file

@ -27,31 +27,22 @@ public:
const auto& entry = local.find(std::forward<N>(name));
if ( entry != local.end() )
return entry->second;
return entry->second.get();
return nullptr;
}
template<typename N>
void Insert(N&& name, ID* id)
{
auto [it, inserted] = local.emplace(std::forward<N>(name), id);
if ( ! inserted )
{
Unref(it->second);
it->second = id;
}
}
template<typename N, typename I>
void Insert(N&& name, I&& id) { local[std::forward<N>(name)] = std::forward<I>(id); }
template<typename N>
ID* Remove(N&& name)
IntrusivePtr<ID> Remove(N&& name)
{
const auto& entry = local.find(std::forward<N>(name));
if ( entry != local.end() )
{
ID* id = entry->second;
auto id = std::move(entry->second);
local.erase(entry);
return id;
}
@ -64,7 +55,7 @@ public:
BroType* ReturnType() const { return return_type.get(); }
size_t Length() const { return local.size(); }
const std::map<std::string, ID*>& Vars() { return local; }
const auto& Vars() { return local; }
ID* GenerateTemporary(const char* name);
@ -83,7 +74,7 @@ protected:
IntrusivePtr<ID> scope_id;
attr_list* attrs;
IntrusivePtr<BroType> return_type;
std::map<std::string, ID*> local;
std::map<std::string, IntrusivePtr<ID>> local;
id_list* inits;
};

View file

@ -252,7 +252,7 @@ void ProfileLogger::Log()
for ( const auto& global : globals )
{
ID* id = global.second;
ID* id = global.second.get();
// We don't show/count internal globals as they are always
// contained in some other global user-visible container.

View file

@ -31,7 +31,7 @@ Config::Config(ReaderFrontend *frontend) : ReaderBackend(frontend)
for ( const auto& entry : globals )
{
ID* id = entry.second;
ID* id = entry.second.get();
if ( ! id->IsOption() )
continue;

View file

@ -1924,7 +1924,7 @@ function global_sizes%(%): var_sizes
for ( const auto& global : globals )
{
ID* id = global.second;
ID* id = global.second.get();
if ( id->HasVal() )
{
Val* id_name = new StringVal(id->Name());
@ -1952,7 +1952,7 @@ function global_ids%(%): id_table
for ( const auto& global : globals )
{
ID* id = global.second;
ID* id = global.second.get();
auto rec = make_intrusive<RecordVal>(script_id);
rec->Assign(0, make_intrusive<StringVal>(type_name(id->Type()->Tag())));
rec->Assign(1, val_mgr->GetBool(id->IsExport()));