From efd03d41f8364fe7f5c1207846452180be7af4d2 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sun, 30 May 2021 17:20:29 -0700 Subject: [PATCH] convert scopes to be managed using IntrusivePtr's --- src/Debug.cc | 3 --- src/DebugCmds.cc | 2 +- src/Expr.cc | 4 ++-- src/Expr.h | 3 ++- src/Func.cc | 4 ++-- src/Func.h | 4 ++-- src/Scope.cc | 23 ++++++++++------------- src/Scope.h | 6 +++--- src/Stmt.cc | 2 +- src/Traverse.h | 4 ++-- src/Var.cc | 6 +++--- src/Var.h | 3 ++- src/parse.y | 5 ++--- src/script_opt/GenRDs.cc | 2 +- src/script_opt/GenRDs.h | 7 ++----- src/script_opt/ScriptOpt.cc | 11 ++++++----- 16 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/Debug.cc b/src/Debug.cc index db4f487b75..f35521a0bf 100644 --- a/src/Debug.cc +++ b/src/Debug.cc @@ -972,10 +972,7 @@ ValPtr dbg_eval_expr(const char* expr) const ScriptFunc* func = frame->GetFunction(); if ( func ) - { - Ref(func->GetScope()); push_existing_scope(func->GetScope()); - } // ### Possibly push a debugger-local scope? diff --git a/src/DebugCmds.cc b/src/DebugCmds.cc index 64d9050278..9d02afb0c4 100644 --- a/src/DebugCmds.cc +++ b/src/DebugCmds.cc @@ -61,7 +61,7 @@ static void lookup_global_symbols_regex(const string& orig_regex, vector& m return; } - Scope* global = global_scope(); + auto global = global_scope(); const auto& syms = global->Vars(); ID* nextid; diff --git a/src/Expr.cc b/src/Expr.cc index 0550720ec0..46b8ade8ac 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -4554,9 +4554,9 @@ void LambdaExpr::CheckCaptures() } } -Scope* LambdaExpr::GetScope() const +ScopePtr LambdaExpr::GetScope() const { - return ingredients->scope.get(); + return ingredients->scope; } ValPtr LambdaExpr::Eval(Frame* f) const diff --git a/src/Expr.h b/src/Expr.h index d7d64ceb99..bc5ea43403 100644 --- a/src/Expr.h +++ b/src/Expr.h @@ -27,6 +27,7 @@ class Frame; class Scope; struct function_ingredients; using IDPtr = IntrusivePtr; +using ScopePtr = IntrusivePtr; enum BroExprTag : int { EXPR_ANY = -1, @@ -1341,7 +1342,7 @@ public: ValPtr Eval(Frame* f) const override; TraversalCode Traverse(TraversalCallback* cb) const override; - Scope* GetScope() const; + ScopePtr GetScope() const; // Optimization-related: ExprPtr Duplicate() override; diff --git a/src/Func.cc b/src/Func.cc index 7464f76ece..4859ab7aad 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -201,8 +201,8 @@ void Func::DescribeDebug(ODesc* d, const Args* args) const detail::TraversalCode Func::Traverse(detail::TraversalCallback* cb) const { // FIXME: Make a fake scope for builtins? - detail::Scope* old_scope = cb->current_scope; - cb->current_scope = scope.get(); + auto old_scope = cb->current_scope; + cb->current_scope = scope; detail::TraversalCode tc = cb->PreFunction(this); HANDLE_TC_STMT_PRE(tc); diff --git a/src/Func.h b/src/Func.h index c1871270cb..7f42286b9f 100644 --- a/src/Func.h +++ b/src/Func.h @@ -12,7 +12,7 @@ #include "zeek/ZeekList.h" #include "zeek/Stmt.h" #include "zeek/Obj.h" -#include "zeek/IntrusivePtr.h" +#include "zeek/Scope.h" #include "zeek/Type.h" /* for function_flavor */ #include "zeek/TraverseTypes.h" #include "zeek/ZeekArgs.h" @@ -103,7 +103,7 @@ public: size_t new_frame_size, int priority = 0); virtual void SetScope(detail::ScopePtr newscope); - virtual detail::Scope* GetScope() const { return scope.get(); } + virtual detail::ScopePtr GetScope() const { return scope; } const FuncTypePtr& GetType() const { return type; } diff --git a/src/Scope.cc b/src/Scope.cc index 2aea8086ea..b0c124ec73 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -12,10 +12,8 @@ namespace zeek::detail { -using scope_list = PList; - -static scope_list scopes; -static Scope* top_scope; +static std::vector scopes; +static ScopePtr top_scope; Scope::Scope(IDPtr id, std::unique_ptr> al) @@ -117,7 +115,7 @@ const IDPtr& lookup_ID(const char* name, const char* curr_module, bool need_export = check_export && (ID_module != GLOBAL_MODULE_NAME && ID_module != curr_module); - for ( int i = scopes.length() - 1; i >= 0; --i ) + for ( int i = scopes.size() - 1; i >= 0; --i ) { const auto& id = scopes[i]->Find(fullname); @@ -172,16 +170,15 @@ IDPtr install_ID(const char* name, const char* module_name, return id; } -void push_existing_scope(Scope* scope) +void push_existing_scope(ScopePtr scope) { top_scope = scope; scopes.push_back(scope); } -void push_scope(IDPtr id, - std::unique_ptr> attrs) +void push_scope(IDPtr id, std::unique_ptr> attrs) { - top_scope = new Scope(std::move(id), std::move(attrs)); + top_scope = make_intrusive(std::move(id), std::move(attrs)); scopes.push_back(top_scope); } @@ -191,19 +188,19 @@ ScopePtr pop_scope() reporter->InternalError("scope underflow"); scopes.pop_back(); - Scope* old_top = top_scope; + auto old_top = top_scope; top_scope = scopes.empty() ? nullptr : scopes.back(); - return {AdoptRef{}, old_top}; + return old_top; } -Scope* current_scope() +ScopePtr current_scope() { return top_scope; } -Scope* global_scope() +ScopePtr global_scope() { return scopes.empty() ? 0 : scopes.front(); } diff --git a/src/Scope.h b/src/Scope.h index c5fb08d6fe..3971ecca5e 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -96,12 +96,12 @@ extern IDPtr install_ID( bool is_global, bool is_export); extern void push_scope(IDPtr id, std::unique_ptr> attrs); -extern void push_existing_scope(Scope* scope); +extern void push_existing_scope(ScopePtr scope); // Returns the one popped off. extern ScopePtr pop_scope(); -extern Scope* current_scope(); -extern Scope* global_scope(); +extern ScopePtr current_scope(); +extern ScopePtr global_scope(); // Current module (identified by its name). extern std::string current_module; diff --git a/src/Stmt.cc b/src/Stmt.cc index a695b86d30..0bac3c9c72 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -1514,7 +1514,7 @@ TraversalCode FallthroughStmt::Traverse(TraversalCallback* cb) const ReturnStmt::ReturnStmt(ExprPtr arg_e) : ExprStmt(STMT_RETURN, std::move(arg_e)) { - Scope* s = current_scope(); + auto s = current_scope(); if ( ! s || ! s->GetID() ) { diff --git a/src/Traverse.h b/src/Traverse.h index 36fc04afb7..4cfe51f3c8 100644 --- a/src/Traverse.h +++ b/src/Traverse.h @@ -2,6 +2,7 @@ #pragma once +#include "zeek/Scope.h" #include "zeek/TraverseTypes.h" namespace zeek { @@ -10,7 +11,6 @@ class Func; namespace detail { -class Scope; class Stmt; class Expr; class ID; @@ -38,7 +38,7 @@ public: virtual TraversalCode PreDecl(const ID*) { return TC_CONTINUE; } virtual TraversalCode PostDecl(const ID*) { return TC_CONTINUE; } - Scope* current_scope; + ScopePtr current_scope; }; TraversalCode traverse_all(TraversalCallback* cb); diff --git a/src/Var.cc b/src/Var.cc index 189b657c14..9ef09d24aa 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -672,7 +672,7 @@ void begin_func(IDPtr id, const char* module_name, class OuterIDBindingFinder : public TraversalCallback { public: - OuterIDBindingFinder(Scope* s) + OuterIDBindingFinder(ScopePtr s) { scopes.emplace_back(s); } @@ -680,7 +680,7 @@ public: TraversalCode PreExpr(const Expr*) override; TraversalCode PostExpr(const Expr*) override; - std::vector scopes; + std::vector scopes; std::unordered_set outer_id_references; }; @@ -766,7 +766,7 @@ void end_func(StmtPtr body) ingredients.release(); } -IDPList gather_outer_ids(Scope* scope, Stmt* body) +IDPList gather_outer_ids(ScopePtr scope, StmtPtr body) { OuterIDBindingFinder cb(scope); body->Traverse(&cb); diff --git a/src/Var.h b/src/Var.h index bd5ae5b020..c54d97eea5 100644 --- a/src/Var.h +++ b/src/Var.h @@ -20,6 +20,7 @@ class Expr; class Scope; class Stmt; using StmtPtr = IntrusivePtr; +using ScopePtr = IntrusivePtr; enum DeclType { VAR_REGULAR, VAR_CONST, VAR_REDEF, VAR_OPTION, }; @@ -41,7 +42,7 @@ extern void begin_func(IDPtr id, const char* module_name, FunctionFlavor flavor, extern void end_func(StmtPtr body); // Gather all IDs referenced inside a body that aren't part of a given scope. -extern IDPList gather_outer_ids(Scope* scope, Stmt* body); +extern IDPList gather_outer_ids(ScopePtr scope, StmtPtr body); } // namespace detail } // namespace zeek diff --git a/src/parse.y b/src/parse.y index e63b33595b..bea4b90066 100644 --- a/src/parse.y +++ b/src/parse.y @@ -1327,9 +1327,8 @@ lambda_body: // Gather the ingredients for a Func from the // current scope. auto ingredients = std::make_unique( - IntrusivePtr{NewRef{}, current_scope()}, - IntrusivePtr{AdoptRef{}, $3}); - IDPList outer_ids = gather_outer_ids(pop_scope().get(), ingredients->body.get()); + current_scope(), IntrusivePtr{AdoptRef{}, $3}); + 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/GenRDs.cc b/src/script_opt/GenRDs.cc index 5003c72b57..7ced9b3180 100644 --- a/src/script_opt/GenRDs.cc +++ b/src/script_opt/GenRDs.cc @@ -10,7 +10,7 @@ namespace zeek::detail { -void RD_Decorate::TraverseFunction(const Func* f, Scope* scope, StmtPtr body) +void RD_Decorate::TraverseFunction(const Func* f, ScopePtr scope, StmtPtr body) { func_flavor = f->Flavor(); diff --git a/src/script_opt/GenRDs.h b/src/script_opt/GenRDs.h index 38202cbb6c..cfdfc5e34c 100644 --- a/src/script_opt/GenRDs.h +++ b/src/script_opt/GenRDs.h @@ -54,11 +54,8 @@ public: { } // Traverses the given function body, using the first two - // arguments for context. "scope" is a Scope* rather than - // a ScopePtr because the various scope management functions - // (e.g., push_existing_scope(), current_scope()) traffic in - // Scope*'s. - void TraverseFunction(const Func* f, Scope* scope, StmtPtr body); + // arguments for context. + void TraverseFunction(const Func* f, ScopePtr scope, StmtPtr body); TraversalCode PreStmt(const Stmt*) override; TraversalCode PostStmt(const Stmt*) override; diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index 5bfc4dfee3..0ac62f4e68 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -28,9 +28,11 @@ void (*CPP_init_hook)() = nullptr; // Tracks all of the loaded functions (including event handlers and hooks). static std::vector funcs; +static ZAMCompiler* ZAM = nullptr; + void optimize_func(ScriptFunc* f, std::shared_ptr pf, - ScopePtr scope_ptr, StmtPtr& body, + ScopePtr scope, StmtPtr& body, AnalyOpt& analysis_options) { if ( reporter->Errors() > 0 ) @@ -57,7 +59,6 @@ void optimize_func(ScriptFunc* f, std::shared_ptr pf, return; } - auto scope = scope_ptr.release(); push_existing_scope(scope); auto rc = std::make_shared(); @@ -162,8 +163,8 @@ void analyze_func(ScriptFuncPtr f) *analysis_options.only_func != f->Name() ) return; - funcs.emplace_back(f, ScopePtr{NewRef{}, f->GetScope()}, - f->CurrentBody(), f->CurrentPriority()); + funcs.emplace_back(f, f->GetScope(), f->CurrentBody(), + f->CurrentPriority()); } const FuncInfo* analyze_global_stmts(Stmt* stmts) @@ -182,7 +183,7 @@ const FuncInfo* analyze_global_stmts(Stmt* stmts) StmtPtr stmts_p{NewRef{}, stmts}; auto sf = make_intrusive(id, stmts_p, empty_inits, sc->Length(), 0); - funcs.emplace_back(sf, ScopePtr{NewRef{}, sc}, stmts_p, 0); + funcs.emplace_back(sf, sc, stmts_p, 0); return &funcs.back(); }