mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 22:58:20 +00:00
Reformat Zeek in Spicy style
This largely copies over Spicy's `.clang-format` configuration file. The one place where we deviate is header include order since Zeek depends on headers being included in a certain order.
This commit is contained in:
parent
7b8e7ed72c
commit
f5a76c1aed
786 changed files with 131714 additions and 153609 deletions
|
@ -71,8 +71,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
namespace zeek::detail
|
||||
{
|
||||
namespace zeek::detail {
|
||||
|
||||
class CPPCompile;
|
||||
|
||||
|
@ -81,595 +80,535 @@ class CPP_InitInfo;
|
|||
|
||||
// Abstract class for tracking information about a collection of initialization
|
||||
// items.
|
||||
class CPP_InitsInfo
|
||||
{
|
||||
class CPP_InitsInfo {
|
||||
public:
|
||||
CPP_InitsInfo(std::string _tag, std::string type) : tag(std::move(_tag))
|
||||
{
|
||||
base_name = std::string("CPP__") + tag + "__";
|
||||
CPP_type = tag + type;
|
||||
}
|
||||
CPP_InitsInfo(std::string _tag, std::string type) : tag(std::move(_tag)) {
|
||||
base_name = std::string("CPP__") + tag + "__";
|
||||
CPP_type = tag + type;
|
||||
}
|
||||
|
||||
virtual ~CPP_InitsInfo() { }
|
||||
virtual ~CPP_InitsInfo() {}
|
||||
|
||||
// Returns the name of the C++ global that will hold the items' values
|
||||
// at run-time, once initialized. These are all vectors, for which
|
||||
// the generated code accesses a particular item by indexing the vector.
|
||||
const std::string& InitsName() const { return base_name; }
|
||||
// Returns the name of the C++ global that will hold the items' values
|
||||
// at run-time, once initialized. These are all vectors, for which
|
||||
// the generated code accesses a particular item by indexing the vector.
|
||||
const std::string& InitsName() const { return base_name; }
|
||||
|
||||
// Returns the name of the C++ global used to hold the table we employ
|
||||
// for table-driven initialization.
|
||||
std::string InitializersName() const { return base_name + "init"; }
|
||||
// Returns the name of the C++ global used to hold the table we employ
|
||||
// for table-driven initialization.
|
||||
std::string InitializersName() const { return base_name + "init"; }
|
||||
|
||||
// Returns the "name" of the given element in the run-time vector
|
||||
// associated with this collection of initialization items. It's not
|
||||
// really a name but rather a vector index, so for example Name(12)
|
||||
// might return "CPP__Pattern__[12]", but we use the term Name because
|
||||
// the representation used to be individualized globals, such as
|
||||
// "CPP__Pattern__12".
|
||||
std::string Name(int index) const;
|
||||
// Returns the "name" of the given element in the run-time vector
|
||||
// associated with this collection of initialization items. It's not
|
||||
// really a name but rather a vector index, so for example Name(12)
|
||||
// might return "CPP__Pattern__[12]", but we use the term Name because
|
||||
// the representation used to be individualized globals, such as
|
||||
// "CPP__Pattern__12".
|
||||
std::string Name(int index) const;
|
||||
|
||||
// Returns the name that will correspond to the next item added to
|
||||
// this set.
|
||||
std::string NextName() const { return Name(size); }
|
||||
// Returns the name that will correspond to the next item added to
|
||||
// this set.
|
||||
std::string NextName() const { return Name(size); }
|
||||
|
||||
// The largest initialization cohort of any item in this collection.
|
||||
int MaxCohort() const { return static_cast<int>(instances.size()) - 1; }
|
||||
// The largest initialization cohort of any item in this collection.
|
||||
int MaxCohort() const { return static_cast<int>(instances.size()) - 1; }
|
||||
|
||||
// Returns the number of initializations in this collection that belong
|
||||
// to the given cohort c.
|
||||
int CohortSize(int c) const { return c > MaxCohort() ? 0 : instances[c].size(); }
|
||||
// Returns the number of initializations in this collection that belong
|
||||
// to the given cohort c.
|
||||
int CohortSize(int c) const { return c > MaxCohort() ? 0 : instances[c].size(); }
|
||||
|
||||
// Returns the C++ type associated with this collection's run-time vector.
|
||||
// This might be, for example, "PatternVal"
|
||||
const std::string& CPPType() const { return CPP_type; }
|
||||
// Returns the C++ type associated with this collection's run-time vector.
|
||||
// This might be, for example, "PatternVal"
|
||||
const std::string& CPPType() const { return CPP_type; }
|
||||
|
||||
// Sets the associated C++ type.
|
||||
virtual void SetCPPType(std::string ct) { CPP_type = std::move(ct); }
|
||||
// Sets the associated C++ type.
|
||||
virtual void SetCPPType(std::string ct) { CPP_type = std::move(ct); }
|
||||
|
||||
// Returns the type associated with the table used for initialization
|
||||
// (i.e., this is the type of the global returned by InitializersName()).
|
||||
std::string InitsType() const { return inits_type; }
|
||||
// Returns the type associated with the table used for initialization
|
||||
// (i.e., this is the type of the global returned by InitializersName()).
|
||||
std::string InitsType() const { return inits_type; }
|
||||
|
||||
// Add a new initialization instance to the collection.
|
||||
void AddInstance(std::shared_ptr<CPP_InitInfo> g);
|
||||
// Add a new initialization instance to the collection.
|
||||
void AddInstance(std::shared_ptr<CPP_InitInfo> g);
|
||||
|
||||
// Emit code to populate the table used to initialize this collection.
|
||||
void GenerateInitializers(CPPCompile* c);
|
||||
// Emit code to populate the table used to initialize this collection.
|
||||
void GenerateInitializers(CPPCompile* c);
|
||||
|
||||
protected:
|
||||
// Computes offset_set - see below.
|
||||
void BuildOffsetSet(CPPCompile* c);
|
||||
// Computes offset_set - see below.
|
||||
void BuildOffsetSet(CPPCompile* c);
|
||||
|
||||
// Returns a declaration suitable for the run-time vector that holds
|
||||
// the initialized items in the collection.
|
||||
std::string Declare() const;
|
||||
// Returns a declaration suitable for the run-time vector that holds
|
||||
// the initialized items in the collection.
|
||||
std::string Declare() const;
|
||||
|
||||
// For a given cohort, generates the associated table elements for
|
||||
// creating it.
|
||||
void BuildCohort(CPPCompile* c, std::vector<std::shared_ptr<CPP_InitInfo>>& cohort);
|
||||
// For a given cohort, generates the associated table elements for
|
||||
// creating it.
|
||||
void BuildCohort(CPPCompile* c, std::vector<std::shared_ptr<CPP_InitInfo>>& cohort);
|
||||
|
||||
// Given the initialization type and initializers for with a given
|
||||
// cohort element, build the associated table element.
|
||||
virtual void BuildCohortElement(CPPCompile* c, std::string init_type,
|
||||
std::vector<std::string>& ivs);
|
||||
// Given the initialization type and initializers for with a given
|
||||
// cohort element, build the associated table element.
|
||||
virtual void BuildCohortElement(CPPCompile* c, std::string init_type, std::vector<std::string>& ivs);
|
||||
|
||||
// Total number of initializers.
|
||||
int size = 0;
|
||||
// Total number of initializers.
|
||||
int size = 0;
|
||||
|
||||
// Each cohort is represented by a vector whose elements correspond
|
||||
// to the initialization information for a single item. This variable
|
||||
// holds a vector of cohorts, indexed by the number of the cohort.
|
||||
// (Note, some cohorts may be empty.)
|
||||
std::vector<std::vector<std::shared_ptr<CPP_InitInfo>>> instances;
|
||||
// Each cohort is represented by a vector whose elements correspond
|
||||
// to the initialization information for a single item. This variable
|
||||
// holds a vector of cohorts, indexed by the number of the cohort.
|
||||
// (Note, some cohorts may be empty.)
|
||||
std::vector<std::vector<std::shared_ptr<CPP_InitInfo>>> instances;
|
||||
|
||||
// Each cohort has associated with it a vector of offsets, specifying
|
||||
// positions in the run-time vector of the items in the cohort.
|
||||
//
|
||||
// We reduce each such vector to an index into the collection of
|
||||
// such vectors (as managed by an IndicesManager - see below).
|
||||
//
|
||||
// Once we've done that reduction, we can represent each cohort
|
||||
// using a single index, and thus all of the cohorts using a vector
|
||||
// of indices. We then reduce *that* vector to a single index,
|
||||
// again using the IndicesManager. We store that single index
|
||||
// in the "offset_set" variable.
|
||||
int offset_set = 0;
|
||||
// Each cohort has associated with it a vector of offsets, specifying
|
||||
// positions in the run-time vector of the items in the cohort.
|
||||
//
|
||||
// We reduce each such vector to an index into the collection of
|
||||
// such vectors (as managed by an IndicesManager - see below).
|
||||
//
|
||||
// Once we've done that reduction, we can represent each cohort
|
||||
// using a single index, and thus all of the cohorts using a vector
|
||||
// of indices. We then reduce *that* vector to a single index,
|
||||
// again using the IndicesManager. We store that single index
|
||||
// in the "offset_set" variable.
|
||||
int offset_set = 0;
|
||||
|
||||
// Tag used to distinguish a particular collection of constants.
|
||||
std::string tag;
|
||||
// Tag used to distinguish a particular collection of constants.
|
||||
std::string tag;
|
||||
|
||||
// C++ name for this collection of constants.
|
||||
std::string base_name;
|
||||
// C++ name for this collection of constants.
|
||||
std::string base_name;
|
||||
|
||||
// C++ type associated with a single instance of these constants.
|
||||
std::string CPP_type;
|
||||
// C++ type associated with a single instance of these constants.
|
||||
std::string CPP_type;
|
||||
|
||||
// C++ type associated with the collection of initializers.
|
||||
std::string inits_type;
|
||||
};
|
||||
// C++ type associated with the collection of initializers.
|
||||
std::string inits_type;
|
||||
};
|
||||
|
||||
// A class for a collection of initialization items for which each item
|
||||
// has a "custom" initializer (that is, a bespoke C++ object, rather than
|
||||
// a simple C++ type or a vector of indices).
|
||||
class CPP_CustomInitsInfo : public CPP_InitsInfo
|
||||
{
|
||||
class CPP_CustomInitsInfo : public CPP_InitsInfo {
|
||||
public:
|
||||
CPP_CustomInitsInfo(std::string _tag, std::string _type)
|
||||
: CPP_InitsInfo(std::move(_tag), std::move(_type))
|
||||
{
|
||||
BuildInitType();
|
||||
}
|
||||
CPP_CustomInitsInfo(std::string _tag, std::string _type) : CPP_InitsInfo(std::move(_tag), std::move(_type)) {
|
||||
BuildInitType();
|
||||
}
|
||||
|
||||
void SetCPPType(std::string ct) override
|
||||
{
|
||||
CPP_InitsInfo::SetCPPType(std::move(ct));
|
||||
BuildInitType();
|
||||
}
|
||||
void SetCPPType(std::string ct) override {
|
||||
CPP_InitsInfo::SetCPPType(std::move(ct));
|
||||
BuildInitType();
|
||||
}
|
||||
|
||||
private:
|
||||
void BuildInitType() { inits_type = std::string("CPP_CustomInits<") + CPPType() + ">"; }
|
||||
};
|
||||
void BuildInitType() { inits_type = std::string("CPP_CustomInits<") + CPPType() + ">"; }
|
||||
};
|
||||
|
||||
// A class for a collection of initialization items corresponding to "basic"
|
||||
// constants, i.e., those that can be represented either directly as C++
|
||||
// constants, or as indices into a vector of C++ objects.
|
||||
class CPP_BasicConstInitsInfo : public CPP_CustomInitsInfo
|
||||
{
|
||||
class CPP_BasicConstInitsInfo : public CPP_CustomInitsInfo {
|
||||
public:
|
||||
// In the following, if "c_type" is non-empty then it specifies the
|
||||
// C++ type used to directly represent the constant. If empty, it
|
||||
// indicates that we instead use an index into a separate vector.
|
||||
CPP_BasicConstInitsInfo(std::string _tag, std::string type, std::string c_type)
|
||||
: CPP_CustomInitsInfo(std::move(_tag), std::move(type))
|
||||
{
|
||||
if ( c_type.empty() )
|
||||
inits_type = std::string("CPP_") + tag + "Consts";
|
||||
else
|
||||
inits_type = std::string("CPP_BasicConsts<") + CPP_type + ", " + c_type + ", " + tag +
|
||||
"Val>";
|
||||
}
|
||||
// In the following, if "c_type" is non-empty then it specifies the
|
||||
// C++ type used to directly represent the constant. If empty, it
|
||||
// indicates that we instead use an index into a separate vector.
|
||||
CPP_BasicConstInitsInfo(std::string _tag, std::string type, std::string c_type)
|
||||
: CPP_CustomInitsInfo(std::move(_tag), std::move(type)) {
|
||||
if ( c_type.empty() )
|
||||
inits_type = std::string("CPP_") + tag + "Consts";
|
||||
else
|
||||
inits_type = std::string("CPP_BasicConsts<") + CPP_type + ", " + c_type + ", " + tag + "Val>";
|
||||
}
|
||||
|
||||
void BuildCohortElement(CPPCompile* c, std::string init_type,
|
||||
std::vector<std::string>& ivs) override;
|
||||
};
|
||||
void BuildCohortElement(CPPCompile* c, std::string init_type, std::vector<std::string>& ivs) override;
|
||||
};
|
||||
|
||||
// A class for a collection of initialization items that are defined using
|
||||
// other initialization items.
|
||||
class CPP_CompoundInitsInfo : public CPP_InitsInfo
|
||||
{
|
||||
class CPP_CompoundInitsInfo : public CPP_InitsInfo {
|
||||
public:
|
||||
CPP_CompoundInitsInfo(std::string _tag, std::string type)
|
||||
: CPP_InitsInfo(std::move(_tag), std::move(type))
|
||||
{
|
||||
if ( tag == "Type" )
|
||||
// These need a refined version of CPP_IndexedInits
|
||||
// in order to build different types dynamically.
|
||||
inits_type = "CPP_TypeInits";
|
||||
else
|
||||
inits_type = std::string("CPP_IndexedInits<") + CPPType() + ">";
|
||||
}
|
||||
CPP_CompoundInitsInfo(std::string _tag, std::string type) : CPP_InitsInfo(std::move(_tag), std::move(type)) {
|
||||
if ( tag == "Type" )
|
||||
// These need a refined version of CPP_IndexedInits
|
||||
// in order to build different types dynamically.
|
||||
inits_type = "CPP_TypeInits";
|
||||
else
|
||||
inits_type = std::string("CPP_IndexedInits<") + CPPType() + ">";
|
||||
}
|
||||
|
||||
void BuildCohortElement(CPPCompile* c, std::string init_type,
|
||||
std::vector<std::string>& ivs) override;
|
||||
};
|
||||
void BuildCohortElement(CPPCompile* c, std::string init_type, std::vector<std::string>& ivs) override;
|
||||
};
|
||||
|
||||
// Abstract class for tracking information about a single initialization item.
|
||||
class CPP_InitInfo
|
||||
{
|
||||
class CPP_InitInfo {
|
||||
public:
|
||||
CPP_InitInfo(const IntrusivePtr<Obj>& _o) : o(_o.get()) { }
|
||||
CPP_InitInfo(const Obj* _o) : o(_o) { }
|
||||
CPP_InitInfo(const IntrusivePtr<Obj>& _o) : o(_o.get()) {}
|
||||
CPP_InitInfo(const Obj* _o) : o(_o) {}
|
||||
|
||||
virtual ~CPP_InitInfo() { }
|
||||
virtual ~CPP_InitInfo() {}
|
||||
|
||||
// Associates this item with an initialization collection and run-time
|
||||
// vector offset.
|
||||
void SetOffset(const CPP_InitsInfo* _inits_collection, int _offset)
|
||||
{
|
||||
inits_collection = _inits_collection;
|
||||
offset = _offset;
|
||||
}
|
||||
// Associates this item with an initialization collection and run-time
|
||||
// vector offset.
|
||||
void SetOffset(const CPP_InitsInfo* _inits_collection, int _offset) {
|
||||
inits_collection = _inits_collection;
|
||||
offset = _offset;
|
||||
}
|
||||
|
||||
// Returns the offset for this item into the associated run-time vector.
|
||||
int Offset() const { return offset; }
|
||||
// Returns the offset for this item into the associated run-time vector.
|
||||
int Offset() const { return offset; }
|
||||
|
||||
// Returns the name that should be used for referring to this
|
||||
// value in the generated code.
|
||||
std::string Name() const { return inits_collection->Name(offset); }
|
||||
// Returns the name that should be used for referring to this
|
||||
// value in the generated code.
|
||||
std::string Name() const { return inits_collection->Name(offset); }
|
||||
|
||||
// Returns this item's initialization cohort.
|
||||
int InitCohort() const { return init_cohort; }
|
||||
// Returns this item's initialization cohort.
|
||||
int InitCohort() const { return init_cohort; }
|
||||
|
||||
// Returns this item's "final" initialization cohort. See
|
||||
// discussion below.
|
||||
int FinalInitCohort() const { return final_init_cohort ? final_init_cohort : init_cohort; }
|
||||
// Returns this item's "final" initialization cohort. See
|
||||
// discussion below.
|
||||
int FinalInitCohort() const { return final_init_cohort ? final_init_cohort : init_cohort; }
|
||||
|
||||
// Returns the type used for this initializer.
|
||||
virtual std::string InitializerType() const { return "<shouldn't-be-used>"; }
|
||||
// Returns the type used for this initializer.
|
||||
virtual std::string InitializerType() const { return "<shouldn't-be-used>"; }
|
||||
|
||||
// Returns values used for creating this value, one element per
|
||||
// constructor parameter.
|
||||
virtual void InitializerVals(std::vector<std::string>& ivs) const = 0;
|
||||
// Returns values used for creating this value, one element per
|
||||
// constructor parameter.
|
||||
virtual void InitializerVals(std::vector<std::string>& ivs) const = 0;
|
||||
|
||||
const Obj* InitObj() const { return o; }
|
||||
const Obj* InitObj() const { return o; }
|
||||
|
||||
protected:
|
||||
// Returns an offset (into the run-time vector holding all Zeek
|
||||
// constant values) corresponding to the given value. Registers
|
||||
// the constant if needed.
|
||||
std::string ValElem(CPPCompile* c, ValPtr v);
|
||||
// Returns an offset (into the run-time vector holding all Zeek
|
||||
// constant values) corresponding to the given value. Registers
|
||||
// the constant if needed.
|
||||
std::string ValElem(CPPCompile* c, ValPtr v);
|
||||
|
||||
// By default, values have no dependencies on other values
|
||||
// being first initialized. Those that do must increase this
|
||||
// value in their constructors.
|
||||
int init_cohort = 0;
|
||||
// By default, values have no dependencies on other values
|
||||
// being first initialized. Those that do must increase this
|
||||
// value in their constructors.
|
||||
int init_cohort = 0;
|
||||
|
||||
// Some initializers (record and list types, in particular) become
|
||||
// available for other initializers to use them after the first
|
||||
// cohort is initialized; however, the final initialization comes
|
||||
// later. If non-zero, this variable tracks the latter.
|
||||
int final_init_cohort = 0;
|
||||
// Some initializers (record and list types, in particular) become
|
||||
// available for other initializers to use them after the first
|
||||
// cohort is initialized; however, the final initialization comes
|
||||
// later. If non-zero, this variable tracks the latter.
|
||||
int final_init_cohort = 0;
|
||||
|
||||
// Tracks the collection to which this item belongs.
|
||||
const CPP_InitsInfo* inits_collection = nullptr;
|
||||
// Tracks the collection to which this item belongs.
|
||||
const CPP_InitsInfo* inits_collection = nullptr;
|
||||
|
||||
// Offset of this item in the collection, or -1 if no association.
|
||||
int offset = -1;
|
||||
// Offset of this item in the collection, or -1 if no association.
|
||||
int offset = -1;
|
||||
|
||||
// Associated object. Used for annotating output.
|
||||
const Obj* o;
|
||||
};
|
||||
// Associated object. Used for annotating output.
|
||||
const Obj* o;
|
||||
};
|
||||
|
||||
// Information associated with initializing a basic (non-compound) constant.
|
||||
class BasicConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class BasicConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
BasicConstInfo(std::string _val) : CPP_InitInfo(nullptr), val(std::move(_val)) { }
|
||||
BasicConstInfo(std::string _val) : CPP_InitInfo(nullptr), val(std::move(_val)) {}
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(val); }
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(val); }
|
||||
|
||||
private:
|
||||
// All we need to track is the C++ representation of the constant.
|
||||
std::string val;
|
||||
};
|
||||
// All we need to track is the C++ representation of the constant.
|
||||
std::string val;
|
||||
};
|
||||
|
||||
// Information associated with initializing a constant whose Val constructor
|
||||
// takes a string.
|
||||
class DescConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class DescConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
DescConstInfo(CPPCompile* c, ValPtr v);
|
||||
DescConstInfo(CPPCompile* c, ValPtr v);
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(init); }
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(init); }
|
||||
|
||||
private:
|
||||
std::string init;
|
||||
};
|
||||
std::string init;
|
||||
};
|
||||
|
||||
class EnumConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class EnumConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
EnumConstInfo(CPPCompile* c, ValPtr v);
|
||||
EnumConstInfo(CPPCompile* c, ValPtr v);
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
ivs.emplace_back(std::to_string(e_type));
|
||||
ivs.emplace_back(std::to_string(e_val));
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override {
|
||||
ivs.emplace_back(std::to_string(e_type));
|
||||
ivs.emplace_back(std::to_string(e_val));
|
||||
}
|
||||
|
||||
private:
|
||||
int e_type; // an index into the enum's Zeek type
|
||||
int e_val; // integer value of the enum
|
||||
};
|
||||
int e_type; // an index into the enum's Zeek type
|
||||
int e_val; // integer value of the enum
|
||||
};
|
||||
|
||||
class StringConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class StringConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
StringConstInfo(CPPCompile* c, ValPtr v);
|
||||
StringConstInfo(CPPCompile* c, ValPtr v);
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
ivs.emplace_back(std::to_string(chars));
|
||||
ivs.emplace_back(std::to_string(len));
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override {
|
||||
ivs.emplace_back(std::to_string(chars));
|
||||
ivs.emplace_back(std::to_string(len));
|
||||
}
|
||||
|
||||
private:
|
||||
int chars; // index into vector of char*'s
|
||||
int len; // length of the string
|
||||
};
|
||||
int chars; // index into vector of char*'s
|
||||
int len; // length of the string
|
||||
};
|
||||
|
||||
class PatternConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class PatternConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
PatternConstInfo(CPPCompile* c, ValPtr v);
|
||||
PatternConstInfo(CPPCompile* c, ValPtr v);
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
ivs.emplace_back(std::to_string(pattern));
|
||||
ivs.emplace_back(std::to_string(is_case_insensitive));
|
||||
ivs.emplace_back(std::to_string(is_single_line));
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override {
|
||||
ivs.emplace_back(std::to_string(pattern));
|
||||
ivs.emplace_back(std::to_string(is_case_insensitive));
|
||||
ivs.emplace_back(std::to_string(is_single_line));
|
||||
}
|
||||
|
||||
private:
|
||||
int pattern; // index into string representation of pattern
|
||||
int is_case_insensitive; // case-insensitivity flag, 0 or 1
|
||||
int is_single_line; // single-line flag, 0 or 1
|
||||
};
|
||||
int pattern; // index into string representation of pattern
|
||||
int is_case_insensitive; // case-insensitivity flag, 0 or 1
|
||||
int is_single_line; // single-line flag, 0 or 1
|
||||
};
|
||||
|
||||
class PortConstInfo : public CPP_InitInfo
|
||||
{
|
||||
class PortConstInfo : public CPP_InitInfo {
|
||||
public:
|
||||
PortConstInfo(ValPtr v)
|
||||
: CPP_InitInfo(v), p(static_cast<UnsignedValImplementation*>(v->AsPortVal())->Get())
|
||||
{
|
||||
}
|
||||
PortConstInfo(ValPtr v) : CPP_InitInfo(v), p(static_cast<UnsignedValImplementation*>(v->AsPortVal())->Get()) {}
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
ivs.emplace_back(std::to_string(p) + "U");
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(std::to_string(p) + "U"); }
|
||||
|
||||
private:
|
||||
zeek_uint_t p;
|
||||
};
|
||||
zeek_uint_t p;
|
||||
};
|
||||
|
||||
// Abstract class for compound items (those defined in terms of other items).
|
||||
class CompoundItemInfo : public CPP_InitInfo
|
||||
{
|
||||
class CompoundItemInfo : public CPP_InitInfo {
|
||||
public:
|
||||
// The first of these is used for items with custom Zeek types,
|
||||
// the second when the type is generic/inapplicable.
|
||||
CompoundItemInfo(CPPCompile* c, ValPtr v);
|
||||
CompoundItemInfo(CPPCompile* _c) : CPP_InitInfo(nullptr), c(_c) { type = -1; }
|
||||
// The first of these is used for items with custom Zeek types,
|
||||
// the second when the type is generic/inapplicable.
|
||||
CompoundItemInfo(CPPCompile* c, ValPtr v);
|
||||
CompoundItemInfo(CPPCompile* _c) : CPP_InitInfo(nullptr), c(_c) { type = -1; }
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
if ( type >= 0 )
|
||||
ivs.emplace_back(std::to_string(type));
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override {
|
||||
if ( type >= 0 )
|
||||
ivs.emplace_back(std::to_string(type));
|
||||
|
||||
for ( auto& v : vals )
|
||||
ivs.push_back(v);
|
||||
}
|
||||
for ( auto& v : vals )
|
||||
ivs.push_back(v);
|
||||
}
|
||||
|
||||
protected:
|
||||
CPPCompile* c;
|
||||
int type;
|
||||
std::vector<std::string> vals; // initialization values
|
||||
};
|
||||
CPPCompile* c;
|
||||
int type;
|
||||
std::vector<std::string> vals; // initialization values
|
||||
};
|
||||
|
||||
// This next set corresponds to compound Zeek constants of various types.
|
||||
class ListConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class ListConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
ListConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
ListConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
|
||||
class VectorConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class VectorConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
VectorConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
VectorConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
|
||||
class RecordConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class RecordConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
RecordConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
RecordConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
|
||||
class TableConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class TableConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
TableConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
TableConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
|
||||
class FileConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class FileConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
FileConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
FileConstInfo(CPPCompile* c, ValPtr v);
|
||||
};
|
||||
|
||||
class FuncConstInfo : public CompoundItemInfo
|
||||
{
|
||||
class FuncConstInfo : public CompoundItemInfo {
|
||||
public:
|
||||
FuncConstInfo(CPPCompile* _c, ValPtr v);
|
||||
FuncConstInfo(CPPCompile* _c, ValPtr v);
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
FuncVal* fv;
|
||||
};
|
||||
FuncVal* fv;
|
||||
};
|
||||
|
||||
// Initialization information for single attributes and sets of attributes.
|
||||
class AttrInfo : public CompoundItemInfo
|
||||
{
|
||||
class AttrInfo : public CompoundItemInfo {
|
||||
public:
|
||||
AttrInfo(CPPCompile* c, const AttrPtr& attr);
|
||||
};
|
||||
AttrInfo(CPPCompile* c, const AttrPtr& attr);
|
||||
};
|
||||
|
||||
class AttrsInfo : public CompoundItemInfo
|
||||
{
|
||||
class AttrsInfo : public CompoundItemInfo {
|
||||
public:
|
||||
AttrsInfo(CPPCompile* c, const AttributesPtr& attrs);
|
||||
};
|
||||
AttrsInfo(CPPCompile* c, const AttributesPtr& attrs);
|
||||
};
|
||||
|
||||
// Information for initialization a Zeek global.
|
||||
class GlobalInitInfo : public CPP_InitInfo
|
||||
{
|
||||
class GlobalInitInfo : public CPP_InitInfo {
|
||||
public:
|
||||
GlobalInitInfo(CPPCompile* c, const ID* g, std::string CPP_name);
|
||||
GlobalInitInfo(CPPCompile* c, const ID* g, std::string CPP_name);
|
||||
|
||||
std::string InitializerType() const override { return "CPP_GlobalInit"; }
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
std::string InitializerType() const override { return "CPP_GlobalInit"; }
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
protected:
|
||||
std::string Zeek_name;
|
||||
std::string CPP_name;
|
||||
int type;
|
||||
int attrs;
|
||||
std::string val;
|
||||
bool exported;
|
||||
bool func_with_no_val = false; // needed to handle some error situations
|
||||
};
|
||||
std::string Zeek_name;
|
||||
std::string CPP_name;
|
||||
int type;
|
||||
int attrs;
|
||||
std::string val;
|
||||
bool exported;
|
||||
bool func_with_no_val = false; // needed to handle some error situations
|
||||
};
|
||||
|
||||
// Information for initializing an item corresponding to a Zeek function
|
||||
// call, needed to associate complex expressions with attributes.
|
||||
class CallExprInitInfo : public CPP_InitInfo
|
||||
{
|
||||
class CallExprInitInfo : public CPP_InitInfo {
|
||||
public:
|
||||
CallExprInitInfo(CPPCompile* c, ExprPtr e, std::string e_name, std::string wrapper_class);
|
||||
CallExprInitInfo(CPPCompile* c, ExprPtr e, std::string e_name, std::string wrapper_class);
|
||||
|
||||
std::string InitializerType() const override
|
||||
{
|
||||
return std::string("CPP_CallExprInit<") + wrapper_class + ">";
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(e_name); }
|
||||
std::string InitializerType() const override { return std::string("CPP_CallExprInit<") + wrapper_class + ">"; }
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override { ivs.emplace_back(e_name); }
|
||||
|
||||
// Accessors, since code to initialize these is generated separately
|
||||
// from that of most initialization collections.
|
||||
const ExprPtr& GetExpr() const { return e; }
|
||||
const std::string& Name() const { return e_name; }
|
||||
const std::string& WrapperClass() const { return wrapper_class; }
|
||||
// Accessors, since code to initialize these is generated separately
|
||||
// from that of most initialization collections.
|
||||
const ExprPtr& GetExpr() const { return e; }
|
||||
const std::string& Name() const { return e_name; }
|
||||
const std::string& WrapperClass() const { return wrapper_class; }
|
||||
|
||||
protected:
|
||||
ExprPtr e;
|
||||
std::string e_name;
|
||||
std::string wrapper_class;
|
||||
};
|
||||
ExprPtr e;
|
||||
std::string e_name;
|
||||
std::string wrapper_class;
|
||||
};
|
||||
|
||||
// Information for registering the class/function associated with a lambda.
|
||||
class LambdaRegistrationInfo : public CPP_InitInfo
|
||||
{
|
||||
class LambdaRegistrationInfo : public CPP_InitInfo {
|
||||
public:
|
||||
LambdaRegistrationInfo(CPPCompile* c, std::string name, FuncTypePtr ft,
|
||||
std::string wrapper_class, p_hash_type h, bool has_captures);
|
||||
LambdaRegistrationInfo(CPPCompile* c, std::string name, FuncTypePtr ft, std::string wrapper_class, p_hash_type h,
|
||||
bool has_captures);
|
||||
|
||||
std::string InitializerType() const override
|
||||
{
|
||||
return std::string("CPP_LambdaRegistration<") + wrapper_class + ">";
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
std::string InitializerType() const override {
|
||||
return std::string("CPP_LambdaRegistration<") + wrapper_class + ">";
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
protected:
|
||||
std::string name;
|
||||
int func_type;
|
||||
std::string wrapper_class;
|
||||
p_hash_type h;
|
||||
bool has_captures;
|
||||
};
|
||||
std::string name;
|
||||
int func_type;
|
||||
std::string wrapper_class;
|
||||
p_hash_type h;
|
||||
bool has_captures;
|
||||
};
|
||||
|
||||
// Abstract class for representing information for initializing a Zeek type.
|
||||
class AbstractTypeInfo : public CPP_InitInfo
|
||||
{
|
||||
class AbstractTypeInfo : public CPP_InitInfo {
|
||||
public:
|
||||
AbstractTypeInfo(CPPCompile* _c, TypePtr _t) : CPP_InitInfo(_t), c(_c), t(std::move(_t)) { }
|
||||
AbstractTypeInfo(CPPCompile* _c, TypePtr _t) : CPP_InitInfo(_t), c(_c), t(std::move(_t)) {}
|
||||
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override
|
||||
{
|
||||
ivs.emplace_back(std::to_string(static_cast<int>(t->Tag())));
|
||||
AddInitializerVals(ivs);
|
||||
}
|
||||
void InitializerVals(std::vector<std::string>& ivs) const override {
|
||||
ivs.emplace_back(std::to_string(static_cast<int>(t->Tag())));
|
||||
AddInitializerVals(ivs);
|
||||
}
|
||||
|
||||
virtual void AddInitializerVals(std::vector<std::string>& ivs) const { }
|
||||
virtual void AddInitializerVals(std::vector<std::string>& ivs) const {}
|
||||
|
||||
protected:
|
||||
CPPCompile* c;
|
||||
TypePtr t; // the type we're initializing
|
||||
};
|
||||
CPPCompile* c;
|
||||
TypePtr t; // the type we're initializing
|
||||
};
|
||||
|
||||
// The following capture information for different Zeek types.
|
||||
class BaseTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class BaseTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
BaseTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) { }
|
||||
};
|
||||
BaseTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) {}
|
||||
};
|
||||
|
||||
class EnumTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class EnumTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
EnumTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) { }
|
||||
EnumTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) {}
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
};
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
};
|
||||
|
||||
class OpaqueTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class OpaqueTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
OpaqueTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) { }
|
||||
OpaqueTypeInfo(CPPCompile* _c, TypePtr _t) : AbstractTypeInfo(_c, std::move(_t)) {}
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
};
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
};
|
||||
|
||||
class TypeTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class TypeTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
TypeTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
TypeTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
TypePtr tt; // the type referred to by t
|
||||
};
|
||||
TypePtr tt; // the type referred to by t
|
||||
};
|
||||
|
||||
class VectorTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class VectorTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
VectorTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
VectorTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
TypePtr yield;
|
||||
};
|
||||
TypePtr yield;
|
||||
};
|
||||
|
||||
class ListTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class ListTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
ListTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
ListTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
const std::vector<TypePtr>& types;
|
||||
};
|
||||
const std::vector<TypePtr>& types;
|
||||
};
|
||||
|
||||
class TableTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class TableTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
TableTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
TableTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
int indices;
|
||||
TypePtr yield;
|
||||
};
|
||||
int indices;
|
||||
TypePtr yield;
|
||||
};
|
||||
|
||||
class FuncTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class FuncTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
FuncTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
FuncTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
FunctionFlavor flavor;
|
||||
TypePtr params;
|
||||
TypePtr yield;
|
||||
};
|
||||
FunctionFlavor flavor;
|
||||
TypePtr params;
|
||||
TypePtr yield;
|
||||
};
|
||||
|
||||
class RecordTypeInfo : public AbstractTypeInfo
|
||||
{
|
||||
class RecordTypeInfo : public AbstractTypeInfo {
|
||||
public:
|
||||
RecordTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
RecordTypeInfo(CPPCompile* c, TypePtr _t);
|
||||
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
void AddInitializerVals(std::vector<std::string>& ivs) const override;
|
||||
|
||||
private:
|
||||
std::vector<std::string> field_names;
|
||||
std::vector<TypePtr> field_types;
|
||||
std::vector<int> field_attrs;
|
||||
};
|
||||
std::vector<std::string> field_names;
|
||||
std::vector<TypePtr> field_types;
|
||||
std::vector<int> field_attrs;
|
||||
};
|
||||
|
||||
// Much of the table-driven initialization is based on vectors of indices,
|
||||
// which we represent as vectors of int's, where each int is used to index a
|
||||
|
@ -683,31 +622,29 @@ private:
|
|||
// to a potentially large, deep set of indices using a single value - such as
|
||||
// for CPP_InitsInfo's "offset_set" member variable.
|
||||
|
||||
class IndicesManager
|
||||
{
|
||||
class IndicesManager {
|
||||
public:
|
||||
IndicesManager() { }
|
||||
IndicesManager() {}
|
||||
|
||||
// Adds a new vector-of-indices to the collection we're tracking,
|
||||
// returning the offset that will be associated with it at run-time.
|
||||
int AddIndices(std::vector<int> indices)
|
||||
{
|
||||
int n = indices_set.size();
|
||||
indices_set.emplace_back(std::move(indices));
|
||||
return n;
|
||||
}
|
||||
// Adds a new vector-of-indices to the collection we're tracking,
|
||||
// returning the offset that will be associated with it at run-time.
|
||||
int AddIndices(std::vector<int> indices) {
|
||||
int n = indices_set.size();
|
||||
indices_set.emplace_back(std::move(indices));
|
||||
return n;
|
||||
}
|
||||
|
||||
// Generates the initializations used to construct the managed
|
||||
// vectors at run-time.
|
||||
void Generate(CPPCompile* c);
|
||||
// Generates the initializations used to construct the managed
|
||||
// vectors at run-time.
|
||||
void Generate(CPPCompile* c);
|
||||
|
||||
private:
|
||||
// Each vector-of-indices being tracked. We could obtain some
|
||||
// space and time savings by recognizing duplicate vectors
|
||||
// (for example, empty vectors are very common), but as long
|
||||
// as the code compiles and executes without undue overhead,
|
||||
// this doesn't appear necessary.
|
||||
std::vector<std::vector<int>> indices_set;
|
||||
};
|
||||
// Each vector-of-indices being tracked. We could obtain some
|
||||
// space and time savings by recognizing duplicate vectors
|
||||
// (for example, empty vectors are very common), but as long
|
||||
// as the code compiles and executes without undue overhead,
|
||||
// this doesn't appear necessary.
|
||||
std::vector<std::vector<int>> indices_set;
|
||||
};
|
||||
|
||||
} // zeek::detail
|
||||
} // namespace zeek::detail
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue