Change Scope/Func inits from id_list* to vector<IntrusivePtr<ID>>

This commit is contained in:
Jon Siwek 2020-05-27 17:27:40 -07:00
parent 46e23b49fb
commit 2cee468eac
8 changed files with 48 additions and 91 deletions

View file

@ -4168,29 +4168,6 @@ void CallExpr::ExprDescribe(ODesc* d) const
args->Describe(d); args->Describe(d);
} }
static std::unique_ptr<id_list> shallow_copy_func_inits(const IntrusivePtr<Stmt>& body,
const id_list* src)
{
if ( ! body )
return nullptr;
if ( ! src )
return nullptr;
if ( src->empty() )
return nullptr;
auto dest = std::make_unique<id_list>(src->length());
for ( ID* i : *src )
{
Ref(i);
dest->push_back(i);
}
return dest;
}
LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing, LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing,
id_list arg_outer_ids) : Expr(EXPR_LAMBDA) id_list arg_outer_ids) : Expr(EXPR_LAMBDA)
{ {
@ -4204,7 +4181,7 @@ LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing,
auto dummy_func = make_intrusive<BroFunc>( auto dummy_func = make_intrusive<BroFunc>(
ingredients->id.get(), ingredients->id.get(),
ingredients->body, ingredients->body,
shallow_copy_func_inits(ingredients->body, ingredients->inits).release(), ingredients->inits,
ingredients->frame_size, ingredients->frame_size,
ingredients->priority); ingredients->priority);
@ -4255,7 +4232,7 @@ IntrusivePtr<Val> LambdaExpr::Eval(Frame* f) const
auto lamb = make_intrusive<BroFunc>( auto lamb = make_intrusive<BroFunc>(
ingredients->id.get(), ingredients->id.get(),
ingredients->body, ingredients->body,
shallow_copy_func_inits(ingredients->body, ingredients->inits).release(), ingredients->inits,
ingredients->frame_size, ingredients->frame_size,
ingredients->priority); ingredients->priority);

View file

@ -121,8 +121,9 @@ Func::Func(Kind arg_kind) : kind(arg_kind)
Func::~Func() = default; Func::~Func() = default;
void Func::AddBody(IntrusivePtr<Stmt> /* new_body */, id_list* /* new_inits */, void Func::AddBody(IntrusivePtr<Stmt> /* new_body */,
size_t /* new_frame_size */, int /* priority */) const std::vector<IntrusivePtr<ID>>& /* new_inits */,
size_t /* new_frame_size */, int /* priority */)
{ {
Internal("Func::AddBody called"); Internal("Func::AddBody called");
} }
@ -267,7 +268,8 @@ void Func::CheckPluginResult(bool handled, const IntrusivePtr<Val>& hook_result,
} }
} }
BroFunc::BroFunc(ID* arg_id, IntrusivePtr<Stmt> arg_body, id_list* aggr_inits, BroFunc::BroFunc(ID* arg_id, IntrusivePtr<Stmt> arg_body,
const std::vector<IntrusivePtr<ID>>& aggr_inits,
size_t arg_frame_size, int priority) size_t arg_frame_size, int priority)
: Func(BRO_FUNC) : Func(BRO_FUNC)
{ {
@ -447,7 +449,8 @@ IntrusivePtr<Val> BroFunc::operator()(zeek::Args* args, Frame* parent) const
return result; return result;
} }
void BroFunc::AddBody(IntrusivePtr<Stmt> new_body, id_list* new_inits, void BroFunc::AddBody(IntrusivePtr<Stmt> new_body,
const std::vector<IntrusivePtr<ID>>& new_inits,
size_t new_frame_size, int priority) size_t new_frame_size, int priority)
{ {
if ( new_frame_size > frame_size ) if ( new_frame_size > frame_size )
@ -571,9 +574,10 @@ void BroFunc::Describe(ODesc* d) const
} }
} }
IntrusivePtr<Stmt> BroFunc::AddInits(IntrusivePtr<Stmt> body, id_list* inits) IntrusivePtr<Stmt> BroFunc::AddInits(IntrusivePtr<Stmt> body,
const std::vector<IntrusivePtr<ID>>& inits)
{ {
if ( ! inits || inits->length() == 0 ) if ( inits.empty() )
return body; return body;
auto stmt_series = make_intrusive<StmtList>(); auto stmt_series = make_intrusive<StmtList>();
@ -889,14 +893,6 @@ function_ingredients::function_ingredients(IntrusivePtr<Scope> scope, IntrusiveP
this->body = std::move(body); this->body = std::move(body);
} }
function_ingredients::~function_ingredients()
{
for ( const auto& i : *inits )
Unref(i);
delete inits;
}
BifReturnVal::BifReturnVal(std::nullptr_t) noexcept BifReturnVal::BifReturnVal(std::nullptr_t) noexcept
{ } { }

View file

@ -78,7 +78,8 @@ public:
} }
// Add a new event handler to an existing function (event). // Add a new event handler to an existing function (event).
virtual void AddBody(IntrusivePtr<Stmt> new_body, id_list* new_inits, virtual void AddBody(IntrusivePtr<Stmt> new_body,
const std::vector<IntrusivePtr<ID>>& new_inits,
size_t new_frame_size, int priority = 0); size_t new_frame_size, int priority = 0);
virtual void SetScope(IntrusivePtr<Scope> newscope); virtual void SetScope(IntrusivePtr<Scope> newscope);
@ -128,7 +129,10 @@ protected:
class BroFunc final : public Func { class BroFunc final : public Func {
public: public:
BroFunc(ID* id, IntrusivePtr<Stmt> body, id_list* inits, size_t frame_size, int priority); BroFunc(ID* id, IntrusivePtr<Stmt> body,
const std::vector<IntrusivePtr<ID>>& inits,
size_t frame_size, int priority);
~BroFunc() override; ~BroFunc() override;
bool IsPure() const override; bool IsPure() const override;
@ -163,8 +167,9 @@ public:
*/ */
broker::expected<broker::data> SerializeClosure() const; broker::expected<broker::data> SerializeClosure() const;
void AddBody(IntrusivePtr<Stmt> new_body, id_list* new_inits, void AddBody(IntrusivePtr<Stmt> new_body,
size_t new_frame_size, int priority) override; const std::vector<IntrusivePtr<ID>>& new_inits,
size_t new_frame_size, int priority) override;
/** Sets this function's outer_id list. */ /** Sets this function's outer_id list. */
void SetOuterIDs(id_list ids) void SetOuterIDs(id_list ids)
@ -174,7 +179,8 @@ public:
protected: protected:
BroFunc() : Func(BRO_FUNC) {} BroFunc() : Func(BRO_FUNC) {}
IntrusivePtr<Stmt> AddInits(IntrusivePtr<Stmt> body, id_list* inits); IntrusivePtr<Stmt> AddInits(IntrusivePtr<Stmt> body,
const std::vector<IntrusivePtr<ID>>& inits);
/** /**
* Clones this function along with its closures. * Clones this function along with its closures.
@ -263,11 +269,9 @@ struct function_ingredients {
// to build a function. // to build a function.
function_ingredients(IntrusivePtr<Scope> scope, IntrusivePtr<Stmt> body); function_ingredients(IntrusivePtr<Scope> scope, IntrusivePtr<Stmt> body);
~function_ingredients();
IntrusivePtr<ID> id; IntrusivePtr<ID> id;
IntrusivePtr<Stmt> body; IntrusivePtr<Stmt> body;
id_list* inits; std::vector<IntrusivePtr<ID>> inits;
int frame_size; int frame_size;
int priority; int priority;
IntrusivePtr<Scope> scope; IntrusivePtr<Scope> scope;

View file

@ -21,8 +21,6 @@ Scope::Scope(IntrusivePtr<ID> id,
{ {
return_type = nullptr; return_type = nullptr;
inits = new id_list;
if ( id ) if ( id )
{ {
const auto& id_type = scope_id->GetType(); const auto& id_type = scope_id->GetType();
@ -37,17 +35,6 @@ Scope::Scope(IntrusivePtr<ID> id,
} }
} }
Scope::~Scope()
{
if ( inits )
{
for ( const auto& i : *inits )
Unref(i);
delete inits;
}
}
const IntrusivePtr<ID>& Scope::Find(std::string_view name) const const IntrusivePtr<ID>& Scope::Find(std::string_view name) const
{ {
auto entry = local.find(name); auto entry = local.find(name);
@ -77,11 +64,11 @@ IntrusivePtr<ID> Scope::GenerateTemporary(const char* name)
return make_intrusive<ID>(name, SCOPE_FUNCTION, false); return make_intrusive<ID>(name, SCOPE_FUNCTION, false);
} }
id_list* Scope::GetInits() std::vector<IntrusivePtr<ID>> Scope::GetInits()
{ {
id_list* ids = inits; auto rval = std::move(inits);
inits = nullptr; inits = {};
return ids; return rval;
} }
void Scope::Describe(ODesc* d) const void Scope::Describe(ODesc* d) const

View file

@ -21,7 +21,6 @@ class Scope : public BroObj {
public: public:
explicit Scope(IntrusivePtr<ID> id, explicit Scope(IntrusivePtr<ID> id,
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> al); std::unique_ptr<std::vector<IntrusivePtr<Attr>>> al);
~Scope() override;
const IntrusivePtr<ID>& Find(std::string_view name) const; const IntrusivePtr<ID>& Find(std::string_view name) const;
@ -57,10 +56,11 @@ public:
// 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(); std::vector<IntrusivePtr<ID>> GetInits();
// Adds a variable to the list. // Adds a variable to the list.
void AddInit(IntrusivePtr<ID> id) { inits->push_back(id.release()); } void AddInit(IntrusivePtr<ID> id)
{ inits.emplace_back(std::move(id)); }
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
@ -71,7 +71,7 @@ protected:
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs; std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs;
IntrusivePtr<BroType> return_type; IntrusivePtr<BroType> return_type;
std::map<std::string, IntrusivePtr<ID>, std::less<>> local; std::map<std::string, IntrusivePtr<ID>, std::less<>> local;
id_list* inits; std::vector<IntrusivePtr<ID>> inits;
}; };

View file

@ -1629,19 +1629,12 @@ void EventBodyList::Describe(ODesc* d) const
StmtList::Describe(d); StmtList::Describe(d);
} }
InitStmt::InitStmt(id_list* arg_inits) : Stmt(STMT_INIT) InitStmt::InitStmt(std::vector<IntrusivePtr<ID>> arg_inits) : Stmt(STMT_INIT)
{ {
inits = arg_inits; inits = std::move(arg_inits);
if ( arg_inits && arg_inits->length() )
SetLocationInfo((*arg_inits)[0]->GetLocationInfo());
}
InitStmt::~InitStmt() if ( ! inits.empty() )
{ SetLocationInfo(inits[0]->GetLocationInfo());
for ( const auto& init : *inits )
Unref(init);
delete inits;
} }
IntrusivePtr<Val> InitStmt::Exec(Frame* f, stmt_flow_type& flow) const IntrusivePtr<Val> InitStmt::Exec(Frame* f, stmt_flow_type& flow) const
@ -1649,7 +1642,7 @@ IntrusivePtr<Val> InitStmt::Exec(Frame* f, stmt_flow_type& flow) const
RegisterAccess(); RegisterAccess();
flow = FLOW_NEXT; flow = FLOW_NEXT;
for ( const auto& aggr : *inits ) for ( const auto& aggr : inits )
{ {
const auto& t = aggr->GetType(); const auto& t = aggr->GetType();
@ -1670,7 +1663,7 @@ IntrusivePtr<Val> InitStmt::Exec(Frame* f, stmt_flow_type& flow) const
break; break;
} }
f->SetElement(aggr, std::move(v)); f->SetElement(aggr.get(), std::move(v));
} }
return nullptr; return nullptr;
@ -1681,14 +1674,14 @@ void InitStmt::Describe(ODesc* d) const
AddTag(d); AddTag(d);
if ( ! d->IsReadable() ) if ( ! d->IsReadable() )
d->AddCount(inits->length()); d->AddCount(inits.size());
loop_over_list(*inits, i) for ( size_t i = 0; i < inits.size(); ++i )
{ {
if ( ! d->IsBinary() && i > 0 ) if ( ! d->IsBinary() && i > 0 )
d->AddSP(","); d->AddSP(",");
(*inits)[i]->Describe(d); inits[i]->Describe(d);
} }
DescribeDone(d); DescribeDone(d);
@ -1699,7 +1692,7 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const
TraversalCode tc = cb->PreStmt(this); TraversalCode tc = cb->PreStmt(this);
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);
for ( const auto& init : *inits ) for ( const auto& init : inits )
{ {
tc = init->Traverse(cb); tc = init->Traverse(cb);
HANDLE_TC_STMT_PRE(tc); HANDLE_TC_STMT_PRE(tc);

View file

@ -393,20 +393,19 @@ protected:
class InitStmt final : public Stmt { class InitStmt final : public Stmt {
public: public:
explicit InitStmt(id_list* arg_inits); explicit InitStmt(std::vector<IntrusivePtr<ID>> arg_inits);
~InitStmt() override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override; IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
const id_list* Inits() const { return inits; } const std::vector<IntrusivePtr<ID>>& Inits() const
{ return inits; }
void Describe(ODesc* d) const override; void Describe(ODesc* d) const override;
TraversalCode Traverse(TraversalCallback* cb) const override; TraversalCode Traverse(TraversalCallback* cb) const override;
protected: protected:
id_list* inits; std::vector<IntrusivePtr<ID>> inits;
}; };
class NullStmt final : public Stmt { class NullStmt final : public Stmt {

View file

@ -301,7 +301,8 @@ static void make_var(ID* id, IntrusivePtr<BroType> t, init_class c,
// For events, add a function value (without any body) here so that // For events, add a function value (without any body) here so that
// we can later access the ID even if no implementations have been // we can later access the ID even if no implementations have been
// defined. // defined.
auto f = make_intrusive<BroFunc>(id, nullptr, nullptr, 0, 0); std::vector<IntrusivePtr<ID>> inits;
auto f = make_intrusive<BroFunc>(id, nullptr, inits, 0, 0);
id->SetVal(make_intrusive<Val>(std::move(f))); id->SetVal(make_intrusive<Val>(std::move(f)));
} }
} }