zeekygen: use class IntrusivePtr

This commit is contained in:
Max Kellermann 2020-02-26 20:21:49 +01:00
parent edde591748
commit 95e2d66fb0
8 changed files with 40 additions and 38 deletions

View file

@ -1183,7 +1183,7 @@ void EnumType::CheckAndAddName(const string& module_name, const char* name,
if ( deprecation ) if ( deprecation )
id->MakeDeprecated(deprecation); id->MakeDeprecated(deprecation);
zeekygen_mgr->Identifier(id.get()); zeekygen_mgr->Identifier(std::move(id));
} }
else else
{ {

View file

@ -1086,27 +1086,31 @@ decl:
| TOK_GLOBAL def_global_id opt_type init_class opt_init opt_attr ';' | TOK_GLOBAL def_global_id opt_type init_class opt_init opt_attr ';'
{ {
add_global($2, {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_REGULAR); IntrusivePtr id{AdoptRef{}, $2};
zeekygen_mgr->Identifier($2); add_global(id.get(), {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_REGULAR);
zeekygen_mgr->Identifier(std::move(id));
} }
| TOK_OPTION def_global_id opt_type init_class opt_init opt_attr ';' | TOK_OPTION def_global_id opt_type init_class opt_init opt_attr ';'
{ {
add_global($2, {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_OPTION); IntrusivePtr id{AdoptRef{}, $2};
zeekygen_mgr->Identifier($2); add_global(id.get(), {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_OPTION);
zeekygen_mgr->Identifier(std::move(id));
} }
| TOK_CONST def_global_id opt_type init_class opt_init opt_attr ';' | TOK_CONST def_global_id opt_type init_class opt_init opt_attr ';'
{ {
add_global($2, {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_CONST); IntrusivePtr id{AdoptRef{}, $2};
zeekygen_mgr->Identifier($2); add_global(id.get(), {AdoptRef{}, $3}, $4, {AdoptRef{}, $5}, $6, VAR_CONST);
zeekygen_mgr->Identifier(std::move(id));
} }
| TOK_REDEF global_id opt_type init_class opt_init opt_attr ';' | TOK_REDEF global_id opt_type init_class opt_init opt_attr ';'
{ {
IntrusivePtr id{AdoptRef{}, $2};
IntrusivePtr<Expr> init{AdoptRef{}, $5}; IntrusivePtr<Expr> init{AdoptRef{}, $5};
add_global($2, {AdoptRef{}, $3}, $4, init, $6, VAR_REDEF); add_global(id.get(), {AdoptRef{}, $3}, $4, init, $6, VAR_REDEF);
zeekygen_mgr->Redef($2, ::filename, $4, init.release()); zeekygen_mgr->Redef(id.get(), ::filename, $4, init.release());
} }
| TOK_REDEF TOK_ENUM global_id TOK_ADD_TO '{' | TOK_REDEF TOK_ENUM global_id TOK_ADD_TO '{'
@ -1133,12 +1137,13 @@ decl:
} }
| TOK_TYPE global_id ':' | TOK_TYPE global_id ':'
{ cur_decl_type_id = $2; zeekygen_mgr->StartType($2); } { cur_decl_type_id = $2; zeekygen_mgr->StartType({NewRef{}, $2}); }
type opt_attr ';' type opt_attr ';'
{ {
cur_decl_type_id = 0; cur_decl_type_id = 0;
add_type($2, {AdoptRef{}, $5}, $6); IntrusivePtr id{AdoptRef{}, $2};
zeekygen_mgr->Identifier($2); add_type(id.get(), {AdoptRef{}, $5}, $6);
zeekygen_mgr->Identifier(std::move(id));
} }
| func_hdr { func_hdr_location = @1; } func_body | func_hdr { func_hdr_location = @1; } func_body
@ -1168,10 +1173,11 @@ conditional:
func_hdr: func_hdr:
TOK_FUNCTION def_global_id func_params opt_attr TOK_FUNCTION def_global_id func_params opt_attr
{ {
begin_func($2, current_module.c_str(), IntrusivePtr id{AdoptRef{}, $2};
begin_func(id.get(), current_module.c_str(),
FUNC_FLAVOR_FUNCTION, 0, {NewRef{}, $3}, $4); FUNC_FLAVOR_FUNCTION, 0, {NewRef{}, $3}, $4);
$$ = $3; $$ = $3;
zeekygen_mgr->Identifier($2); zeekygen_mgr->Identifier(std::move(id));
} }
| TOK_EVENT event_id func_params opt_attr | TOK_EVENT event_id func_params opt_attr
{ {

View file

@ -135,7 +135,7 @@ ComponentManager<T, C>::ComponentManager(const string& arg_module, const string&
{ {
auto id = install_ID(local_id.c_str(), module.c_str(), true, true); auto id = install_ID(local_id.c_str(), module.c_str(), true, true);
add_type(id.get(), tag_enum_type, 0); add_type(id.get(), tag_enum_type, 0);
zeekygen_mgr->Identifier(id.get()); zeekygen_mgr->Identifier(std::move(id));
} }
template <class T, class C> template <class T, class C>

View file

@ -11,22 +11,17 @@
using namespace std; using namespace std;
using namespace zeekygen; using namespace zeekygen;
IdentifierInfo::IdentifierInfo(ID* arg_id, ScriptInfo* script) IdentifierInfo::IdentifierInfo(IntrusivePtr<ID> arg_id, ScriptInfo* script)
: Info(), : Info(),
comments(), id(arg_id), initial_val(), redefs(), fields(), comments(), id(std::move(arg_id)), initial_val(), redefs(), fields(),
last_field_seen(), declaring_script(script) last_field_seen(), declaring_script(script)
{ {
Ref(id);
if ( id->ID_Val() && (id->IsOption() || id->IsRedefinable()) ) if ( id->ID_Val() && (id->IsOption() || id->IsRedefinable()) )
initial_val = id->ID_Val()->Clone(); initial_val = {AdoptRef{}, id->ID_Val()->Clone()};
} }
IdentifierInfo::~IdentifierInfo() IdentifierInfo::~IdentifierInfo()
{ {
Unref(id);
Unref(initial_val);
for ( redef_list::const_iterator it = redefs.begin(); it != redefs.end(); for ( redef_list::const_iterator it = redefs.begin(); it != redefs.end();
++it ) ++it )
delete *it; delete *it;

View file

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "Info.h" #include "Info.h"
#include "IntrusivePtr.h"
#include "ID.h" #include "ID.h"
#include <string> #include <string>
@ -31,7 +32,7 @@ public:
* @param script The info object associated with the script in which \a id * @param script The info object associated with the script in which \a id
* is declared. * is declared.
*/ */
IdentifierInfo(ID* id, ScriptInfo* script); IdentifierInfo(IntrusivePtr<ID> id, ScriptInfo* script);
/** /**
* Dtor. Releases any references to script-level objects. * Dtor. Releases any references to script-level objects.
@ -42,7 +43,7 @@ public:
* Returns the initial value of the identifier. * Returns the initial value of the identifier.
*/ */
Val* InitialVal() const Val* InitialVal() const
{ return initial_val; } { return initial_val.get(); }
/** /**
* Add a comment associated with the identifier. If the identifier is a * Add a comment associated with the identifier. If the identifier is a
@ -96,7 +97,7 @@ public:
* @return the script-level ID tracked by this info object. * @return the script-level ID tracked by this info object.
*/ */
ID* GetID() const ID* GetID() const
{ return id; } { return id.get(); }
/** /**
* @return The script which declared the script-level identifier. * @return The script which declared the script-level identifier.
@ -177,8 +178,8 @@ private:
typedef std::map<std::string, RecordField*> record_field_map; typedef std::map<std::string, RecordField*> record_field_map;
std::vector<std::string> comments; std::vector<std::string> comments;
ID* id; IntrusivePtr<ID> id;
Val* initial_val; IntrusivePtr<Val> initial_val;
redef_list redefs; redef_list redefs;
record_field_map fields; record_field_map fields;
RecordField* last_field_seen; RecordField* last_field_seen;

View file

@ -215,7 +215,7 @@ void Manager::ModuleUsage(const string& path, const string& module)
module.c_str(), name.c_str()); module.c_str(), name.c_str());
} }
IdentifierInfo* Manager::CreateIdentifierInfo(ID* id, ScriptInfo* script) IdentifierInfo* Manager::CreateIdentifierInfo(IntrusivePtr<ID> id, ScriptInfo* script)
{ {
auto prev = identifiers.GetInfo(id->Name()); auto prev = identifiers.GetInfo(id->Name());
IdentifierInfo* rval = prev ? prev : new IdentifierInfo(id, script); IdentifierInfo* rval = prev ? prev : new IdentifierInfo(id, script);
@ -245,7 +245,7 @@ IdentifierInfo* Manager::CreateIdentifierInfo(ID* id, ScriptInfo* script)
return rval; return rval;
} }
void Manager::StartType(ID* id) void Manager::StartType(IntrusivePtr<ID> id)
{ {
if ( disabled ) if ( disabled )
return; return;
@ -262,7 +262,7 @@ void Manager::StartType(ID* id)
if ( ! script_info ) if ( ! script_info )
{ {
WarnMissingScript("identifier", id, script); WarnMissingScript("identifier", id.get(), script);
return; return;
} }
@ -276,7 +276,7 @@ static bool IsEnumType(ID* id)
return id->AsType() ? id->AsType()->Tag() == TYPE_ENUM : false; return id->AsType() ? id->AsType()->Tag() == TYPE_ENUM : false;
} }
void Manager::Identifier(ID* id) void Manager::Identifier(IntrusivePtr<ID> id)
{ {
if ( disabled ) if ( disabled )
return; return;
@ -326,7 +326,7 @@ void Manager::Identifier(ID* id)
if ( ! script_info ) if ( ! script_info )
{ {
WarnMissingScript("identifier", id, script); WarnMissingScript("identifier", id.get(), script);
return; return;
} }

View file

@ -109,14 +109,14 @@ public:
* Signal that a record or enum type is now being parsed. * Signal that a record or enum type is now being parsed.
* @param id The record or enum type identifier. * @param id The record or enum type identifier.
*/ */
void StartType(ID* id); void StartType(IntrusivePtr<ID> id);
/** /**
* Register a script-level identifier for which information/documentation * Register a script-level identifier for which information/documentation
* will be gathered. * will be gathered.
* @param id The script-level identifier. * @param id The script-level identifier.
*/ */
void Identifier(ID* id); void Identifier(IntrusivePtr<ID> id);
/** /**
* Register a record-field for which information/documentation will be * Register a record-field for which information/documentation will be
@ -214,7 +214,7 @@ private:
typedef std::vector<std::string> comment_buffer_t; typedef std::vector<std::string> comment_buffer_t;
typedef std::map<std::string, comment_buffer_t> comment_buffer_map_t; typedef std::map<std::string, comment_buffer_t> comment_buffer_map_t;
IdentifierInfo* CreateIdentifierInfo(ID* id, ScriptInfo* script); IdentifierInfo* CreateIdentifierInfo(IntrusivePtr<ID> id, ScriptInfo* script);
bool disabled; bool disabled;
comment_buffer_t comment_buffer; // For whatever next identifier comes in. comment_buffer_t comment_buffer; // For whatever next identifier comes in.

View file

@ -258,12 +258,12 @@ void ScriptInfo::DoInitPostScript()
if ( name == "base/frameworks/input/main.zeek" ) if ( name == "base/frameworks/input/main.zeek" )
{ {
auto id = global_scope()->Lookup("Input::Reader"); auto id = global_scope()->Lookup("Input::Reader");
types.push_back(new IdentifierInfo(id, this)); types.push_back(new IdentifierInfo({NewRef{}, id}, this));
} }
else if ( name == "base/frameworks/logging/main.zeek" ) else if ( name == "base/frameworks/logging/main.zeek" )
{ {
auto id = global_scope()->Lookup("Log::Writer"); auto id = global_scope()->Lookup("Log::Writer");
types.push_back(new IdentifierInfo(id, this)); types.push_back(new IdentifierInfo({NewRef{}, id}, this));
} }
} }