How would I refer to an enum type as type for an argument in a BiF or function? I have an enum named Logging::ID, and I want it to be the first argument in this BiF...
function logging_log%(id: Logging::ID, rec: any%): bool
That obviously doesn't parse though, are there any workarounds?
Would be nice if that worked. Seems like something we should fix
eventually.
I'm actually not quite sure to which degree enums are supported by
bifcl right now. Two things you could try: (1) do they work if they
don't have namespace?; and (2) does it work to say just ":enum"
(probably not).
The alternative would be to use "any" and check for the type's
correctness dynamically inside the function body until we have fixed
this.
(The other work-around is using strings, as I think you said you are
alrady doing now) .
I'm actually not quite sure to which degree enums are supported by
bifcl right now. Two things you could try: (1) do they work if they
don't have namespace?;
I think that does work, it's the namespacing that messes things up because the Bro and C syntaxes don't mesh. Maybe bifcl could munge namespaces from Logging::ID to _Logging__ID or something to provide the transition?
and (2) does it work to say just ":enum"
(probably not).
I can't imagine that it would work. I'll give it a try though.
The alternative would be to use "any" and check for the type's
correctness dynamically inside the function body until we have fixed
this.
Good point, I may go that way once I have the other kinks worked out.
bifcl supports enums (it just casts them as a "neutral" Val*, same as
for record types). So it would just be a matter of telling bifcl to
accept the Namespace::Type name syntax. That should be it.
The C part doesn't really care about the type. It's all a "netural"
Val*. The type is only used for the policy file and type checking.
EnumType and EnumVal should make accessing the enum's value relatively
easy. I'm using that in my re-working of the NFS analyzer. Let me know
if you want details.
After taking a deeper look into this, I realized that I may be misunderstanding how enums are supposed to be used. Does it make sense that you'd provide an enum name as a type to get static type checking? Like this...
<code example>
type TEST_ENUM: enum { test_one, test_two, test_three };
function blah(b: TEST_ENUM)
{
print "did it work?";
}
event bro_init()
{
local a = test_two;
print blah(a);
}
</code example>
I get this error from that code..
[seth@Blake build (master)]$ ./src/bro test.bro
./test.bro, line 11 (print blah(a)): error, value of type void illegal
Before I send my example for namespaced enums, I think I need to get more of the base usage of enums clarified. Are there any examples that you know of where enums are used as parameter values like this?
I don't think that enums are used extensively between C and policy
scripts. AFAIK, my NFS analyzer is one of the first that passes enums as
event arguments.