Scope: remove uses of PDict

This commit is contained in:
Tim Wojtulewicz 2019-07-26 13:04:29 -07:00 committed by Jon Siwek
parent bbf49406c1
commit a3deb0446c
7 changed files with 51 additions and 41 deletions

View file

@ -51,14 +51,16 @@ void lookup_global_symbols_regex(const string& orig_regex, vector<ID*>& matches,
} }
Scope* global = global_scope(); Scope* global = global_scope();
PDict<ID>* syms = global->Vars(); auto syms = global->Vars();
ID* nextid; ID* nextid;
IterCookie* cookie = syms->InitForIteration(); for ( const auto& sym : syms )
while ( (nextid = syms->NextEntry( cookie )) ) {
ID* nextid = sym.second;
if ( ! func_only || nextid->Type()->Tag() == TYPE_FUNC ) if ( ! func_only || nextid->Type()->Tag() == TYPE_FUNC )
if ( ! regexec (&re, nextid->Name(), 0, 0, 0) ) if ( ! regexec (&re, nextid->Name(), 0, 0, 0) )
matches.push_back(nextid); matches.push_back(nextid);
}
} }
void choose_global_symbols_regex(const string& regex, vector<ID*>& choices, void choose_global_symbols_regex(const string& regex, vector<ID*>& choices,

View file

@ -19,7 +19,6 @@ Scope::Scope(ID* id, attr_list* al)
attrs = al; attrs = al;
return_type = 0; return_type = 0;
local = new PDict<ID>(ORDERED);
inits = new id_list; inits = new id_list;
if ( id ) if ( id )
@ -42,8 +41,8 @@ Scope::Scope(ID* id, attr_list* al)
Scope::~Scope() Scope::~Scope()
{ {
for ( int i = 0; i < local->Length(); ++i ) for ( const auto& entry : local )
Unref(local->NthEntry(i)); Unref(entry.second);
if ( attrs ) if ( attrs )
{ {
@ -55,7 +54,6 @@ Scope::~Scope()
Unref(scope_id); Unref(scope_id);
Unref(return_type); Unref(return_type);
delete local;
delete inits; delete inits;
} }
@ -82,7 +80,7 @@ void Scope::Describe(ODesc* d) const
d->SP(); d->SP();
d->Add(return_type != 0); d->Add(return_type != 0);
d->SP(); d->SP();
d->Add(local->Length()); d->Add(static_cast<uint64_t>(local.size()));
d->SP(); d->SP();
} }
@ -98,9 +96,9 @@ void Scope::Describe(ODesc* d) const
d->NL(); d->NL();
} }
for ( int i = 0; i < local->Length(); ++i ) for ( const auto& entry : local )
{ {
ID* id = local->NthEntry(i); ID* id = entry.second;
id->Describe(d); id->Describe(d);
d->NL(); d->NL();
} }
@ -108,13 +106,9 @@ void Scope::Describe(ODesc* d) const
TraversalCode Scope::Traverse(TraversalCallback* cb) const TraversalCode Scope::Traverse(TraversalCallback* cb) const
{ {
PDict<ID>* ids = GetIDs(); for ( const auto& entry : local )
IterCookie* iter = ids->InitForIteration();
HashKey* key;
ID* id;
while ( (id = ids->NextEntry(key, iter)) )
{ {
ID* id = entry.second;
TraversalCode tc = id->Traverse(cb); TraversalCode tc = id->Traverse(cb);
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
} }

View file

@ -4,6 +4,7 @@
#define scope_h #define scope_h
#include <string> #include <string>
#include <map>
#include "Dict.h" #include "Dict.h"
#include "Obj.h" #include "Obj.h"
@ -20,25 +21,37 @@ public:
explicit Scope(ID* id, attr_list* al); explicit Scope(ID* id, attr_list* al);
~Scope() override; ~Scope() override;
ID* Lookup(const char* name) const { return local->Lookup(name); } ID* Lookup(const std::string& name) const
void Insert(const char* name, ID* id) { local->Insert(name, id); }
ID* Remove(const char* name)
{ {
HashKey key(name); const auto& entry = local.find(name);
return (ID*) local->Remove(&key); if ( entry != local.end() )
return entry->second;
return nullptr;
}
void Insert(const std::string& name, ID* id) { local[name] = id; }
ID* Remove(const std::string& name)
{
const auto& entry = local.find(name);
if ( entry != local.end() )
{
ID* id = entry->second;
local.erase(entry);
return id;
}
return nullptr;
} }
ID* ScopeID() const { return scope_id; } ID* ScopeID() const { return scope_id; }
attr_list* Attrs() const { return attrs; } attr_list* Attrs() const { return attrs; }
BroType* ReturnType() const { return return_type; } BroType* ReturnType() const { return return_type; }
int Length() const { return local->Length(); } size_t Length() const { return local.size(); }
PDict<ID>* Vars() const { return local; } std::map<string, ID*>& Vars() { return local; }
ID* GenerateTemporary(const char* name); ID* GenerateTemporary(const char* name);
PDict<ID>* GetIDs() const { return local; }
// Returns the list of variables needing initialization, and // Returns the list of variables needing initialization, and
// removes it from this Scope. // removes it from this Scope.
id_list* GetInits(); id_list* GetInits();
@ -54,7 +67,7 @@ protected:
ID* scope_id; ID* scope_id;
attr_list* attrs; attr_list* attrs;
BroType* return_type; BroType* return_type;
PDict<ID>* local; std::map<string, ID*> local;
id_list* inits; id_list* inits;
}; };

