IR Types¶
The Intermediate Representation (IR) is the core data model of headerkit. Parser backends produce IR objects; writers consume them to generate output in various formats.
All IR types are Python dataclasses defined in the headerkit.ir module.
Container¶
The top-level object returned by all parser backends. Header is maintained as a backward-compatible alias for SourceUnit.
SourceUnit
dataclass
¶
SourceUnit(path, declarations=list(), included_headers=set(), language='c', classification='header')
Container for a parsed source or interface unit.
This is the top-level result returned by all parser backends. It contains the file path, extracted declarations, and input metadata.
::
from headerkit.backends import get_backend
from headerkit.ir import Struct, Function, SourceUnit
backend = get_backend()
unit = backend.parse(code, "myheader.h")
print(f"Parsed {len(unit.declarations)} declarations from {unit.path}")
for decl in unit.declarations:
if isinstance(decl, Function):
print(f" Function: {decl.name}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the original source file. |
required |
declarations
|
list[Declaration]
|
List of extracted declarations (structs, functions, etc.). |
list()
|
included_headers
|
set[str]
|
Set of header file basenames included by this unit (populated by libclang backend only). |
set()
|
language
|
str
|
Language identifier (e.g. 'c', 'cpp', 'nim', 'rust'). |
'c'
|
classification
|
str
|
Source classification (e.g. 'header', 'source', 'interface'). Example¶ |
'header'
|
InputSpec
dataclass
¶
Specification of an input source unit, its language, and classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the source file or virtual file. |
required |
language
|
str
|
Language identifier (e.g. 'c', 'cpp', 'nim', 'rust', 'zig'). |
'c'
|
classification
|
str
|
Classification ('header', 'source', 'interface', 'idl'). |
'header'
|
content
|
str | None
|
Optional raw string content. |
None
|
from_path
classmethod
¶
Infer language and classification from a file path extension.
Type Expressions¶
Type expressions form a recursive tree structure representing C type syntax.
For example, const char** becomes Pointer(Pointer(CType("char", ["const"]))).
CType
dataclass
¶
A C type expression representing a base type with optional qualifiers.
This is the fundamental building block for all type representations.
Qualifiers like const, volatile, unsigned are stored separately
from the type name for easier manipulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The base type name (e.g., |
required |
qualifiers
|
list[str]
|
Type qualifiers (e.g., |
list()
|
is_elaborated
|
bool | None
|
Whether the source wrote an elaborated type specifier
-- Examples¶Simple types:: Composite types with pointers:: |
None
|
Pointer
dataclass
¶
Pointer to another type.
Represents pointer types with optional qualifiers. Pointers can be
nested to represent multi-level indirection (e.g., char**).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pointee
|
TypeExpr
|
The type being pointed to. |
required |
qualifiers
|
list[str]
|
Qualifiers on the pointer itself (e.g., Examples¶Basic pointer:: Pointer to const:: Double pointer:: Const pointer (pointer itself is const):: |
list()
|
Array
dataclass
¶
Fixed-size or flexible array type.
Represents C array types, which can have a fixed numeric size, a symbolic size (macro or constant), or be flexible (incomplete).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element_type
|
TypeExpr
|
The type of array elements. |
required |
size
|
Union[int, str] | None
|
Array size - an integer for fixed size, a string for
symbolic/expression size (e.g., Examples¶Fixed-size array:: Flexible array (incomplete):: Symbolic size:: Multi-dimensional array:: |
None
|
FunctionPointer
dataclass
¶
Function pointer type.
Represents a pointer to a function with a specific signature. Used for callbacks, vtables, and function tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
return_type
|
TypeExpr
|
The function's return type. |
required |
parameters
|
list[Parameter]
|
List of function parameters. |
list()
|
is_variadic
|
bool
|
True if the function accepts variable arguments
(ends with |
False
|
calling_convention
|
str | None
|
The calling convention if non-default
(e.g., Examples¶Simple function pointer:: With parameters:: Variadic function pointer:: |
None
|
Declarations¶
Declaration types represent the top-level constructs found in C/C++ headers.
Enum
dataclass
¶
Enum(name, values=list(), is_typedef=False, namespace=None, location=None, is_scoped=False, cpp_name=None, underlying_type=None, underlying_type_known=True)
Enumeration declaration.
Represents a C enum type with named constants. Enums may be named or anonymous (used in typedefs or inline).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The enum tag name, or None for anonymous enums. |
required |
values
|
list[EnumValue]
|
List of enumeration constants. |
list()
|
is_typedef
|
bool
|
True if this enum came from a typedef declaration. |
False
|
namespace
|
str | None
|
Enclosing C++ namespace, or None at global scope. Part of
the enum's identity: |
None
|
location
|
SourceLocation | None
|
Source location for error reporting. |
None
|
is_scoped
|
bool
|
True for a C++ |
False
|
cpp_name
|
str | None
|
Fully-qualified C++ spelling of the tag, when it is not
derivable from |
None
|
underlying_type
|
str | None
|
The integer type the enum is represented as, as a C
type spelling such as C leaves this implementation-defined, requiring only that it represent
every enumerator, and both C++11 forms may fix it explicitly -- on a
scoped enum (
|
None
|
underlying_type_known
|
bool
|
Whether Examples¶Named enum:: Anonymous enum (typically used with typedef):: |
True
|
EnumValue
dataclass
¶
Single enumeration constant.
Represents one named constant within an enum definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The constant name. |
required |
value
|
Union[int, str] | None
|
The constant's value - an integer for explicit values,
a string for expressions (e.g., Examples¶Explicit value:: Auto-increment (implicit value):: Expression value:: |
None
|
BaseSpecifier
dataclass
¶
C++ base class specifier in class inheritance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the base class. |
required |
access
|
str
|
Access specifier ("public", "protected", "private"). |
'public'
|
is_virtual
|
bool
|
True if virtually inherited. |
False
|
Struct
dataclass
¶
Struct(name, fields=list(), methods=list(), is_union=False, is_cppclass=False, is_typedef=False, is_packed=False, namespace=None, template_params=list(), cpp_name=None, notes=list(), inner_typedefs=dict(), nested_records=list(), bases=list(), is_abstract=False, constructors=list(), destructor=None, conversions=list(), vtable_entries=list(), attributes=list(), is_deprecated=False, alignment=None, location=None)
Struct or union declaration.
Represents a C struct or union type definition. Both use the same
IR class with is_union distinguishing between them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
The struct/union tag name, or None for anonymous types. |
required |
fields
|
list[Field]
|
List of member fields. |
list()
|
methods
|
list[Function]
|
List of methods (for C++ classes only). |
list()
|
is_union
|
bool
|
True for unions, False for structs. |
False
|
is_cppclass
|
bool
|
True for C++ classes (uses |
False
|
is_typedef
|
bool
|
True if this came from a typedef declaration. |
False
|
is_packed
|
bool
|
True if the struct has |
False
|
nested_records
|
list[Struct]
|
Records defined inside this record's body. A C++ nested class stays here rather than being lifted to the top level, because its name is only meaningful when qualified by the enclosing scope. |
list()
|
location
|
SourceLocation | None
|
Source location for error reporting. Examples¶Simple struct:: Union:: C++ class with method:: Anonymous struct:: |
None
|
Field
dataclass
¶
Field(name, type, bit_width=None, anonymous_struct=None, is_anonymous_transparent=False, access=None, is_static=False, is_padding=False)
Struct or union field declaration.
Represents a single field within a struct or union definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The field name. |
required |
type
|
TypeExpr
|
The field's type expression. |
required |
bit_width
|
int | None
|
C bitfield width in bits, or None for non-bitfield
fields. For example, |
None
|
anonymous_struct
|
Struct | None
|
When this field is an anonymous nested
struct or union, holds the :class: |
None
|
access
|
str | None
|
Access specifier ( |
None
|
is_static
|
bool
|
True if this is a static data member. |
False
|
is_padding
|
bool
|
True for an unnamed bitfield ( Examples¶Simple field:: Pointer field:: Array field:: Bitfield:: Anonymous nested struct:: Unnamed bitfield padding:: |
False
|
Function
dataclass
¶
Function(name, return_type, parameters=list(), is_variadic=False, calling_convention=None, namespace=None, template_params=list(), is_static=False, is_const=False, is_virtual=False, is_pure_virtual=False, is_explicit=False, access=None, is_deleted=False, is_defaulted=False, is_noexcept=False, is_inline=False, body=None, attributes=list(), is_deprecated=False, location=None)
Function declaration.
Represents a C function prototype or declaration. Does not include the function body (declarations only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The function name. |
required |
return_type
|
TypeExpr
|
The function's return type. |
required |
parameters
|
list[Parameter]
|
List of function parameters. |
list()
|
is_variadic
|
bool
|
True if the function accepts variable arguments. |
False
|
calling_convention
|
str | None
|
The calling convention if non-default
(e.g., |
None
|
template_params
|
list[str]
|
Template parameter names for C++ function templates. |
list()
|
location
|
SourceLocation | None
|
Source location for error reporting. Examples¶Simple function:: With return value:: Variadic function:: |
None
|
Parameter
dataclass
¶
Function parameter declaration.
Represents a single parameter in a function signature. Parameters may be named or anonymous (common in prototypes).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Parameter name, or None for anonymous parameters. |
required |
type
|
TypeExpr
|
The parameter's type expression. |
required |
default_value
|
str | None
|
Default argument expression string, or None if none. Examples¶Named parameter:: With default value:: Anonymous parameter:: Complex type:: |
None
|
Typedef
dataclass
¶
Typedef(name, underlying_type, namespace=None, attributes=list(), is_deprecated=False, location=None)
Type alias declaration.
Represents a C typedef that creates an alias for another type. Common patterns include aliasing primitives, struct tags, and function pointer types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The new type name being defined. |
required |
underlying_type
|
TypeExpr
|
The type being aliased. |
required |
location
|
SourceLocation | None
|
Source location for error reporting. Examples¶Simple alias:: Struct alias:: Function pointer alias:: |
None
|
Variable
dataclass
¶
Variable(name, type, namespace=None, attributes=list(), is_deprecated=False, alignment=None, location=None)
Global or extern variable declaration.
Represents variable declarations at file scope, including extern
variables and file-scope data definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The variable identifier. |
required |
type
|
TypeExpr
|
The variable's type expression. |
required |
location
|
SourceLocation | None
|
Source location for error reporting. Examples¶Extern variable:: Const string:: Array variable:: |
None
|
Constant
dataclass
¶
Constant(name, value=None, evaluated_value=None, raw_expression=None, type=None, is_macro=False, location=None)
Compile-time constant declaration.
Represents #define macros with constant values or const
variable declarations. Only backends that support macro extraction
(e.g., libclang) can populate macro constants.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The constant name. |
required |
value
|
Union[int, float, str] | None
|
The constant's value - an integer, float, or string expression. None if the value cannot be determined. |
None
|
evaluated_value
|
Union[int, float, str] | None
|
The evaluated numeric or string value if evaluable. |
None
|
raw_expression
|
str | None
|
The un-evaluated macro or constant expression string. |
None
|
type
|
CType | None
|
For typed constants ( |
None
|
is_macro
|
bool
|
True if this is a |
False
|
location
|
SourceLocation | None
|
Source location for error reporting. Examples¶Numeric macro:: Expression macro:: Typed const:: String macro:: |
None
|
Union Types¶
These are typing.Union aliases used in type annotations throughout headerkit.
Declaration¶
Any top-level declaration that can appear in a Header.
TypeExpr¶
Any type expression that can appear in a declaration's type fields.
Source Location¶
SourceLocation
dataclass
¶
Location in source file for error reporting and filtering.
Used to track where declarations originated, enabling:
- Better error messages during parsing
- Filtering declarations by file (e.g., exclude system headers)
- Source mapping for debugging
::
loc = SourceLocation("myheader.h", 42, 5)
print(f"Declaration at {loc.file}:{loc.line}")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str
|
Path to the source file. |
required |
line
|
int
|
Line number (1-indexed). |
required |
column
|
int | None
|
Column number (1-indexed), or None if unknown. Example¶ |
None
|
Parser Backend Protocol¶
The parser backend protocol is defined alongside the IR types since backends produce IR directly. See also the Backends page for registry functions.
ParserBackend
¶
Bases: Protocol
Protocol defining the interface for parser backends.
All parser backends must implement this protocol to be usable with headerkit.
Backends are responsible for translating from their native AST format
(pycparser, libclang, etc.) to the common :class:Header IR format.
Available Backends¶
libclang- LLVM clang-based parser with C++ support
Example¶
::
from headerkit.backends import get_backend
# Get default backend
backend = get_backend()
# Get specific backend
libclang = get_backend("libclang")
# Parse code
header = backend.parse("int foo(void);", "test.h")
supported_languages
property
¶
Set of source languages supported by this backend (e.g., frozenset({"c", "cpp"})).
supported_classifications
property
¶
Set of input classifications supported by this backend (e.g., frozenset({"header", "source"})).
parse
¶
parse(code, filename, include_dirs=None, extra_args=None, *, use_default_includes=True, recursive_includes=True, max_depth=10, project_prefixes=None, allowlist=None, denylist=None)
Parse C/C++ code and return the IR representation.
Traversal and filtering¶
recursive_includes governs traversal: descending into each
non-system included header and merging what it declares.
project_prefixes decides which paths count as project rather than
system headers, and max_depth (with a backend-internal cycle guard)
bounds the descent.
allowlist and denylist govern filtering, and they narrow the
merged result in both traversal modes:
recursive_includes=Truewith noallowlistreturns declarations from every non-system included header.recursive_includes=Truewith anallowlistreturns the main file's declarations plus those of the named files, and nothing else.recursive_includes=Falsewith noallowlistreturns the main file's declarations alone.- Deny wins over allow. A file named by both lists is excluded.
- The main file is never denied; a denylist governs included files only.
- A list entry matching nothing is not an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Source code to parse. |
required |
filename
|
str
|
Name of the source file. Used for error messages
and |
required |
include_dirs
|
list[str] | None
|
Directories to search for |
None
|
extra_args
|
list[str] | None
|
Additional arguments for the preprocessor/compiler. Format is backend-specific. |
None
|
use_default_includes
|
bool
|
If True, add system include directories. |
True
|
recursive_includes
|
bool
|
If True, descend into included project headers and merge their declarations into the result. False parses only the main file, and is the only way to exclude included declarations entirely. |
True
|
max_depth
|
int
|
Maximum recursion depth for include processing. |
10
|
project_prefixes
|
tuple[str, ...] | None
|
Path prefixes to treat as project headers rather than system headers, so that they are descended into. |
None
|
allowlist
|
list[str] | None
|
Files whose declarations are kept, alongside the main
file's own. |
None
|
denylist
|
list[str] | None
|
Files whose declarations are dropped, using the same
resolution and glob rules as |
None
|
Returns:
| Type | Description |
|---|---|
Header
|
Parsed header containing all extracted declarations. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If parsing fails due to syntax errors. |