-3

I am reading some code, and came across this rather odd C struct definition:

typedef struct dataObject
  {
      ...;
  } DATA_OBJECT;

Can anyone explain (with references if possible):

  1. If this is a valid struct definition.
  2. What would be the purpose of such a definition (where no fields/members are defined).
wovano
  • 4,543
  • 5
  • 22
  • 49
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • I presented the code **exactly** how I encountered it in the code base. Like I said this is from the code source - **verbatim**. – Homunculus Reticulli Feb 26 '21 at 11:07
  • 1
    I'm pretty sure it's meant to be pseudo code? – Lundin Feb 26 '21 at 11:08
  • Then the code is invalid - `typedef struct dataObject { ...; } DATA_OBJECT;` will not compile. `...` is not a valid member name. – KamilCuk Feb 26 '21 at 11:09
  • `The code above is available here` did you research the code base to find out is some proeprocessing is done to the source code? Even just `sed 's/\.\.\./int a/'` is enough... `What is it searching for?` - for 3 dots `...` and replaces with `int a`. – KamilCuk Feb 26 '21 at 11:11
  • @KamilCuk I can't grok that sed expression. What is it searching for? – Homunculus Reticulli Feb 26 '21 at 11:13
  • 3
    That is not a C source file; the file extension is `.cdef`. Based on the name alone, I conjecture it is something to be processed by other software into a C source file. – Eric Postpischil Feb 26 '21 at 11:13
  • 1
    Are you sure that the code mentioned can be compiled? Name the compiler accepting it! – Basile Starynkevitch Feb 26 '21 at 11:13
  • 3
    [here is the file used](https://github.com/noxdafox/clipspy/blob/90d71813aaff6ed5467b69e1b4bbdf6a82fea58e/clips/clips_build.py#L15) , [here is doc for FFI.cdef()](https://cffi.readthedocs.io/en/latest/cdef.html#ffi-ffibuilder-cdef-declaring-types-and-functions) and is says: `The declarations can also contain “...” at various places; these are placeholders that will be completed by the compiler.`. Time to brush up google skillz. And [this doc](https://cffi.readthedocs.io/en/latest/cdef.html#letting-the-c-compiler-fill-the-gaps) I think explains semantics. – KamilCuk Feb 26 '21 at 11:17
  • It's not unheard of that people write things like `Fix this struct definition first thing in the morning` straight into the source, then when they sleepily compile the program first thing in the morning, the compiler will land them on that nonsense line. – Lundin Feb 26 '21 at 11:17
  • @KamilCuk Umm well what happens if someone decides to write a variadic function or macro then? Kaboom? – Lundin Feb 26 '21 at 11:18
  • I think you should mention clearly that this is about CFFI. You're getting answers that say it is not valid in C, which is not what you're really asking for. – Armin Rigo Feb 26 '21 at 11:31
  • @ArminRigo Please check the tags on the question – Homunculus Reticulli Feb 26 '21 at 11:41
  • @HomunculusReticulli: The tags are good, but the title asks about a “C struct definition,” and the body asks about “this rather odd C struct definition.” – Eric Postpischil Feb 26 '21 at 11:44

2 Answers2

4

If this is a valid struct definition

No.

What would be the purpose of such a definition (where no fields/members are defined)?

The file purpose is to provide the python CFFI parser with type and function declarations to use.

The purpose of this file is to preprocessed by python CFFI ffibuilder.cdef(). From letting C compiler fill the gaps:

Moreover, you can use “...” (literally, dot-dot-dot) in the cdef() at various places, in order to ask the C compiler to fill in the details. These places are:

  • structure declarations: any struct { } or union { } that ends with “...;” as the last “field” is partial: it may be missing fields, have them declared out of order, use non-standard alignment, etc. Precisely, the field offsets, total struct size, and total struct alignment deduced by looking at the cdef are not relied upon and will instead be corrected by the compiler. (But note that you can only access fields that you declared, not others.) Any struct declaration which doesn’t use “...” is assumed to be exact, but this is checked: you get an error if it is not correct.
  • [...]
  • unknown types: [....] In some cases you need to say that foo_t is not opaque, but just a struct where you don’t know any field; then you would use typedef struct { ...; } foo_t;.

I suspect it means to the CFFI that struct dataObject and DATA_OBJECT are opaque types meant to be used only as pointers and the CFFI parser does not support struct declarations.

The file is used here in clips_build.py as I understand to build clipspy python interface to C.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    This syntax makes `DATA_OBJECT` *not* opaque. For opaque structs, the regular C syntax would work in cffi as well. The difference is that a non-opaque struct has got a known size and can be instantiated. Some C APIs work like that, where you call a function to initialize your structure (and never by writing its fields directly), but the structure itself must be declared by your code and you pass a pointer to it to the initializer function. – Armin Rigo Feb 26 '21 at 11:39
  • Yep, in case of actually opaque structs there will be no typedef in the definition, it must (optionally) be placed in the struct forward declaration. – Lundin Feb 26 '21 at 13:27
2
  1. If this is a valid struct definition

No it isn't. To grab part of the C17 6.7.2.1 formal grammar:

struct-declaration:
specifier-qualifier-list struct-declarator-listopt ;
static_assert-declaration

So to begin with, the struct needs to contain a "specifier-qualifier list" which in plain English is the const int etc stuff before the variable name. Since this isn't present, gcc for example whines about a syntax error:

error: expected specifier-qualifier-list before '...' token


2.What would be the purpose of such a definition (where no fields/members are defined)?

I'm guessing it's either pseudo code or a dev "TODO" where they committed code that doesn't compile, since it has yet to be written.

Lundin
  • 195,001
  • 40
  • 254
  • 396