cffi / cffi

The Common Foreign Function Interface
http://common-lisp.net/project/cffi
MIT License
439 stars 133 forks source link

Feature requrest: support for bitfields in `defcstruct` #365

Open Gleefre opened 1 year ago

Gleefre commented 1 year ago

It would be nice to be able to specify C bitfields with defcstruct.

Here is an example of possible syntax:

struct bfield {
  unsigned int a : 3;
  unsigned int b : 2;
  unsigned int c : 8;
};

to

(cffi:defcstruct (bfield)
  (a :unsigned-int :offset 0   :bitfield (cl:byte 3 0))
  (b :unsigned-int :offset 3/8 :bitfield (cl:byte 2 0))
  (c :unsigned-int :offset 5/8 :bitfield (cl:byte 8 0)))
;; or
(cffi:defcstruct (bfield)
  (a :unsigned-int :offset 0 :bitfield (cl:byte 3 0))
  (b :unsigned-int :offset 0 :bitfield (cl:byte 2 3))
  (c :unsigned-int :offset 0 :bitfield (cl:byte 8 5)))

It would be also nice if it calculated the next offset automatically:

(cffi:defcstruct (bfield)
  (a :unsigned-int :bitfield (byte 3 0))
  (b :unsigned-int :bitfield (byte 2 0))
  (c :unsigned-int :bitfield (byte 8 0)))