simplify and tidy up some interfaces

This commit is contained in:
Vern Paxson 2021-02-27 11:07:39 -08:00
parent d38cc04e83
commit 64ef7f0eb2
3 changed files with 14 additions and 26 deletions

View file

@ -15,17 +15,6 @@
namespace zeek::detail { namespace zeek::detail {
Reducer::Reducer(Scope* s)
{
scope = s;
}
Reducer::~Reducer()
{
for ( int i = 0; i < temps.length(); ++i )
delete temps[i];
}
ExprPtr Reducer::GenTemporaryExpr(const TypePtr& t, ExprPtr rhs) ExprPtr Reducer::GenTemporaryExpr(const TypePtr& t, ExprPtr rhs)
{ {
auto e = make_intrusive<NameExpr>(GenTemporary(t, rhs)); auto e = make_intrusive<NameExpr>(GenTemporary(t, rhs));
@ -38,7 +27,7 @@ NameExprPtr Reducer::UpdateName(NameExprPtr n)
if ( NameIsReduced(n.get()) ) if ( NameIsReduced(n.get()) )
return n; return n;
return make_intrusive<NameExpr>(FindNewLocal(n.get())); return make_intrusive<NameExpr>(FindNewLocal(n));
} }
bool Reducer::NameIsReduced(const NameExpr* n) const bool Reducer::NameIsReduced(const NameExpr* n) const
@ -80,7 +69,7 @@ bool Reducer::IDsAreReduced(const IDPList* ids) const
bool Reducer::IDsAreReduced(const std::vector<IDPtr>& ids) const bool Reducer::IDsAreReduced(const std::vector<IDPtr>& ids) const
{ {
for ( auto& id : ids ) for ( const auto& id : ids )
if ( ! ID_IsReduced(id) ) if ( ! ID_IsReduced(id) )
return false; return false;
@ -92,7 +81,7 @@ IDPtr Reducer::UpdateID(IDPtr id)
if ( ID_IsReduced(id) ) if ( ID_IsReduced(id) )
return id; return id;
return FindNewLocal(id.get()); return FindNewLocal(id);
} }
bool Reducer::ID_IsReduced(const ID* id) const bool Reducer::ID_IsReduced(const ID* id) const
@ -103,7 +92,7 @@ bool Reducer::ID_IsReduced(const ID* id) const
NameExprPtr Reducer::GenInlineBlockName(IDPtr id) NameExprPtr Reducer::GenInlineBlockName(IDPtr id)
{ {
return make_intrusive<NameExpr>(GenLocal(id.get())); return make_intrusive<NameExpr>(GenLocal(id));
} }
NameExprPtr Reducer::PushInlineBlock(TypePtr type) NameExprPtr Reducer::PushInlineBlock(TypePtr type)
@ -155,9 +144,9 @@ IDPtr Reducer::GenTemporary(const TypePtr& t, ExprPtr rhs)
return temp_id; return temp_id;
} }
IDPtr Reducer::FindNewLocal(ID* id) IDPtr Reducer::FindNewLocal(const IDPtr& id)
{ {
auto mapping = orig_to_new_locals.find(id); auto mapping = orig_to_new_locals.find(id.get());
if ( mapping != orig_to_new_locals.end() ) if ( mapping != orig_to_new_locals.end() )
return mapping->second; return mapping->second;
@ -165,7 +154,7 @@ IDPtr Reducer::FindNewLocal(ID* id)
return GenLocal(id); return GenLocal(id);
} }
IDPtr Reducer::GenLocal(ID* orig) IDPtr Reducer::GenLocal(const IDPtr& orig)
{ {
if ( Optimizing() ) if ( Optimizing() )
reporter->InternalError("Generating a new local while optimizing"); reporter->InternalError("Generating a new local while optimizing");
@ -179,7 +168,7 @@ IDPtr Reducer::GenLocal(ID* orig)
local_id->SetAttrs(orig->GetAttrs()); local_id->SetAttrs(orig->GetAttrs());
new_locals.insert(local_id.get()); new_locals.insert(local_id.get());
orig_to_new_locals[orig] = local_id; orig_to_new_locals[orig.get()] = local_id;
return local_id; return local_id;
} }

View file

@ -15,8 +15,7 @@ class ProfileFunc;
class Reducer { class Reducer {
public: public:
Reducer(Scope* s); Reducer() { }
~Reducer();
StmtPtr Reduce(StmtPtr s) StmtPtr Reduce(StmtPtr s)
{ {
@ -160,15 +159,15 @@ protected:
// Retrieve the identifier corresponding to the new local for // Retrieve the identifier corresponding to the new local for
// the given expression. Creates the local if necessary. // the given expression. Creates the local if necessary.
IDPtr FindNewLocal(ID* id); IDPtr FindNewLocal(const IDPtr& id);
IDPtr FindNewLocal(const NameExpr* n) IDPtr FindNewLocal(const NameExprPtr& n)
{ return FindNewLocal(n->Id()); } { return FindNewLocal(n->IdPtr()); }
// Generate a new local to use in lieu of the original (seen // Generate a new local to use in lieu of the original (seen
// in an inlined block). The difference is that the new // in an inlined block). The difference is that the new
// version has a distinct name and has a correct frame offset // version has a distinct name and has a correct frame offset
// for the current function. // for the current function.
IDPtr GenLocal(ID* orig); IDPtr GenLocal(const IDPtr& orig);
// Track that we're replacing instances of "orig" with a new // Track that we're replacing instances of "orig" with a new
// expression. This allows us to locate the RDs associated // expression. This allows us to locate the RDs associated

View file

@ -49,7 +49,7 @@ void optimize_func(ScriptFunc* f, std::shared_ptr<ProfileFunc> pf,
auto scope = scope_ptr.release(); auto scope = scope_ptr.release();
push_existing_scope(scope); push_existing_scope(scope);
auto rc = std::make_shared<Reducer>(scope); auto rc = std::make_shared<Reducer>();
auto new_body = rc->Reduce(body); auto new_body = rc->Reduce(body);
if ( reporter->Errors() > 0 ) if ( reporter->Errors() > 0 )