Re: [pygccxml-development] How to distinguish between enum and typedef enum.
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2008-07-27 18:21:33
|
On Sun, Jul 27, 2008 at 5:56 PM, Gustavo Carneiro <gjc...@gm...> wrote: > Hello. I am trying to scan the following definitions: > > > struct SomeStruct > { > int x; > float y; > }; > > enum SomeEnum { > SOME_FOO, > SOME_BAR > }; > > int hello_xpto (struct SomeStruct xto); > int hello_zbr (enum SomeEnum xto); > > > typedef struct { > int x; > float y; > } SomeStruct2; > > typedef enum { > SOME2_FOO, > SOME2_BAR > } SomeEnum2; > > int hello_xpto2 (SomeStruct2 xto); > int hello_zbr2 (SomeEnum2 xto); > > The intent is to generate bindings for, in this case, a pure C library, > using pure C code. My issue here is how to distinguish between an enum > defined via a typedef (SomeEnum2) and an enum the old fashioned C way. > Because the type needs to generated differently in both cases. I have to > generate 'enum SomeEnum', because simply referencing 'SomeEnum' does not > compile with a C compiler. On the other hand, I cannot generate a type > string 'enum SomeEnum2', because SomeEnum2 is actually a typedef, not a > proper enum; it also does not compile. So always putting enum in front of > everything does not work. > > Idem for 'struct SomeStruct' vs 'SomeStruct2'. > > I have been experimenting for a while, but could not find any way to > distinguish between the two cases. Any idea? The short answer is: you cannot, gccxml doesn't support C front-end. The longer answer is: you can write your own "custom" parser. I took next definition and put it within the file: 0 typedef enum { 1 SOME2_FOO, 2 SOME2_BAR 3 } SomeEnum2; 4 5 int hello_zbr2 (SomeEnum2 xto); GCCXML generate piece of xml: <Enumeration id="_34" name="SomeEnum2" context="_1" location="f1:4" file="f1" line="4" size="32" align="32"> <EnumValue name="SOME2_FOO" init="0"/> <EnumValue name="SOME2_BAR" init="1"/> </Enumeration> It points to the line "4". It is 1 based. It seems that all you have to do is to * assume the code is 100% valid code * check that after enum name comes ";" and not "{" In my opinion it should not be too difficult. P.S. If you accept this as a solution, I will accept your patch to pygccxml :-) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |