From 184fb9a9807172aa0bb5f7044c0f230a8be57748 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 3 Mar 2020 17:05:32 +0100 Subject: [PATCH] Type: use class IntrusivePtr in FuncType --- src/Stmt.cc | 2 +- src/Type.cc | 29 ++++++++++------------------- src/Type.h | 18 +++++++++--------- src/parse.y | 10 +++++----- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/Stmt.cc b/src/Stmt.cc index 76dd181b6b..a45b0198a7 100644 --- a/src/Stmt.cc +++ b/src/Stmt.cc @@ -1431,7 +1431,7 @@ ReturnStmt::ReturnStmt(IntrusivePtr arg_e) { if ( e ) { - ft->SetYieldType(e->Type()); + ft->SetYieldType({NewRef{}, e->Type()}); s->ScopeID()->SetInferReturnType(false); } } diff --git a/src/Type.cc b/src/Type.cc index 9a245511c0..1d674f112d 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -423,15 +423,11 @@ SetType* SetType::ShallowClone() SetType::~SetType() = default; -FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg_flavor) -: BroType(TYPE_FUNC) +FuncType::FuncType(IntrusivePtr arg_args, IntrusivePtr arg_yield, function_flavor arg_flavor) +: BroType(TYPE_FUNC), args(std::move(arg_args)), arg_types(make_intrusive()), yield(std::move(arg_yield)) { - args = arg_args; - yield = arg_yield; flavor = arg_flavor; - arg_types = new TypeList(); - bool has_default_arg = false; for ( int i = 0; i < args->NumFields(); ++i ) @@ -455,9 +451,9 @@ FuncType::FuncType(RecordType* arg_args, BroType* arg_yield, function_flavor arg FuncType* FuncType::ShallowClone() { auto f = new FuncType(); - f->args = args->Ref()->AsRecordType(); - f->arg_types = arg_types->Ref()->AsTypeList(); - f->yield = yield->Ref(); + f->args = {NewRef{}, args->AsRecordType()}; + f->arg_types = {NewRef{}, arg_types->AsTypeList()}; + f->yield = yield; f->flavor = flavor; return f; } @@ -481,26 +477,21 @@ string FuncType::FlavorString() const } } -FuncType::~FuncType() - { - Unref(args); - Unref(arg_types); - Unref(yield); - } +FuncType::~FuncType() = default; BroType* FuncType::YieldType() { - return yield; + return yield.get(); } const BroType* FuncType::YieldType() const { - return yield; + return yield.get(); } int FuncType::MatchesIndex(ListExpr* const index) const { - return check_and_promote_args(index, args) ? + return check_and_promote_args(index, args.get()) ? MATCHES_INDEX_SCALAR : DOES_NOT_MATCH_INDEX; } @@ -1868,7 +1859,7 @@ BroType* merge_types(const BroType* t1, const BroType* t2) BroType* yield = t1->YieldType() ? merge_types(t1->YieldType(), t2->YieldType()) : 0; - return new FuncType(args->AsRecordType(), yield, ft1->Flavor()); + return new FuncType({AdoptRef{}, args->AsRecordType()}, {AdoptRef{}, yield}, ft1->Flavor()); } case TYPE_RECORD: diff --git a/src/Type.h b/src/Type.h index c63962c95d..c92768a26c 100644 --- a/src/Type.h +++ b/src/Type.h @@ -434,35 +434,35 @@ protected: class FuncType : public BroType { public: - FuncType(RecordType* args, BroType* yield, function_flavor f); + FuncType(IntrusivePtr args, IntrusivePtr yield, function_flavor f); FuncType* ShallowClone() override; ~FuncType() override; - RecordType* Args() const { return args; } + RecordType* Args() const { return args.get(); } BroType* YieldType() override; const BroType* YieldType() const override; - void SetYieldType(BroType* arg_yield) { yield = arg_yield; } + void SetYieldType(IntrusivePtr arg_yield) { yield = std::move(arg_yield); } function_flavor Flavor() const { return flavor; } std::string FlavorString() const; // Used to convert a function type to an event or hook type. void ClearYieldType(function_flavor arg_flav) - { Unref(yield); yield = 0; flavor = arg_flav; } + { yield = nullptr; flavor = arg_flav; } int MatchesIndex(ListExpr* index) const override; int CheckArgs(const type_list* args, bool is_init = false) const; - TypeList* ArgTypes() const { return arg_types; } + TypeList* ArgTypes() const { return arg_types.get(); } void Describe(ODesc* d) const override; void DescribeReST(ODesc* d, bool roles_only = false) const override; protected: - FuncType() : BroType(TYPE_FUNC) { args = 0; arg_types = 0; yield = 0; flavor = FUNC_FLAVOR_FUNCTION; } - RecordType* args; - TypeList* arg_types; - BroType* yield; + FuncType() : BroType(TYPE_FUNC) { flavor = FUNC_FLAVOR_FUNCTION; } + IntrusivePtr args; + IntrusivePtr arg_types; + IntrusivePtr yield; function_flavor flavor; }; diff --git a/src/parse.y b/src/parse.y index 6a52961152..1395cae0f4 100644 --- a/src/parse.y +++ b/src/parse.y @@ -979,13 +979,13 @@ type: | TOK_EVENT '(' formal_args ')' { set_location(@1, @3); - $$ = new FuncType($3, 0, FUNC_FLAVOR_EVENT); + $$ = new FuncType({AdoptRef{}, $3}, nullptr, FUNC_FLAVOR_EVENT); } | TOK_HOOK '(' formal_args ')' { set_location(@1, @3); - $$ = new FuncType($3, base_type(TYPE_BOOL), FUNC_FLAVOR_HOOK); + $$ = new FuncType({AdoptRef{}, $3}, {AdoptRef{}, base_type(TYPE_BOOL)}, FUNC_FLAVOR_HOOK); } | TOK_FILE TOK_OF type @@ -1204,7 +1204,7 @@ func_hdr: | TOK_HOOK def_global_id func_params opt_attr { $3->ClearYieldType(FUNC_FLAVOR_HOOK); - $3->SetYieldType(base_type(TYPE_BOOL)); + $3->SetYieldType({AdoptRef{}, base_type(TYPE_BOOL)}); begin_func($2, current_module.c_str(), FUNC_FLAVOR_HOOK, 0, {NewRef{}, $3}, $4); $$ = $3; @@ -1278,9 +1278,9 @@ begin_func: func_params: '(' formal_args ')' ':' type - { $$ = new FuncType($2, $5, FUNC_FLAVOR_FUNCTION); } + { $$ = new FuncType({AdoptRef{}, $2}, {AdoptRef{}, $5}, FUNC_FLAVOR_FUNCTION); } | '(' formal_args ')' - { $$ = new FuncType($2, base_type(TYPE_VOID), FUNC_FLAVOR_FUNCTION); } + { $$ = new FuncType({AdoptRef{}, $2}, {AdoptRef{}, base_type(TYPE_VOID)}, FUNC_FLAVOR_FUNCTION); } ; opt_type: