From d72884064a44b89bd481bcbe2b635b41ee7bb645 Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 13 May 2020 23:37:48 -0700 Subject: [PATCH] bifcl: Deprecate names in BifConst, replace with zeek::BifConst Some Val* types are also replaced with IntrusivePtr --- tools/bifcl/bif_arg.cc | 6 ++++-- tools/bifcl/builtin-func.y | 31 +++++++++++++++++++++++------ tools/bifcl/include/bif_arg.h | 2 +- tools/bifcl/include/bif_type.def | 34 ++++++++++++++++---------------- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/tools/bifcl/bif_arg.cc b/tools/bifcl/bif_arg.cc index cc9fc27cac..d87c5bb43e 100644 --- a/tools/bifcl/bif_arg.cc +++ b/tools/bifcl/bif_arg.cc @@ -13,11 +13,13 @@ static struct { const char* c_type; const char* c_type_smart; const char* accessor; + const char* accessor_smart; + const char* cast_smart; const char* constructor; const char* ctor_smart; } builtin_func_arg_type[] = { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ - {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart}, +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart) \ + {bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart}, #include "bif_type.def" #undef DEFINE_BIF_TYPE }; diff --git a/tools/bifcl/builtin-func.y b/tools/bifcl/builtin-func.y index 44e26ca8cf..27fd8aea8a 100644 --- a/tools/bifcl/builtin-func.y +++ b/tools/bifcl/builtin-func.y @@ -165,11 +165,13 @@ static struct { const char* c_type; const char* c_type_smart; const char* accessor; + const char* accessor_smart; + const char* cast_smart; const char* constructor; const char* ctor_smatr; } builtin_types[] = { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ - {bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart}, +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart) \ + {bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart}, #include "bif_type.def" #undef DEFINE_BIF_TYPE }; @@ -512,21 +514,38 @@ const_def: TOK_CONST opt_ws TOK_ID opt_ws ':' opt_ws TOK_ID opt_ws ';' set_decl_name($3); int typeidx = get_type_index($7); char accessor[1024]; + char accessor_smart[1024]; snprintf(accessor, sizeof(accessor), builtin_types[typeidx].accessor, ""); + snprintf(accessor_smart, sizeof(accessor_smart), builtin_types[typeidx].accessor_smart, ""); - fprintf(fp_netvar_h, "%s extern %s %s; %s\n", + fprintf(fp_netvar_h, "namespace zeek { %s extern %s %s; %s }\n", decl.c_namespace_start.c_str(), + builtin_types[typeidx].c_type_smart, decl.bare_name.c_str(), + decl.c_namespace_end.c_str()); + fprintf(fp_netvar_h, "%s [[deprecated(\"Remove in v4.1. Use zeek::%s.\")]] extern %s %s; %s\n", + decl.c_namespace_start.c_str(), decl.c_fullname.c_str(), builtin_types[typeidx].c_type, decl.bare_name.c_str(), decl.c_namespace_end.c_str()); + + fprintf(fp_netvar_def, "namespace zeek { %s %s %s; %s }\n", + decl.c_namespace_start.c_str(), + builtin_types[typeidx].c_type_smart, decl.bare_name.c_str(), + decl.c_namespace_end.c_str()); fprintf(fp_netvar_def, "%s %s %s; %s\n", decl.c_namespace_start.c_str(), builtin_types[typeidx].c_type, decl.bare_name.c_str(), decl.c_namespace_end.c_str()); - fprintf(fp_netvar_init, "\t%s = zeek::id::lookup_const(\"%s\").get()%s;\n", - decl.c_fullname.c_str(), decl.bro_fullname.c_str(), - accessor); + + fprintf(fp_netvar_init, "\t{\n"); + fprintf(fp_netvar_init, "\tconst auto& v = zeek::id::lookup_const%s(\"%s\");\n", + builtin_types[typeidx].cast_smart, decl.bro_fullname.c_str()); + fprintf(fp_netvar_init, "\tzeek::%s = v%s;\n", + decl.c_fullname.c_str(), accessor_smart); + fprintf(fp_netvar_init, "\t%s = v.get()%s;\n", + decl.c_fullname.c_str(), accessor); + fprintf(fp_netvar_init, "\t}\n"); record_bif_item(decl.bro_fullname.c_str(), "CONSTANT"); } diff --git a/tools/bifcl/include/bif_arg.h b/tools/bifcl/include/bif_arg.h index c1dd5064b6..bd8b9c58d2 100644 --- a/tools/bifcl/include/bif_arg.h +++ b/tools/bifcl/include/bif_arg.h @@ -3,7 +3,7 @@ #include enum builtin_func_arg_type { -#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) \ +#define DEFINE_BIF_TYPE(id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart) \ id, #include "bif_type.def" #undef DEFINE_BIF_TYPE diff --git a/tools/bifcl/include/bif_type.def b/tools/bifcl/include/bif_type.def index 974e8c6db5..86c8673f77 100644 --- a/tools/bifcl/include/bif_type.def +++ b/tools/bifcl/include/bif_type.def @@ -1,17 +1,17 @@ -// (id, bif_type, bro_type, c_type, c_type_smart, accessor, constructor, ctor_smart) -DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "IntrusivePtr", "%s->AsAddrVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "int", "%s->AsBool()", "val_mgr->Bool(%s)", "val_mgr->Bool(%s)") -DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->ConnVal()", "%s->ConnVal()") -DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "bro_uint_t", "%s->AsCount()", "val_mgr->Count(%s)", "val_mgr->Count(%s)") -DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "double", "%s->AsDouble()", "make_intrusive(%s, TYPE_DOUBLE)", "make_intrusive(%s, TYPE_DOUBLE)") -DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "IntrusivePtr", "%s->AsFile()", "make_intrusive(%s)", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "bro_int_t", "%s->AsInt()", "val_mgr->Int(%s)", "val_mgr->Int(%s)") -DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "double", "%s->AsInterval()", "make_intrusive(%s, Seconds)", "make_intrusive(%s, Seconds)") -DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "IntrusivePtr", "%s->AsPattern()", "make_intrusive(%s)", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "IntrusivePtr", "%s->AsPortVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "IntrusivePtr", "%s->AsStringVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "IntrusivePtr", "%s->AsSubNetVal()", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") -DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "double", "%s->AsTime()", "make_intrusive(%s, TYPE_TIME)", "make_intrusive(%s, TYPE_TIME)") -DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "IntrusivePtr", "%s", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +// (id, bif_type, bro_type, c_type, c_type_smart, accessor, accessor_smart, cast_smart, constructor, ctor_smart) +DEFINE_BIF_TYPE(TYPE_ADDR, "addr", "addr", "AddrVal*", "IntrusivePtr", "%s->AsAddrVal()", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_ANY, "any", "any", "Val*", "IntrusivePtr", "%s", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_BOOL, "bool", "bool", "int", "int", "%s->AsBool()", "%s->AsBool()", "", "val_mgr->Bool(%s)", "val_mgr->Bool(%s)") +DEFINE_BIF_TYPE(TYPE_CONN_ID, "conn_id", "conn_id", "Val*", "IntrusivePtr", "%s", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_CONNECTION, "connection", "connection", "Connection*", "Connection*", "%s->AsRecordVal()->GetOrigin()", "%s->AsRecordVal()->GetOrigin()", "", "%s->ConnVal()", "%s->ConnVal()") +DEFINE_BIF_TYPE(TYPE_COUNT, "count", "count", "bro_uint_t", "bro_uint_t", "%s->AsCount()", "%s->AsCount()", "", "val_mgr->Count(%s)", "val_mgr->Count(%s)") +DEFINE_BIF_TYPE(TYPE_DOUBLE, "double", "double", "double", "double", "%s->AsDouble()", "%s->AsDouble()", "", "make_intrusive(%s, TYPE_DOUBLE)", "make_intrusive(%s, TYPE_DOUBLE)") +DEFINE_BIF_TYPE(TYPE_FILE, "file", "file", "BroFile*", "IntrusivePtr", "%s->AsFile()", "%s", "", "make_intrusive(%s)", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_INT, "int", "int", "bro_int_t", "bro_int_t", "%s->AsInt()", "%s->AsInt()", "", "val_mgr->Int(%s)", "val_mgr->Int(%s)") +DEFINE_BIF_TYPE(TYPE_INTERVAL, "interval", "interval", "double", "double", "%s->AsInterval()", "%s->AsInterval()", "", "make_intrusive(%s, Seconds)", "make_intrusive(%s, Seconds)") +DEFINE_BIF_TYPE(TYPE_PATTERN, "pattern", "pattern", "RE_Matcher*", "IntrusivePtr", "%s->AsPattern()", "%s", "", "make_intrusive(%s)", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_PORT, "port", "port", "PortVal*", "IntrusivePtr", "%s->AsPortVal()", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_STRING, "string", "string", "StringVal*", "IntrusivePtr", "%s->AsStringVal()", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_SUBNET, "subnet", "subnet", "SubNetVal*", "IntrusivePtr", "%s->AsSubNetVal()", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)") +DEFINE_BIF_TYPE(TYPE_TIME, "time", "time", "double", "double", "%s->AsTime()", "%s->AsTime()", "", "make_intrusive(%s, TYPE_TIME)", "make_intrusive(%s, TYPE_TIME)") +DEFINE_BIF_TYPE(TYPE_OTHER, "", "", "Val*", "IntrusivePtr", "%s", "%s", "", "IntrusivePtr{AdoptRef{}, %s}", "std::move(%s)")