Change Scope::GenerateTemporary() to return IntrusivePtr

This commit is contained in:
Jon Siwek 2020-05-27 16:51:25 -07:00
parent a13899c95e
commit 46e23b49fb
5 changed files with 24 additions and 22 deletions

View file

@ -72,9 +72,9 @@ IntrusivePtr<ID> Scope::Remove(std::string_view name)
return nullptr; return nullptr;
} }
ID* Scope::GenerateTemporary(const char* name) IntrusivePtr<ID> Scope::GenerateTemporary(const char* name)
{ {
return new ID(name, SCOPE_FUNCTION, false); return make_intrusive<ID>(name, SCOPE_FUNCTION, false);
} }
id_list* Scope::GetInits() id_list* Scope::GetInits()

View file

@ -53,7 +53,7 @@ public:
size_t Length() const { return local.size(); } size_t Length() const { return local.size(); }
const auto& Vars() { return local; } const auto& Vars() { return local; }
ID* GenerateTemporary(const char* name); IntrusivePtr<ID> GenerateTemporary(const char* name);
// Returns the list of variables needing initialization, and // Returns the list of variables needing initialization, and
// removes it from this Scope. // removes it from this Scope.

View file

@ -458,8 +458,9 @@ static bool canonical_arg_types_match(const FuncType* decl, const FuncType* impl
return true; return true;
} }
void begin_func(ID* id, const char* module_name, function_flavor flavor, void begin_func(IntrusivePtr<ID> id, const char* module_name,
bool is_redef, IntrusivePtr<FuncType> t, function_flavor flavor, bool is_redef,
IntrusivePtr<FuncType> t,
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs) std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs)
{ {
if ( flavor == FUNC_FLAVOR_EVENT ) if ( flavor == FUNC_FLAVOR_EVENT )
@ -512,7 +513,7 @@ void begin_func(ID* id, const char* module_name, function_flavor flavor,
} }
if ( prototype->deprecated ) if ( prototype->deprecated )
t->Warn("use of deprecated prototype", id); t->Warn("use of deprecated prototype", id.get());
} }
else 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()) ) if ( canonical_arg_types_match(decl, t.get()) )
prototype = decl->Prototypes()[0]; prototype = decl->Prototypes()[0];
else 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 else
id->SetType(t); id->SetType(t);
push_scope({NewRef{}, id}, std::move(attrs)); push_scope(std::move(id), std::move(attrs));
const auto& args = t->Params(); const auto& args = t->Params();
int num_args = args->NumFields(); 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(), if ( Attr* depr_attr = find_attr(current_scope()->Attrs().get(),
ATTR_DEPRECATED) ) ATTR_DEPRECATED) )
id->MakeDeprecated(depr_attr->GetExpr()); current_scope()->GetID()->MakeDeprecated(depr_attr->GetExpr());
} }
class OuterIDBindingFinder : public TraversalCallback { class OuterIDBindingFinder : public TraversalCallback {

View file

@ -38,8 +38,9 @@ extern IntrusivePtr<Expr> add_and_assign_local(IntrusivePtr<ID> id,
extern void add_type(ID* id, IntrusivePtr<BroType> t, extern void add_type(ID* id, IntrusivePtr<BroType> t,
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attr); std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attr);
extern void begin_func(ID* id, const char* module_name, function_flavor flavor, extern void begin_func(IntrusivePtr<ID> id, const char* module_name,
bool is_redef, IntrusivePtr<FuncType> t, function_flavor flavor, bool is_redef,
IntrusivePtr<FuncType> t,
std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs = nullptr); std::unique_ptr<std::vector<IntrusivePtr<Attr>>> attrs = nullptr);
extern void end_func(IntrusivePtr<Stmt> body); extern void end_func(IntrusivePtr<Stmt> body);

View file

@ -134,7 +134,6 @@ bool resolving_global_ID = false;
bool defining_global_ID = false; bool defining_global_ID = false;
std::vector<int> saved_in_init; std::vector<int> saved_in_init;
ID* func_id = 0;
static Location func_hdr_location; static Location func_hdr_location;
EnumType *cur_enum_type = 0; EnumType *cur_enum_type = 0;
static ID* cur_decl_type_id = 0; static ID* cur_decl_type_id = 0;
@ -496,15 +495,15 @@ expr:
| '$' TOK_ID func_params '=' | '$' TOK_ID func_params '='
{ {
func_hdr_location = @1; func_hdr_location = @1;
func_id = current_scope()->GenerateTemporary("anonymous-function"); auto func_id = current_scope()->GenerateTemporary("anonymous-function");
func_id->SetInferReturnType(true); func_id->SetInferReturnType(true);
begin_func(func_id, current_module.c_str(), FUNC_FLAVOR_FUNCTION, begin_func(std::move(func_id), current_module.c_str(),
0, {AdoptRef{}, $3}); FUNC_FLAVOR_FUNCTION, false,
{AdoptRef{}, $3});
} }
lambda_body lambda_body
{ {
$$ = new FieldAssignExpr($2, IntrusivePtr{AdoptRef{}, $6}); $$ = new FieldAssignExpr($2, IntrusivePtr{AdoptRef{}, $6});
Unref(func_id);
} }
| expr TOK_IN expr | expr TOK_IN expr
@ -1180,7 +1179,7 @@ func_hdr:
TOK_FUNCTION def_global_id func_params opt_attr TOK_FUNCTION def_global_id func_params opt_attr
{ {
IntrusivePtr id{AdoptRef{}, $2}; 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}, FUNC_FLAVOR_FUNCTION, 0, {NewRef{}, $3},
std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4}); std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4});
$$ = $3; $$ = $3;
@ -1195,7 +1194,7 @@ func_hdr:
reporter->Error("event %s() is no longer available, use zeek_%s() instead", name, base.c_str()); 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}, FUNC_FLAVOR_EVENT, 0, {NewRef{}, $3},
std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4}); std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4});
$$ = $3; $$ = $3;
@ -1204,14 +1203,14 @@ func_hdr:
{ {
$3->ClearYieldType(FUNC_FLAVOR_HOOK); $3->ClearYieldType(FUNC_FLAVOR_HOOK);
$3->SetYieldType(base_type(TYPE_BOOL)); $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}, FUNC_FLAVOR_HOOK, 0, {NewRef{}, $3},
std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4}); std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$4});
$$ = $3; $$ = $3;
} }
| TOK_REDEF TOK_EVENT event_id func_params opt_attr | 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}, FUNC_FLAVOR_EVENT, 1, {NewRef{}, $4},
std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$5}); std::unique_ptr<std::vector<IntrusivePtr<Attr>>>{$5});
$$ = $4; $$ = $4;
@ -1275,8 +1274,9 @@ anonymous_function:
begin_func: begin_func:
func_params func_params
{ {
$$ = current_scope()->GenerateTemporary("anonymous-function"); auto id = current_scope()->GenerateTemporary("anonymous-function");
begin_func($$, current_module.c_str(), FUNC_FLAVOR_FUNCTION, 0, {AdoptRef{}, $1}); begin_func(id, current_module.c_str(), FUNC_FLAVOR_FUNCTION, 0, {AdoptRef{}, $1});
$$ = id.release();
} }
; ;