Type: use class IntrusivePtr in FuncType

This commit is contained in:
Max Kellermann 2020-03-03 17:05:32 +01:00
parent 73cea5dcad
commit 184fb9a980
4 changed files with 25 additions and 34 deletions

View file

@ -1431,7 +1431,7 @@ ReturnStmt::ReturnStmt(IntrusivePtr<Expr> arg_e)
{
if ( e )
{
ft->SetYieldType(e->Type());
ft->SetYieldType({NewRef{}, e->Type()});
s->ScopeID()->SetInferReturnType(false);
}
}

View file

@ -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<RecordType> arg_args, IntrusivePtr<BroType> arg_yield, function_flavor arg_flavor)
: BroType(TYPE_FUNC), args(std::move(arg_args)), arg_types(make_intrusive<TypeList>()), 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:

View file

@ -434,35 +434,35 @@ protected:
class FuncType : public BroType {
public:
FuncType(RecordType* args, BroType* yield, function_flavor f);
FuncType(IntrusivePtr<RecordType> args, IntrusivePtr<BroType> 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<BroType> 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<RecordType> args;
IntrusivePtr<TypeList> arg_types;
IntrusivePtr<BroType> yield;
function_flavor flavor;
};

View file

@ -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: