cl: cffi - type translation

Table of Contents

2025-12-19 here are some note I took while trying to understand how to tell cffi to automatically translates some type. I did not review these notes, but they will be a good starting point next time I want to do something similar.

1. translate, expand and functions

  • the translate-* methods are called at run-time, the expand-* methods are called at macro-expansion-time (à la compiler macros).
    • usually!
    • expand-* "bail-out" to translate-* methods
      • again, very similar to compiler-macros
  • defcfun and foreign-funcall uses the internal function translate-objects, which in turns uses expand-from-foreign, expand-to-foreign-dyn-indirect and expand-to-foreign-dyn

2. TODO

  • ~(namespace-table :default)
  • parse-type
  • define-parse-method
  • find-type-parser

3. defcstruct

  • defcstruct
    • always define a clos class
      • the default name of the class is <name>-tclass
      • it has the superclasses foreign-struct-type and translatable-foreign-type
      • it has no slot!
    • calls notice-foreign-struct-definition
      • it does a lot: validation, compute offsets, padding, alignment, etc
      • it creates an instance of the class 🤔
      • with-tentative-type-definition
        • notice-foreign-type + unwind-protect
          • (setf (find-type-parser name namespace) (lambda () type))
          • namespace defaults to… :default
          • type is actually the instance created at the start of notice-foreign-type
        • in the body:
          • make-struct-slot
            • either an instance of aggregate-struct-slot or simple-struct-slot
    • calls ~generate-struct-accessors-
      • only if the option :conc-name is not nil
      • defines a function and a setf function for each slots
      • named <conc-name>-<slot>
    • also define-parse-method

4. TODO define-foreign-type

  • declass
    • the slots are NOT the slots of the instance, they are part of the TYPE.
  • define-parse-method

maybe I need to add a define-parse-method??

Created: 2025-12-19 Fri 18:45