Add support for enum with explicit enumerator values.

* Adding support for enums with explicit enumerator values (see doc
  below) to bifcl and policy layer.

* Bifcl: remove (partially written) output files on error and
  do a nice exit(1) instead of harsh abort() on parse errors.

* CMakeText: if bifcl fails, remove output files (failsafe,
  in case bifcl fails to clean up after itself).

Enum description
----------------

Enum's are supported in .bif and .bro scripts.
An enum in a bif will become available in the event engine and
the policy layer.

Enums are "C-style". The first element in an enum will have a
value of 0, the next value will be 1, etc.
It is possible to assign an enumerator value to an element. If
next element does not have an explicit value, its values will be
the value of the last element + 1

Example::
    type foo: enum {
        BAR_A,      # value will be  0
        BAR_B,      # value will be  1
        BAR_C = 10, # value will be 10
        BAR_D,      # value will be 11
    };

Enumerator values can only by positive integer literals.
The literals can be specified in (0x....), but not in octal (bro policy
layer limitation). So, do not use 0123 as value in bifs!

Each enumerator value can only be used once per enum (C allows
to use the same value multiple times). This makes reverse mapping from
value to name (e.g., in %s format strings) unambigious. This is enforced
in by the policy script.

Enums can be redef'ed, i.e., extended. Enumerator values will continue
to increment. If there are multiple redefs in different policy scripts,
then name <-> value mappings will obviously depend on the order in
which scripts are loaded (which might not be obvious).

Example::

    redef enum foo += {
        BAR_E,      # value will be 12
        BAR_F = 5,  # value will be  5
        BAR_G,      # value will be  6
    };
This commit is contained in:
Gregor Maier 2010-12-09 18:29:47 -08:00
parent 2f7fa3470b
commit 72454c230b
6 changed files with 188 additions and 103 deletions

View file

@ -452,31 +452,30 @@ protected:
class EnumType : public BroType {
public:
EnumType(bool arg_is_export);
EnumType();
~EnumType();
// The value of this name is next counter value, which is returned.
// A return value of -1 means that the identifier already existed
// (and thus could not be used).
int AddName(const string& module_name, const char* name);
// A return value of -1 means that the identifier or the counter values
// already existed (and thus could not be used).
bro_int_t AddName(const string& module_name, const char* name, bool is_export);
// Add in names from the suppled EnumType; the return value is
// the value of the last enum added.
int AddNamesFrom(const string& module_name, EnumType* et);
// The value of this name is set to val, which is return. The counter will
// be updated, so the next name (without val) will have val+1
// A return value of -1 means that the identifier or val
// already existed (and thus could not be used).
bro_int_t AddName(const string& module_name, const char* name, bro_int_t val, bool is_export);
// -1 indicates not found.
int Lookup(const string& module_name, const char* name);
const char* Lookup(int value); // Returns 0 if not found
bro_int_t Lookup(const string& module_name, const char* name);
const char* Lookup(bro_int_t value); // Returns 0 if not found
protected:
EnumType() {}
DECLARE_SERIAL(EnumType)
typedef std::map< const char*, int, ltstr > NameMap;
typedef std::map< const char*, bro_int_t, ltstr > NameMap;
NameMap names;
int counter;
bool is_export;
bro_int_t counter;
};
class VectorType : public BroType {