View file

@ -239,7 +239,7 @@ void ProfileLogger::Log()
// Script-level state. // Script-level state.
unsigned int size, mem = 0; unsigned int size, mem = 0;
PDict<ID>* globals = global_scope()->Vars(); auto globals = global_scope()->Vars();
if ( expensive ) if ( expensive )
{ {
@ -249,10 +249,10 @@ void ProfileLogger::Log()
file->Write(fmt("%.06f Global_sizes > 100k: %dK\n", file->Write(fmt("%.06f Global_sizes > 100k: %dK\n",
network_time, mem / 1024)); network_time, mem / 1024));
ID* id; for ( const auto& global : globals )
IterCookie* c = globals->InitForIteration(); {
ID* id = global.second;
while ( (id = globals->NextEntry(c)) )
// We don't show/count internal globals as they are always // We don't show/count internal globals as they are always
// contained in some other global user-visible container. // contained in some other global user-visible container.
if ( id->HasVal() ) if ( id->HasVal() )
@ -298,6 +298,7 @@ void ProfileLogger::Log()
file->Write("\n"); file->Write("\n");
} }
} }
}
file->Write(fmt("%.06f Global_sizes total: %dK\n", file->Write(fmt("%.06f Global_sizes total: %dK\n",
network_time, mem / 1024)); network_time, mem / 1024));

View file

@ -429,7 +429,7 @@ TraversalCode OuterIDBindingFinder::PreExpr(const Expr* expr)
if ( e->Id()->IsGlobal() ) if ( e->Id()->IsGlobal() )
return TC_CONTINUE; return TC_CONTINUE;
if ( scope->GetIDs()->Lookup(e->Id()->Name()) ) if ( scope->Lookup(e->Id()->Name()) )
return TC_CONTINUE; return TC_CONTINUE;
outer_id_references.push_back(e); outer_id_references.push_back(e);

View file

@ -29,10 +29,10 @@ Config::Config(ReaderFrontend *frontend) : ReaderBackend(frontend)
// find all option names and their types. // find all option names and their types.
auto globals = global_scope()->Vars(); auto globals = global_scope()->Vars();
auto c = globals->InitForIteration();
while ( auto id = globals->NextEntry(c) ) for ( const auto& entry : globals )
{ {
ID* id = entry.second;
if ( ! id->IsOption() ) if ( ! id->IsOption() )
continue; continue;

View file

@ -1895,11 +1895,11 @@ function reading_traces%(%): bool
function global_sizes%(%): var_sizes function global_sizes%(%): var_sizes
%{ %{
TableVal* sizes = new TableVal(var_sizes); TableVal* sizes = new TableVal(var_sizes);
PDict<ID>* globals = global_scope()->Vars(); auto globals = global_scope()->Vars();
IterCookie* c = globals->InitForIteration();
ID* id; for ( const auto& global : globals )
while ( (id = globals->NextEntry(c)) ) {
ID* id = global.second;
if ( id->HasVal() ) if ( id->HasVal() )
{ {
Val* id_name = new StringVal(id->Name()); Val* id_name = new StringVal(id->Name());
@ -1907,6 +1907,7 @@ function global_sizes%(%): var_sizes
sizes->Assign(id_name, id_size); sizes->Assign(id_name, id_size);
Unref(id_name); Unref(id_name);
} }
}
return sizes; return sizes;
%} %}
@ -1922,12 +1923,11 @@ function global_sizes%(%): var_sizes
function global_ids%(%): id_table function global_ids%(%): id_table
%{ %{
TableVal* ids = new TableVal(id_table); TableVal* ids = new TableVal(id_table);
PDict<ID>* globals = global_scope()->Vars(); auto globals = global_scope()->Vars();
IterCookie* c = globals->InitForIteration();
ID* id; for ( const auto& global : globals )
while ( (id = globals->NextEntry(c)) )
{ {
ID* id = global.second;
RecordVal* rec = new RecordVal(script_id); RecordVal* rec = new RecordVal(script_id);
rec->Assign(0, new StringVal(type_name(id->Type()->Tag()))); rec->Assign(0, new StringVal(type_name(id->Type()->Tag())));
rec->Assign(1, val_mgr->GetBool(id->IsExport())); rec->Assign(1, val_mgr->GetBool(id->IsExport()));