From 46e23b49fb388994a6f2c5ecf60b8c1cea37ead8 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 27 May 2020 16:51:25 -0700 Subject: [PATCH] Change Scope::GenerateTemporary() to return IntrusivePtr --- src/Scope.cc | 4 ++-- src/Scope.h | 2 +- src/Var.cc | 13 +++++++------ src/Var.h | 5 +++-- src/parse.y | 22 +++++++++++----------- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Scope.cc b/src/Scope.cc index c7eaf9fdca..19e3d581c5 100644 --- a/src/Scope.cc +++ b/src/Scope.cc @@ -72,9 +72,9 @@ IntrusivePtr Scope::Remove(std::string_view name) return nullptr; } -ID* Scope::GenerateTemporary(const char* name) +IntrusivePtr Scope::GenerateTemporary(const char* name) { - return new ID(name, SCOPE_FUNCTION, false); + return make_intrusive(name, SCOPE_FUNCTION, false); } id_list* Scope::GetInits() diff --git a/src/Scope.h b/src/Scope.h index fe72eaa72c..931e3d742c 100644 --- a/src/Scope.h +++ b/src/Scope.h @@ -53,7 +53,7 @@ public: size_t Length() const { return local.size(); } const auto& Vars() { return local; } - ID* GenerateTemporary(const char* name); + IntrusivePtr GenerateTemporary(const char* name); // Returns the list of variables needing initialization, and // removes it from this Scope. diff --git a/src/Var.cc b/src/Var.cc index 8e64f27b99..a086f045d2 100644 --- a/src/Var.cc +++ b/src/Var.cc @@ -458,8 +458,9 @@ static bool canonical_arg_types_match(const FuncType* decl, const FuncType* impl return true; } -void begin_func(ID* id, const char* module_name, function_flavor flavor, - bool is_redef, IntrusivePtr t, +void begin_func(IntrusivePtr id, const char* module_name, + function_flavor flavor, bool is_redef, + IntrusivePtr t, std::unique_ptr>> attrs) { if ( flavor == FUNC_FLAVOR_EVENT ) @@ -512,7 +513,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, } if ( prototype->deprecated ) - t->Warn("use of deprecated prototype", id); + t->Warn("use of deprecated prototype", id.get()); } else { @@ -521,7 +522,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, if ( canonical_arg_types_match(decl, t.get()) ) prototype = decl->Prototypes()[0]; else - t->Error("use of undeclared alternate prototype", id); + t->Error("use of undeclared alternate prototype", id.get()); } } @@ -557,7 +558,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, else id->SetType(t); - push_scope({NewRef{}, id}, std::move(attrs)); + push_scope(std::move(id), std::move(attrs)); const auto& args = t->Params(); int num_args = args->NumFields(); @@ -579,7 +580,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor, if ( Attr* depr_attr = find_attr(current_scope()->Attrs().get(), ATTR_DEPRECATED) ) - id->MakeDeprecated(depr_attr->GetExpr()); + current_scope()->GetID()->MakeDeprecated(depr_attr->GetExpr()); } class OuterIDBindingFinder : public TraversalCallback { diff --git a/src/Var.h b/src/Var.h index 13fedce1dc..7806e25585 100644 --- a/src/Var.h +++ b/src/Var.h @@ -38,8 +38,9 @@ extern IntrusivePtr add_and_assign_local(IntrusivePtr id, extern void add_type(ID* id, IntrusivePtr t, std::unique_ptr>> attr); -extern void begin_func(ID* id, const char* module_name, function_flavor flavor, - bool is_redef, IntrusivePtr t, +extern void begin_func(IntrusivePtr id, const char* module_name, + function_flavor flavor, bool is_redef, + IntrusivePtr t, std::unique_ptr>> attrs = nullptr); extern void end_func(IntrusivePtr body); diff --git a/src/parse.y b/src/parse.y index 1a3aa37424..bfb403f97a 100644 --- a/src/parse.y +++ b/src/parse.y @@ -134,7 +134,6 @@ bool resolving_global_ID = false; bool defining_global_ID = false; std::vector saved_in_init; -ID* func_id = 0; static Location func_hdr_location; EnumType *cur_enum_type = 0; static ID* cur_decl_type_id = 0; @@ -496,15 +495,15 @@ expr: | '$' TOK_ID func_params '=' { func_hdr_location = @1; - func_id = current_scope()->GenerateTemporary("anonymous-function"); + auto func_id = current_scope()->GenerateTemporary("anonymous-function"); func_id->SetInferReturnType(true); - begin_func(func_id, current_module.c_str(), FUNC_FLAVOR_FUNCTION, - 0, {AdoptRef{}, $3}); + begin_func(std::move(func_id), current_module.c_str(), + FUNC_FLAVOR_FUNCTION, false, + {AdoptRef{}, $3}); } lambda_body { $$ = new FieldAssignExpr($2, IntrusivePtr{AdoptRef{}, $6}); - Unref(func_id); } | expr TOK_IN expr @@ -1180,7 +1179,7 @@ func_hdr: TOK_FUNCTION def_global_id func_params opt_attr { IntrusivePtr id{AdoptRef{}, $2}; - begin_func(id.get(), current_module.c_str(), + begin_func(id, current_module.c_str(), FUNC_FLAVOR_FUNCTION, 0, {NewRef{}, $3}, std::unique_ptr>>{$4}); $$ = $3; @@ -1195,7 +1194,7 @@ func_hdr: reporter->Error("event %s() is no longer available, use zeek_%s() instead", name, base.c_str()); } - begin_func($2, current_module.c_str(), + begin_func({NewRef{}, $2}, current_module.c_str(), FUNC_FLAVOR_EVENT, 0, {NewRef{}, $3}, std::unique_ptr>>{$4}); $$ = $3; @@ -1204,14 +1203,14 @@ func_hdr: { $3->ClearYieldType(FUNC_FLAVOR_HOOK); $3->SetYieldType(base_type(TYPE_BOOL)); - begin_func($2, current_module.c_str(), + begin_func({NewRef{}, $2}, current_module.c_str(), FUNC_FLAVOR_HOOK, 0, {NewRef{}, $3}, std::unique_ptr>>{$4}); $$ = $3; } | TOK_REDEF TOK_EVENT event_id func_params opt_attr { - begin_func($3, current_module.c_str(), + begin_func({NewRef{}, $3}, current_module.c_str(), FUNC_FLAVOR_EVENT, 1, {NewRef{}, $4}, std::unique_ptr>>{$5}); $$ = $4; @@ -1275,8 +1274,9 @@ anonymous_function: begin_func: func_params { - $$ = current_scope()->GenerateTemporary("anonymous-function"); - begin_func($$, current_module.c_str(), FUNC_FLAVOR_FUNCTION, 0, {AdoptRef{}, $1}); + auto id = current_scope()->GenerateTemporary("anonymous-function"); + begin_func(id, current_module.c_str(), FUNC_FLAVOR_FUNCTION, 0, {AdoptRef{}, $1}); + $$ = id.release(); } ;