mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
changed function_ingredients struct to FunctionIngredients class with accessors
This commit is contained in:
parent
5718046b96
commit
0c434ca4f8
11 changed files with 63 additions and 53 deletions
24
src/Expr.cc
24
src/Expr.cc
|
@ -4624,14 +4624,14 @@ void CallExpr::ExprDescribe(ODesc* d) const
|
||||||
args->Describe(d);
|
args->Describe(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing, IDPList arg_outer_ids,
|
LambdaExpr::LambdaExpr(std::unique_ptr<FunctionIngredients> arg_ing, IDPList arg_outer_ids,
|
||||||
StmtPtr when_parent)
|
StmtPtr when_parent)
|
||||||
: Expr(EXPR_LAMBDA)
|
: Expr(EXPR_LAMBDA)
|
||||||
{
|
{
|
||||||
ingredients = std::move(arg_ing);
|
ingredients = std::move(arg_ing);
|
||||||
outer_ids = std::move(arg_outer_ids);
|
outer_ids = std::move(arg_outer_ids);
|
||||||
|
|
||||||
SetType(ingredients->id->GetType());
|
SetType(ingredients->GetID()->GetType());
|
||||||
|
|
||||||
if ( ! CheckCaptures(when_parent) )
|
if ( ! CheckCaptures(when_parent) )
|
||||||
{
|
{
|
||||||
|
@ -4641,9 +4641,9 @@ LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing, IDPList ar
|
||||||
|
|
||||||
// Install a dummy version of the function globally for use only
|
// Install a dummy version of the function globally for use only
|
||||||
// when broker provides a closure.
|
// when broker provides a closure.
|
||||||
auto dummy_func = make_intrusive<ScriptFunc>(ingredients->id);
|
auto dummy_func = make_intrusive<ScriptFunc>(ingredients->GetID());
|
||||||
dummy_func->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size,
|
dummy_func->AddBody(ingredients->Body(), ingredients->Inits(), ingredients->FrameSize(),
|
||||||
ingredients->priority);
|
ingredients->Priority());
|
||||||
|
|
||||||
dummy_func->SetOuterIDs(outer_ids);
|
dummy_func->SetOuterIDs(outer_ids);
|
||||||
|
|
||||||
|
@ -4678,7 +4678,7 @@ LambdaExpr::LambdaExpr(std::unique_ptr<function_ingredients> arg_ing, IDPList ar
|
||||||
|
|
||||||
auto v = make_intrusive<FuncVal>(std::move(dummy_func));
|
auto v = make_intrusive<FuncVal>(std::move(dummy_func));
|
||||||
lambda_id->SetVal(std::move(v));
|
lambda_id->SetVal(std::move(v));
|
||||||
lambda_id->SetType(ingredients->id->GetType());
|
lambda_id->SetType(ingredients->GetID()->GetType());
|
||||||
lambda_id->SetConst();
|
lambda_id->SetConst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4766,14 +4766,14 @@ bool LambdaExpr::CheckCaptures(StmtPtr when_parent)
|
||||||
|
|
||||||
ScopePtr LambdaExpr::GetScope() const
|
ScopePtr LambdaExpr::GetScope() const
|
||||||
{
|
{
|
||||||
return ingredients->scope;
|
return ingredients->Scope();
|
||||||
}
|
}
|
||||||
|
|
||||||
ValPtr LambdaExpr::Eval(Frame* f) const
|
ValPtr LambdaExpr::Eval(Frame* f) const
|
||||||
{
|
{
|
||||||
auto lamb = make_intrusive<ScriptFunc>(ingredients->id);
|
auto lamb = make_intrusive<ScriptFunc>(ingredients->GetID());
|
||||||
lamb->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size,
|
lamb->AddBody(ingredients->Body(), ingredients->Inits(), ingredients->FrameSize(),
|
||||||
ingredients->priority);
|
ingredients->Priority());
|
||||||
|
|
||||||
lamb->CreateCaptures(f);
|
lamb->CreateCaptures(f);
|
||||||
|
|
||||||
|
@ -4787,7 +4787,7 @@ ValPtr LambdaExpr::Eval(Frame* f) const
|
||||||
void LambdaExpr::ExprDescribe(ODesc* d) const
|
void LambdaExpr::ExprDescribe(ODesc* d) const
|
||||||
{
|
{
|
||||||
d->Add(expr_name(Tag()));
|
d->Add(expr_name(Tag()));
|
||||||
ingredients->body->Describe(d);
|
ingredients->Body()->Describe(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
TraversalCode LambdaExpr::Traverse(TraversalCallback* cb) const
|
TraversalCode LambdaExpr::Traverse(TraversalCallback* cb) const
|
||||||
|
@ -4802,7 +4802,7 @@ TraversalCode LambdaExpr::Traverse(TraversalCallback* cb) const
|
||||||
tc = lambda_id->Traverse(cb);
|
tc = lambda_id->Traverse(cb);
|
||||||
HANDLE_TC_EXPR_PRE(tc);
|
HANDLE_TC_EXPR_PRE(tc);
|
||||||
|
|
||||||
tc = ingredients->body->Traverse(cb);
|
tc = ingredients->Body()->Traverse(cb);
|
||||||
HANDLE_TC_EXPR_PRE(tc);
|
HANDLE_TC_EXPR_PRE(tc);
|
||||||
|
|
||||||
tc = cb->PostExpr(this);
|
tc = cb->PostExpr(this);
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace detail
|
||||||
|
|
||||||
class Frame;
|
class Frame;
|
||||||
class Scope;
|
class Scope;
|
||||||
struct function_ingredients;
|
class FunctionIngredients;
|
||||||
using IDPtr = IntrusivePtr<ID>;
|
using IDPtr = IntrusivePtr<ID>;
|
||||||
using ScopePtr = IntrusivePtr<Scope>;
|
using ScopePtr = IntrusivePtr<Scope>;
|
||||||
|
|
||||||
|
@ -1454,12 +1454,12 @@ protected:
|
||||||
class LambdaExpr final : public Expr
|
class LambdaExpr final : public Expr
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LambdaExpr(std::unique_ptr<function_ingredients> ingredients, IDPList outer_ids,
|
LambdaExpr(std::unique_ptr<FunctionIngredients> ingredients, IDPList outer_ids,
|
||||||
StmtPtr when_parent = nullptr);
|
StmtPtr when_parent = nullptr);
|
||||||
|
|
||||||
const std::string& Name() const { return my_name; }
|
const std::string& Name() const { return my_name; }
|
||||||
const IDPList& OuterIDs() const { return outer_ids; }
|
const IDPList& OuterIDs() const { return outer_ids; }
|
||||||
const function_ingredients& Ingredients() const { return *ingredients; }
|
const FunctionIngredients& Ingredients() const { return *ingredients; }
|
||||||
|
|
||||||
ValPtr Eval(Frame* f) const override;
|
ValPtr Eval(Frame* f) const override;
|
||||||
TraversalCode Traverse(TraversalCallback* cb) const override;
|
TraversalCode Traverse(TraversalCallback* cb) const override;
|
||||||
|
@ -1478,7 +1478,7 @@ protected:
|
||||||
private:
|
private:
|
||||||
bool CheckCaptures(StmtPtr when_parent);
|
bool CheckCaptures(StmtPtr when_parent);
|
||||||
|
|
||||||
std::unique_ptr<function_ingredients> ingredients;
|
std::unique_ptr<FunctionIngredients> ingredients;
|
||||||
IDPtr lambda_id;
|
IDPtr lambda_id;
|
||||||
IDPList outer_ids;
|
IDPList outer_ids;
|
||||||
|
|
||||||
|
|
18
src/Func.cc
18
src/Func.cc
|
@ -862,16 +862,18 @@ static std::set<EventGroupPtr> get_func_groups(const std::vector<AttrPtr>& attrs
|
||||||
return groups;
|
return groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
function_ingredients::function_ingredients(ScopePtr scope, StmtPtr body,
|
FunctionIngredients::FunctionIngredients(ScopePtr _scope, StmtPtr _body,
|
||||||
const std::string& module_name)
|
const std::string& module_name)
|
||||||
{
|
{
|
||||||
|
scope = std::move(_scope);
|
||||||
|
body = std::move(_body);
|
||||||
|
|
||||||
frame_size = scope->Length();
|
frame_size = scope->Length();
|
||||||
inits = scope->GetInits();
|
inits = scope->GetInits();
|
||||||
|
|
||||||
this->scope = std::move(scope);
|
id = scope->GetID();
|
||||||
id = this->scope->GetID();
|
|
||||||
|
|
||||||
const auto& attrs = this->scope->Attrs();
|
const auto& attrs = scope->Attrs();
|
||||||
|
|
||||||
if ( attrs )
|
if ( attrs )
|
||||||
{
|
{
|
||||||
|
@ -890,15 +892,11 @@ function_ingredients::function_ingredients(ScopePtr scope, StmtPtr body,
|
||||||
else
|
else
|
||||||
priority = 0;
|
priority = 0;
|
||||||
|
|
||||||
this->body = std::move(body);
|
|
||||||
this->module_name = module_name;
|
|
||||||
|
|
||||||
// Implicit module event groups for events and hooks.
|
// Implicit module event groups for events and hooks.
|
||||||
auto flavor = id->GetType<zeek::FuncType>()->Flavor();
|
auto flavor = id->GetType<zeek::FuncType>()->Flavor();
|
||||||
if ( flavor == FUNC_FLAVOR_EVENT || flavor == FUNC_FLAVOR_HOOK )
|
if ( flavor == FUNC_FLAVOR_EVENT || flavor == FUNC_FLAVOR_HOOK )
|
||||||
{
|
{
|
||||||
auto module_group = event_registry->RegisterGroup(EventGroupKind::Module,
|
auto module_group = event_registry->RegisterGroup(EventGroupKind::Module, module_name);
|
||||||
this->module_name);
|
|
||||||
groups.insert(module_group);
|
groups.insert(module_group);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
19
src/Func.h
19
src/Func.h
|
@ -334,16 +334,27 @@ struct CallInfo
|
||||||
|
|
||||||
// Struct that collects all the specifics defining a Func. Used for ScriptFuncs
|
// Struct that collects all the specifics defining a Func. Used for ScriptFuncs
|
||||||
// with closures.
|
// with closures.
|
||||||
struct function_ingredients
|
class FunctionIngredients
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
// Gathers all of the information from a scope and a function body needed
|
// Gathers all of the information from a scope and a function body needed
|
||||||
// to build a function.
|
// to build a function.
|
||||||
function_ingredients(ScopePtr scope, StmtPtr body, const std::string& module_name);
|
FunctionIngredients(ScopePtr scope, StmtPtr body, const std::string& module_name);
|
||||||
|
|
||||||
|
const IDPtr& GetID() const { return id; }
|
||||||
|
|
||||||
|
const StmtPtr& Body() const { return body; }
|
||||||
|
void SetBody(StmtPtr _body) { body = std::move(_body); }
|
||||||
|
|
||||||
|
const auto& Inits() const { return inits; }
|
||||||
|
size_t FrameSize() const { return frame_size; }
|
||||||
|
int Priority() const { return priority; }
|
||||||
|
const ScopePtr& Scope() const { return scope; }
|
||||||
|
const auto& Groups() const { return groups; }
|
||||||
|
|
||||||
|
private:
|
||||||
IDPtr id;
|
IDPtr id;
|
||||||
StmtPtr body;
|
StmtPtr body;
|
||||||
std::string module_name; // current module name where function body is defined
|
|
||||||
std::vector<IDPtr> inits;
|
std::vector<IDPtr> inits;
|
||||||
size_t frame_size = 0;
|
size_t frame_size = 0;
|
||||||
int priority = 0;
|
int priority = 0;
|
||||||
|
|
25
src/Var.cc
25
src/Var.cc
|
@ -842,24 +842,25 @@ void end_func(StmtPtr body, const char* module_name, bool free_of_conditionals)
|
||||||
oi->num_stmts = Stmt::GetNumStmts();
|
oi->num_stmts = Stmt::GetNumStmts();
|
||||||
oi->num_exprs = Expr::GetNumExprs();
|
oi->num_exprs = Expr::GetNumExprs();
|
||||||
|
|
||||||
auto ingredients = std::make_unique<function_ingredients>(pop_scope(), std::move(body),
|
auto ingredients = std::make_unique<FunctionIngredients>(pop_scope(), std::move(body),
|
||||||
module_name);
|
module_name);
|
||||||
if ( ! ingredients->id->HasVal() )
|
auto id = ingredients->GetID();
|
||||||
|
if ( ! id->HasVal() )
|
||||||
{
|
{
|
||||||
auto f = make_intrusive<ScriptFunc>(ingredients->id);
|
auto f = make_intrusive<ScriptFunc>(id);
|
||||||
ingredients->id->SetVal(make_intrusive<FuncVal>(std::move(f)));
|
id->SetVal(make_intrusive<FuncVal>(std::move(f)));
|
||||||
ingredients->id->SetConst();
|
id->SetConst();
|
||||||
}
|
}
|
||||||
|
|
||||||
ingredients->id->GetVal()->AsFunc()->AddBody(ingredients->body, ingredients->inits,
|
id->GetVal()->AsFunc()->AddBody(ingredients->Body(), ingredients->Inits(),
|
||||||
ingredients->frame_size, ingredients->priority,
|
ingredients->FrameSize(), ingredients->Priority(),
|
||||||
ingredients->groups);
|
ingredients->Groups());
|
||||||
|
|
||||||
auto func_ptr = cast_intrusive<FuncVal>(ingredients->id->GetVal())->AsFuncPtr();
|
auto func_ptr = cast_intrusive<FuncVal>(id->GetVal())->AsFuncPtr();
|
||||||
auto func = cast_intrusive<ScriptFunc>(func_ptr);
|
auto func = cast_intrusive<ScriptFunc>(func_ptr);
|
||||||
func->SetScope(ingredients->scope);
|
func->SetScope(ingredients->Scope());
|
||||||
|
|
||||||
for ( const auto& group : ingredients->groups )
|
for ( const auto& group : ingredients->Groups() )
|
||||||
group->AddFunc(func);
|
group->AddFunc(func);
|
||||||
|
|
||||||
analyze_func(std::move(func));
|
analyze_func(std::move(func));
|
||||||
|
|
|
@ -1572,9 +1572,9 @@ lambda_body:
|
||||||
|
|
||||||
// Gather the ingredients for a Func from the
|
// Gather the ingredients for a Func from the
|
||||||
// current scope.
|
// current scope.
|
||||||
auto ingredients = std::make_unique<function_ingredients>(
|
auto ingredients = std::make_unique<FunctionIngredients>(
|
||||||
current_scope(), IntrusivePtr{AdoptRef{}, $3}, current_module.c_str());
|
current_scope(), IntrusivePtr{AdoptRef{}, $3}, current_module.c_str());
|
||||||
auto outer_ids = gather_outer_ids(pop_scope(), ingredients->body);
|
auto outer_ids = gather_outer_ids(pop_scope(), ingredients->Body());
|
||||||
|
|
||||||
$$ = new LambdaExpr(std::move(ingredients), std::move(outer_ids));
|
$$ = new LambdaExpr(std::move(ingredients), std::move(outer_ids));
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,8 +29,8 @@ void CPPCompile::DeclareLambda(const LambdaExpr* l, const ProfileFunc* pf)
|
||||||
ASSERT(is_CPP_compilable(pf));
|
ASSERT(is_CPP_compilable(pf));
|
||||||
|
|
||||||
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
|
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
|
||||||
auto body = l->Ingredients().body;
|
auto body = l->Ingredients().Body();
|
||||||
auto l_id = l->Ingredients().id;
|
auto l_id = l->Ingredients().GetID();
|
||||||
auto& ids = l->OuterIDs();
|
auto& ids = l->OuterIDs();
|
||||||
|
|
||||||
for ( auto id : ids )
|
for ( auto id : ids )
|
||||||
|
|
|
@ -150,7 +150,7 @@ void CPPCompile::Compile(bool report_uncompilable)
|
||||||
for ( const auto& l : pfs.Lambdas() )
|
for ( const auto& l : pfs.Lambdas() )
|
||||||
{
|
{
|
||||||
const auto& n = l->Name();
|
const auto& n = l->Name();
|
||||||
const auto body = l->Ingredients().body.get();
|
const auto body = l->Ingredients().Body().get();
|
||||||
if ( lambda_ASTs.count(n) > 0 )
|
if ( lambda_ASTs.count(n) > 0 )
|
||||||
// Reuse previous body.
|
// Reuse previous body.
|
||||||
body_names[body] = body_names[lambda_ASTs[n]];
|
body_names[body] = body_names[lambda_ASTs[n]];
|
||||||
|
@ -176,7 +176,7 @@ void CPPCompile::Compile(bool report_uncompilable)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
CompileLambda(l, pfs.ExprProf(l).get());
|
CompileLambda(l, pfs.ExprProf(l).get());
|
||||||
lambda_ASTs[n] = l->Ingredients().body.get();
|
lambda_ASTs[n] = l->Ingredients().Body().get();
|
||||||
}
|
}
|
||||||
|
|
||||||
NL();
|
NL();
|
||||||
|
|
|
@ -23,8 +23,8 @@ void CPPCompile::CompileFunc(const FuncInfo& func)
|
||||||
void CPPCompile::CompileLambda(const LambdaExpr* l, const ProfileFunc* pf)
|
void CPPCompile::CompileLambda(const LambdaExpr* l, const ProfileFunc* pf)
|
||||||
{
|
{
|
||||||
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
|
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
|
||||||
auto body = l->Ingredients().body;
|
auto body = l->Ingredients().Body();
|
||||||
auto l_id = l->Ingredients().id;
|
auto l_id = l->Ingredients().GetID();
|
||||||
auto& ids = l->OuterIDs();
|
auto& ids = l->OuterIDs();
|
||||||
|
|
||||||
DefineBody(l_id->GetType<FuncType>(), pf, lname, body, &ids, FUNC_FLAVOR_FUNCTION);
|
DefineBody(l_id->GetType<FuncType>(), pf, lname, body, &ids, FUNC_FLAVOR_FUNCTION);
|
||||||
|
|
|
@ -2415,8 +2415,8 @@ StmtPtr CallExpr::ReduceToSingletons(Reducer* c)
|
||||||
|
|
||||||
ExprPtr LambdaExpr::Duplicate()
|
ExprPtr LambdaExpr::Duplicate()
|
||||||
{
|
{
|
||||||
auto ingr = std::make_unique<function_ingredients>(*ingredients);
|
auto ingr = std::make_unique<FunctionIngredients>(*ingredients);
|
||||||
ingr->body = ingr->body->Duplicate();
|
ingr->SetBody(ingr->Body()->Duplicate());
|
||||||
return SetSucc(new LambdaExpr(std::move(ingr), outer_ids));
|
return SetSucc(new LambdaExpr(std::move(ingr), outer_ids));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ ProfileFunc::ProfileFunc(const Expr* e, bool _abs_rec_fields)
|
||||||
for ( auto oid : func->OuterIDs() )
|
for ( auto oid : func->OuterIDs() )
|
||||||
captures.insert(oid);
|
captures.insert(oid);
|
||||||
|
|
||||||
Profile(func->GetType()->AsFuncType(), func->Ingredients().body);
|
Profile(func->GetType()->AsFuncType(), func->Ingredients().Body());
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue