decluttered code by removing "std::" - no semantic changes

(also some whitespace fixes)
This commit is contained in:
Vern Paxson 2021-04-26 12:49:08 -07:00
parent bbe5ab39f6
commit 72413f315c
18 changed files with 420 additions and 397 deletions

View file

@ -5,6 +5,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::RegisterAttributes(const AttributesPtr& attrs) void CPPCompile::RegisterAttributes(const AttributesPtr& attrs)
{ {
if ( ! attrs || attributes.HasKey(attrs) ) if ( ! attrs || attributes.HasKey(attrs) )
@ -43,8 +45,8 @@ void CPPCompile::RegisterAttributes(const AttributesPtr& attrs)
} }
} }
void CPPCompile::BuildAttrs(const AttributesPtr& attrs, std::string& attr_tags, void CPPCompile::BuildAttrs(const AttributesPtr& attrs, string& attr_tags,
std::string& attr_vals) string& attr_vals)
{ {
if ( attrs ) if ( attrs )
{ {
@ -67,8 +69,8 @@ void CPPCompile::BuildAttrs(const AttributesPtr& attrs, std::string& attr_tags,
} }
} }
attr_tags = std::string("{") + attr_tags + "}"; attr_tags = string("{") + attr_tags + "}";
attr_vals = std::string("{") + attr_vals + "}"; attr_vals = string("{") + attr_vals + "}";
} }
void CPPCompile::GenAttrs(const AttributesPtr& attrs) void CPPCompile::GenAttrs(const AttributesPtr& attrs)
@ -80,7 +82,7 @@ void CPPCompile::GenAttrs(const AttributesPtr& attrs)
StartBlock(); StartBlock();
const auto& avec = attrs->GetAttrs(); const auto& avec = attrs->GetAttrs();
Emit("auto attrs = std::vector<AttrPtr>();"); Emit("auto attrs = vector<AttrPtr>();");
AddInit(attrs); AddInit(attrs);
@ -99,7 +101,7 @@ void CPPCompile::GenAttrs(const AttributesPtr& attrs)
NoteInitDependency(attrs, e); NoteInitDependency(attrs, e);
AddInit(e); AddInit(e);
std::string e_arg; string e_arg;
if ( IsSimpleInitExpr(e) ) if ( IsSimpleInitExpr(e) )
e_arg = GenAttrExpr(e); e_arg = GenAttrExpr(e);
else else
@ -114,21 +116,21 @@ void CPPCompile::GenAttrs(const AttributesPtr& attrs)
EndBlock(); EndBlock();
} }
std::string CPPCompile::GenAttrExpr(const ExprPtr& e) string CPPCompile::GenAttrExpr(const ExprPtr& e)
{ {
switch ( e->Tag() ) { switch ( e->Tag() ) {
case EXPR_CONST: case EXPR_CONST:
return std::string("make_intrusive<ConstExpr>(") + return string("make_intrusive<ConstExpr>(") +
GenExpr(e, GEN_VAL_PTR) + ")"; GenExpr(e, GEN_VAL_PTR) + ")";
case EXPR_NAME: case EXPR_NAME:
NoteInitDependency(e, e->AsNameExpr()->IdPtr()); NoteInitDependency(e, e->AsNameExpr()->IdPtr());
return std::string("make_intrusive<NameExpr>(") + return string("make_intrusive<NameExpr>(") +
globals[e->AsNameExpr()->Id()->Name()] + ")"; globals[e->AsNameExpr()->Id()->Name()] + ")";
case EXPR_RECORD_COERCE: case EXPR_RECORD_COERCE:
NoteInitDependency(e, TypeRep(e->GetType())); NoteInitDependency(e, TypeRep(e->GetType()));
return std::string("make_intrusive<RecordCoerceExpr>(make_intrusive<RecordConstructorExpr>(make_intrusive<ListExpr>()), cast_intrusive<RecordType>(") + return string("make_intrusive<RecordCoerceExpr>(make_intrusive<RecordConstructorExpr>(make_intrusive<ListExpr>()), cast_intrusive<RecordType>(") +
GenTypeName(e->GetType()) + "))"; GenTypeName(e->GetType()) + "))";
default: default:
@ -137,7 +139,7 @@ std::string CPPCompile::GenAttrExpr(const ExprPtr& e)
} }
} }
std::string CPPCompile::AttrsName(const AttributesPtr& a) string CPPCompile::AttrsName(const AttributesPtr& a)
{ {
return attributes.KeyName(a) + "()"; return attributes.KeyName(a) + "()";
} }

View file

@ -7,7 +7,9 @@
namespace zeek::detail { namespace zeek::detail {
std::string CPPCompile::BuildConstant(const Obj* parent, const ValPtr& vp) using namespace std;
string CPPCompile::BuildConstant(const Obj* parent, const ValPtr& vp)
{ {
if ( ! vp ) if ( ! vp )
return "nullptr"; return "nullptr";
@ -55,7 +57,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
// Formulate a key that's unique per distinct constant. // Formulate a key that's unique per distinct constant.
const auto& t = v->GetType(); const auto& t = v->GetType();
std::string c_desc; string c_desc;
if ( t->Tag() == TYPE_STRING ) if ( t->Tag() == TYPE_STRING )
{ {
@ -63,7 +65,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
// escaping, sigh. Just use the raw string. // escaping, sigh. Just use the raw string.
auto s = v->AsString(); auto s = v->AsString();
auto b = (const char*)(s->Bytes()); auto b = (const char*)(s->Bytes());
c_desc = std::string(b, s->Len()) + "string"; c_desc = string(b, s->Len()) + "string";
} }
else else
{ {
@ -90,8 +92,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
} }
// Need a C++ global for this constant. // Need a C++ global for this constant.
auto const_name = std::string("CPP__const__") + auto const_name = string("CPP__const__") + Fmt(int(constants.size()));
Fmt(int(constants.size()));
const_vals[v] = constants[c_desc] = const_name; const_vals[v] = constants[c_desc] = const_name;
constants_to_vals[c_desc] = v; constants_to_vals[c_desc] = v;
@ -134,7 +135,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
v->Describe(&d); v->Describe(&d);
AddInit(v, const_name, AddInit(v, const_name,
std::string("make_intrusive<") + prefix + string("make_intrusive<") + prefix +
"Val>(\"" + d.Description() + "\")"); "Val>(\"" + d.Description() + "\")");
} }
break; break;
@ -156,7 +157,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
auto f = cast_intrusive<FileVal>(vp)->Get(); auto f = cast_intrusive<FileVal>(vp)->Get();
AddInit(v, const_name, AddInit(v, const_name,
std::string("make_intrusive<FileVal>(") + string("make_intrusive<FileVal>(") +
"make_intrusive<File>(\"" + f->Name() + "\", \"w\"))"); "make_intrusive<File>(\"" + f->Name() + "\", \"w\"))");
} }
break; break;
@ -168,7 +169,7 @@ bool CPPCompile::AddConstant(const ValPtr& vp)
return true; return true;
} }
void CPPCompile::AddStringConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddStringConstant(const ValPtr& v, string& const_name)
{ {
Emit("StringValPtr %s;", const_name); Emit("StringValPtr %s;", const_name);
@ -179,13 +180,13 @@ void CPPCompile::AddStringConstant(const ValPtr& v, std::string& const_name)
AddInit(v, const_name, GenString(b, len)); AddInit(v, const_name, GenString(b, len));
} }
void CPPCompile::AddPatternConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddPatternConstant(const ValPtr& v, string& const_name)
{ {
Emit("PatternValPtr %s;", const_name); Emit("PatternValPtr %s;", const_name);
auto re = v->AsPatternVal()->Get(); auto re = v->AsPatternVal()->Get();
AddInit(v, std::string("{ auto re = new RE_Matcher(") + AddInit(v, string("{ auto re = new RE_Matcher(") +
CPPEscape(re->OrigText()) + ");"); CPPEscape(re->OrigText()) + ");");
if ( re->IsCaseInsensitive() ) if ( re->IsCaseInsensitive() )
@ -196,7 +197,7 @@ void CPPCompile::AddPatternConstant(const ValPtr& v, std::string& const_name)
AddInit(v, "}"); AddInit(v, "}");
} }
void CPPCompile::AddListConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddListConstant(const ValPtr& v, string& const_name)
{ {
Emit("ListValPtr %s;", const_name); Emit("ListValPtr %s;", const_name);
@ -204,7 +205,7 @@ void CPPCompile::AddListConstant(const ValPtr& v, std::string& const_name)
// use the underlying TypeList. However, we *do* use the types of // use the underlying TypeList. However, we *do* use the types of
// the elements. // the elements.
AddInit(v, const_name, std::string("make_intrusive<ListVal>(TYPE_ANY)")); AddInit(v, const_name, string("make_intrusive<ListVal>(TYPE_ANY)"));
auto lv = cast_intrusive<ListVal>(v); auto lv = cast_intrusive<ListVal>(v);
auto n = lv->Length(); auto n = lv->Length();
@ -218,7 +219,7 @@ void CPPCompile::AddListConstant(const ValPtr& v, std::string& const_name)
} }
} }
void CPPCompile::AddRecordConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddRecordConstant(const ValPtr& v, string& const_name)
{ {
const auto& t = v->GetType(); const auto& t = v->GetType();
@ -226,7 +227,7 @@ void CPPCompile::AddRecordConstant(const ValPtr& v, std::string& const_name)
NoteInitDependency(v, TypeRep(t)); NoteInitDependency(v, TypeRep(t));
AddInit(v, const_name, std::string("make_intrusive<RecordVal>(") + AddInit(v, const_name, string("make_intrusive<RecordVal>(") +
"cast_intrusive<RecordType>(" + GenTypeName(t) + "))"); "cast_intrusive<RecordType>(" + GenTypeName(t) + "))");
auto r = cast_intrusive<RecordVal>(v); auto r = cast_intrusive<RecordVal>(v);
@ -245,7 +246,7 @@ void CPPCompile::AddRecordConstant(const ValPtr& v, std::string& const_name)
} }
} }
void CPPCompile::AddTableConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddTableConstant(const ValPtr& v, string& const_name)
{ {
const auto& t = v->GetType(); const auto& t = v->GetType();
@ -253,7 +254,7 @@ void CPPCompile::AddTableConstant(const ValPtr& v, std::string& const_name)
NoteInitDependency(v, TypeRep(t)); NoteInitDependency(v, TypeRep(t));
AddInit(v, const_name, std::string("make_intrusive<TableVal>(") + AddInit(v, const_name, string("make_intrusive<TableVal>(") +
"cast_intrusive<TableType>(" + GenTypeName(t) + "))"); "cast_intrusive<TableType>(" + GenTypeName(t) + "))");
auto tv = cast_intrusive<TableVal>(v); auto tv = cast_intrusive<TableVal>(v);
@ -267,7 +268,7 @@ void CPPCompile::AddTableConstant(const ValPtr& v, std::string& const_name)
} }
} }
void CPPCompile::AddVectorConstant(const ValPtr& v, std::string& const_name) void CPPCompile::AddVectorConstant(const ValPtr& v, string& const_name)
{ {
const auto& t = v->GetType(); const auto& t = v->GetType();
@ -275,7 +276,7 @@ void CPPCompile::AddVectorConstant(const ValPtr& v, std::string& const_name)
NoteInitDependency(v, TypeRep(t)); NoteInitDependency(v, TypeRep(t));
AddInit(v, const_name, std::string("make_intrusive<VectorVal>(") + AddInit(v, const_name, string("make_intrusive<VectorVal>(") +
"cast_intrusive<VectorType>(" + GenTypeName(t) + "))"); "cast_intrusive<VectorType>(" + GenTypeName(t) + "))");
auto vv = cast_intrusive<VectorVal>(v); auto vv = cast_intrusive<VectorVal>(v);

View file

@ -9,6 +9,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::DeclareFunc(const FuncInfo& func) void CPPCompile::DeclareFunc(const FuncInfo& func)
{ {
if ( ! IsCompilable(func) ) if ( ! IsCompilable(func) )
@ -41,7 +43,7 @@ void CPPCompile::DeclareLambda(const LambdaExpr* l, const ProfileFunc* pf)
} }
void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf, void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf,
const std::string& fname, const string& fname,
const StmtPtr& body, int priority, const StmtPtr& body, int priority,
const LambdaExpr* l, FunctionFlavor flavor) const LambdaExpr* l, FunctionFlavor flavor)
{ {
@ -59,8 +61,8 @@ void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf,
Emit("public:"); Emit("public:");
std::string addl_args; // captures passed in on construction string addl_args; // captures passed in on construction
std::string inits; // initializers for corresponding member vars string inits; // initializers for corresponding member vars
if ( lambda_ids ) if ( lambda_ids )
{ {
@ -131,13 +133,13 @@ void CPPCompile::DeclareSubclass(const FuncTypePtr& ft, const ProfileFunc* pf,
body_hashes[fname] = h; body_hashes[fname] = h;
body_priorities[fname] = priority; body_priorities[fname] = priority;
body_names.emplace(body.get(), fname); body_names.emplace(body.get(), fname);
names_to_bodies.emplace(std::move(fname), body.get()); names_to_bodies.emplace(move(fname), body.get());
total_hash = merge_p_hashes(total_hash, h); total_hash = merge_p_hashes(total_hash, h);
} }
void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf, void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf,
const std::string& fname, const StmtPtr& body, const string& fname, const StmtPtr& body,
const LambdaExpr* l, const IDPList* lambda_ids) const LambdaExpr* l, const IDPList* lambda_ids)
{ {
// Declare the member variables for holding the captures. // Declare the member variables for holding the captures.
@ -149,14 +151,14 @@ void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf,
} }
// Generate initialization to create and register the lambda. // Generate initialization to create and register the lambda.
auto literal_name = std::string("\"") + l->Name() + "\""; auto literal_name = string("\"") + l->Name() + "\"";
auto instantiate = std::string("make_intrusive<") + fname + "_cl>(" + auto instantiate = string("make_intrusive<") + fname + "_cl>(" +
literal_name + ")"; literal_name + ")";
int nl = lambda_ids->length(); int nl = lambda_ids->length();
auto h = Fmt(pf->HashVal()); auto h = Fmt(pf->HashVal());
auto has_captures = nl > 0 ? "true" : "false"; auto has_captures = nl > 0 ? "true" : "false";
auto l_init = std::string("register_lambda__CPP(") + instantiate + auto l_init = string("register_lambda__CPP(") + instantiate +
", " + h + ", \"" + l->Name() + "\", " + ", " + h + ", \"" + l->Name() + "\", " +
GenTypeName(ft) + ", " + has_captures + ");"; GenTypeName(ft) + ", " + has_captures + ");";
@ -178,7 +180,7 @@ void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf,
{ {
auto l_i = (*lambda_ids)[i]; auto l_i = (*lambda_ids)[i];
const auto& t_i = l_i->GetType(); const auto& t_i = l_i->GetType();
auto cap_i = std::string("f->GetElement(") + Fmt(i) + ")"; auto cap_i = string("f->GetElement(") + Fmt(i) + ")";
Emit("%s = %s;", lambda_names[l_i], Emit("%s = %s;", lambda_names[l_i],
GenericValPtrToGT(cap_i, t_i, GEN_NATIVE)); GenericValPtrToGT(cap_i, t_i, GEN_NATIVE));
} }
@ -206,17 +208,17 @@ void CPPCompile::BuildLambda(const FuncTypePtr& ft, const ProfileFunc* pf,
EndBlock(); EndBlock();
} }
std::string CPPCompile::BindArgs(const FuncTypePtr& ft, const IDPList* lambda_ids) string CPPCompile::BindArgs(const FuncTypePtr& ft, const IDPList* lambda_ids)
{ {
const auto& params = ft->Params(); const auto& params = ft->Params();
auto t = params->Types(); auto t = params->Types();
std::string res; string res;
int n = t ? t->size() : 0; int n = t ? t->size() : 0;
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
auto arg_i = std::string("f->GetElement(") + Fmt(i) + ")"; auto arg_i = string("f->GetElement(") + Fmt(i) + ")";
const auto& ft = params->GetFieldType(i); const auto& ft = params->GetFieldType(i);
if ( IsNativeType(ft) ) if ( IsNativeType(ft) )
@ -237,21 +239,20 @@ std::string CPPCompile::BindArgs(const FuncTypePtr& ft, const IDPList* lambda_id
return res + "f"; return res + "f";
} }
std::string CPPCompile::ParamDecl(const FuncTypePtr& ft, string CPPCompile::ParamDecl(const FuncTypePtr& ft, const IDPList* lambda_ids,
const IDPList* lambda_ids, const ProfileFunc* pf)
const ProfileFunc* pf)
{ {
const auto& params = ft->Params(); const auto& params = ft->Params();
int n = params->NumFields(); int n = params->NumFields();
std::string decl; string decl;
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
const auto& t = params->GetFieldType(i); const auto& t = params->GetFieldType(i);
auto tn = FullTypeName(t); auto tn = FullTypeName(t);
auto param_id = FindParam(i, pf); auto param_id = FindParam(i, pf);
std::string fn; string fn;
if ( param_id ) if ( param_id )
{ {
@ -260,7 +261,7 @@ std::string CPPCompile::ParamDecl(const FuncTypePtr& ft,
// We'll need to translate the parameter // We'll need to translate the parameter
// from its current representation to // from its current representation to
// type "any". // type "any".
fn = std::string("any_param__CPP_") + Fmt(i); fn = string("any_param__CPP_") + Fmt(i);
else else
fn = LocalName(param_id); fn = LocalName(param_id);
} }
@ -270,7 +271,7 @@ std::string CPPCompile::ParamDecl(const FuncTypePtr& ft,
// name out of the function's declaration, we // name out of the function's declaration, we
// explicitly name them to reflect that they're // explicitly name them to reflect that they're
// unused. // unused.
fn = std::string("unused_param__CPP_") + Fmt(i); fn = string("unused_param__CPP_") + Fmt(i);
if ( IsNativeType(t) ) if ( IsNativeType(t) )
// Native types are always pass-by-value. // Native types are always pass-by-value.

View file

@ -9,8 +9,10 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
CPPCompile::CPPCompile(std::vector<FuncInfo>& _funcs, ProfileFuncs& _pfs,
CPPCompile::CPPCompile(vector<FuncInfo>& _funcs, ProfileFuncs& _pfs,
const char* gen_name, CPPHashManager& _hm, const char* gen_name, CPPHashManager& _hm,
bool _update, bool _standalone) bool _update, bool _standalone)
: funcs(_funcs), pfs(_pfs), hm(_hm), update(_update), standalone(_standalone) : funcs(_funcs), pfs(_pfs), hm(_hm), update(_update), standalone(_standalone)
@ -124,14 +126,14 @@ void CPPCompile::Compile()
RegisterAttributes(g->GetAttrs()); RegisterAttributes(g->GetAttrs());
if ( g->HasVal() ) if ( g->HasVal() )
{ {
auto gn = std::string(g->Name()); auto gn = string(g->Name());
GenGlobalInit(g, globals[gn], g->GetVal()); GenGlobalInit(g, globals[gn], g->GetVal());
} }
} }
for ( const auto& e : pfs.Events() ) for ( const auto& e : pfs.Events() )
if ( AddGlobal(e, "gl", false) ) if ( AddGlobal(e, "gl", false) )
Emit("EventHandlerPtr %s_ev;", globals[std::string(e)]); Emit("EventHandlerPtr %s_ev;", globals[string(e)]);
for ( const auto& t : pfs.RepTypes() ) for ( const auto& t : pfs.RepTypes() )
{ {
@ -149,7 +151,7 @@ void CPPCompile::Compile()
// LambdaExpr's can wind up referring to the same underlying lambda // LambdaExpr's can wind up referring to the same underlying lambda
// if the bodies happen to be identical. In that case, we don't // if the bodies happen to be identical. In that case, we don't
// want to generate the lambda twice. // want to generate the lambda twice.
std::unordered_set<std::string> lambda_names; unordered_set<string> lambda_names;
for ( const auto& l : pfs.Lambdas() ) for ( const auto& l : pfs.Lambdas() )
{ {
const auto& n = l->Name(); const auto& n = l->Name();
@ -202,13 +204,13 @@ void CPPCompile::GenProlog()
NL(); NL();
} }
void CPPCompile::RegisterCompiledBody(const std::string& f) void CPPCompile::RegisterCompiledBody(const string& f)
{ {
auto h = body_hashes[f]; auto h = body_hashes[f];
auto p = body_priorities[f]; auto p = body_priorities[f];
// Build up an initializer of the events relevant to the function. // Build up an initializer of the events relevant to the function.
std::string events; string events;
if ( body_events.count(f) > 0 ) if ( body_events.count(f) > 0 )
for ( auto e : body_events[f] ) for ( auto e : body_events[f] )
{ {
@ -217,7 +219,7 @@ void CPPCompile::RegisterCompiledBody(const std::string& f)
events = events + "\"" + e + "\""; events = events + "\"" + e + "\"";
} }
events = std::string("{") + events + "}"; events = string("{") + events + "}";
if ( addl_tag > 0 ) if ( addl_tag > 0 )
// Hash in the location associated with this compilation // Hash in the location associated with this compilation
@ -229,7 +231,7 @@ void CPPCompile::RegisterCompiledBody(const std::string& f)
// same binary). // same binary).
h = merge_p_hashes(h, p_hash(cf_locs[f])); h = merge_p_hashes(h, p_hash(cf_locs[f]));
auto init = std::string("register_body__CPP(make_intrusive<") + auto init = string("register_body__CPP(make_intrusive<") +
f + "_cl>(\"" + f + "\"), " + Fmt(p) + ", " + f + "_cl>(\"" + f + "\"), " + Fmt(p) + ", " +
Fmt(h) + ", " + events + ");"; Fmt(h) + ", " + events + ");";
@ -274,7 +276,7 @@ void CPPCompile::GenEpilog()
GenPreInits(); GenPreInits();
std::unordered_set<const Obj*> to_do; unordered_set<const Obj*> to_do;
for ( const auto& oi : obj_inits ) for ( const auto& oi : obj_inits )
to_do.insert(oi.first); to_do.insert(oi.first);

View file

@ -9,6 +9,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::StartBlock() void CPPCompile::StartBlock()
{ {
++block_level; ++block_level;
@ -21,15 +23,15 @@ void CPPCompile::EndBlock(bool needs_semi)
--block_level; --block_level;
} }
std::string CPPCompile::GenString(const char* b, int len) const string CPPCompile::GenString(const char* b, int len) const
{ {
return std::string("make_intrusive<StringVal>(") + Fmt(len) + return string("make_intrusive<StringVal>(") + Fmt(len) + ", " +
", " + CPPEscape(b, len) + ")"; CPPEscape(b, len) + ")";
} }
std::string CPPCompile::CPPEscape(const char* b, int len) const string CPPCompile::CPPEscape(const char* b, int len) const
{ {
std::string res = "\""; string res = "\"";
for ( int i = 0; i < len; ++i ) for ( int i = 0; i < len; ++i )
{ {

View file

@ -11,21 +11,23 @@
namespace zeek::detail { namespace zeek::detail {
std::string CPPCompile::GenExprs(const Expr* e) using namespace std;
string CPPCompile::GenExprs(const Expr* e)
{ {
std::string gen; string gen;
if ( e->Tag() == EXPR_LIST ) if ( e->Tag() == EXPR_LIST )
gen = GenListExpr(e, GEN_VAL_PTR, true); gen = GenListExpr(e, GEN_VAL_PTR, true);
else else
gen = GenExpr(e, GEN_VAL_PTR); gen = GenExpr(e, GEN_VAL_PTR);
return std::string("{ ") + gen + " }"; return string("{ ") + gen + " }";
} }
std::string CPPCompile::GenListExpr(const Expr* e, GenType gt, bool nested) string CPPCompile::GenListExpr(const Expr* e, GenType gt, bool nested)
{ {
const auto& exprs = e->AsListExpr()->Exprs(); const auto& exprs = e->AsListExpr()->Exprs();
std::string gen; string gen;
int n = exprs.size(); int n = exprs.size();
@ -36,7 +38,7 @@ std::string CPPCompile::GenListExpr(const Expr* e, GenType gt, bool nested)
if ( nested && e_i->Tag() == EXPR_LIST ) if ( nested && e_i->Tag() == EXPR_LIST )
// These are table or set indices. // These are table or set indices.
gen_i = std::string("index_val__CPP({") + gen_i + "})"; gen_i = string("index_val__CPP({") + gen_i + "})";
gen += gen_i; gen += gen_i;
@ -47,9 +49,9 @@ std::string CPPCompile::GenListExpr(const Expr* e, GenType gt, bool nested)
return gen; return gen;
} }
std::string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level) string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level)
{ {
std::string gen; string gen;
switch ( e->Tag() ) { switch ( e->Tag() ) {
case EXPR_NAME: return GenNameExpr(e->AsNameExpr(), gt); case EXPR_NAME: return GenNameExpr(e->AsNameExpr(), gt);
@ -119,7 +121,7 @@ std::string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level)
ASSERT(0); ASSERT(0);
case EXPR_CAST: case EXPR_CAST:
gen = std::string("cast_value_to_type__CPP(") + gen = string("cast_value_to_type__CPP(") +
GenExpr(e->GetOp1(), GEN_VAL_PTR) + ", " + GenExpr(e->GetOp1(), GEN_VAL_PTR) + ", " +
GenTypeName(e->GetType()) + ")"; GenTypeName(e->GetType()) + ")";
return GenericValPtrToGT(gen, e->GetType(), gt); return GenericValPtrToGT(gen, e->GetType(), gt);
@ -134,11 +136,11 @@ std::string CPPCompile::GenExpr(const Expr* e, GenType gt, bool top_level)
default: default:
// Intended to catch errors in overlooking the possible // Intended to catch errors in overlooking the possible
// expressions that might appear. // expressions that might appear.
return std::string("EXPR"); return string("EXPR");
} }
} }
std::string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt) string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt)
{ {
const auto& t = ne->GetType(); const auto& t = ne->GetType();
auto n = ne->Id(); auto n = ne->Id();
@ -154,10 +156,10 @@ std::string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt)
if ( is_global_var ) if ( is_global_var )
{ {
std::string gen; string gen;
if ( n->IsType() ) if ( n->IsType() )
gen = std::string("make_intrusive<TypeVal>(") + gen = string("make_intrusive<TypeVal>(") +
globals[n->Name()] + globals[n->Name()] +
"->GetType(), true)"; "->GetType(), true)";
@ -170,7 +172,7 @@ std::string CPPCompile::GenNameExpr(const NameExpr* ne, GenType gt)
return NativeToGT(IDNameStr(n), t, gt); return NativeToGT(IDNameStr(n), t, gt);
} }
std::string CPPCompile::GenConstExpr(const ConstExpr* c, GenType gt) string CPPCompile::GenConstExpr(const ConstExpr* c, GenType gt)
{ {
const auto& t = c->GetType(); const auto& t = c->GetType();
@ -180,7 +182,7 @@ std::string CPPCompile::GenConstExpr(const ConstExpr* c, GenType gt)
return NativeToGT(GenVal(c->ValuePtr()), t, gt); return NativeToGT(GenVal(c->ValuePtr()), t, gt);
} }
std::string CPPCompile::GenIncrExpr(const Expr* e, GenType gt, bool is_incr, bool top_level) string CPPCompile::GenIncrExpr(const Expr* e, GenType gt, bool is_incr, bool top_level)
{ {
// For compound operands (table indexing, record fields), // For compound operands (table indexing, record fields),
// Zeek's interpreter will actually evaluate the operand // Zeek's interpreter will actually evaluate the operand
@ -214,7 +216,7 @@ std::string CPPCompile::GenIncrExpr(const Expr* e, GenType gt, bool is_incr, boo
return gen; return gen;
} }
std::string CPPCompile::GenCondExpr(const Expr* e, GenType gt) string CPPCompile::GenCondExpr(const Expr* e, GenType gt)
{ {
auto op1 = e->GetOp1(); auto op1 = e->GetOp1();
auto op2 = e->GetOp2(); auto op2 = e->GetOp2();
@ -225,14 +227,13 @@ std::string CPPCompile::GenCondExpr(const Expr* e, GenType gt)
auto gen3 = GenExpr(op3, gt); auto gen3 = GenExpr(op3, gt);
if ( op1->GetType()->Tag() == TYPE_VECTOR ) if ( op1->GetType()->Tag() == TYPE_VECTOR )
return std::string("vector_select__CPP(") + return string("vector_select__CPP(") +
gen1 + ", " + gen2 + ", " + gen3 + ")"; gen1 + ", " + gen2 + ", " + gen3 + ")";
return std::string("(") + gen1 + ") ? (" + return string("(") + gen1 + ") ? (" + gen2 + ") : (" + gen3 + ")";
gen2 + ") : (" + gen3 + ")";
} }
std::string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt) string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt)
{ {
const auto& t = c->GetType(); const auto& t = c->GetType();
auto f = c->Func(); auto f = c->Func();
@ -276,17 +277,15 @@ std::string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt)
else if ( pfs.Globals().count(f_id) > 0 ) else if ( pfs.Globals().count(f_id) > 0 )
// The BiF version has an extra "_", per // The BiF version has an extra "_", per
// AddBiF(..., true). // AddBiF(..., true).
gen = globals[std::string(id_name) + "_"]; gen = globals[string(id_name) + "_"];
} }
else else
// Indirect call. // Indirect call.
gen = std::string("(") + gen + ")->AsFunc()"; gen = string("(") + gen + ")->AsFunc()";
auto args_list = std::string(", {") + auto args_list = string(", {") + GenExpr(args_l, GEN_VAL_PTR) + "}";
GenExpr(args_l, GEN_VAL_PTR) + "}"; auto invoker = string("invoke__CPP(") + gen + args_list + ", f__CPP)";
auto invoker = std::string("invoke__CPP(") +
gen + args_list + ", f__CPP)";
if ( IsNativeType(t) && gt != GEN_VAL_PTR ) if ( IsNativeType(t) && gt != GEN_VAL_PTR )
return invoker + NativeAccessor(t); return invoker + NativeAccessor(t);
@ -294,7 +293,7 @@ std::string CPPCompile::GenCallExpr(const CallExpr* c, GenType gt)
return GenericValPtrToGT(invoker, t, gt); return GenericValPtrToGT(invoker, t, gt);
} }
std::string CPPCompile::GenInExpr(const Expr* e, GenType gt) string CPPCompile::GenInExpr(const Expr* e, GenType gt)
{ {
auto op1 = e->GetOp1(); auto op1 = e->GetOp1();
auto op2 = e->GetOp2(); auto op2 = e->GetOp2();
@ -302,20 +301,20 @@ std::string CPPCompile::GenInExpr(const Expr* e, GenType gt)
auto t1 = op1->GetType(); auto t1 = op1->GetType();
auto t2 = op2->GetType(); auto t2 = op2->GetType();
std::string gen; string gen;
if ( t1->Tag() == TYPE_PATTERN ) if ( t1->Tag() == TYPE_PATTERN )
gen = std::string("(") + GenExpr(op1, GEN_DONT_CARE) + gen = string("(") + GenExpr(op1, GEN_DONT_CARE) +
")->MatchAnywhere(" + ")->MatchAnywhere(" +
GenExpr(op2, GEN_DONT_CARE) + "->AsString())"; GenExpr(op2, GEN_DONT_CARE) + "->AsString())";
else if ( t2->Tag() == TYPE_STRING ) else if ( t2->Tag() == TYPE_STRING )
gen = std::string("str_in__CPP(") + gen = string("str_in__CPP(") +
GenExpr(op1, GEN_DONT_CARE) + "->AsString(), " + GenExpr(op1, GEN_DONT_CARE) + "->AsString(), " +
GenExpr(op2, GEN_DONT_CARE) + "->AsString())"; GenExpr(op2, GEN_DONT_CARE) + "->AsString())";
else if ( t1->Tag() == TYPE_ADDR && t2->Tag() == TYPE_SUBNET ) else if ( t1->Tag() == TYPE_ADDR && t2->Tag() == TYPE_SUBNET )
gen = std::string("(") + GenExpr(op2, GEN_DONT_CARE) + gen = string("(") + GenExpr(op2, GEN_DONT_CARE) +
")->Contains(" + GenExpr(op1, GEN_VAL_PTR) + "->Get())"; ")->Contains(" + GenExpr(op1, GEN_VAL_PTR) + "->Get())";
else if ( t2->Tag() == TYPE_VECTOR ) else if ( t2->Tag() == TYPE_VECTOR )
@ -323,47 +322,47 @@ std::string CPPCompile::GenInExpr(const Expr* e, GenType gt)
GenExpr(op1, GEN_NATIVE) + ")"; GenExpr(op1, GEN_NATIVE) + ")";
else else
gen = std::string("(") + GenExpr(op2, GEN_DONT_CARE) + gen = string("(") + GenExpr(op2, GEN_DONT_CARE) +
"->Find(index_val__CPP({" + GenExpr(op1, GEN_VAL_PTR) + "->Find(index_val__CPP({" + GenExpr(op1, GEN_VAL_PTR) +
"})) ? true : false)"; "})) ? true : false)";
return NativeToGT(gen, e->GetType(), gt); return NativeToGT(gen, e->GetType(), gt);
} }
std::string CPPCompile::GenFieldExpr(const FieldExpr* fe, GenType gt) string CPPCompile::GenFieldExpr(const FieldExpr* fe, GenType gt)
{ {
auto r = fe->GetOp1(); auto r = fe->GetOp1();
auto f = fe->Field(); auto f = fe->Field();
auto f_s = GenField(r, f); auto f_s = GenField(r, f);
auto gen = std::string("field_access__CPP(") + auto gen = string("field_access__CPP(") + GenExpr(r, GEN_VAL_PTR) +
GenExpr(r, GEN_VAL_PTR) + ", " + f_s + ")"; ", " + f_s + ")";
return GenericValPtrToGT(gen, fe->GetType(), gt); return GenericValPtrToGT(gen, fe->GetType(), gt);
} }
std::string CPPCompile::GenHasFieldExpr(const HasFieldExpr* hfe, GenType gt) string CPPCompile::GenHasFieldExpr(const HasFieldExpr* hfe, GenType gt)
{ {
auto r = hfe->GetOp1(); auto r = hfe->GetOp1();
auto f = hfe->Field(); auto f = hfe->Field();
auto f_s = GenField(r, f); auto f_s = GenField(r, f);
// Need to use accessors for native types. // Need to use accessors for native types.
auto gen = std::string("(") + GenExpr(r, GEN_DONT_CARE) + auto gen = string("(") + GenExpr(r, GEN_DONT_CARE) +
"->GetField(" + f_s + ") != nullptr)"; "->GetField(" + f_s + ") != nullptr)";
return NativeToGT(gen, hfe->GetType(), gt); return NativeToGT(gen, hfe->GetType(), gt);
} }
std::string CPPCompile::GenIndexExpr(const Expr* e, GenType gt) string CPPCompile::GenIndexExpr(const Expr* e, GenType gt)
{ {
auto aggr = e->GetOp1(); auto aggr = e->GetOp1();
const auto& aggr_t = aggr->GetType(); const auto& aggr_t = aggr->GetType();
std::string gen; string gen;
if ( aggr_t->Tag() == TYPE_TABLE ) if ( aggr_t->Tag() == TYPE_TABLE )
gen = std::string("index_table__CPP(") + gen = string("index_table__CPP(") +
GenExpr(aggr, GEN_NATIVE) + ", {" + GenExpr(aggr, GEN_NATIVE) + ", {" +
GenExpr(e->GetOp2(), GEN_VAL_PTR) + "})"; GenExpr(e->GetOp2(), GEN_VAL_PTR) + "})";
@ -379,26 +378,26 @@ std::string CPPCompile::GenIndexExpr(const Expr* e, GenType gt)
auto& inds = op2->AsListExpr()->Exprs(); auto& inds = op2->AsListExpr()->Exprs();
auto first = inds[0]; auto first = inds[0];
auto last = inds[1]; auto last = inds[1];
gen = std::string("index_slice(") + gen = string("index_slice(") +
GenExpr(aggr, GEN_VAL_PTR) + ".get(), " + GenExpr(aggr, GEN_VAL_PTR) + ".get(), " +
GenExpr(first, GEN_NATIVE) + ", " + GenExpr(first, GEN_NATIVE) + ", " +
GenExpr(last, GEN_NATIVE) + ")"; GenExpr(last, GEN_NATIVE) + ")";
} }
else else
gen = std::string("index_vec__CPP(") + gen = string("index_vec__CPP(") +
GenExpr(aggr, GEN_NATIVE) + ", " + GenExpr(aggr, GEN_NATIVE) + ", " +
GenExpr(e->GetOp2(), GEN_NATIVE) + ")"; GenExpr(e->GetOp2(), GEN_NATIVE) + ")";
} }
else if ( aggr_t->Tag() == TYPE_STRING ) else if ( aggr_t->Tag() == TYPE_STRING )
gen = std::string("index_string__CPP(") + gen = string("index_string__CPP(") +
GenExpr(aggr, GEN_NATIVE) + ", {" + GenExpr(aggr, GEN_NATIVE) + ", {" +
GenExpr(e->GetOp2(), GEN_VAL_PTR) + "})"; GenExpr(e->GetOp2(), GEN_VAL_PTR) + "})";
return GenericValPtrToGT(gen, e->GetType(), gt); return GenericValPtrToGT(gen, e->GetType(), gt);
} }
std::string CPPCompile::GenAssignExpr(const Expr* e, GenType gt, bool top_level) string CPPCompile::GenAssignExpr(const Expr* e, GenType gt, bool top_level)
{ {
auto op1 = e->GetOp1()->AsRefExprPtr()->GetOp1(); auto op1 = e->GetOp1()->AsRefExprPtr()->GetOp1();
auto op2 = e->GetOp2(); auto op2 = e->GetOp2();
@ -422,13 +421,13 @@ std::string CPPCompile::GenAssignExpr(const Expr* e, GenType gt, bool top_level)
return GenAssign(op1, op2, rhs_native, rhs_val_ptr, gt, top_level); return GenAssign(op1, op2, rhs_native, rhs_val_ptr, gt, top_level);
} }
std::string CPPCompile::GenAddToExpr(const Expr* e, GenType gt, bool top_level) string CPPCompile::GenAddToExpr(const Expr* e, GenType gt, bool top_level)
{ {
const auto& t = e->GetType(); const auto& t = e->GetType();
if ( t->Tag() == TYPE_VECTOR ) if ( t->Tag() == TYPE_VECTOR )
{ {
auto gen = std::string("vector_append__CPP(") + auto gen = string("vector_append__CPP(") +
GenExpr(e->GetOp1(), GEN_VAL_PTR) + GenExpr(e->GetOp1(), GEN_VAL_PTR) +
", " + GenExpr(e->GetOp2(), GEN_VAL_PTR) + ")"; ", " + GenExpr(e->GetOp2(), GEN_VAL_PTR) + ")";
return GenericValPtrToGT(gen, t, gt); return GenericValPtrToGT(gen, t, gt);
@ -463,7 +462,7 @@ std::string CPPCompile::GenAddToExpr(const Expr* e, GenType gt, bool top_level)
return GenBinary(e, gt, "+="); return GenBinary(e, gt, "+=");
} }
std::string CPPCompile::GenSizeExpr(const Expr* e, GenType gt) string CPPCompile::GenSizeExpr(const Expr* e, GenType gt)
{ {
const auto& t = e->GetType(); const auto& t = e->GetType();
const auto& t1 = e->GetOp1()->GetType(); const auto& t1 = e->GetOp1()->GetType();
@ -472,22 +471,22 @@ std::string CPPCompile::GenSizeExpr(const Expr* e, GenType gt)
auto gen = GenExpr(e->GetOp1(), GEN_NATIVE); auto gen = GenExpr(e->GetOp1(), GEN_NATIVE);
if ( t1->Tag() == TYPE_BOOL ) if ( t1->Tag() == TYPE_BOOL )
gen = std::string("((") + gen + ") ? 1 : 0)"; gen = string("((") + gen + ") ? 1 : 0)";
else if ( it == TYPE_INTERNAL_UNSIGNED ) else if ( it == TYPE_INTERNAL_UNSIGNED )
// no-op // no-op
; ;
else if ( it == TYPE_INTERNAL_INT ) else if ( it == TYPE_INTERNAL_INT )
gen = std::string("iabs__CPP(") + gen + ")"; gen = string("iabs__CPP(") + gen + ")";
else if ( it == TYPE_INTERNAL_DOUBLE ) else if ( it == TYPE_INTERNAL_DOUBLE )
gen = std::string("fabs__CPP(") + gen + ")"; gen = string("fabs__CPP(") + gen + ")";
else if ( it == TYPE_INTERNAL_INT || it == TYPE_INTERNAL_DOUBLE ) else if ( it == TYPE_INTERNAL_INT || it == TYPE_INTERNAL_DOUBLE )
{ {
auto cast = (it == TYPE_INTERNAL_INT) ? "bro_int_t" : "double"; auto cast = (it == TYPE_INTERNAL_INT) ? "bro_int_t" : "double";
gen = std::string("abs__CPP(") + cast + "(" + gen + "))"; gen = string("abs__CPP(") + cast + "(" + gen + "))";
} }
else else
@ -496,51 +495,51 @@ std::string CPPCompile::GenSizeExpr(const Expr* e, GenType gt)
return NativeToGT(gen, t, gt); return NativeToGT(gen, t, gt);
} }
std::string CPPCompile::GenScheduleExpr(const Expr* e) string CPPCompile::GenScheduleExpr(const Expr* e)
{ {
auto s = static_cast<const ScheduleExpr*>(e); auto s = static_cast<const ScheduleExpr*>(e);
auto when = s->When(); auto when = s->When();
auto event = s->Event(); auto event = s->Event();
std::string event_name(event->Handler()->Name()); string event_name(event->Handler()->Name());
RegisterEvent(event_name); RegisterEvent(event_name);
std::string when_s = GenExpr(when, GEN_NATIVE); string when_s = GenExpr(when, GEN_NATIVE);
if ( when->GetType()->Tag() == TYPE_INTERVAL ) if ( when->GetType()->Tag() == TYPE_INTERVAL )
when_s += " + run_state::network_time"; when_s += " + run_state::network_time";
return std::string("schedule__CPP(") + when_s + return string("schedule__CPP(") + when_s +
", " + globals[event_name] + "_ev, { " + ", " + globals[event_name] + "_ev, { " +
GenExpr(event->Args(), GEN_VAL_PTR) + " })"; GenExpr(event->Args(), GEN_VAL_PTR) + " })";
} }
std::string CPPCompile::GenLambdaExpr(const Expr* e) string CPPCompile::GenLambdaExpr(const Expr* e)
{ {
auto l = static_cast<const LambdaExpr*>(e); auto l = static_cast<const LambdaExpr*>(e);
auto name = Canonicalize(l->Name().c_str()) + "_lb_cl"; auto name = Canonicalize(l->Name().c_str()) + "_lb_cl";
auto cl_args = std::string("\"") + name + "\""; auto cl_args = string("\"") + name + "\"";
if ( l->OuterIDs().size() > 0 ) if ( l->OuterIDs().size() > 0 )
cl_args = cl_args + GenLambdaClone(l, false); cl_args = cl_args + GenLambdaClone(l, false);
auto body = std::string("make_intrusive<") + name + ">(" + cl_args + ")"; auto body = string("make_intrusive<") + name + ">(" + cl_args + ")";
auto func = std::string("make_intrusive<CPPLambdaFunc>(\"") + auto func = string("make_intrusive<CPPLambdaFunc>(\"") +
l->Name() + "\", cast_intrusive<FuncType>(" + l->Name() + "\", cast_intrusive<FuncType>(" +
GenTypeName(l->GetType()) + "), " + body + ")"; GenTypeName(l->GetType()) + "), " + body + ")";
return std::string("make_intrusive<FuncVal>(") + func + ")"; return string("make_intrusive<FuncVal>(") + func + ")";
} }
std::string CPPCompile::GenIsExpr(const Expr* e, GenType gt) string CPPCompile::GenIsExpr(const Expr* e, GenType gt)
{ {
auto ie = static_cast<const IsExpr*>(e); auto ie = static_cast<const IsExpr*>(e);
auto gen = std::string("can_cast_value_to_type(") + auto gen = string("can_cast_value_to_type(") +
GenExpr(ie->GetOp1(), GEN_VAL_PTR) + ".get(), " + GenExpr(ie->GetOp1(), GEN_VAL_PTR) + ".get(), " +
GenTypeName(ie->TestType()) + ".get())"; GenTypeName(ie->TestType()) + ".get())";
return NativeToGT(gen, ie->GetType(), gt); return NativeToGT(gen, ie->GetType(), gt);
} }
std::string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt) string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt)
{ {
const auto& t = e->GetType(); const auto& t = e->GetType();
auto op = e->GetOp1(); auto op = e->GetOp1();
@ -552,7 +551,7 @@ std::string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt)
auto coerce_t = is_vec ? t->Yield() : t; auto coerce_t = is_vec ? t->Yield() : t;
std::string cast_name; string cast_name;
switch ( coerce_t->InternalType() ) { switch ( coerce_t->InternalType() ) {
case TYPE_INTERNAL_INT: cast_name = "bro_int_t"; break; case TYPE_INTERNAL_INT: cast_name = "bro_int_t"; break;
@ -564,14 +563,14 @@ std::string CPPCompile::GenArithCoerceExpr(const Expr* e, GenType gt)
} }
if ( is_vec ) if ( is_vec )
return std::string("vec_coerce_") + cast_name + return string("vec_coerce_") + cast_name +
"__CPP(" + GenExpr(op, GEN_NATIVE) + "__CPP(" + GenExpr(op, GEN_NATIVE) +
", " + GenTypeName(t) + ")"; ", " + GenTypeName(t) + ")";
return NativeToGT(cast_name + "(" + GenExpr(op, GEN_NATIVE) + ")", t, gt); return NativeToGT(cast_name + "(" + GenExpr(op, GEN_NATIVE) + ")", t, gt);
} }
std::string CPPCompile::GenRecordCoerceExpr(const Expr* e) string CPPCompile::GenRecordCoerceExpr(const Expr* e)
{ {
auto rc = static_cast<const RecordCoerceExpr*>(e); auto rc = static_cast<const RecordCoerceExpr*>(e);
auto op1 = rc->GetOp1(); auto op1 = rc->GetOp1();
@ -585,39 +584,39 @@ std::string CPPCompile::GenRecordCoerceExpr(const Expr* e)
const auto& map = rc->Map(); const auto& map = rc->Map();
auto type_var = GenTypeName(to_type); auto type_var = GenTypeName(to_type);
return std::string("coerce_to_record(cast_intrusive<RecordType>(") + return string("coerce_to_record(cast_intrusive<RecordType>(") +
type_var + "), " + GenExpr(op1, GEN_VAL_PTR) + ".get(), " + type_var + "), " + GenExpr(op1, GEN_VAL_PTR) + ".get(), " +
GenIntVector(map) + ")"; GenIntVector(map) + ")";
} }
std::string CPPCompile::GenTableCoerceExpr(const Expr* e) string CPPCompile::GenTableCoerceExpr(const Expr* e)
{ {
auto tc = static_cast<const TableCoerceExpr*>(e); auto tc = static_cast<const TableCoerceExpr*>(e);
const auto& t = tc->GetType(); const auto& t = tc->GetType();
auto op1 = tc->GetOp1(); auto op1 = tc->GetOp1();
return std::string("table_coerce__CPP(") + GenExpr(op1, GEN_VAL_PTR) + return string("table_coerce__CPP(") + GenExpr(op1, GEN_VAL_PTR) +
", " + GenTypeName(t) + ")"; ", " + GenTypeName(t) + ")";
} }
std::string CPPCompile::GenVectorCoerceExpr(const Expr* e) string CPPCompile::GenVectorCoerceExpr(const Expr* e)
{ {
auto vc = static_cast<const VectorCoerceExpr*>(e); auto vc = static_cast<const VectorCoerceExpr*>(e);
const auto& op = vc->GetOp1(); const auto& op = vc->GetOp1();
const auto& t = vc->GetType<VectorType>(); const auto& t = vc->GetType<VectorType>();
return std::string("vector_coerce__CPP(" + GenExpr(op, GEN_VAL_PTR) + return string("vector_coerce__CPP(" + GenExpr(op, GEN_VAL_PTR) +
", " + GenTypeName(t) + ")"); ", " + GenTypeName(t) + ")");
} }
std::string CPPCompile::GenRecordConstructorExpr(const Expr* e) string CPPCompile::GenRecordConstructorExpr(const Expr* e)
{ {
auto rc = static_cast<const RecordConstructorExpr*>(e); auto rc = static_cast<const RecordConstructorExpr*>(e);
const auto& t = rc->GetType(); const auto& t = rc->GetType();
const auto& exprs = rc->Op()->AsListExpr()->Exprs(); const auto& exprs = rc->Op()->AsListExpr()->Exprs();
auto n = exprs.length(); auto n = exprs.length();
std::string vals; string vals;
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
@ -631,38 +630,38 @@ std::string CPPCompile::GenRecordConstructorExpr(const Expr* e)
vals += ", "; vals += ", ";
} }
return std::string("record_constructor__CPP({") + vals + "}, " + return string("record_constructor__CPP({") + vals + "}, " +
"cast_intrusive<RecordType>(" + GenTypeName(t) + "))"; "cast_intrusive<RecordType>(" + GenTypeName(t) + "))";
} }
std::string CPPCompile::GenSetConstructorExpr(const Expr* e) string CPPCompile::GenSetConstructorExpr(const Expr* e)
{ {
auto sc = static_cast<const SetConstructorExpr*>(e); auto sc = static_cast<const SetConstructorExpr*>(e);
const auto& t = sc->GetType(); const auto& t = sc->GetType();
auto attrs = sc->GetAttrs(); auto attrs = sc->GetAttrs();
std::string attr_tags; string attr_tags;
std::string attr_vals; string attr_vals;
BuildAttrs(attrs, attr_tags, attr_vals); BuildAttrs(attrs, attr_tags, attr_vals);
return std::string("set_constructor__CPP(") + return string("set_constructor__CPP(") +
GenExprs(sc->GetOp1().get()) + ", " + GenExprs(sc->GetOp1().get()) + ", " +
"cast_intrusive<TableType>(" + GenTypeName(t) + "), " + "cast_intrusive<TableType>(" + GenTypeName(t) + "), " +
attr_tags + ", " + attr_vals + ")"; attr_tags + ", " + attr_vals + ")";
} }
std::string CPPCompile::GenTableConstructorExpr(const Expr* e) string CPPCompile::GenTableConstructorExpr(const Expr* e)
{ {
auto tc = static_cast<const TableConstructorExpr*>(e); auto tc = static_cast<const TableConstructorExpr*>(e);
const auto& t = tc->GetType(); const auto& t = tc->GetType();
auto attrs = tc->GetAttrs(); auto attrs = tc->GetAttrs();
std::string attr_tags; string attr_tags;
std::string attr_vals; string attr_vals;
BuildAttrs(attrs, attr_tags, attr_vals); BuildAttrs(attrs, attr_tags, attr_vals);
std::string indices; string indices;
std::string vals; string vals;
const auto& exprs = tc->GetOp1()->AsListExpr()->Exprs(); const auto& exprs = tc->GetOp1()->AsListExpr()->Exprs();
auto n = exprs.length(); auto n = exprs.length();
@ -692,30 +691,30 @@ std::string CPPCompile::GenTableConstructorExpr(const Expr* e)
} }
} }
return std::string("table_constructor__CPP({") + return string("table_constructor__CPP({") +
indices + "}, {" + vals + "}, " + indices + "}, {" + vals + "}, " +
"cast_intrusive<TableType>(" + GenTypeName(t) + "), " + "cast_intrusive<TableType>(" + GenTypeName(t) + "), " +
attr_tags + ", " + attr_vals + ")"; attr_tags + ", " + attr_vals + ")";
} }
std::string CPPCompile::GenVectorConstructorExpr(const Expr* e) string CPPCompile::GenVectorConstructorExpr(const Expr* e)
{ {
auto vc = static_cast<const VectorConstructorExpr*>(e); auto vc = static_cast<const VectorConstructorExpr*>(e);
const auto& t = vc->GetType(); const auto& t = vc->GetType();
return std::string("vector_constructor__CPP({") + return string("vector_constructor__CPP({") +
GenExpr(vc->GetOp1(), GEN_VAL_PTR) + "}, " + GenExpr(vc->GetOp1(), GEN_VAL_PTR) + "}, " +
"cast_intrusive<VectorType>(" + GenTypeName(t) + "))"; "cast_intrusive<VectorType>(" + GenTypeName(t) + "))";
} }
std::string CPPCompile::GenVal(const ValPtr& v) string CPPCompile::GenVal(const ValPtr& v)
{ {
const auto& t = v->GetType(); const auto& t = v->GetType();
auto tag = t->Tag(); auto tag = t->Tag();
auto it = t->InternalType(); auto it = t->InternalType();
if ( tag == TYPE_BOOL ) if ( tag == TYPE_BOOL )
return std::string(v->IsZero() ? "false" : "true"); return string(v->IsZero() ? "false" : "true");
if ( tag == TYPE_ENUM ) if ( tag == TYPE_ENUM )
return GenEnum(t, v); return GenEnum(t, v);
@ -732,18 +731,18 @@ std::string CPPCompile::GenVal(const ValPtr& v)
return d.Description(); return d.Description();
} }
std::string CPPCompile::GenUnary(const Expr* e, GenType gt, string CPPCompile::GenUnary(const Expr* e, GenType gt,
const char* op, const char* vec_op) const char* op, const char* vec_op)
{ {
if ( e->GetType()->Tag() == TYPE_VECTOR ) if ( e->GetType()->Tag() == TYPE_VECTOR )
return GenVectorOp(e, GenExpr(e->GetOp1(), GEN_NATIVE), vec_op); return GenVectorOp(e, GenExpr(e->GetOp1(), GEN_NATIVE), vec_op);
return NativeToGT(std::string(op) + "(" + return NativeToGT(string(op) + "(" +
GenExpr(e->GetOp1(), GEN_NATIVE) + ")", GenExpr(e->GetOp1(), GEN_NATIVE) + ")",
e->GetType(), gt); e->GetType(), gt);
} }
std::string CPPCompile::GenBinary(const Expr* e, GenType gt, string CPPCompile::GenBinary(const Expr* e, GenType gt,
const char* op, const char* vec_op) const char* op, const char* vec_op)
{ {
const auto& op1 = e->GetOp1(); const auto& op1 = e->GetOp1();
@ -758,7 +757,7 @@ std::string CPPCompile::GenBinary(const Expr* e, GenType gt,
if ( t->Tag() == TYPE_VECTOR && if ( t->Tag() == TYPE_VECTOR &&
t->Yield()->Tag() == TYPE_STRING && t->Yield()->Tag() == TYPE_STRING &&
op2->GetType()->Tag() == TYPE_VECTOR ) op2->GetType()->Tag() == TYPE_VECTOR )
return std::string("vec_str_op_") + vec_op + "__CPP(" + return string("vec_str_op_") + vec_op + "__CPP(" +
gen1 + ", " + gen2 + ")"; gen1 + ", " + gen2 + ")";
return GenVectorOp(e, gen1, gen2, vec_op); return GenVectorOp(e, gen1, gen2, vec_op);
@ -771,7 +770,7 @@ std::string CPPCompile::GenBinary(const Expr* e, GenType gt,
// operations. For those, it holds the prefix we use to // operations. For those, it holds the prefix we use to
// distinguish different instances of inlined functions // distinguish different instances of inlined functions
// employed to support an operation. // employed to support an operation.
std::string flavor; string flavor;
switch ( t->InternalType() ) { switch ( t->InternalType() ) {
case TYPE_INTERNAL_INT: flavor = "i"; break; case TYPE_INTERNAL_INT: flavor = "i"; break;
@ -791,7 +790,7 @@ std::string CPPCompile::GenBinary(const Expr* e, GenType gt,
auto g1 = GenExpr(e->GetOp1(), GEN_NATIVE); auto g1 = GenExpr(e->GetOp1(), GEN_NATIVE);
auto g2 = GenExpr(e->GetOp2(), GEN_NATIVE); auto g2 = GenExpr(e->GetOp2(), GEN_NATIVE);
std::string gen; string gen;
if ( e->Tag() == EXPR_DIVIDE ) if ( e->Tag() == EXPR_DIVIDE )
gen = flavor + "div__CPP(" + g1 + ", " + g2 + ")"; gen = flavor + "div__CPP(" + g1 + ", " + g2 + ")";
@ -800,17 +799,17 @@ std::string CPPCompile::GenBinary(const Expr* e, GenType gt,
gen = flavor + "mod__CPP(" + g1 + ", " + g2 + ")"; gen = flavor + "mod__CPP(" + g1 + ", " + g2 + ")";
else else
gen = std::string("(") + g1 + ")" + op + "(" + g2 + ")"; gen = string("(") + g1 + ")" + op + "(" + g2 + ")";
return NativeToGT(gen, e->GetType(), gt); return NativeToGT(gen, e->GetType(), gt);
} }
std::string CPPCompile::GenBinarySet(const Expr* e, GenType gt, const char* op) string CPPCompile::GenBinarySet(const Expr* e, GenType gt, const char* op)
{ {
auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsTableVal()"; auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsTableVal()";
auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsTableVal()"; auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsTableVal()";
std::string res; string res;
switch ( e->Tag() ) { switch ( e->Tag() ) {
case EXPR_AND: case EXPR_AND:
@ -830,7 +829,7 @@ std::string CPPCompile::GenBinarySet(const Expr* e, GenType gt, const char* op)
break; break;
case EXPR_NE: case EXPR_NE:
res = std::string("! ") + v1 + "->EqualTo(*" + v2 + ")"; res = string("! ") + v1 + "->EqualTo(*" + v2 + ")";
break; break;
case EXPR_LE: case EXPR_LE:
@ -838,7 +837,7 @@ std::string CPPCompile::GenBinarySet(const Expr* e, GenType gt, const char* op)
break; break;
case EXPR_LT: case EXPR_LT:
res = std::string("(") + v1 + "->IsSubsetOf(*" + v2 + ") &&" + res = string("(") + v1 + "->IsSubsetOf(*" + v2 + ") &&" +
v1 + "->Size() < " + v2 + "->Size())"; v1 + "->Size() < " + v2 + "->Size())";
break; break;
@ -849,23 +848,23 @@ std::string CPPCompile::GenBinarySet(const Expr* e, GenType gt, const char* op)
return NativeToGT(res, e->GetType(), gt); return NativeToGT(res, e->GetType(), gt);
} }
std::string CPPCompile::GenBinaryString(const Expr* e, GenType gt, string CPPCompile::GenBinaryString(const Expr* e, GenType gt,
const char* op) const char* op)
{ {
auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsString()"; auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsString()";
auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsString()"; auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsString()";
std::string res; string res;
if ( e->Tag() == EXPR_ADD || e->Tag() == EXPR_ADD_TO ) if ( e->Tag() == EXPR_ADD || e->Tag() == EXPR_ADD_TO )
res = std::string("str_concat__CPP(") + v1 + ", " + v2 + ")"; res = string("str_concat__CPP(") + v1 + ", " + v2 + ")";
else else
res = std::string("(Bstr_cmp(") + v1 + ", " + v2 + ") " + op + " 0)"; res = string("(Bstr_cmp(") + v1 + ", " + v2 + ") " + op + " 0)";
return NativeToGT(res, e->GetType(), gt); return NativeToGT(res, e->GetType(), gt);
} }
std::string CPPCompile::GenBinaryPattern(const Expr* e, GenType gt, string CPPCompile::GenBinaryPattern(const Expr* e, GenType gt,
const char* op) const char* op)
{ {
auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsPattern()"; auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsPattern()";
@ -874,17 +873,17 @@ std::string CPPCompile::GenBinaryPattern(const Expr* e, GenType gt,
auto func = e->Tag() == EXPR_AND ? auto func = e->Tag() == EXPR_AND ?
"RE_Matcher_conjunction" : "RE_Matcher_disjunction"; "RE_Matcher_conjunction" : "RE_Matcher_disjunction";
return NativeToGT(std::string("make_intrusive<PatternVal>(") + return NativeToGT(string("make_intrusive<PatternVal>(") +
func + "(" + v1 + ", " + v2 + "))", e->GetType(), gt); func + "(" + v1 + ", " + v2 + "))", e->GetType(), gt);
} }
std::string CPPCompile::GenBinaryAddr(const Expr* e, GenType gt, const char* op) string CPPCompile::GenBinaryAddr(const Expr* e, GenType gt, const char* op)
{ {
auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsAddr()"; auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsAddr()";
if ( e->Tag() == EXPR_DIVIDE ) if ( e->Tag() == EXPR_DIVIDE )
{ {
auto gen = std::string("addr_mask__CPP(") + v1 + ", " + auto gen = string("addr_mask__CPP(") + v1 + ", " +
GenExpr(e->GetOp2(), GEN_NATIVE) + ")"; GenExpr(e->GetOp2(), GEN_NATIVE) + ")";
return NativeToGT(gen, e->GetType(), gt); return NativeToGT(gen, e->GetType(), gt);
@ -895,8 +894,7 @@ std::string CPPCompile::GenBinaryAddr(const Expr* e, GenType gt, const char* op)
return NativeToGT(v1 + op + v2, e->GetType(), gt); return NativeToGT(v1 + op + v2, e->GetType(), gt);
} }
std::string CPPCompile::GenBinarySubNet(const Expr* e, GenType gt, string CPPCompile::GenBinarySubNet(const Expr* e, GenType gt, const char* op)
const char* op)
{ {
auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsSubNet()"; auto v1 = GenExpr(e->GetOp1(), GEN_DONT_CARE) + "->AsSubNet()";
auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsSubNet()"; auto v2 = GenExpr(e->GetOp2(), GEN_DONT_CARE) + "->AsSubNet()";
@ -904,8 +902,8 @@ std::string CPPCompile::GenBinarySubNet(const Expr* e, GenType gt,
return NativeToGT(v1 + op + v2, e->GetType(), gt); return NativeToGT(v1 + op + v2, e->GetType(), gt);
} }
std::string CPPCompile::GenEQ(const Expr* e, GenType gt, string CPPCompile::GenEQ(const Expr* e, GenType gt,
const char* op, const char* vec_op) const char* op, const char* vec_op)
{ {
auto op1 = e->GetOp1(); auto op1 = e->GetOp1();
auto op2 = e->GetOp2(); auto op2 = e->GetOp2();
@ -918,7 +916,7 @@ std::string CPPCompile::GenEQ(const Expr* e, GenType gt,
} }
auto tag = op1->GetType()->Tag(); auto tag = op1->GetType()->Tag();
std::string negated(e->Tag() == EXPR_EQ ? "" : "! "); string negated(e->Tag() == EXPR_EQ ? "" : "! ");
if ( tag == TYPE_PATTERN ) if ( tag == TYPE_PATTERN )
return NativeToGT(negated + GenExpr(op1, GEN_DONT_CARE) + return NativeToGT(negated + GenExpr(op1, GEN_DONT_CARE) +
@ -934,7 +932,7 @@ std::string CPPCompile::GenEQ(const Expr* e, GenType gt,
gen_f1 += "->AsFunc()"; gen_f1 += "->AsFunc()";
gen_f2 += "->AsFunc()"; gen_f2 += "->AsFunc()";
auto gen = std::string("(") + gen_f1 + "==" + gen_f2 + ")"; auto gen = string("(") + gen_f1 + "==" + gen_f2 + ")";
return NativeToGT(negated + gen, e->GetType(), gt); return NativeToGT(negated + gen, e->GetType(), gt);
} }
@ -942,10 +940,10 @@ std::string CPPCompile::GenEQ(const Expr* e, GenType gt,
return GenBinary(e, gt, op, vec_op); return GenBinary(e, gt, op, vec_op);
} }
std::string CPPCompile::GenAssign(const ExprPtr& lhs, const ExprPtr& rhs, string CPPCompile::GenAssign(const ExprPtr& lhs, const ExprPtr& rhs,
const std::string& rhs_native, const string& rhs_native,
const std::string& rhs_val_ptr, const string& rhs_val_ptr,
GenType gt, bool top_level) GenType gt, bool top_level)
{ {
switch ( lhs->Tag() ) { switch ( lhs->Tag() ) {
case EXPR_NAME: case EXPR_NAME:
@ -966,15 +964,14 @@ std::string CPPCompile::GenAssign(const ExprPtr& lhs, const ExprPtr& rhs,
} }
} }
std::string CPPCompile::GenDirectAssign(const ExprPtr& lhs, string CPPCompile::GenDirectAssign(const ExprPtr& lhs, const string& rhs_native,
const std::string& rhs_native, const string& rhs_val_ptr, GenType gt,
const std::string& rhs_val_ptr, bool top_level)
GenType gt, bool top_level)
{ {
auto n = lhs->AsNameExpr()->Id(); auto n = lhs->AsNameExpr()->Id();
auto name = IDNameStr(n); auto name = IDNameStr(n);
std::string gen; string gen;
if ( n->IsGlobal() ) if ( n->IsGlobal() )
{ {
@ -984,7 +981,7 @@ std::string CPPCompile::GenDirectAssign(const ExprPtr& lhs,
if ( t->Tag() == TYPE_FUNC && if ( t->Tag() == TYPE_FUNC &&
t->AsFuncType()->Flavor() == FUNC_FLAVOR_EVENT ) t->AsFuncType()->Flavor() == FUNC_FLAVOR_EVENT )
{ {
gen = std::string("set_event__CPP(") + gn + ", " + gen = string("set_event__CPP(") + gn + ", " +
rhs_val_ptr + ", " + gn + "_ev)"; rhs_val_ptr + ", " + gn + "_ev)";
if ( ! top_level ) if ( ! top_level )
@ -996,7 +993,7 @@ std::string CPPCompile::GenDirectAssign(const ExprPtr& lhs,
else else
{ {
gen = std::string("set_global__CPP(") + gen = string("set_global__CPP(") +
gn + ", " + rhs_val_ptr + ")"; gn + ", " + rhs_val_ptr + ")";
gen = GenericValPtrToGT(gen, n->GetType(), gt); gen = GenericValPtrToGT(gen, n->GetType(), gt);
} }
@ -1007,11 +1004,11 @@ std::string CPPCompile::GenDirectAssign(const ExprPtr& lhs,
return gen; return gen;
} }
std::string CPPCompile::GenIndexAssign(const ExprPtr& lhs, const ExprPtr& rhs, string CPPCompile::GenIndexAssign(const ExprPtr& lhs, const ExprPtr& rhs,
const std::string& rhs_val_ptr, const string& rhs_val_ptr,
GenType gt, bool top_level) GenType gt, bool top_level)
{ {
auto gen = std::string("assign_to_index__CPP("); auto gen = string("assign_to_index__CPP(");
gen += GenExpr(lhs->GetOp1(), GEN_VAL_PTR) + ", " + "index_val__CPP({" + gen += GenExpr(lhs->GetOp1(), GEN_VAL_PTR) + ", " + "index_val__CPP({" +
GenExpr(lhs->GetOp2(), GEN_VAL_PTR) + "}), " + rhs_val_ptr + ")"; GenExpr(lhs->GetOp2(), GEN_VAL_PTR) + "}), " + rhs_val_ptr + ")";
@ -1022,9 +1019,9 @@ std::string CPPCompile::GenIndexAssign(const ExprPtr& lhs, const ExprPtr& rhs,
return gen; return gen;
} }
std::string CPPCompile::GenFieldAssign(const ExprPtr& lhs, const ExprPtr& rhs, string CPPCompile::GenFieldAssign(const ExprPtr& lhs, const ExprPtr& rhs,
const std::string& rhs_val_ptr, const string& rhs_val_ptr, GenType gt,
GenType gt, bool top_level) bool top_level)
{ {
auto rec = lhs->GetOp1(); auto rec = lhs->GetOp1();
auto rec_gen = GenExpr(rec, GEN_VAL_PTR); auto rec_gen = GenExpr(rec, GEN_VAL_PTR);
@ -1034,18 +1031,18 @@ std::string CPPCompile::GenFieldAssign(const ExprPtr& lhs, const ExprPtr& rhs,
return rec_gen + "->Assign(" + field + ", " + rhs_val_ptr + ")"; return rec_gen + "->Assign(" + field + ", " + rhs_val_ptr + ")";
else else
{ {
auto gen = std::string("assign_field__CPP(") + rec_gen + auto gen = string("assign_field__CPP(") + rec_gen +
", " + field + ", " + rhs_val_ptr + ")"; ", " + field + ", " + rhs_val_ptr + ")";
return GenericValPtrToGT(gen, rhs->GetType(), gt); return GenericValPtrToGT(gen, rhs->GetType(), gt);
} }
} }
std::string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs) string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs)
{ {
if ( rhs->Tag() != EXPR_NAME ) if ( rhs->Tag() != EXPR_NAME )
reporter->InternalError("compound RHS expression in multi-assignment"); reporter->InternalError("compound RHS expression in multi-assignment");
std::string gen; string gen;
const auto& vars = lhs->AsListExpr()->Exprs(); const auto& vars = lhs->AsListExpr()->Exprs();
auto n = vars.length(); auto n = vars.length();
@ -1071,42 +1068,41 @@ std::string CPPCompile::GenListAssign(const ExprPtr& lhs, const ExprPtr& rhs)
return "(" + gen + ")"; return "(" + gen + ")";
} }
std::string CPPCompile::GenVectorOp(const Expr* e, std::string op, string CPPCompile::GenVectorOp(const Expr* e, string op, const char* vec_op)
const char* vec_op)
{ {
auto gen = std::string("vec_op_") + vec_op + "__CPP(" + op + ")"; auto gen = string("vec_op_") + vec_op + "__CPP(" + op + ")";
if ( ! IsArithmetic(e->GetType()->Yield()->Tag()) ) if ( ! IsArithmetic(e->GetType()->Yield()->Tag()) )
gen = std::string("vector_coerce_to__CPP(") + gen + ", " + gen = string("vector_coerce_to__CPP(") + gen + ", " +
GenTypeName(e->GetType()) + ")"; GenTypeName(e->GetType()) + ")";
return gen; return gen;
} }
std::string CPPCompile::GenVectorOp(const Expr* e, std::string op1, string CPPCompile::GenVectorOp(const Expr* e, string op1, string op2,
std::string op2, const char* vec_op) const char* vec_op)
{ {
auto invoke = std::string(vec_op) + "__CPP(" + op1 + ", " + op2 + ")"; auto invoke = string(vec_op) + "__CPP(" + op1 + ", " + op2 + ")";
if ( e->GetOp1()->GetType()->Yield()->Tag() == TYPE_STRING ) if ( e->GetOp1()->GetType()->Yield()->Tag() == TYPE_STRING )
return std::string("str_vec_op_") + invoke; return string("str_vec_op_") + invoke;
auto gen = std::string("vec_op_") + invoke; auto gen = string("vec_op_") + invoke;
auto yt = e->GetType()->Yield()->Tag(); auto yt = e->GetType()->Yield()->Tag();
if ( ! IsArithmetic(yt) && yt != TYPE_STRING ) if ( ! IsArithmetic(yt) && yt != TYPE_STRING )
gen = std::string("vector_coerce_to__CPP(") + gen + ", " + gen = string("vector_coerce_to__CPP(") + gen + ", " +
GenTypeName(e->GetType()) + ")"; GenTypeName(e->GetType()) + ")";
return gen; return gen;
} }
std::string CPPCompile::GenLambdaClone(const LambdaExpr* l, bool all_deep) string CPPCompile::GenLambdaClone(const LambdaExpr* l, bool all_deep)
{ {
auto& ids = l->OuterIDs(); auto& ids = l->OuterIDs();
const auto& captures = l->GetType<FuncType>()->GetCaptures(); const auto& captures = l->GetType<FuncType>()->GetCaptures();
std::string cl_args; string cl_args;
for ( const auto& id : ids ) for ( const auto& id : ids )
{ {
@ -1117,7 +1113,7 @@ std::string CPPCompile::GenLambdaClone(const LambdaExpr* l, bool all_deep)
{ {
for ( const auto& c : *captures ) for ( const auto& c : *captures )
if ( id == c.id && (c.deep_copy || all_deep) ) if ( id == c.id && (c.deep_copy || all_deep) )
arg = std::string("cast_intrusive<") + TypeName(id_t) + ">(" + arg + "->Clone())"; arg = string("cast_intrusive<") + TypeName(id_t) + ">(" + arg + "->Clone())";
} }
cl_args = cl_args + ", " + arg; cl_args = cl_args + ", " + arg;
@ -1126,9 +1122,9 @@ std::string CPPCompile::GenLambdaClone(const LambdaExpr* l, bool all_deep)
return cl_args; return cl_args;
} }
std::string CPPCompile::GenIntVector(const std::vector<int>& vec) string CPPCompile::GenIntVector(const vector<int>& vec)
{ {
std::string res("{ "); string res("{ ");
for ( auto i = 0; i < vec.size(); ++i ) for ( auto i = 0; i < vec.size(); ++i )
{ {
@ -1141,7 +1137,7 @@ std::string CPPCompile::GenIntVector(const std::vector<int>& vec)
return res + " }"; return res + " }";
} }
std::string CPPCompile::GenField(const ExprPtr& rec, int field) string CPPCompile::GenField(const ExprPtr& rec, int field)
{ {
auto t = TypeRep(rec->GetType()); auto t = TypeRep(rec->GetType());
auto rt = t->AsRecordType(); auto rt = t->AsRecordType();
@ -1163,8 +1159,8 @@ std::string CPPCompile::GenField(const ExprPtr& rec, int field)
// New mapping. // New mapping.
mapping_slot = num_rf_mappings++; mapping_slot = num_rf_mappings++;
std::string field_name = rt->FieldName(field); string field_name = rt->FieldName(field);
field_decls.emplace_back(std::pair(rt, rt->FieldDecl(field))); field_decls.emplace_back(pair(rt, rt->FieldDecl(field)));
if ( record_field_mappings.count(rt) > 0 ) if ( record_field_mappings.count(rt) > 0 )
// We're already tracking this record. // We're already tracking this record.
@ -1172,16 +1168,16 @@ std::string CPPCompile::GenField(const ExprPtr& rec, int field)
else else
{ {
// Need to start tracking this record. // Need to start tracking this record.
std::unordered_map<int, int> rt_mapping; unordered_map<int, int> rt_mapping;
rt_mapping[field] = mapping_slot; rt_mapping[field] = mapping_slot;
record_field_mappings[rt] = rt_mapping; record_field_mappings[rt] = rt_mapping;
} }
} }
return std::string("field_mapping[") + Fmt(mapping_slot) + "]"; return string("field_mapping[") + Fmt(mapping_slot) + "]";
} }
std::string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev) string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
{ {
auto et = TypeRep(t)->AsEnumType(); auto et = TypeRep(t)->AsEnumType();
auto v = ev->AsEnum(); auto v = ev->AsEnum();
@ -1203,8 +1199,8 @@ std::string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
// New mapping. // New mapping.
mapping_slot = num_ev_mappings++; mapping_slot = num_ev_mappings++;
std::string enum_name = et->Lookup(v); string enum_name = et->Lookup(v);
enum_names.emplace_back(std::pair(et, std::move(enum_name))); enum_names.emplace_back(pair(et, move(enum_name)));
if ( enum_val_mappings.count(et) > 0 ) if ( enum_val_mappings.count(et) > 0 )
{ {
@ -1214,13 +1210,13 @@ std::string CPPCompile::GenEnum(const TypePtr& t, const ValPtr& ev)
else else
{ {
// Need to start tracking this enum. // Need to start tracking this enum.
std::unordered_map<int, int> et_mapping; unordered_map<int, int> et_mapping;
et_mapping[v] = mapping_slot; et_mapping[v] = mapping_slot;
enum_val_mappings[et] = et_mapping; enum_val_mappings[et] = et_mapping;
} }
} }
return std::string("enum_mapping[") + Fmt(mapping_slot) + "]"; return string("enum_mapping[") + Fmt(mapping_slot) + "]";
} }
} // zeek::detail } // zeek::detail

View file

@ -9,9 +9,11 @@
namespace zeek::detail { namespace zeek::detail {
std::unordered_map<p_hash_type, CompiledScript> compiled_scripts; using namespace std;
std::unordered_map<p_hash_type, void (*)()> standalone_callbacks;
std::vector<void (*)()> standalone_activations; unordered_map<p_hash_type, CompiledScript> compiled_scripts;
unordered_map<p_hash_type, void (*)()> standalone_callbacks;
vector<void (*)()> standalone_activations;
void CPPFunc::Describe(ODesc* d) const void CPPFunc::Describe(ODesc* d) const
{ {
@ -19,11 +21,10 @@ void CPPFunc::Describe(ODesc* d) const
d->Add(name); d->Add(name);
} }
CPPLambdaFunc::CPPLambdaFunc(std::string _name, FuncTypePtr ft, CPPLambdaFunc::CPPLambdaFunc(string _name, FuncTypePtr ft, CPPStmtPtr _l_body)
CPPStmtPtr _l_body) : ScriptFunc(move(_name), move(ft), {_l_body}, {0})
: ScriptFunc(std::move(_name), std::move(ft), {_l_body}, {0})
{ {
l_body = std::move(_l_body); l_body = move(_l_body);
} }
broker::expected<broker::data> CPPLambdaFunc::SerializeClosure() const broker::expected<broker::data> CPPLambdaFunc::SerializeClosure() const
@ -31,7 +32,7 @@ broker::expected<broker::data> CPPLambdaFunc::SerializeClosure() const
auto vals = l_body->SerializeLambdaCaptures(); auto vals = l_body->SerializeLambdaCaptures();
broker::vector rval; broker::vector rval;
rval.emplace_back(std::string("CopyFrame")); rval.emplace_back(string("CopyFrame"));
broker::vector body; broker::vector body;
@ -43,14 +44,14 @@ broker::expected<broker::data> CPPLambdaFunc::SerializeClosure() const
return broker::ec::invalid_data; return broker::ec::invalid_data;
TypeTag tag = val->GetType()->Tag(); TypeTag tag = val->GetType()->Tag();
broker::vector val_tuple {std::move(*expected), broker::vector val_tuple {move(*expected),
static_cast<broker::integer>(tag)}; static_cast<broker::integer>(tag)};
body.emplace_back(std::move(val_tuple)); body.emplace_back(move(val_tuple));
} }
rval.emplace_back(std::move(body)); rval.emplace_back(move(body));
return {std::move(rval)}; return {move(rval)};
} }
void CPPLambdaFunc::SetCaptures(Frame* f) void CPPLambdaFunc::SetCaptures(Frame* f)

View file

@ -9,6 +9,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::CompileFunc(const FuncInfo& func) void CPPCompile::CompileFunc(const FuncInfo& func)
{ {
if ( ! IsCompilable(func) ) if ( ! IsCompilable(func) )
@ -33,8 +35,8 @@ void CPPCompile::CompileLambda(const LambdaExpr* l, const ProfileFunc* pf)
FUNC_FLAVOR_FUNCTION); FUNC_FLAVOR_FUNCTION);
} }
void CPPCompile::GenInvokeBody(const std::string& fname, const TypePtr& t, void CPPCompile::GenInvokeBody(const string& fname, const TypePtr& t,
const std::string& args) const string& args)
{ {
auto call = fname + "(" + args + ")"; auto call = fname + "(" + args + ")";
@ -48,7 +50,7 @@ void CPPCompile::GenInvokeBody(const std::string& fname, const TypePtr& t,
} }
void CPPCompile::DefineBody(const FuncTypePtr& ft, const ProfileFunc* pf, void CPPCompile::DefineBody(const FuncTypePtr& ft, const ProfileFunc* pf,
const std::string& fname, const StmtPtr& body, const string& fname, const StmtPtr& body,
const IDPList* lambda_ids, FunctionFlavor flavor) const IDPList* lambda_ids, FunctionFlavor flavor)
{ {
locals.clear(); locals.clear();
@ -117,7 +119,7 @@ void CPPCompile::TranslateAnyParams(const FuncTypePtr& ft, const ProfileFunc* pf
// It's already "any", nothing more to do. // It's already "any", nothing more to do.
continue; continue;
auto any_i = std::string("any_param__CPP_") + Fmt(i); auto any_i = string("any_param__CPP_") + Fmt(i);
Emit("%s %s = %s;", FullTypeName(pt), LocalName(param_id), Emit("%s %s = %s;", FullTypeName(pt), LocalName(param_id),
GenericValPtrToGT(any_i, pt, GEN_NATIVE)); GenericValPtrToGT(any_i, pt, GEN_NATIVE));
@ -156,7 +158,7 @@ void CPPCompile::InitializeEvents(const ProfileFunc* pf)
void CPPCompile::DeclareLocals(const ProfileFunc* pf, const IDPList* lambda_ids) void CPPCompile::DeclareLocals(const ProfileFunc* pf, const IDPList* lambda_ids)
{ {
// It's handy to have a set of the lambda captures rather than a list. // It's handy to have a set of the lambda captures rather than a list.
std::unordered_set<const ID*> lambda_set; unordered_set<const ID*> lambda_set;
if ( lambda_ids ) if ( lambda_ids )
for ( auto li : *lambda_ids ) for ( auto li : *lambda_ids )
lambda_set.insert(li); lambda_set.insert(li);
@ -189,11 +191,11 @@ void CPPCompile::DeclareLocals(const ProfileFunc* pf, const IDPList* lambda_ids)
NL(); NL();
} }
std::string CPPCompile::BodyName(const FuncInfo& func) string CPPCompile::BodyName(const FuncInfo& func)
{ {
const auto& f = func.Func(); const auto& f = func.Func();
const auto& bodies = f->GetBodies(); const auto& bodies = f->GetBodies();
std::string fname = f->Name(); string fname = f->Name();
if ( bodies.size() == 1 ) if ( bodies.size() == 1 )
return fname; return fname;
@ -212,10 +214,10 @@ std::string CPPCompile::BodyName(const FuncInfo& func)
return fname + "__" + Fmt(i); return fname + "__" + Fmt(i);
} }
std::string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e) string CPPCompile::GenArgs(const RecordTypePtr& params, const Expr* e)
{ {
const auto& exprs = e->AsListExpr()->Exprs(); const auto& exprs = e->AsListExpr()->Exprs();
std::string gen; string gen;
int n = exprs.size(); int n = exprs.size();

View file

@ -6,13 +6,15 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
VarMapper compiled_items; VarMapper compiled_items;
CPPHashManager::CPPHashManager(const char* hash_name_base, bool _append) CPPHashManager::CPPHashManager(const char* hash_name_base, bool _append)
{ {
append = _append; append = _append;
hash_name = std::string(hash_name_base) + ".dat"; hash_name = string(hash_name_base) + ".dat";
if ( append ) if ( append )
{ {
@ -52,7 +54,7 @@ CPPHashManager::~CPPHashManager()
void CPPHashManager::LoadHashes(FILE* f) void CPPHashManager::LoadHashes(FILE* f)
{ {
std::string key; string key;
// The hash file format is inefficient but simple to scan. // The hash file format is inefficient but simple to scan.
// It doesn't appear to pose a bottleneck, so until it does // It doesn't appear to pose a bottleneck, so until it does
@ -60,7 +62,7 @@ void CPPHashManager::LoadHashes(FILE* f)
while ( GetLine(f, key) ) while ( GetLine(f, key) )
{ {
std::string line; string line;
RequireLine(f, line); RequireLine(f, line);
@ -133,7 +135,7 @@ void CPPHashManager::LoadHashes(FILE* f)
} }
} }
void CPPHashManager::RequireLine(FILE* f, std::string& line) void CPPHashManager::RequireLine(FILE* f, string& line)
{ {
if ( ! GetLine(f, line) ) if ( ! GetLine(f, line) )
{ {
@ -142,7 +144,7 @@ void CPPHashManager::RequireLine(FILE* f, std::string& line)
} }
} }
bool CPPHashManager::GetLine(FILE* f, std::string& line) bool CPPHashManager::GetLine(FILE* f, string& line)
{ {
char buf[8192]; char buf[8192];
if ( ! fgets(buf, sizeof buf, f) ) if ( ! fgets(buf, sizeof buf, f) )
@ -156,7 +158,7 @@ bool CPPHashManager::GetLine(FILE* f, std::string& line)
return true; return true;
} }
void CPPHashManager::BadLine(std::string& line) void CPPHashManager::BadLine(string& line)
{ {
reporter->Error("bad %s hash file entry: %s", reporter->Error("bad %s hash file entry: %s",
hash_name.c_str(), line.c_str()); hash_name.c_str(), line.c_str());

View file

@ -10,6 +10,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::GenInitExpr(const ExprPtr& e) void CPPCompile::GenInitExpr(const ExprPtr& e)
{ {
NL(); NL();
@ -18,7 +20,7 @@ void CPPCompile::GenInitExpr(const ExprPtr& e)
auto ename = InitExprName(e); auto ename = InitExprName(e);
// First, create a CPPFunc that we can compile to compute 'e'. // First, create a CPPFunc that we can compile to compute 'e'.
auto name = std::string("wrapper_") + ename; auto name = string("wrapper_") + ename;
// Forward declaration of the function that computes 'e'. // Forward declaration of the function that computes 'e'.
Emit("static %s %s(Frame* f__CPP);", FullTypeName(t), name); Emit("static %s %s(Frame* f__CPP);", FullTypeName(t), name);
@ -58,7 +60,7 @@ void CPPCompile::GenInitExpr(const ExprPtr& e)
Emit("CallExprPtr %s;", ename); Emit("CallExprPtr %s;", ename);
NoteInitDependency(e, TypeRep(t)); NoteInitDependency(e, TypeRep(t));
AddInit(e, ename, std::string("make_intrusive<CallExpr>(make_intrusive<ConstExpr>(make_intrusive<FuncVal>(make_intrusive<") + AddInit(e, ename, string("make_intrusive<CallExpr>(make_intrusive<ConstExpr>(make_intrusive<FuncVal>(make_intrusive<") +
name + "_cl>())), make_intrusive<ListExpr>(), false)"); name + "_cl>())), make_intrusive<ListExpr>(), false)");
} }
@ -87,12 +89,12 @@ bool CPPCompile::IsSimpleInitExpr(const ExprPtr& e) const
} }
} }
std::string CPPCompile::InitExprName(const ExprPtr& e) string CPPCompile::InitExprName(const ExprPtr& e)
{ {
return init_exprs.KeyName(e); return init_exprs.KeyName(e);
} }
void CPPCompile::GenGlobalInit(const ID* g, std::string& gl, const ValPtr& v) void CPPCompile::GenGlobalInit(const ID* g, string& gl, const ValPtr& v)
{ {
const auto& t = v->GetType(); const auto& t = v->GetType();
auto tag = t->Tag(); auto tag = t->Tag();
@ -102,7 +104,7 @@ void CPPCompile::GenGlobalInit(const ID* g, std::string& gl, const ValPtr& v)
// the function's body. // the function's body.
return; return;
std::string init_val; string init_val;
if ( tag == TYPE_OPAQUE ) if ( tag == TYPE_OPAQUE )
{ {
// We can only generate these by reproducing the expression // We can only generate these by reproducing the expression
@ -127,7 +129,7 @@ void CPPCompile::GenGlobalInit(const ID* g, std::string& gl, const ValPtr& v)
auto& attrs = g->GetAttrs(); auto& attrs = g->GetAttrs();
AddInit(g, std::string("if ( ! ") + gl + "->HasVal() )"); AddInit(g, string("if ( ! ") + gl + "->HasVal() )");
if ( attrs ) if ( attrs )
{ {
@ -157,7 +159,7 @@ void CPPCompile::GenFuncVarInits()
const auto& bodies = f->GetBodies(); const auto& bodies = f->GetBodies();
std::string hashes = "{"; string hashes = "{";
for ( auto b : bodies ) for ( auto b : bodies )
{ {
@ -178,7 +180,7 @@ void CPPCompile::GenFuncVarInits()
hashes += "}"; hashes += "}";
auto init = std::string("lookup_func__CPP(\"") + fn + auto init = string("lookup_func__CPP(\"") + fn +
"\", " + hashes + ", " + GenTypeName(ft) + ")"; "\", " + hashes + ", " + GenTypeName(ft) + ")";
AddInit(fv, const_name, init); AddInit(fv, const_name, init);
@ -187,7 +189,7 @@ void CPPCompile::GenFuncVarInits()
void CPPCompile::GenPreInit(const Type* t) void CPPCompile::GenPreInit(const Type* t)
{ {
std::string pre_init; string pre_init;
switch ( t->Tag() ) { switch ( t->Tag() ) {
case TYPE_ADDR: case TYPE_ADDR:
@ -204,43 +206,43 @@ void CPPCompile::GenPreInit(const Type* t)
case TYPE_TIME: case TYPE_TIME:
case TYPE_TIMER: case TYPE_TIMER:
case TYPE_VOID: case TYPE_VOID:
pre_init = std::string("base_type(") + TypeTagName(t->Tag()) + ")"; pre_init = string("base_type(") + TypeTagName(t->Tag()) + ")";
break; break;
case TYPE_ENUM: case TYPE_ENUM:
pre_init = std::string("get_enum_type__CPP(\"") + pre_init = string("get_enum_type__CPP(\"") +
t->GetName() + "\")"; t->GetName() + "\")";
break; break;
case TYPE_SUBNET: case TYPE_SUBNET:
pre_init = std::string("make_intrusive<SubNetType>()"); pre_init = string("make_intrusive<SubNetType>()");
break; break;
case TYPE_FILE: case TYPE_FILE:
pre_init = std::string("make_intrusive<FileType>(") + pre_init = string("make_intrusive<FileType>(") +
GenTypeName(t->AsFileType()->Yield()) + ")"; GenTypeName(t->AsFileType()->Yield()) + ")";
break; break;
case TYPE_OPAQUE: case TYPE_OPAQUE:
pre_init = std::string("make_intrusive<OpaqueType>(\"") + pre_init = string("make_intrusive<OpaqueType>(\"") +
t->AsOpaqueType()->Name() + "\")"; t->AsOpaqueType()->Name() + "\")";
break; break;
case TYPE_RECORD: case TYPE_RECORD:
{ {
std::string name; string name;
if ( t->GetName() != "" ) if ( t->GetName() != "" )
name = std::string("\"") + t->GetName() + std::string("\""); name = string("\"") + t->GetName() + string("\"");
else else
name = "nullptr"; name = "nullptr";
pre_init = std::string("get_record_type__CPP(") + name + ")"; pre_init = string("get_record_type__CPP(") + name + ")";
} }
break; break;
case TYPE_LIST: case TYPE_LIST:
pre_init = std::string("make_intrusive<TypeList>()"); pre_init = string("make_intrusive<TypeList>()");
break; break;
case TYPE_TYPE: case TYPE_TYPE:
@ -268,7 +270,7 @@ void CPPCompile::GenPreInits()
EndBlock(); EndBlock();
} }
void CPPCompile::AddInit(const Obj* o, const std::string& init) void CPPCompile::AddInit(const Obj* o, const string& init)
{ {
obj_inits[o].emplace_back(init); obj_inits[o].emplace_back(init);
} }
@ -277,7 +279,7 @@ void CPPCompile::AddInit(const Obj* o)
{ {
if ( obj_inits.count(o) == 0 ) if ( obj_inits.count(o) == 0 )
{ {
std::vector<std::string> empty; vector<string> empty;
obj_inits[o] = empty; obj_inits[o] = empty;
} }
} }
@ -287,7 +289,7 @@ void CPPCompile::NoteInitDependency(const Obj* o1, const Obj* o2)
obj_deps[o1].emplace(o2); obj_deps[o1].emplace(o2);
} }
void CPPCompile::CheckInitConsistency(std::unordered_set<const Obj*>& to_do) void CPPCompile::CheckInitConsistency(unordered_set<const Obj*>& to_do)
{ {
for ( const auto& od : obj_deps ) for ( const auto& od : obj_deps )
{ {
@ -312,7 +314,7 @@ void CPPCompile::CheckInitConsistency(std::unordered_set<const Obj*>& to_do)
} }
} }
int CPPCompile::GenDependentInits(std::unordered_set<const Obj*>& to_do) int CPPCompile::GenDependentInits(unordered_set<const Obj*>& to_do)
{ {
int n = 0; int n = 0;
@ -323,7 +325,7 @@ int CPPCompile::GenDependentInits(std::unordered_set<const Obj*>& to_do)
// are no more to-do items. // are no more to-do items.
while ( to_do.size() > 0 ) while ( to_do.size() > 0 )
{ {
std::unordered_set<const Obj*> cohort; unordered_set<const Obj*> cohort;
for ( const auto& o : to_do ) for ( const auto& o : to_do )
{ {
@ -361,7 +363,7 @@ int CPPCompile::GenDependentInits(std::unordered_set<const Obj*>& to_do)
return n; return n;
} }
void CPPCompile::GenInitCohort(int nc, std::unordered_set<const Obj*>& cohort) void CPPCompile::GenInitCohort(int nc, unordered_set<const Obj*>& cohort)
{ {
NL(); NL();
Emit("void init_%s__CPP()", Fmt(nc)); Emit("void init_%s__CPP()", Fmt(nc));
@ -430,7 +432,7 @@ void CPPCompile::InitializeEnumMappings()
} }
void CPPCompile::InitializeEnumMappings(const EnumType* et, void CPPCompile::InitializeEnumMappings(const EnumType* et,
const std::string& e_name, int index) const string& e_name, int index)
{ {
AddInit(et, "{"); AddInit(et, "{");
@ -490,7 +492,7 @@ void CPPCompile::GenStandaloneActivation()
// associated scripts. // associated scripts.
// First, build up a list of per-hook/event handler bodies. // First, build up a list of per-hook/event handler bodies.
std::unordered_map<const Func*, std::vector<p_hash_type>> func_bodies; unordered_map<const Func*, vector<p_hash_type>> func_bodies;
for ( const auto& func : funcs ) for ( const auto& func : funcs )
{ {
@ -517,7 +519,7 @@ void CPPCompile::GenStandaloneActivation()
const auto fn = f->Name(); const auto fn = f->Name();
const auto& ft = f->GetType(); const auto& ft = f->GetType();
std::string hashes; string hashes;
for ( auto h : fb.second ) for ( auto h : fb.second )
{ {
if ( hashes.size() > 0 ) if ( hashes.size() > 0 )
@ -540,7 +542,7 @@ void CPPCompile::GenLoad()
{ {
// First, generate a hash unique to this compilation. // First, generate a hash unique to this compilation.
auto t = util::current_time(); auto t = util::current_time();
auto th = std::hash<double>{}(t); auto th = hash<double>{}(t);
total_hash = merge_p_hashes(total_hash, th); total_hash = merge_p_hashes(total_hash, th);

View file

@ -6,7 +6,9 @@
namespace zeek::detail { namespace zeek::detail {
std::vector<CPP_init_func> CPP_init_funcs; using namespace std;
vector<CPP_init_func> CPP_init_funcs;
// Calls all of the initialization hooks, in the order they were added. // Calls all of the initialization hooks, in the order they were added.
void init_CPPs() void init_CPPs()
@ -29,9 +31,9 @@ static int dummy = flag_init_CPP();
void register_body__CPP(CPPStmtPtr body, int priority, p_hash_type hash, void register_body__CPP(CPPStmtPtr body, int priority, p_hash_type hash,
std::vector<std::string> events) vector<string> events)
{ {
compiled_scripts[hash] = { std::move(body), priority, std::move(events) }; compiled_scripts[hash] = { move(body), priority, move(events) };
} }
void register_lambda__CPP(CPPStmtPtr body, p_hash_type hash, const char* name, void register_lambda__CPP(CPPStmtPtr body, p_hash_type hash, const char* name,
@ -44,8 +46,8 @@ void register_lambda__CPP(CPPStmtPtr body, p_hash_type hash, const char* name,
auto func = make_intrusive<CPPLambdaFunc>(name, ft, body); auto func = make_intrusive<CPPLambdaFunc>(name, ft, body);
func->SetName(name); func->SetName(name);
auto v = make_intrusive<FuncVal>(std::move(func)); auto v = make_intrusive<FuncVal>(move(func));
id->SetVal(std::move(v)); id->SetVal(move(v));
id->SetType(ft); id->SetType(ft);
// Lambdas used in initializing global functions need to // Lambdas used in initializing global functions need to
@ -65,8 +67,7 @@ void register_scripts__CPP(p_hash_type h, void (*callback)())
standalone_callbacks[h] = callback; standalone_callbacks[h] = callback;
} }
void activate_bodies__CPP(const char* fn, TypePtr t, void activate_bodies__CPP(const char* fn, TypePtr t, vector<p_hash_type> hashes)
std::vector<p_hash_type> hashes)
{ {
auto ft = cast_intrusive<FuncType>(t); auto ft = cast_intrusive<FuncType>(t);
auto fg = lookup_ID(fn, GLOBAL_MODULE_NAME, false, false, false); auto fg = lookup_ID(fn, GLOBAL_MODULE_NAME, false, false, false);
@ -81,7 +82,7 @@ void activate_bodies__CPP(const char* fn, TypePtr t,
const auto& bodies = f->GetBodies(); const auto& bodies = f->GetBodies();
// Track hashes of compiled bodies already associated with f. // Track hashes of compiled bodies already associated with f.
std::unordered_set<p_hash_type> existing_CPP_bodies; unordered_set<p_hash_type> existing_CPP_bodies;
for ( auto& b : bodies ) for ( auto& b : bodies )
{ {
auto s = b.stmts; auto s = b.stmts;
@ -93,12 +94,12 @@ void activate_bodies__CPP(const char* fn, TypePtr t,
} }
// Events we need to register. // Events we need to register.
std::unordered_set<std::string> events; unordered_set<string> events;
if ( ft->Flavor() == FUNC_FLAVOR_EVENT ) if ( ft->Flavor() == FUNC_FLAVOR_EVENT )
events.insert(fn); events.insert(fn);
std::vector<detail::IDPtr> no_inits; // empty initialization vector vector<detail::IDPtr> no_inits; // empty initialization vector
int num_params = ft->Params()->NumFields(); int num_params = ft->Params()->NumFields();
for ( auto h : hashes ) for ( auto h : hashes )
@ -144,13 +145,13 @@ Func* lookup_bif__CPP(const char* bif)
return b ? b->GetVal()->AsFunc() : nullptr; return b ? b->GetVal()->AsFunc() : nullptr;
} }
FuncValPtr lookup_func__CPP(std::string name, std::vector<p_hash_type> hashes, FuncValPtr lookup_func__CPP(string name, vector<p_hash_type> hashes,
const TypePtr& t) const TypePtr& t)
{ {
auto ft = cast_intrusive<FuncType>(t); auto ft = cast_intrusive<FuncType>(t);
std::vector<StmtPtr> bodies; vector<StmtPtr> bodies;
std::vector<int> priorities; vector<int> priorities;
for ( auto h : hashes ) for ( auto h : hashes )
{ {
@ -170,11 +171,10 @@ FuncValPtr lookup_func__CPP(std::string name, std::vector<p_hash_type> hashes,
} }
} }
auto sf = make_intrusive<ScriptFunc>(std::move(name), std::move(ft), auto sf = make_intrusive<ScriptFunc>(move(name), move(ft), move(bodies),
std::move(bodies), move(priorities));
std::move(priorities));
return make_intrusive<FuncVal>(std::move(sf)); return make_intrusive<FuncVal>(move(sf));
} }
@ -190,7 +190,7 @@ RecordTypePtr get_record_type__CPP(const char* record_type_name)
return make_intrusive<RecordType>(new type_decl_list()); return make_intrusive<RecordType>(new type_decl_list());
} }
EnumTypePtr get_enum_type__CPP(const std::string& enum_type_name) EnumTypePtr get_enum_type__CPP(const string& enum_type_name)
{ {
auto existing_type = global_scope()->Find(enum_type_name); auto existing_type = global_scope()->Find(enum_type_name);
@ -202,7 +202,7 @@ EnumTypePtr get_enum_type__CPP(const std::string& enum_type_name)
EnumValPtr make_enum__CPP(TypePtr t, int i) EnumValPtr make_enum__CPP(TypePtr t, int i)
{ {
auto et = cast_intrusive<EnumType>(std::move(t)); auto et = cast_intrusive<EnumType>(move(t));
return make_intrusive<EnumVal>(et, i); return make_intrusive<EnumVal>(et, i);
} }

View file

@ -8,9 +8,11 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
StringValPtr str_concat__CPP(const String* s1, const String* s2) StringValPtr str_concat__CPP(const String* s1, const String* s2)
{ {
std::vector<const String*> strings(2); vector<const String*> strings(2);
strings[0] = s1; strings[0] = s1;
strings[1] = s2; strings[1] = s2;
@ -23,7 +25,7 @@ bool str_in__CPP(const String* s1, const String* s2)
return util::strstr_n(s2->Len(), s2->Bytes(), s1->Len(), s) != -1; return util::strstr_n(s2->Len(), s2->Bytes(), s1->Len(), s) != -1;
} }
ListValPtr index_val__CPP(std::vector<ValPtr> indices) ListValPtr index_val__CPP(vector<ValPtr> indices)
{ {
auto ind_v = make_intrusive<ListVal>(TYPE_ANY); auto ind_v = make_intrusive<ListVal>(TYPE_ANY);
@ -35,9 +37,9 @@ ListValPtr index_val__CPP(std::vector<ValPtr> indices)
return ind_v; return ind_v;
} }
ValPtr index_table__CPP(const TableValPtr& t, std::vector<ValPtr> indices) ValPtr index_table__CPP(const TableValPtr& t, vector<ValPtr> indices)
{ {
auto v = t->FindOrDefault(index_val__CPP(std::move(indices))); auto v = t->FindOrDefault(index_val__CPP(move(indices)));
if ( ! v ) if ( ! v )
reporter->CPPRuntimeError("no such index"); reporter->CPPRuntimeError("no such index");
return v; return v;
@ -51,15 +53,15 @@ ValPtr index_vec__CPP(const VectorValPtr& vec, int index)
return v; return v;
} }
ValPtr index_string__CPP(const StringValPtr& svp, std::vector<ValPtr> indices) ValPtr index_string__CPP(const StringValPtr& svp, vector<ValPtr> indices)
{ {
return index_string(svp->AsString(), return index_string(svp->AsString(),
index_val__CPP(std::move(indices)).get()); index_val__CPP(move(indices)).get());
} }
ValPtr set_event__CPP(IDPtr g, ValPtr v, EventHandlerPtr& gh) ValPtr set_event__CPP(IDPtr g, ValPtr v, EventHandlerPtr& gh)
{ {
g->SetVal(std::move(v)); g->SetVal(move(v));
gh = event_registry->Register(g->Name()); gh = event_registry->Register(g->Name());
return v; return v;
} }
@ -102,7 +104,7 @@ template <typename T>
ValPtr assign_to_index__CPP(T v1, ValPtr v2, ValPtr v3) ValPtr assign_to_index__CPP(T v1, ValPtr v2, ValPtr v3)
{ {
bool iterators_invalidated = false; bool iterators_invalidated = false;
auto err_msg = assign_to_index(std::move(v1), std::move(v2), v3, iterators_invalidated); auto err_msg = assign_to_index(move(v1), move(v2), v3, iterators_invalidated);
check_iterators__CPP(iterators_invalidated); check_iterators__CPP(iterators_invalidated);
@ -143,10 +145,10 @@ void remove_element__CPP(TableValPtr aggr, ListValPtr indices)
// and values and returns a collective AttributesPtr corresponding to // and values and returns a collective AttributesPtr corresponding to
// those instantiated attributes. For attributes that don't have // those instantiated attributes. For attributes that don't have
// associated expressions, the correspoinding value should be nil. // associated expressions, the correspoinding value should be nil.
static AttributesPtr build_attrs__CPP(std::vector<int> attr_tags, static AttributesPtr build_attrs__CPP(vector<int> attr_tags,
std::vector<ValPtr> attr_vals) vector<ValPtr> attr_vals)
{ {
std::vector<AttrPtr> attrs; vector<AttrPtr> attrs;
int nattrs = attr_tags.size(); int nattrs = attr_tags.size();
for ( auto i = 0; i < nattrs; ++i ) for ( auto i = 0; i < nattrs; ++i )
{ {
@ -160,46 +162,45 @@ static AttributesPtr build_attrs__CPP(std::vector<int> attr_tags,
attrs.emplace_back(make_intrusive<Attr>(t_i, e)); attrs.emplace_back(make_intrusive<Attr>(t_i, e));
} }
return make_intrusive<Attributes>(std::move(attrs), nullptr, false, false); return make_intrusive<Attributes>(move(attrs), nullptr, false, false);
} }
TableValPtr set_constructor__CPP(std::vector<ValPtr> elements, TableTypePtr t, TableValPtr set_constructor__CPP(vector<ValPtr> elements, TableTypePtr t,
std::vector<int> attr_tags, vector<int> attr_tags,
std::vector<ValPtr> attr_vals) vector<ValPtr> attr_vals)
{ {
auto attrs = build_attrs__CPP(std::move(attr_tags), std::move(attr_vals)); auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals));
auto aggr = make_intrusive<TableVal>(std::move(t), std::move(attrs)); auto aggr = make_intrusive<TableVal>(move(t), move(attrs));
for ( const auto& elem : elements ) for ( const auto& elem : elements )
aggr->Assign(std::move(elem), nullptr); aggr->Assign(move(elem), nullptr);
return aggr; return aggr;
} }
TableValPtr table_constructor__CPP(std::vector<ValPtr> indices, TableValPtr table_constructor__CPP(vector<ValPtr> indices, vector<ValPtr> vals,
std::vector<ValPtr> vals, TableTypePtr t, TableTypePtr t, vector<int> attr_tags,
std::vector<int> attr_tags, vector<ValPtr> attr_vals)
std::vector<ValPtr> attr_vals)
{ {
const auto& yt = t->Yield().get(); const auto& yt = t->Yield().get();
auto n = indices.size(); auto n = indices.size();
auto attrs = build_attrs__CPP(std::move(attr_tags), std::move(attr_vals)); auto attrs = build_attrs__CPP(move(attr_tags), move(attr_vals));
auto aggr = make_intrusive<TableVal>(std::move(t), std::move(attrs)); auto aggr = make_intrusive<TableVal>(move(t), move(attrs));
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
{ {
auto v = check_and_promote(vals[i], yt, true); auto v = check_and_promote(vals[i], yt, true);
if ( v ) if ( v )
aggr->Assign(std::move(indices[i]), std::move(v)); aggr->Assign(move(indices[i]), move(v));
} }
return aggr; return aggr;
} }
RecordValPtr record_constructor__CPP(std::vector<ValPtr> vals, RecordTypePtr t) RecordValPtr record_constructor__CPP(vector<ValPtr> vals, RecordTypePtr t)
{ {
auto rv = make_intrusive<RecordVal>(std::move(t)); auto rv = make_intrusive<RecordVal>(move(t));
auto n = vals.size(); auto n = vals.size();
rv->Reserve(n); rv->Reserve(n);
@ -210,9 +211,9 @@ RecordValPtr record_constructor__CPP(std::vector<ValPtr> vals, RecordTypePtr t)
return rv; return rv;
} }
VectorValPtr vector_constructor__CPP(std::vector<ValPtr> vals, VectorTypePtr t) VectorValPtr vector_constructor__CPP(vector<ValPtr> vals, VectorTypePtr t)
{ {
auto vv = make_intrusive<VectorVal>(std::move(t)); auto vv = make_intrusive<VectorVal>(move(t));
auto n = vals.size(); auto n = vals.size();
for ( auto i = 0; i < n; ++i ) for ( auto i = 0; i < n; ++i )
@ -221,10 +222,10 @@ VectorValPtr vector_constructor__CPP(std::vector<ValPtr> vals, VectorTypePtr t)
return vv; return vv;
} }
ValPtr schedule__CPP(double dt, EventHandlerPtr event, std::vector<ValPtr> args) ValPtr schedule__CPP(double dt, EventHandlerPtr event, vector<ValPtr> args)
{ {
if ( ! run_state::terminating ) if ( ! run_state::terminating )
timer_mgr->Add(new ScheduleTimer(event, std::move(args), dt)); timer_mgr->Add(new ScheduleTimer(event, move(args), dt));
return nullptr; return nullptr;
} }

View file

@ -5,6 +5,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
// Helper function for ensuring that two vectors have matching sizes. // Helper function for ensuring that two vectors have matching sizes.
static bool check_vec_sizes__CPP(const VectorValPtr& v1, const VectorValPtr& v2) static bool check_vec_sizes__CPP(const VectorValPtr& v1, const VectorValPtr& v2)
{ {
@ -224,7 +226,7 @@ VectorValPtr vec_op_add__CPP(VectorValPtr v, int incr)
VectorValPtr vec_op_sub__CPP(VectorValPtr v, int i) VectorValPtr vec_op_sub__CPP(VectorValPtr v, int i)
{ {
return vec_op_add__CPP(std::move(v), -i); return vec_op_add__CPP(move(v), -i);
} }
// This function provides the core functionality. The arguments // This function provides the core functionality. The arguments
@ -241,7 +243,7 @@ static VectorValPtr str_vec_op_str_vec_add__CPP(const StringValPtr& s1,
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )
{ {
std::vector<const String*> strings; vector<const String*> strings;
auto v2_i = v2->ValAt(i); auto v2_i = v2->ValAt(i);
if ( ! v2_i ) if ( ! v2_i )
@ -354,7 +356,7 @@ VectorValPtr vector_select__CPP(const VectorValPtr& v1, VectorValPtr v2,
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )
{ {
auto vr_i = v1->BoolAt(i) ? v2->ValAt(i) : v3->ValAt(i); auto vr_i = v1->BoolAt(i) ? v2->ValAt(i) : v3->ValAt(i);
v_result->Assign(i, std::move(vr_i)); v_result->Assign(i, move(vr_i));
} }
return v_result; return v_result;
@ -363,7 +365,7 @@ VectorValPtr vector_select__CPP(const VectorValPtr& v1, VectorValPtr v2,
VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ) VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ)
{ {
auto res_t = cast_intrusive<VectorType>(targ); auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(std::move(res_t)); auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size(); auto n = v->Size();
auto yt = targ->Yield(); auto yt = targ->Yield();
auto ytag = yt->Tag(); auto ytag = yt->Tag();
@ -397,7 +399,7 @@ VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ)
reporter->InternalError("bad vector type in vector_coerce_to__CPP"); reporter->InternalError("bad vector type in vector_coerce_to__CPP");
} }
v_result->Assign(i, std::move(r_i)); v_result->Assign(i, move(r_i));
} }
return v_result; return v_result;
@ -406,7 +408,7 @@ VectorValPtr vector_coerce_to__CPP(const VectorValPtr& v, const TypePtr& targ)
VectorValPtr vec_coerce_to_bro_int_t__CPP(const VectorValPtr& v, TypePtr targ) VectorValPtr vec_coerce_to_bro_int_t__CPP(const VectorValPtr& v, TypePtr targ)
{ {
auto res_t = cast_intrusive<VectorType>(targ); auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(std::move(res_t)); auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size(); auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )
@ -418,7 +420,7 @@ VectorValPtr vec_coerce_to_bro_int_t__CPP(const VectorValPtr& v, TypePtr targ)
VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr targ) VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr targ)
{ {
auto res_t = cast_intrusive<VectorType>(targ); auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(std::move(res_t)); auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size(); auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )
@ -430,7 +432,7 @@ VectorValPtr vec_coerce_to_bro_uint_t__CPP(const VectorValPtr& v, TypePtr targ)
VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ) VectorValPtr vec_coerce_to_double__CPP(const VectorValPtr& v, TypePtr targ)
{ {
auto res_t = cast_intrusive<VectorType>(targ); auto res_t = cast_intrusive<VectorType>(targ);
auto v_result = make_intrusive<VectorVal>(std::move(res_t)); auto v_result = make_intrusive<VectorVal>(move(res_t));
auto n = v->Size(); auto n = v->Size();
for ( unsigned int i = 0; i < n; ++i ) for ( unsigned int i = 0; i < n; ++i )

View file

@ -6,6 +6,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
void CPPCompile::GenStmt(const Stmt* s) void CPPCompile::GenStmt(const Stmt* s)
{ {
switch ( s->Tag() ) { switch ( s->Tag() ) {
@ -222,11 +224,11 @@ void CPPCompile::GenEventStmt(const EventStmt* ev)
if ( ev_e->Args()->Exprs().length() > 0 ) if ( ev_e->Args()->Exprs().length() > 0 )
Emit("event_mgr.Enqueue(%s_ev, %s);", Emit("event_mgr.Enqueue(%s_ev, %s);",
globals[std::string(ev_n)], globals[string(ev_n)],
GenExpr(ev_e->Args(), GEN_VAL_PTR)); GenExpr(ev_e->Args(), GEN_VAL_PTR));
else else
Emit("event_mgr.Enqueue(%s_ev, Args{});", Emit("event_mgr.Enqueue(%s_ev, Args{});",
globals[std::string(ev_n)]); globals[string(ev_n)]);
} }
void CPPCompile::GenSwitchStmt(const SwitchStmt* sw) void CPPCompile::GenSwitchStmt(const SwitchStmt* sw)
@ -239,12 +241,12 @@ void CPPCompile::GenSwitchStmt(const SwitchStmt* sw)
bool is_uint = e_it == TYPE_INTERNAL_UNSIGNED; bool is_uint = e_it == TYPE_INTERNAL_UNSIGNED;
bool organic = is_int || is_uint; bool organic = is_int || is_uint;
std::string sw_val; string sw_val;
if ( organic ) if ( organic )
sw_val = GenExpr(e, GEN_NATIVE); sw_val = GenExpr(e, GEN_NATIVE);
else else
sw_val = std::string("p_hash(") + GenExpr(e, GEN_VAL_PTR) + ")"; sw_val = string("p_hash(") + GenExpr(e, GEN_VAL_PTR) + ")";
Emit("switch ( %s ) {", sw_val.c_str()); Emit("switch ( %s ) {", sw_val.c_str());
@ -262,7 +264,7 @@ void CPPCompile::GenSwitchStmt(const SwitchStmt* sw)
auto c_v = c_e->Eval(nullptr); auto c_v = c_e->Eval(nullptr);
ASSERT(c_v); ASSERT(c_v);
std::string c_v_rep; string c_v_rep;
if ( is_int ) if ( is_int )
c_v_rep = Fmt(int(c_v->AsInt())); c_v_rep = Fmt(int(c_v->AsInt()));

View file

@ -8,6 +8,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
template<class T> template<class T>
void CPPTracker<T>::AddKey(IntrusivePtr<T> key, p_hash_type h) void CPPTracker<T>::AddKey(IntrusivePtr<T> key, p_hash_type h)
{ {
@ -43,7 +45,7 @@ void CPPTracker<T>::AddKey(IntrusivePtr<T> key, p_hash_type h)
} }
template<class T> template<class T>
std::string CPPTracker<T>::KeyName(const T* key) string CPPTracker<T>::KeyName(const T* key)
{ {
ASSERT(HasKey(key)); ASSERT(HasKey(key));
@ -52,11 +54,11 @@ std::string CPPTracker<T>::KeyName(const T* key)
auto index = map2[hash]; auto index = map2[hash];
std::string scope; string scope;
if ( IsInherited(hash) ) if ( IsInherited(hash) )
scope = scope_prefix(scope2[hash]); scope = scope_prefix(scope2[hash]);
return scope + std::string(base_name) + "_" + Fmt(index) + "__CPP"; return scope + string(base_name) + "_" + Fmt(index) + "__CPP";
} }
template<class T> template<class T>
@ -77,8 +79,8 @@ p_hash_type CPPTracker<T>::Hash(IntrusivePtr<T> key) const
ODesc d; ODesc d;
d.SetDeterminism(true); d.SetDeterminism(true);
key->Describe(&d); key->Describe(&d);
std::string desc = d.Description(); string desc = d.Description();
auto h = std::hash<std::string>{}(base_name + desc); auto h = hash<string>{}(base_name + desc);
return p_hash_type(h); return p_hash_type(h);
} }

View file

@ -5,6 +5,8 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
bool CPPCompile::IsNativeType(const TypePtr& t) const bool CPPCompile::IsNativeType(const TypePtr& t) const
{ {
if ( ! t ) if ( ! t )
@ -45,8 +47,7 @@ bool CPPCompile::IsNativeType(const TypePtr& t) const
} }
} }
std::string CPPCompile::NativeToGT(const std::string& expr, const TypePtr& t, string CPPCompile::NativeToGT(const string& expr, const TypePtr& t, GenType gt)
GenType gt)
{ {
if ( gt == GEN_DONT_CARE ) if ( gt == GEN_DONT_CARE )
return expr; return expr;
@ -60,34 +61,34 @@ std::string CPPCompile::NativeToGT(const std::string& expr, const TypePtr& t,
return expr; return expr;
case TYPE_BOOL: case TYPE_BOOL:
return std::string("val_mgr->Bool(") + expr + ")"; return string("val_mgr->Bool(") + expr + ")";
case TYPE_INT: case TYPE_INT:
return std::string("val_mgr->Int(") + expr + ")"; return string("val_mgr->Int(") + expr + ")";
case TYPE_COUNT: case TYPE_COUNT:
return std::string("val_mgr->Count(") + expr + ")"; return string("val_mgr->Count(") + expr + ")";
case TYPE_PORT: case TYPE_PORT:
return std::string("val_mgr->Port(") + expr + ")"; return string("val_mgr->Port(") + expr + ")";
case TYPE_ENUM: case TYPE_ENUM:
return std::string("make_enum__CPP(") + GenTypeName(t) + ", " + return string("make_enum__CPP(") + GenTypeName(t) + ", " +
expr + ")"; expr + ")";
default: default:
return std::string("make_intrusive<") + IntrusiveVal(t) + return string("make_intrusive<") + IntrusiveVal(t) +
">(" + expr + ")"; ">(" + expr + ")";
} }
} }
std::string CPPCompile::GenericValPtrToGT(const std::string& expr, string CPPCompile::GenericValPtrToGT(const string& expr, const TypePtr& t,
const TypePtr& t, GenType gt) GenType gt)
{ {
if ( gt != GEN_VAL_PTR && IsNativeType(t) ) if ( gt != GEN_VAL_PTR && IsNativeType(t) )
return expr + NativeAccessor(t); return expr + NativeAccessor(t);
else else
return std::string("cast_intrusive<") + IntrusiveVal(t) + return string("cast_intrusive<") + IntrusiveVal(t) +
">(" + expr + ")"; ">(" + expr + ")";
} }
@ -117,12 +118,12 @@ void CPPCompile::ExpandTypeVar(const TypePtr& t)
break; break;
case TYPE_TYPE: case TYPE_TYPE:
AddInit(t, tn, std::string("make_intrusive<TypeType>(") + AddInit(t, tn, string("make_intrusive<TypeType>(") +
GenTypeName(t->AsTypeType()->GetType()) + ")"); GenTypeName(t->AsTypeType()->GetType()) + ")");
break; break;
case TYPE_VECTOR: case TYPE_VECTOR:
AddInit(t, tn, std::string("make_intrusive<VectorType>(") + AddInit(t, tn, string("make_intrusive<VectorType>(") +
GenTypeName(t->AsVectorType()->Yield()) + ")"); GenTypeName(t->AsVectorType()->Yield()) + ")");
break; break;
@ -137,7 +138,7 @@ void CPPCompile::ExpandTypeVar(const TypePtr& t)
AddInit(t); AddInit(t);
} }
void CPPCompile::ExpandListTypeVar(const TypePtr& t, std::string& tn) void CPPCompile::ExpandListTypeVar(const TypePtr& t, string& tn)
{ {
auto tl = t->AsTypeList()->GetTypes(); auto tl = t->AsTypeList()->GetTypes();
auto t_name = tn + "->AsTypeList()"; auto t_name = tn + "->AsTypeList()";
@ -147,12 +148,12 @@ void CPPCompile::ExpandListTypeVar(const TypePtr& t, std::string& tn)
GenTypeName(tl[i]) + ");"); GenTypeName(tl[i]) + ");");
} }
void CPPCompile::ExpandRecordTypeVar(const TypePtr& t, std::string& tn) void CPPCompile::ExpandRecordTypeVar(const TypePtr& t, string& tn)
{ {
auto r = t->AsRecordType()->Types(); auto r = t->AsRecordType()->Types();
auto t_name = tn + "->AsRecordType()"; auto t_name = tn + "->AsRecordType()";
AddInit(t, std::string("if ( ") + t_name + "->NumFields() == 0 )"); AddInit(t, string("if ( ") + t_name + "->NumFields() == 0 )");
AddInit(t, "{"); AddInit(t, "{");
AddInit(t, "type_decl_list tl;"); AddInit(t, "type_decl_list tl;");
@ -167,7 +168,7 @@ void CPPCompile::ExpandRecordTypeVar(const TypePtr& t, std::string& tn)
AddInit(t, "}"); AddInit(t, "}");
} }
void CPPCompile::ExpandEnumTypeVar(const TypePtr& t, std::string& tn) void CPPCompile::ExpandEnumTypeVar(const TypePtr& t, string& tn)
{ {
auto e_name = tn + "->AsEnumType()"; auto e_name = tn + "->AsEnumType()";
auto et = t->AsEnumType(); auto et = t->AsEnumType();
@ -177,14 +178,14 @@ void CPPCompile::ExpandEnumTypeVar(const TypePtr& t, std::string& tn)
AddInit(t, "if ( et->Names().size() == 0 ) {"); AddInit(t, "if ( et->Names().size() == 0 ) {");
for ( const auto& name_pair : et->Names() ) for ( const auto& name_pair : et->Names() )
AddInit(t, std::string("\tet->AddNameInternal(\"") + AddInit(t, string("\tet->AddNameInternal(\"") +
name_pair.first + "\", " + name_pair.first + "\", " +
Fmt(int(name_pair.second)) + ");"); Fmt(int(name_pair.second)) + ");");
AddInit(t, "}}"); AddInit(t, "}}");
} }
void CPPCompile::ExpandTableTypeVar(const TypePtr& t, std::string& tn) void CPPCompile::ExpandTableTypeVar(const TypePtr& t, string& tn)
{ {
auto tbl = t->AsTableType(); auto tbl = t->AsTableType();
@ -193,23 +194,23 @@ void CPPCompile::ExpandTableTypeVar(const TypePtr& t, std::string& tn)
if ( tbl->IsSet() ) if ( tbl->IsSet() )
AddInit(t, tn, AddInit(t, tn,
std::string("make_intrusive<SetType>(cast_intrusive<TypeList>(") + string("make_intrusive<SetType>(cast_intrusive<TypeList>(") +
GenTypeName(indices) + " ), nullptr)"); GenTypeName(indices) + " ), nullptr)");
else else
AddInit(t, tn, AddInit(t, tn,
std::string("make_intrusive<TableType>(cast_intrusive<TypeList>(") + string("make_intrusive<TableType>(cast_intrusive<TypeList>(") +
GenTypeName(indices) + "), " + GenTypeName(indices) + "), " +
GenTypeName(yield) + ")"); GenTypeName(yield) + ")");
} }
void CPPCompile::ExpandFuncTypeVar(const TypePtr& t, std::string& tn) void CPPCompile::ExpandFuncTypeVar(const TypePtr& t, string& tn)
{ {
auto f = t->AsFuncType(); auto f = t->AsFuncType();
auto args_type_accessor = GenTypeName(f->Params()); auto args_type_accessor = GenTypeName(f->Params());
auto yt = f->Yield(); auto yt = f->Yield();
std::string yield_type_accessor; string yield_type_accessor;
if ( yt ) if ( yt )
yield_type_accessor += GenTypeName(yt); yield_type_accessor += GenTypeName(yt);
@ -218,7 +219,7 @@ void CPPCompile::ExpandFuncTypeVar(const TypePtr& t, std::string& tn)
auto fl = f->Flavor(); auto fl = f->Flavor();
std::string fl_name; string fl_name;
if ( fl == FUNC_FLAVOR_FUNCTION ) if ( fl == FUNC_FLAVOR_FUNCTION )
fl_name = "FUNC_FLAVOR_FUNCTION"; fl_name = "FUNC_FLAVOR_FUNCTION";
else if ( fl == FUNC_FLAVOR_EVENT ) else if ( fl == FUNC_FLAVOR_EVENT )
@ -226,29 +227,29 @@ void CPPCompile::ExpandFuncTypeVar(const TypePtr& t, std::string& tn)
else if ( fl == FUNC_FLAVOR_HOOK ) else if ( fl == FUNC_FLAVOR_HOOK )
fl_name = "FUNC_FLAVOR_HOOK"; fl_name = "FUNC_FLAVOR_HOOK";
auto type_init = std::string("make_intrusive<FuncType>(cast_intrusive<RecordType>(") + auto type_init = string("make_intrusive<FuncType>(cast_intrusive<RecordType>(") +
args_type_accessor + "), " + args_type_accessor + "), " +
yield_type_accessor + ", " + fl_name + ")"; yield_type_accessor + ", " + fl_name + ")";
AddInit(t, tn, type_init); AddInit(t, tn, type_init);
} }
std::string CPPCompile::GenTypeDecl(const TypeDecl* td) string CPPCompile::GenTypeDecl(const TypeDecl* td)
{ {
auto type_accessor = GenTypeName(td->type); auto type_accessor = GenTypeName(td->type);
auto td_name = std::string("util::copy_string(\"") + td->id + "\")"; auto td_name = string("util::copy_string(\"") + td->id + "\")";
if ( td->attrs ) if ( td->attrs )
return std::string("tl.append(new TypeDecl(") + return string("tl.append(new TypeDecl(") +
td_name + ", " + type_accessor + td_name + ", " + type_accessor +
", " + AttrsName(td->attrs) +"));"; ", " + AttrsName(td->attrs) +"));";
return std::string("tl.append(new TypeDecl(") + td_name + ", " + return string("tl.append(new TypeDecl(") + td_name + ", " +
type_accessor +"));"; type_accessor +"));";
} }
std::string CPPCompile::GenTypeName(const Type* t) string CPPCompile::GenTypeName(const Type* t)
{ {
return types.KeyName(TypeRep(t)); return types.KeyName(TypeRep(t));
} }

View file

@ -8,7 +8,9 @@
namespace zeek::detail { namespace zeek::detail {
std::string Fmt(double d) using namespace std;
string Fmt(double d)
{ {
// Special hack to preserve the signed-ness of the magic -0.0. // Special hack to preserve the signed-ness of the magic -0.0.
if ( d == 0.0 && signbit(d) ) if ( d == 0.0 && signbit(d) )
@ -21,14 +23,14 @@ std::string Fmt(double d)
return buf; return buf;
} }
std::string scope_prefix(const std::string& scope) string scope_prefix(const string& scope)
{ {
return std::string("zeek::detail::CPP_") + scope + "::"; return string("zeek::detail::CPP_") + scope + "::";
} }
std::string scope_prefix(int scope) string scope_prefix(int scope)
{ {
return scope_prefix(std::to_string(scope)); return scope_prefix(to_string(scope));
} }
bool is_CPP_compilable(const ProfileFunc* pf) bool is_CPP_compilable(const ProfileFunc* pf)
@ -42,7 +44,7 @@ bool is_CPP_compilable(const ProfileFunc* pf)
return true; return true;
} }
void lock_file(const std::string& fname, FILE* f) void lock_file(const string& fname, FILE* f)
{ {
if ( flock(fileno(f), LOCK_EX) < 0 ) if ( flock(fileno(f), LOCK_EX) < 0 )
{ {
@ -53,7 +55,7 @@ void lock_file(const std::string& fname, FILE* f)
} }
} }
void unlock_file(const std::string& fname, FILE* f) void unlock_file(const string& fname, FILE* f)
{ {
if ( flock(fileno(f), LOCK_UN) < 0 ) if ( flock(fileno(f), LOCK_UN) < 0 )
{ {

View file

@ -10,11 +10,13 @@
namespace zeek::detail { namespace zeek::detail {
using namespace std;
bool CPPCompile::CheckForCollisions() bool CPPCompile::CheckForCollisions()
{ {
for ( auto& g : pfs.AllGlobals() ) for ( auto& g : pfs.AllGlobals() )
{ {
auto gn = std::string(g->Name()); auto gn = string(g->Name());
if ( hm.HasGlobal(gn) ) if ( hm.HasGlobal(gn) )
{ {
@ -75,7 +77,7 @@ bool CPPCompile::CheckForCollisions()
void CPPCompile::CreateGlobal(const ID* g) void CPPCompile::CreateGlobal(const ID* g)
{ {
auto gn = std::string(g->Name()); auto gn = string(g->Name());
bool is_bif = pfs.BiFGlobals().count(g) > 0; bool is_bif = pfs.BiFGlobals().count(g) > 0;
if ( pfs.Globals().count(g) == 0 ) if ( pfs.Globals().count(g) == 0 )
@ -108,7 +110,7 @@ void CPPCompile::CreateGlobal(const ID* g)
NoteInitDependency(g, TypeRep(t)); NoteInitDependency(g, TypeRep(t));
AddInit(g, globals[gn], AddInit(g, globals[gn],
std::string("lookup_global__CPP(\"") + gn + "\", " + string("lookup_global__CPP(\"") + gn + "\", " +
GenTypeName(t) + ")"); GenTypeName(t) + ")");
} }
@ -159,17 +161,17 @@ void CPPCompile::UpdateGlobalHashes()
void CPPCompile::AddBiF(const ID* b, bool is_var) void CPPCompile::AddBiF(const ID* b, bool is_var)
{ {
auto bn = b->Name(); auto bn = b->Name();
auto n = std::string(bn); auto n = string(bn);
if ( is_var ) if ( is_var )
n = n + "_"; // make the name distinct n = n + "_"; // make the name distinct
if ( AddGlobal(n, "bif", true) ) if ( AddGlobal(n, "bif", true) )
Emit("Func* %s;", globals[n]); Emit("Func* %s;", globals[n]);
AddInit(b, globals[n], std::string("lookup_bif__CPP(\"") + bn + "\")"); AddInit(b, globals[n], string("lookup_bif__CPP(\"") + bn + "\")");
} }
bool CPPCompile::AddGlobal(const std::string& g, const char* suffix, bool track) bool CPPCompile::AddGlobal(const string& g, const char* suffix, bool track)
{ {
bool new_var = false; bool new_var = false;
@ -194,16 +196,16 @@ bool CPPCompile::AddGlobal(const std::string& g, const char* suffix, bool track)
return new_var; return new_var;
} }
void CPPCompile::RegisterEvent(std::string ev_name) void CPPCompile::RegisterEvent(string ev_name)
{ {
body_events[body_name].emplace_back(std::move(ev_name)); body_events[body_name].emplace_back(move(ev_name));
} }
const std::string& CPPCompile::IDNameStr(const ID* id) const const string& CPPCompile::IDNameStr(const ID* id) const
{ {
if ( id->IsGlobal() ) if ( id->IsGlobal() )
{ {
auto g = std::string(id->Name()); auto g = string(id->Name());
ASSERT(globals.count(g) > 0); ASSERT(globals.count(g) > 0);
return ((CPPCompile*)(this))->globals[g]; return ((CPPCompile*)(this))->globals[g];
} }
@ -213,7 +215,7 @@ const std::string& CPPCompile::IDNameStr(const ID* id) const
return ((CPPCompile*)(this))->locals[id]; return ((CPPCompile*)(this))->locals[id];
} }
std::string CPPCompile::LocalName(const ID* l) const string CPPCompile::LocalName(const ID* l) const
{ {
auto n = l->Name(); auto n = l->Name();
auto without_module = strstr(n, "::"); auto without_module = strstr(n, "::");
@ -224,9 +226,9 @@ std::string CPPCompile::LocalName(const ID* l) const
return Canonicalize(n); return Canonicalize(n);
} }
std::string CPPCompile::Canonicalize(const char* name) const string CPPCompile::Canonicalize(const char* name) const
{ {
std::string cname; string cname;
for ( int i = 0; name[i]; ++i ) for ( int i = 0; name[i]; ++i )
{ {