ffi/ffi-swig-generator — pull request #16 by phemmer (2013-10-18 03:13:00+00:00)
Title: Add named enums & fix non-typedef enums

*This expands upon the pull request for [typedef support](https://github.com/ffi/ffi-swig-generator/pull/14).*

This change fixes an enum bug and also gives type names to typedef enums. The latter becomes useful when combined with the "typedef" commit.

Consider the following swig file:

%module test
typedef enum {
FOO = 1,
BAR = 2,
} baz;

baz pop(int tart);

Without this change, the following code is generated:

baz = enum :foo, 1,
:bar, 2

attach_function :pop, :pop, [ :int ], :int

With this change, the following code is generated:

baz = enum :baz, [
:foo, 1,
:bar, 2,
]

attach_function :pop, :pop, [ :int ], :baz

This is very useful as FFI will now return either `:foo` or `:bar` (yes, the symbol) upon calling `pop()`. Without the fix it would instead return an integer, and it was up to the user to figure out which member of the enum it corresponds to.

----

The bug this fixes is when using non-typedef enums. Consider the following swig file:

%module test
enum {
FOO = 1,
BAR = 2,
};

Without this commit, the following is generated:

= enum :foo, 1,
:bar, 2

Note the missing variable being assigned to.
The fix results in the following instead:

enum [
:foo, 1,
:bar, 2,
]