shift much of the internal use of ID* identifier pointers over to IDPtr objects

This commit is contained in:
Vern Paxson 2025-08-31 08:58:19 -07:00
parent 1c7c1b62f6
commit 693aa244f9
43 changed files with 301 additions and 345 deletions

View file

@ -154,7 +154,7 @@ void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf, c
// An additional constructor just used to generate place-holder
// instances, due to the misdesign that lambdas are identified
// by their Func objects rather than their FuncVal objects.
if ( lambda_ids && lambda_ids->length() > 0 )
if ( lambda_ids && ! lambda_ids->empty() )
Emit("%s_cl(const char* name) : CPPStmt(name, %s) { }", fname, loc_info);
Emit("ValPtr Exec(Frame* f, StmtFlowType& flow) override");
@ -216,7 +216,7 @@ void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf, const
// Generate initialization to create and register the lambda.
auto h = pf->HashVal();
auto nl = lambda_ids->length();
auto nl = lambda_ids->size();
bool has_captures = nl > 0;
auto gi = make_shared<LambdaRegistrationInfo>(this, l->Name(), ft, fname + "_cl", h, has_captures);
@ -364,7 +364,7 @@ void CPPCompile::GatherParamNames(vector<string>& p_names, const FuncTypePtr& ft
p_names.emplace_back(lambda_names[id]);
}
const ID* CPPCompile::FindParam(int i, const ProfileFunc* pf) {
IDPtr CPPCompile::FindParam(int i, const ProfileFunc* pf) {
const auto& params = pf->Params();
for ( const auto& p : params )

View file

@ -68,7 +68,7 @@ void GatherParamNames(std::vector<std::string>& p_names, const FuncTypePtr& ft,
// Inspects the given profile to find the i'th parameter (starting at 0).
// Returns nil if the profile indicates that the parameter is not used by the
// function.
const ID* FindParam(int i, const ProfileFunc* pf);
IDPtr FindParam(int i, const ProfileFunc* pf);
// Information associated with a CPPDynStmt dynamic dispatch.
struct DispatchInfo {
@ -92,7 +92,7 @@ std::unordered_map<std::string, std::string> func_index;
// Names for lambda capture ID's. These require a separate space that
// incorporates the lambda's name, to deal with nested lambda's that refer
// to the identifiers with the same name.
std::unordered_map<const ID*, std::string> lambda_names;
std::unordered_map<IDPtr, std::string> lambda_names;
// The function's parameters. Tracked so we don't re-declare them.
IDSet params;

View file

@ -63,8 +63,8 @@ void CPPCompile::Compile(bool report_uncompilable) {
(void)pfs->HashType(t);
rep_types.insert(TypeRep(t));
all_accessed_globals.insert(g.get());
accessed_globals.insert(g.get());
all_accessed_globals.insert(g);
accessed_globals.insert(g);
for ( const auto& i_e : g->GetOptInfo()->GetInitExprs() ) {
auto pf = std::make_shared<ProfileFunc>(i_e.get());

View file

@ -147,7 +147,7 @@ string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level) {
string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt) {
const auto& t = ne->GetType();
auto n = ne->Id();
const auto& n = ne->IdPtr();
bool is_global_var = global_vars.contains(n);
if ( t->Tag() == TYPE_FUNC && ! is_global_var ) {
@ -272,7 +272,7 @@ string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt, bool top_level) {
auto gen = GenExpr(f, GEN_DONT_CARE);
if ( f->Tag() == EXPR_NAME ) {
auto f_id = f->AsNameExpr()->Id();
const auto& f_id = f->AsNameExpr()->IdPtr();
const auto& params = f_id->GetType()->AsFuncType()->Params();
auto id_name = f_id->Name();
auto nargs = args_l->Exprs().length();
@ -1046,7 +1046,7 @@ string CPPCompile::GenAssign(const ExprPtr& lhs, const ExprPtr& rhs, const strin
string CPPCompile::GenDirectAssign(const ExprPtr& lhs, const string& rhs_native, const string& rhs_val_ptr, GenType gt,
bool top_level) {
auto n = lhs->AsNameExpr()->Id();
const auto& n = lhs->AsNameExpr()->IdPtr();
if ( n->IsBlank() )
return rhs_native;
@ -1134,7 +1134,7 @@ string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs) {
auto rhs_i = GenericValPtrToGT(rhs_i_base, t_i, GEN_NATIVE);
gen += IDNameStr(var->Id()) + " = " + rhs_i;
gen += IDNameStr(var->IdPtr()) + " = " + rhs_i;
if ( i < n - 1 )
gen += ", ";

View file

@ -195,7 +195,7 @@ void CPPCompile::InitializeGlobals() {
auto& ofiles = analysis_options.only_files;
for ( const auto& ginit : IDOptInfo::GetGlobalInitExprs() ) {
auto g = ginit.Id();
IDPtr g{NewRef{}, const_cast<ID*>(ginit.Id())};
if ( ! ofiles.empty() && ! obj_matches_opt_files(g) )
continue;

View file

@ -331,7 +331,7 @@ AttrInfo::AttrInfo(CPPCompile* _c, const AttrPtr& attr) : CompoundItemInfo(_c) {
}
else if ( a_e->Tag() == EXPR_NAME ) {
auto g = a_e->AsNameExpr()->Id();
auto g = a_e->AsNameExpr()->IdPtr();
gi = c->RegisterGlobal(g);
init_cohort = max(init_cohort, gi->InitCohort() + 1);
@ -363,7 +363,7 @@ AttrsInfo::AttrsInfo(CPPCompile* _c, const AttributesPtr& _attrs) : CompoundItem
}
}
GlobalLookupInitInfo::GlobalLookupInitInfo(CPPCompile* c, const ID* g, string _CPP_name, bool do_init)
GlobalLookupInitInfo::GlobalLookupInitInfo(CPPCompile* c, IDPtr g, string _CPP_name, bool do_init)
: CPP_InitInfo(g), CPP_name(std::move(_CPP_name)) {
Zeek_name = g->Name();
val = ValElem(c, do_init ? g->GetVal() : nullptr);
@ -375,7 +375,7 @@ void GlobalLookupInitInfo::InitializerVals(std::vector<std::string>& ivs) const
ivs.push_back(val);
}
GlobalInitInfo::GlobalInitInfo(CPPCompile* c, const ID* g, string _CPP_name)
GlobalInitInfo::GlobalInitInfo(CPPCompile* c, IDPtr g, string _CPP_name)
: GlobalLookupInitInfo(c, g, std::move(_CPP_name)) {
auto& gt = g->GetType();
auto gi = c->RegisterType(gt);

View file

@ -488,7 +488,7 @@ public:
// then the global will be (re-)initialized to its value during compilation.
class GlobalLookupInitInfo : public CPP_InitInfo {
public:
GlobalLookupInitInfo(CPPCompile* c, const ID* g, std::string CPP_name, bool do_init = false);
GlobalLookupInitInfo(CPPCompile* c, IDPtr g, std::string CPP_name, bool do_init = false);
std::string InitializerType() const override { return "CPP_GlobalLookupInit"; }
void InitializerVals(std::vector<std::string>& ivs) const override;
@ -502,7 +502,7 @@ protected:
// Information for initializing a Zeek global.
class GlobalInitInfo : public GlobalLookupInitInfo {
public:
GlobalInitInfo(CPPCompile* c, const ID* g, std::string CPP_name);
GlobalInitInfo(CPPCompile* c, IDPtr g, std::string CPP_name);
std::string InitializerType() const override { return "CPP_GlobalInit"; }
void InitializerVals(std::vector<std::string>& ivs) const override;

View file

@ -11,7 +11,7 @@ using namespace std;
void CPPCompile::GenStmt(const Stmt* s) {
auto loc = s->GetLocationInfo();
if ( loc != &detail::no_location && s->Tag() != STMT_LIST )
Emit("// %s:%d", loc->FileName(), loc->FirstLine());
Emit("// %s:%s", loc->FileName(), to_string(loc->FirstLine()));
switch ( s->Tag() ) {
case STMT_INIT: GenInitStmt(s->AsInitStmt()); break;
@ -223,7 +223,7 @@ void CPPCompile::GenTypeSwitchStmt(const Expr* e, const case_list* cases) {
Emit("}"); // end the scoping block
}
void CPPCompile::GenTypeSwitchCase(const ID* id, int case_offset, bool is_multi) {
void CPPCompile::GenTypeSwitchCase(const IDPtr id, int case_offset, bool is_multi) {
Emit("case %s:", Fmt(case_offset));
if ( ! id->Name() )
@ -310,13 +310,13 @@ void CPPCompile::GenWhenStmt(const WhenStmt* w) {
for ( auto& l : wi->WhenExprLocals() )
if ( IsAggr(l->GetType()) )
local_aggrs.push_back(IDNameStr(l.get()));
local_aggrs.push_back(IDNameStr(l));
auto when_lambda = GenExpr(wi->Lambda(), GEN_NATIVE);
GenWhenStmt(wi.get(), when_lambda, w->GetLocationInfo(), std::move(local_aggrs));
}
void CPPCompile::GenWhenStmt(const WhenInfo* wi, const std::string& when_lambda, const Location* loc,
void CPPCompile::GenWhenStmt(const WhenInfo* wi, const string& when_lambda, const Location* loc,
vector<string> local_aggrs) {
auto is_return = wi->IsReturn() ? "true" : "false";
auto timeout = wi->TimeoutExpr();
@ -333,7 +333,7 @@ void CPPCompile::GenWhenStmt(const WhenInfo* wi, const std::string& when_lambda,
StartBlock();
Emit("CPP__wi = std::make_shared<WhenInfo>(%s);", is_return);
for ( auto& wg : wi->WhenExprGlobals() )
Emit("CPP__w_globals.insert(find_global__CPP(\"%s\").get());", wg->Name());
Emit("CPP__w_globals.insert(find_global__CPP(\"%s\"));", wg->Name());
EndBlock();
NL();
@ -430,7 +430,8 @@ void CPPCompile::GenForOverTable(const ExprPtr& tbl, const IDPtr& value_var, con
Emit("%s = %s;", IDName(value_var),
GenericValPtrToGT("current_tev__CPP->GetVal()", value_var->GetType(), GEN_NATIVE));
for ( int i = 0; i < loop_vars->length(); ++i ) {
int n = static_cast<int>(loop_vars->size());
for ( int i = 0; i < n; ++i ) {
auto var = (*loop_vars)[i];
if ( var->IsBlank() )
continue;
@ -498,8 +499,8 @@ void CPPCompile::GenAssertStmt(const AssertStmt* a) {
Emit("auto msg_val = zeek::val_mgr->EmptyString();");
auto loc = a->GetLocationInfo();
Emit("static Location loc(\"%s\", %s, %s);", loc->FileName(), std::to_string(loc->FirstLine()),
std::to_string(loc->LastLine()));
Emit("static Location loc(\"%s\", %s, %s);", loc->FileName(), to_string(loc->FirstLine()),
to_string(loc->LastLine()));
Emit("report_assert(assert_result, \"%s\", msg_val, &loc);", CPPEscape(a->CondDesc().c_str()).c_str());
EndBlock();

View file

@ -16,7 +16,7 @@ void GenEventStmt(const EventStmt* ev);
void GenSwitchStmt(const SwitchStmt* sw);
void GenTypeSwitchStmt(const Expr* e, const case_list* cases);
void GenTypeSwitchCase(const ID* id, int case_offset, bool is_multi);
void GenTypeSwitchCase(const IDPtr id, int case_offset, bool is_multi);
void GenValueSwitchStmt(const Expr* e, const case_list* cases);
void GenWhenStmt(const WhenStmt* w);

View file

@ -7,7 +7,7 @@ namespace zeek::detail {
using namespace std;
void CPPCompile::CreateGlobal(const ID* g) {
void CPPCompile::CreateGlobal(IDPtr g) {
auto gn = string(g->Name());
bool is_bif = pfs->BiFGlobals().contains(g);
@ -45,7 +45,7 @@ void CPPCompile::CreateGlobal(const ID* g) {
global_vars.emplace(g);
}
std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g) {
std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(IDPtr g) {
auto gg = global_gis.find(g);
if ( gg != global_gis.end() )
@ -71,7 +71,7 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::RegisterGlobal(const ID* g) {
return gi;
}
std::shared_ptr<CPP_InitInfo> CPPCompile::GenerateGlobalInit(const ID* g) {
std::shared_ptr<CPP_InitInfo> CPPCompile::GenerateGlobalInit(IDPtr g) {
auto gn = string(g->Name());
if ( ! standalone )
return make_shared<GlobalLookupInitInfo>(this, g, globals[gn]);
@ -94,7 +94,7 @@ std::shared_ptr<CPP_InitInfo> CPPCompile::GenerateGlobalInit(const ID* g) {
return make_shared<GlobalLookupInitInfo>(this, g, globals[gn], needs_redef);
}
void CPPCompile::AddBiF(const ID* b, bool is_var) {
void CPPCompile::AddBiF(IDPtr b, bool is_var) {
auto bn = b->Name();
auto n = string(bn);
if ( is_var )
@ -117,7 +117,7 @@ bool CPPCompile::AddGlobal(const string& g, const char* suffix) {
void CPPCompile::RegisterEvent(string ev_name) { body_events[body_name].emplace_back(std::move(ev_name)); }
const string& CPPCompile::IDNameStr(const ID* id) {
const string& CPPCompile::IDNameStr(const IDPtr& id) {
if ( id->IsGlobal() ) {
auto g = string(id->Name());
if ( ! globals.contains(g) )
@ -130,7 +130,7 @@ const string& CPPCompile::IDNameStr(const ID* id) {
return l->second;
}
static string trim_name(const ID* id) {
static string trim_name(const IDPtr& id) {
auto n = id->Name();
auto without_module = strstr(n, "::");
@ -152,9 +152,9 @@ static string trim_name(const ID* id) {
return ns;
}
string CPPCompile::LocalName(const ID* l) const { return Canonicalize(trim_name(l)); }
string CPPCompile::LocalName(const IDPtr& l) const { return Canonicalize(trim_name(l)); }
string CPPCompile::CaptureName(const ID* c) const {
string CPPCompile::CaptureName(const IDPtr& c) const {
// We want to strip both the module and any inlining appendage.
auto tn = trim_name(c);

View file

@ -7,20 +7,20 @@
public:
// Tracks a global to generate the necessary initialization.
// Returns the associated initialization info.
std::shared_ptr<CPP_InitInfo> RegisterGlobal(const ID* g);
std::shared_ptr<CPP_InitInfo> RegisterGlobal(IDPtr g);
private:
// Generate declarations associated with the given global, and, if it's used
// as a variable (not just as a function being called), track it as such.
void CreateGlobal(const ID* g);
void CreateGlobal(IDPtr g);
// Low-level function for generating an initializer for a global. Takes
// into account differences for standalone-compilation.
std::shared_ptr<CPP_InitInfo> GenerateGlobalInit(const ID* g);
std::shared_ptr<CPP_InitInfo> GenerateGlobalInit(IDPtr g);
// Register the given identifier as a BiF. If is_var is true then the BiF
// is also used in a non-call context.
void AddBiF(const ID* b, bool is_var);
void AddBiF(IDPtr b, bool is_var);
// Register the given global name. "suffix" distinguishes particular types
// of globals, such as the names of bifs, global (non-function) variables,
@ -32,9 +32,8 @@ void RegisterEvent(std::string ev_name);
// The following match various forms of identifiers to the name used for
// their C++ equivalent.
const char* IDName(const IDPtr& id) { return IDName(id.get()); }
const char* IDName(const ID* id) { return IDNameStr(id).c_str(); }
const std::string& IDNameStr(const ID* id);
const char* IDName(const IDPtr& id) { return IDNameStr(id).c_str(); }
const std::string& IDNameStr(const IDPtr& id);
// Returns a canonicalized version of a variant of a global made distinct by
// the given suffix.
@ -42,12 +41,10 @@ std::string GlobalName(const std::string& g, const char* suffix) { return Canoni
// Returns a canonicalized form of a local identifier's name, expanding its
// module prefix if needed.
std::string LocalName(const ID* l) const;
std::string LocalName(const IDPtr& l) const { return LocalName(l.get()); }
std::string LocalName(const IDPtr& l) const;
// The same, but for a capture.
std::string CaptureName(const ID* l) const;
std::string CaptureName(const IDPtr& l) const { return CaptureName(l.get()); }
std::string CaptureName(const IDPtr& l) const;
// Returns a canonicalized name, with various non-alphanumeric characters
// stripped or transformed, and guaranteed not to conflict with C++ keywords.
@ -59,10 +56,10 @@ std::string GlobalName(const ExprPtr& e) { return globals[e->AsNameExpr()->Id()-
// Globals that are used (appear in the profiles) of the bodies we're
// compiling. Includes globals just used as functions to call.
std::unordered_set<const ID*> all_accessed_globals;
std::unordered_set<IDPtr> all_accessed_globals;
// Same, but just the globals used in contexts beyond function calls.
std::unordered_set<const ID*> accessed_globals;
std::unordered_set<IDPtr> accessed_globals;
// Lambdas that are accessed.
std::unordered_set<const LambdaExpr*> accessed_lambdas;
@ -74,10 +71,10 @@ std::unordered_set<std::string> accessed_events;
std::unordered_map<std::string, std::string> globals;
// Similar for locals, for the function currently being compiled.
std::unordered_map<const ID*, std::string> locals;
std::unordered_map<IDPtr, std::string> locals;
// Retrieves the initialization information associated with the given global.
std::unordered_map<const ID*, std::shared_ptr<CPP_InitInfo>> global_gis;
std::unordered_map<IDPtr, std::shared_ptr<CPP_InitInfo>> global_gis;
// Maps event names to the names we use for them.
std::unordered_map<std::string, std::string> events;