zeekygen/IdentifierInfo: use class IntrusivePtr more

With this in place, we can eliminate the manually imeplemented copy
constructor/operator.
This commit is contained in:
Max Kellermann 2020-03-02 19:41:18 +01:00
parent a0c831a1bd
commit 78712d009f
5 changed files with 22 additions and 49 deletions

View file

@ -1119,7 +1119,7 @@ decl:
IntrusivePtr id{AdoptRef{}, $2};
IntrusivePtr<Expr> init{AdoptRef{}, $5};
add_global(id.get(), {AdoptRef{}, $3}, $4, init, $6, VAR_REDEF);
zeekygen_mgr->Redef(id.get(), ::filename, $4, init.release());
zeekygen_mgr->Redef(id.get(), ::filename, $4, std::move(init));
}
| TOK_REDEF TOK_ENUM global_id TOK_ADD_TO '{'

View file

@ -32,9 +32,9 @@ IdentifierInfo::~IdentifierInfo()
}
void IdentifierInfo::AddRedef(const string& script, init_class ic,
Expr* init_expr, const vector<string>& comments)
IntrusivePtr<Expr> init_expr, const vector<string>& comments)
{
Redefinition* redef = new Redefinition(script, ic, init_expr, comments);
Redefinition* redef = new Redefinition(script, ic, std::move(init_expr), comments);
redefs.push_back(redef);
}
@ -142,49 +142,16 @@ time_t IdentifierInfo::DoGetModificationTime() const
IdentifierInfo::Redefinition::Redefinition(
std::string arg_script,
init_class arg_ic,
Expr* arg_expr,
IntrusivePtr<Expr> arg_expr,
std::vector<std::string> arg_comments)
: from_script(std::move(arg_script)),
ic(arg_ic),
init_expr(arg_expr ? arg_expr->Ref() : nullptr),
init_expr(std::move(arg_expr)),
comments(std::move(arg_comments))
{
}
IdentifierInfo::Redefinition::Redefinition(const IdentifierInfo::Redefinition& other)
{
from_script = other.from_script;
ic = other.ic;
init_expr = other.init_expr;
comments = other.comments;
if ( init_expr )
init_expr->Ref();
}
IdentifierInfo::Redefinition&
IdentifierInfo::Redefinition::operator=(const IdentifierInfo::Redefinition& other)
{
if ( &other == this )
return *this;
Unref(init_expr);
from_script = other.from_script;
ic = other.ic;
init_expr = other.init_expr;
comments = other.comments;
if ( init_expr )
init_expr->Ref();
return *this;
}
IdentifierInfo::Redefinition::~Redefinition()
{
Unref(init_expr);
}
IdentifierInfo::Redefinition::~Redefinition() = default;
IdentifierInfo::RecordField::~RecordField()
{

View file

@ -71,7 +71,7 @@ public:
* @param comments Comments associated with the redef statement.
*/
void AddRedef(const std::string& from_script, init_class ic,
Expr* init_expr,
IntrusivePtr<Expr> init_expr,
const std::vector<std::string>& comments);
/**
@ -128,17 +128,13 @@ public:
struct Redefinition {
std::string from_script; /**< Name of script doing the redef. */
init_class ic;
Expr* init_expr;
IntrusivePtr<Expr> init_expr;
std::vector<std::string> comments; /**< Zeekygen comments on redef. */
Redefinition(std::string arg_script, init_class arg_ic,
Expr* arg_expr,
IntrusivePtr<Expr> arg_expr,
std::vector<std::string> arg_comments);
Redefinition(const Redefinition& other);
Redefinition& operator=(const Redefinition& other);
~Redefinition();
};

View file

@ -7,6 +7,7 @@
#include "PackageInfo.h"
#include "ScriptInfo.h"
#include "IdentifierInfo.h"
#include "Expr.h"
#include <utility>
#include <cstdlib>
@ -359,7 +360,7 @@ void Manager::RecordField(const ID* id, const TypeDecl* field,
}
void Manager::Redef(const ID* id, const string& path,
init_class ic, Expr* init_expr)
init_class ic, IntrusivePtr<Expr> init_expr)
{
if ( disabled )
return;
@ -387,7 +388,7 @@ void Manager::Redef(const ID* id, const string& path,
return;
}
id_info->AddRedef(from_script, ic, init_expr, comment_buffer);
id_info->AddRedef(from_script, ic, std::move(init_expr), comment_buffer);
script_info->AddRedef(id_info);
comment_buffer.clear();
last_identifier_seen = id_info;
@ -395,6 +396,12 @@ void Manager::Redef(const ID* id, const string& path,
id->Name(), from_script.c_str());
}
void Manager::Redef(const ID* id, const std::string& path,
init_class ic)
{
Redef(id, path, ic, nullptr);
}
void Manager::SummaryComment(const string& script, const string& comment)
{
if ( disabled )

View file

@ -14,6 +14,7 @@
#include <sys/stat.h>
#include <errno.h>
template <class T> class IntrusivePtr;
class TypeDecl;
namespace zeekygen {
@ -138,7 +139,9 @@ public:
* @param init_expr The intiialization expression that was used.
*/
void Redef(const ID* id, const std::string& path,
init_class ic = INIT_NONE, Expr* init_expr = nullptr);
init_class ic, IntrusivePtr<Expr> init_expr);
void Redef(const ID* id, const std::string& path,
init_class ic = INIT_NONE);
/**
* Register Zeekygen script summary content.