changed function_ingredients struct to FunctionIngredients class with accessors

This commit is contained in:
Vern Paxson 2023-04-02 11:34:16 -07:00
parent 5718046b96
commit 0c434ca4f8
11 changed files with 63 additions and 53 deletions

View file

@ -4624,14 +4624,14 @@ void CallExpr::ExprDescribe(ODesc* d) const
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)
: Expr(EXPR_LAMBDA)
{
ingredients = std::move(arg_ing);
outer_ids = std::move(arg_outer_ids);
SetType(ingredients->id->GetType());
SetType(ingredients->GetID()->GetType());
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
// when broker provides a closure.
auto dummy_func = make_intrusive<ScriptFunc>(ingredients->id);
dummy_func->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size,
ingredients->priority);
auto dummy_func = make_intrusive<ScriptFunc>(ingredients->GetID());
dummy_func->AddBody(ingredients->Body(), ingredients->Inits(), ingredients->FrameSize(),
ingredients->Priority());
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));
lambda_id->SetVal(std::move(v));
lambda_id->SetType(ingredients->id->GetType());
lambda_id->SetType(ingredients->GetID()->GetType());
lambda_id->SetConst();
}
@ -4766,14 +4766,14 @@ bool LambdaExpr::CheckCaptures(StmtPtr when_parent)
ScopePtr LambdaExpr::GetScope() const
{
return ingredients->scope;
return ingredients->Scope();
}
ValPtr LambdaExpr::Eval(Frame* f) const
{
auto lamb = make_intrusive<ScriptFunc>(ingredients->id);
lamb->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size,
ingredients->priority);
auto lamb = make_intrusive<ScriptFunc>(ingredients->GetID());
lamb->AddBody(ingredients->Body(), ingredients->Inits(), ingredients->FrameSize(),
ingredients->Priority());
lamb->CreateCaptures(f);
@ -4787,7 +4787,7 @@ ValPtr LambdaExpr::Eval(Frame* f) const
void LambdaExpr::ExprDescribe(ODesc* d) const
{
d->Add(expr_name(Tag()));
ingredients->body->Describe(d);
ingredients->Body()->Describe(d);
}
TraversalCode LambdaExpr::Traverse(TraversalCallback* cb) const
@ -4802,7 +4802,7 @@ TraversalCode LambdaExpr::Traverse(TraversalCallback* cb) const
tc = lambda_id->Traverse(cb);
HANDLE_TC_EXPR_PRE(tc);
tc = ingredients->body->Traverse(cb);
tc = ingredients->Body()->Traverse(cb);
HANDLE_TC_EXPR_PRE(tc);
tc = cb->PostExpr(this);

View file

