diff --git a/src/Expr.cc b/src/Expr.cc index 9df823c1a8..f37669eae1 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4624,14 +4624,14 @@ void CallExpr::ExprDescribe(ODesc* d) const args->Describe(d); } -LambdaExpr::LambdaExpr(std::unique_ptr arg_ing, IDPList arg_outer_ids, +LambdaExpr::LambdaExpr(std::unique_ptr 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 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(ingredients->id); - dummy_func->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size, - ingredients->priority); + auto dummy_func = make_intrusive(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 arg_ing, IDPList ar auto v = make_intrusive(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(ingredients->id); - lamb->AddBody(ingredients->body, ingredients->inits, ingredients->frame_size, - ingredients->priority); + auto lamb = make_intrusive(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); diff --git a/src/Expr.h b/src/Expr.h index f73b8f30d8..73658b6e47 100644 --- a/src/Expr.h +++ b/src/Expr.h @@ -27,7 +27,7 @@ namespace detail class Frame; class Scope; -struct function_ingredients; +class FunctionIngredients; using IDPtr = IntrusivePtr; using ScopePtr = IntrusivePtr; @@ -1454,12 +1454,12 @@ protected: class LambdaExpr final : public Expr { public: - LambdaExpr(std::unique_ptr ingredients, IDPList outer_ids, + LambdaExpr(std::unique_ptr 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 ingredients; + std::unique_ptr ingredients; IDPtr lambda_id; IDPList outer_ids; diff --git a/src/Func.cc b/src/Func.cc index c64928fbc9..7cbfaf764a 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -862,16 +862,18 @@ static std::set get_func_groups(const std::vector& 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()->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); } } diff --git a/src/Func.h b/src/Func.h index ef6151829b..a27eb892b7 100644 --- a/src/Func.h +++ b/src/Func.h @@ -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 inits; size_t frame_size = 0; int priority = 0; diff --git a/src/Var.cc b/src/Var.cc index 497cda8e60..587ab9caf4 100644 --- a/src/Var.cc +++ b/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_exprs = Expr::GetNumExprs(); - auto ingredients = std::make_unique(pop_scope(), std::move(body), - module_name); - if ( ! ingredients->id->HasVal() ) + auto ingredients = std::make_unique(pop_scope(), std::move(body), + module_name); + auto id = ingredients->GetID(); + if ( ! id->HasVal() ) { - auto f = make_intrusive(ingredients->id); - ingredients->id->SetVal(make_intrusive(std::move(f))); - ingredients->id->SetConst(); + auto f = make_intrusive(id); + id->SetVal(make_intrusive(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(ingredients->id->GetVal())->AsFuncPtr(); + auto func_ptr = cast_intrusive(id->GetVal())->AsFuncPtr(); auto func = cast_intrusive(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)); diff --git a/src/parse.y b/src/parse.y index 92a22c81d7..e7fe98c074 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1572,9 +1572,9 @@ lambda_body: // Gather the ingredients for a Func from the // current scope. - auto ingredients = std::make_unique( + auto ingredients = std::make_unique( 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)); } diff --git a/src/script_opt/CPP/DeclFunc.cc b/src/script_opt/CPP/DeclFunc.cc index 177e8af91d..f46fe6b76c 100644 --- a/src/script_opt/CPP/DeclFunc.cc +++ b/src/script_opt/CPP/DeclFunc.cc @@ -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 ) diff --git a/src/script_opt/CPP/Driver.cc b/src/script_opt/CPP/Driver.cc index 236baeb0d2..de6396a19a 100644 --- a/src/script_opt/CPP/Driver.cc +++ b/src/script_opt/CPP/Driver.cc @@ -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(); diff --git a/src/script_opt/CPP/GenFunc.cc b/src/script_opt/CPP/GenFunc.cc index f4071c7deb..d6076453cc 100644 --- a/src/script_opt/CPP/GenFunc.cc +++ b/src/script_opt/CPP/GenFunc.cc @@ -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(), pf, lname, body, &ids, FUNC_FLAVOR_FUNCTION); diff --git a/src/script_opt/Expr.cc b/src/script_opt/Expr.cc index ec62756e86..c8ef874880 100644 --- a/src/script_opt/Expr.cc +++ b/src/script_opt/Expr.cc @@ -2415,8 +2415,8 @@ StmtPtr CallExpr::ReduceToSingletons(Reducer* c) ExprPtr LambdaExpr::Duplicate() { - auto ingr = std::make_unique(*ingredients); - ingr->body = ingr->body->Duplicate(); + auto ingr = std::make_unique(*ingredients); + ingr->SetBody(ingr->Body()->Duplicate()); return SetSucc(new LambdaExpr(std::move(ingr), outer_ids)); } diff --git a/src/script_opt/ProfileFunc.cc b/src/script_opt/ProfileFunc.cc index 745ab48f88..1da75df4f0 100644 --- a/src/script_opt/ProfileFunc.cc +++ b/src/script_opt/ProfileFunc.cc @@ -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