full tracking of the characteristics of globals when compiling scripts to C++

This commit is contained in:
Vern Paxson 2025-09-15 14:21:32 -07:00
parent b25a844210
commit bfc6508dff
6 changed files with 48 additions and 22 deletions

View file

@ -390,8 +390,12 @@ GlobalInitInfo::GlobalInitInfo(CPPCompile* c, IDPtr g, string _CPP_name)
else else
attrs = -1; attrs = -1;
is_exported = g->IsExport(); gc.is_exported = g->IsExport();
is_option = g->IsOption(); gc.is_const = g->IsConst();
gc.is_option = g->IsOption();
gc.is_enum_const = g->IsEnumConst();
gc.is_type = g->IsType();
val = ValElem(c, nullptr); // empty because we initialize dynamically val = ValElem(c, nullptr); // empty because we initialize dynamically
if ( gt->Tag() == TYPE_FUNC && (! g->GetVal() || g->GetVal()->AsFunc()->GetKind() == Func::BUILTIN_FUNC) ) if ( gt->Tag() == TYPE_FUNC && (! g->GetVal() || g->GetVal()->AsFunc()->GetKind() == Func::BUILTIN_FUNC) )
@ -407,8 +411,11 @@ void GlobalInitInfo::InitializerVals(std::vector<std::string>& ivs) const {
ivs.push_back(Fmt(type)); ivs.push_back(Fmt(type));
ivs.push_back(Fmt(attrs)); ivs.push_back(Fmt(attrs));
ivs.push_back(val); ivs.push_back(val);
ivs.push_back(Fmt(is_exported)); ivs.push_back(Fmt(gc.is_exported));
ivs.push_back(Fmt(is_option)); ivs.push_back(Fmt(gc.is_const));
ivs.push_back(Fmt(gc.is_option));
ivs.push_back(Fmt(gc.is_enum_const));
ivs.push_back(Fmt(gc.is_type));
ivs.push_back(Fmt(func_with_no_val)); ivs.push_back(Fmt(func_with_no_val));
} }

View file

@ -500,6 +500,16 @@ protected:
}; };
// Information for initializing a Zeek global. // Information for initializing a Zeek global.
// Tracks all of the characteristics associated with a global.
struct GlobalCharacteristics {
bool is_exported = false;
bool is_const = false;
bool is_option = false;
bool is_enum_const = false;
bool is_type = false;
};
class GlobalInitInfo : public GlobalLookupInitInfo { class GlobalInitInfo : public GlobalLookupInitInfo {
public: public:
GlobalInitInfo(CPPCompile* c, IDPtr g, std::string CPP_name); GlobalInitInfo(CPPCompile* c, IDPtr g, std::string CPP_name);
@ -511,8 +521,7 @@ protected:
int type; int type;
int attrs; int attrs;
std::string val; std::string val;
bool is_exported; GlobalCharacteristics gc;
bool is_option;
bool func_with_no_val = false; // needed to handle some error situations bool func_with_no_val = false; // needed to handle some error situations
}; };

View file

@ -135,12 +135,21 @@ void activate_bodies__CPP(const char* fn, const char* module, bool exported, Typ
event_registry->Register(e); event_registry->Register(e);
} }
IDPtr lookup_global__CPP(const char* g, const TypePtr& t, bool exported) { IDPtr lookup_global__CPP(const char* g, const TypePtr& t, const GlobalCharacteristics& gc) {
auto gl = lookup_ID(g, GLOBAL_MODULE_NAME, false, false, false); auto gl = lookup_ID(g, GLOBAL_MODULE_NAME, false, false, false);
if ( ! gl ) { if ( ! gl ) {
gl = install_ID(g, GLOBAL_MODULE_NAME, true, exported); gl = install_ID(g, GLOBAL_MODULE_NAME, true, gc.is_exported);
gl->SetType(t); gl->SetType(t);
if ( gc.is_const )
gl->SetConst();
if ( gc.is_option )
gl->SetOption();
if ( gc.is_enum_const )
gl->SetEnumConst();
if ( gc.is_type )
gl->MakeType();
} }
return gl; return gl;

View file

@ -6,6 +6,7 @@
#include "zeek/Val.h" #include "zeek/Val.h"
#include "zeek/script_opt/CPP/Func.h" #include "zeek/script_opt/CPP/Func.h"
#include "zeek/script_opt/CPP/InitsInfo.h"
namespace zeek { namespace zeek {
@ -65,9 +66,9 @@ extern void register_scripts__CPP(p_hash_type h, void (*callback)());
extern void activate_bodies__CPP(const char* fn, const char* module, bool exported, TypePtr t, extern void activate_bodies__CPP(const char* fn, const char* module, bool exported, TypePtr t,
std::vector<p_hash_type> hashes); std::vector<p_hash_type> hashes);
// Looks for a global with the given name. If not present, creates it // Looks for a global with the given name. If not present, creates it with
// with the given type and export setting. // the given type and characteristics.
extern IDPtr lookup_global__CPP(const char* g, const TypePtr& t, bool exported); extern IDPtr lookup_global__CPP(const char* g, const TypePtr& t, const GlobalCharacteristics& gc);
// Looks for a BiF with the given name. Returns nil if not present. // Looks for a BiF with the given name. Returns nil if not present.
extern Func* lookup_bif__CPP(const char* bif); extern Func* lookup_bif__CPP(const char* bif);

View file

@ -481,10 +481,7 @@ void CPP_GlobalLookupInit::Generate(InitsManager* im, std::vector<void*>& /* ini
void CPP_GlobalInit::Generate(InitsManager* im, std::vector<void*>& /* inits_vec */, int /* offset */) const { void CPP_GlobalInit::Generate(InitsManager* im, std::vector<void*>& /* inits_vec */, int /* offset */) const {
auto& t = im->Types(type); auto& t = im->Types(type);
global = lookup_global__CPP(name, t, is_exported); global = lookup_global__CPP(name, t, gc);
if ( is_option )
global->SetOption();
if ( ! global->HasVal() ) { if ( ! global->HasVal() ) {
if ( val >= 0 ) if ( val >= 0 )

View file

@ -401,17 +401,21 @@ protected:
class CPP_GlobalInit : public CPP_Init<void*> { class CPP_GlobalInit : public CPP_Init<void*> {
public: public:
CPP_GlobalInit(IDPtr& _global, const char* _name, int _type, int _attrs, int _val, bool _is_exported, CPP_GlobalInit(IDPtr& _global, const char* _name, int _type, int _attrs, int _val, bool is_exported, bool is_const,
bool _is_option, bool _func_with_no_val) bool is_option, bool is_enum_const, bool is_type, bool _func_with_no_val)
: CPP_Init<void*>(), : CPP_Init<void*>(),
global(_global), global(_global),
name(_name), name(_name),
type(_type), type(_type),
attrs(_attrs), attrs(_attrs),
val(_val), val(_val),
is_exported(_is_exported), func_with_no_val(_func_with_no_val) {
is_option(_is_option), gc.is_exported = is_exported;
func_with_no_val(_func_with_no_val) {} gc.is_const = is_const;
gc.is_option = is_option;
gc.is_enum_const = is_enum_const;
gc.is_type = is_type;
}
void Generate(InitsManager* im, std::vector<void*>& /* inits_vec */, int /* offset */) const override; void Generate(InitsManager* im, std::vector<void*>& /* inits_vec */, int /* offset */) const override;
@ -421,8 +425,7 @@ protected:
int type; int type;
int attrs; int attrs;
int val; int val;
bool is_exported; GlobalCharacteristics gc;
bool is_option;
bool func_with_no_val; bool func_with_no_val;
}; };