Add support for C-style enums in C mode. And tests.

In backward compatible mode C style enums binding are correctly
generated
This commit is contained in:
Artem Serebriyskiy 2013-11-13 15:16:09 +04:00
commit 6d49a57b53
2 changed files with 21 additions and 2 deletions

View file

@ -19,4 +19,12 @@ assert(enums.globalinstance3==30)
assert(enums.AnonEnum1==0)
assert(enums.AnonEnum2==100)
-- In C enums from struct are exported without prefixing with struct name
-- In C++ they are prefixed.
-- We are emulating xor :)
assert(enums.BAR1 ~= enums.Foo_BAR1) -- It is either C style, or C++ style, but not both
assert((enums.BAR1 ~= nil ) or (enums.Foo_BAR1 ~= nil))
assert(enums.Phoo ~= enums.iFoo_Phoo)
assert((enums.Phoo == 50) or (enums.iFoo_Phoo == 50))
-- no point in checking fns, C will allow any value

View file

@ -139,6 +139,7 @@ private:
STATIC_FUNC,
STATIC_VAR,
STATIC_CONST, // enums and things like static const int x = 5;
ENUM_CONST, // This is only needed for backward compatibility in C mode
STATES_COUNT
};
@ -1010,8 +1011,16 @@ public:
bool make_v2_compatible = v2_compatibility && getCurrentClass() != 0;
if (make_v2_compatible) {
target_name_v2 = Swig_name_member(0, class_symname, target_name);
iname_v2 = Swig_name_member(0, class_symname, iname);
// Special handling for enums in C mode - they are not prefixed with structure name
if(!CPlusPlus && current[ENUM_CONST]) {
target_name_v2 = target_name;
DohIncref(target_name_v2);
iname_v2 = iname;
DohIncref(iname_v2);
} else {
target_name_v2 = Swig_name_member(0, class_symname, target_name);
iname_v2 = Swig_name_member(0, class_symname, iname);
}
n_v2 = Copy(n);
//Printf( stdout, "target name v2: %s, symname v2 %s\n", target_name_v2.ptr(), iname_v2.ptr());// TODO:REMOVE
if (!luaAddSymbol(iname_v2, n, class_parent_nspace)) {
@ -1074,8 +1083,10 @@ public:
virtual int enumDeclaration(Node *n) {
current[STATIC_CONST] = true;
current[ENUM_CONST] = true;
int result = Language::enumDeclaration(n);
current[STATIC_CONST] = false;
current[ENUM_CONST] = false;
return result;
}