CFFI Writer¶
The CFFI writer generates C declaration strings suitable for passing to
ffibuilder.cdef().
It handles structs, unions, enums, functions, typedefs, variables, and
integer constants.
Writer Class¶
CffiWriter
¶
Writer that generates CFFI cdef strings from headerkit IR.
Options¶
exclude_patterns : list[str] | None
Regex patterns. Declarations with names matching any pattern
are excluded from output.
define_patterns : list[str] | None
Regex patterns matched against #define names in the raw header
source. Matching names are appended as #define NAME ... lines
(CFFI's "resolve at verify-time" syntax). The actual matching is
performed by the generate() pipeline, which sets
_matched_defines before calling write().
extra_cdef : list[str] | None
Arbitrary cdef lines appended verbatim after all generated output.
Example¶
::
from headerkit.writers import get_writer
writer = get_writer("cffi", exclude_patterns=["__.*"])
cdef_string = writer.write(header)
# Or directly:
from headerkit.writers.cffi import CffiWriter
writer = CffiWriter(exclude_patterns=["__.*"])
cdef_string = writer.write(header)
Convenience Function¶
header_to_cffi
¶
Convert all declarations in a Header to a CFFI cdef string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
header
|
Header
|
Parsed header IR from headerkit. |
required |
exclude_patterns
|
list[str] | None
|
List of regex patterns. Declarations with names matching any pattern will be excluded. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A string suitable for passing to |
Low-Level Functions¶
These functions are used internally by header_to_cffi
and can be useful when working with individual declarations or type expressions.
type_to_cffi
¶
Convert a type expression to its CFFI string representation.
decl_to_cffi
¶
Convert a single IR declaration to a CFFI cdef string.
Returns None if the declaration should be excluded.