mirror of
https://github.com/zeek/zeek.git
synced 2025-10-08 09:38:19 +00:00
enum type: don't allow mixing of explicit value and auto-increment.
Updated enum type. New 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. It is possible to assign an explicit value to an enum enumerator element, or the enum type can automatically assign values. However, the styles cannot be mixed. If automatic assignement is used, the first element will have a value of 0, the next will have a value of 1, etc. Enum type variables and identifiers can be formated using the "%s" format specifier, in which case the symbolic name will be printed. If the "%d" format specifier is used, the numerical value is printed. Example automatic assignment: type foo: enum { BAR_A, # value will be 0 BAR_B, # value will be 1 BAR_C, # value will be 2 }; Example with explicit assignment: type foobar: enum { BAR_X = 10, # value will be 10 BAR_Y = 23, # value will be 23 BAR_Z = 42, # value will be 42 }; 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). All these restrictions are enforced by the policy script layer and not the bif compiler! Enums can be redef'ed, i.e., extended. If the enum is automatic increment assignment, then the value will continue to increment. If the enum uses explicit assignment, then the redef need to use explicit assignments as well. Example 1:: redef enum foo += { BAR_D, # value will be 3 BAR_E, # value will be 4 BAR_F, # value will be 5 }; Example 2:: redef enum foobar += { BAR_W = 100, };
This commit is contained in:
parent
72454c230b
commit
fdaeea0ea9
3 changed files with 52 additions and 21 deletions
41
src/Type.cc
41
src/Type.cc
|
@ -1094,16 +1094,42 @@ EnumType::~EnumType()
|
|||
delete [] iter->first;
|
||||
}
|
||||
|
||||
bro_int_t EnumType::AddName(const string& module_name, const char* name, bool is_export)
|
||||
// Note, we don't use Error() and SetError(( for EnumType because EnumTypes can
|
||||
// be redefined, the location associated with it is ill-defined and might result
|
||||
// in error messaging with confusing line numbers.
|
||||
void EnumType::AddName(const string& module_name, const char* name, bool is_export)
|
||||
{
|
||||
/* implicit, auto-increment */
|
||||
if ( counter < 0)
|
||||
{
|
||||
error("cannot mix explicit enumerator assignment and implicit auto-increment");
|
||||
return;
|
||||
}
|
||||
AddNameInternal(module_name, name, counter, is_export);
|
||||
counter++;
|
||||
}
|
||||
|
||||
void EnumType::AddName(const string& module_name, const char* name, bro_int_t val, bool is_export)
|
||||
{
|
||||
return AddName(module_name, name, counter, is_export);
|
||||
/* explicit value specified */
|
||||
error_t rv;
|
||||
if ( counter > 0 )
|
||||
{
|
||||
error("cannot mix explicit enumerator assignment and implicit auto-increment");
|
||||
return;
|
||||
}
|
||||
counter = -1;
|
||||
AddNameInternal(module_name, name, val, is_export);
|
||||
}
|
||||
|
||||
bro_int_t EnumType::AddName(const string& module_name, const char* name, bro_int_t val, bool is_export)
|
||||
void EnumType::AddNameInternal(const string& module_name, const char* name, bro_int_t val, bool is_export)
|
||||
{
|
||||
ID *id;
|
||||
if ( Lookup(val) )
|
||||
return -1;
|
||||
{
|
||||
error("enumerator value in enumerated type definition already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
id = lookup_ID(name, module_name.c_str());
|
||||
if ( ! id )
|
||||
|
@ -1113,12 +1139,13 @@ bro_int_t EnumType::AddName(const string& module_name, const char* name, bro_int
|
|||
id->SetEnumConst();
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
{
|
||||
error("identifier or enumerator value in enumerated type definition already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
string fullname = make_full_var_name(module_name.c_str(), name);
|
||||
names[copy_string(fullname.c_str())] = val;
|
||||
counter = val + 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
bro_int_t EnumType::Lookup(const string& module_name, const char* name)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue