[Pyrex] Pyrex and incomplete c types
Greg Ewing
greg at cosc.canterbury.ac.nz
Fri May 2 04:27:58 CEST 2003
"Edward C. Jones" <edcjones at erols.com> wrote:
> Is the file "IncTypes_wrapped.pyx" below correct?
Most of it seems to be okay, except:
> typedef struct {
> int jjj2;
> } AAA2;
>
> typedef struct _BBB {
> ...
> struct AAA2* aaa2; /* Is this correct C? */
It depends on what it's supposed to mean. What you've actually done
is declare two unrelated types, one called "AAA2" which is complete,
and one called "struct AAA2" which is incomplete.
If that's *really* what you mean, you can't translate this
directly into Pyrex, because Pyrex doesn't have a separate
namespace for struct tags like C does, so you can only make
the name AAA2 mean one thing at a time.
But I suspect you really meant these to be the same, in which
case you should just have
AAA2* aaa2;
> cdef struct AAA2 # Is this line harmless?
No, it's not, because you previously declared it using "ctypedef".
Pyrex should complain about that, but it currently doesn't (this
is a bug).
It *should* be harmless to say
ctypedef struct AAA2
but there seems to be another bug which makes this screw up any
previous non-opaque declaration.
But in any case, if you fix the header file as above, you won't
need that line at all. Generally, it's never necessary to put
an incomplete declaration *after* a complete declaration for
the same type.
> ctypedef struct BBB:
> ...
Since you previously declared a struct _BBB, that will have to be
cdef struct _BBB:
...
ctypedef _BBB BBB
and similarly for struct _CCC and CCC.
Finally, as a matter of style, I'd put all the declarations
under a single "cdef extern from" block, since they all belong
to the same header file.
You could also rearrange the declarations a bit to require less
forward declarations and use of _XXX names. Here's how I would
write it:
cdef extern from "IncTypes.h":
ctypedef struct AAA:
int jjj
ctypedef struct AAA2:
int jjj2
cdef struct _BBB
cdef struct _CCC:
int iii
ctypedef _CCC CCC
cdef struct _DDD
ctypedef _DDD DDD
cdef struct _BBB:
AAA * aaa
AAA2 * aaa2
BBB * bbb
CCC * ccc
DDD * ddd
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury, | A citizen of NewZealandCorp, a |
Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. |
greg at cosc.canterbury.ac.nz +--------------------------------------+
More information about the Pyrex
mailing list