@ -27,7 +27,7 @@ namespace detail
class Frame;
class Scope;
struct function_ingredients;
class FunctionIngredients;
using IDPtr = IntrusivePtr<ID>;
using ScopePtr = IntrusivePtr<Scope>;
@ -1454,12 +1454,12 @@ protected:
class LambdaExpr final : public Expr
{
public:
LambdaExpr(std::unique_ptr<function_ingredients> ingredients, IDPList outer_ids,
LambdaExpr(std::unique_ptr<FunctionIngredients> ingredients, IDPList outer_ids,
StmtPtr when_parent = nullptr);
const std::string& Name() const { return my_name; }
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;
TraversalCode Traverse(TraversalCallback* cb) const override;
@ -1478,7 +1478,7 @@ protected:
private:
bool CheckCaptures(StmtPtr when_parent);
std::unique_ptr<function_ingredients> ingredients;
std::unique_ptr<FunctionIngredients> ingredients;
IDPtr lambda_id;
IDPList outer_ids;

View file

@ -862,16 +862,18 @@ static std::set<EventGroupPtr> get_func_groups(const std::vector<AttrPtr>& attrs
return groups;
}
function_ingredients::function_ingredients(ScopePtr scope, StmtPtr body,
const std::string& module_name)
FunctionIngredients::FunctionIngredients(ScopePtr _scope, StmtPtr _body,
const std::string& module_name)
{
scope = std::move(_scope);
body = std::move(_body);
frame_size = scope->Length();
inits = scope->GetInits();
this->scope = std::move(scope);
id = this->scope->GetID();
id = scope->GetID();
const auto& attrs = this->scope->Attrs();
const auto& attrs = scope->Attrs();
if ( attrs )
{
@ -890,15 +892,11 @@ function_ingredients::function_ingredients(ScopePtr scope, StmtPtr body,
else
priority = 0;
this->body = std::move(body);
this->module_name = module_name;
// Implicit module event groups for events and hooks.
auto flavor = id->GetType<zeek::FuncType>()->Flavor();
if ( flavor == FUNC_FLAVOR_EVENT || flavor == FUNC_FLAVOR_HOOK )
{
auto module_group = event_registry->RegisterGroup(EventGroupKind::Module,
this->module_name);
auto module_group = event_registry->RegisterGroup(EventGroupKind::Module, module_name);
groups.insert(module_group);
}
}

View file

@ -334,16 +334,27 @@ struct CallInfo
// Struct that collects all the specifics defining a Func. Used for ScriptFuncs
// with closures.
struct function_ingredients
class FunctionIngredients
{
public:
// Gathers all of the information from a scope and a function body needed
// 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;
StmtPtr body;
std::string module_name; // current module name where function body is defined
std::vector<IDPtr> inits;
size_t frame_size = 0;
int priority = 0;

View file

@ -842,24 +842,25 @@ void end_func(StmtPtr body, const char* module_name, bool free_of_conditionals)
oi->num_stmts = Stmt::GetNumStmts();
oi->num_exprs = Expr::GetNumExprs();
auto ingredients = std::make_unique<function_ingredients>(pop_scope(), std::move(body),
module_name);
if ( ! ingredients->id->HasVal() )
auto ingredients = std::make_unique<FunctionIngredients>(pop_scope(), std::move(body),
module_name);
auto id = ingredients->GetID();
if ( ! id->HasVal() )
{
auto f = make_intrusive<ScriptFunc>(ingredients->id);
ingredients->id->SetVal(make_intrusive<FuncVal>(std::move(f)));
ingredients->id->SetConst();
auto f = make_intrusive<ScriptFunc>(id);
id->SetVal(make_intrusive<FuncVal>(std::move(f)));
id->SetConst();
}
ingredients->id->GetVal()->AsFunc()->AddBody(ingredients->body, ingredients->inits,
ingredients->frame_size, ingredients->priority,
ingredients->groups);
id->GetVal()->AsFunc()->AddBody(ingredients->Body(), ingredients->Inits(),
ingredients->FrameSize(), ingredients->Priority(),
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);
func->SetScope(ingredients->scope);
func->SetScope(ingredients->Scope());
for ( const auto& group : ingredients->groups )
for ( const auto& group : ingredients->Groups() )
group->AddFunc(func);
analyze_func(std::move(func));

View file

@ -1572,9 +1572,9 @@ lambda_body:
// Gather the ingredients for a Func from the
// current scope.
auto ingredients = std::make_unique<function_ingredients>(
auto ingredients = std::make_unique<FunctionIngredients>(
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));
}

View file

@ -29,8 +29,8 @@ void CPPCompile::DeclareLambda(const LambdaExpr* l, const ProfileFunc* pf)
ASSERT(is_CPP_compilable(pf));
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
auto body = l->Ingredients().body;
auto l_id = l->Ingredients().id;
auto body = l->Ingredients().Body();
auto l_id = l->Ingredients().GetID();
auto& ids = l->OuterIDs();
for ( auto id : ids )

View file

@ -150,7 +150,7 @@ void CPPCompile::Compile(bool report_uncompilable)
for ( const auto& l : pfs.Lambdas() )
{
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 )
// Reuse previous body.
body_names[body] = body_names[lambda_ASTs[n]];
@ -176,7 +176,7 @@ void CPPCompile::Compile(bool report_uncompilable)
continue;
CompileLambda(l, pfs.ExprProf(l).get());
lambda_ASTs[n] = l->Ingredients().body.get();
lambda_ASTs[n] = l->Ingredients().Body().get();
}
NL();

View file

@ -23,8 +23,8 @@ void CPPCompile::CompileFunc(const FuncInfo& func)
void CPPCompile::CompileLambda(const LambdaExpr* l, const ProfileFunc* pf)
{
auto lname = Canonicalize(l->Name().c_str()) + "_lb";
auto body = l->Ingredients().body;
auto l_id = l->Ingredients().id;
auto body = l->Ingredients().Body();
auto l_id = l->Ingredients().GetID();
auto& ids = l->OuterIDs();
DefineBody(l_id->GetType<FuncType>(), pf, lname, body, &ids, FUNC_FLAVOR_FUNCTION);

View file

@ -2415,8 +2415,8 @@ StmtPtr CallExpr::ReduceToSingletons(Reducer* c)
ExprPtr LambdaExpr::Duplicate()
{
auto ingr = std::make_unique<function_ingredients>(*ingredients);
ingr->body = ingr->body->Duplicate();
auto ingr = std::make_unique<FunctionIngredients>(*ingredients);
ingr->SetBody(ingr->Body()->Duplicate());
return SetSucc(new LambdaExpr(std::move(ingr), outer_ids));
}

View file

@ -51,7 +51,7 @@ ProfileFunc::ProfileFunc(const Expr* e, bool _abs_rec_fields)
for ( auto oid : func->OuterIDs() )
captures.insert(oid);
Profile(func->GetType()->AsFuncType(), func->Ingredients().body);
Profile(func->GetType()->AsFuncType(), func->Ingredients().Body());
}
else