IR Module¶
The IR (Intermediate Representation) module provides data structures for representing C/C++ declarations in a parser-agnostic format. It is headerkit.ir; autopxd2 consumes it rather than defining its own.
ir
¶
Intermediate Representation (IR) for C/C++ declarations.
This module defines the IR that all parser backends produce. Writers consume this IR to generate output in various formats (CFFI cdef, Cython pxd, etc.).
Design Principles¶
- Parser-agnostic: Works with pycparser, libclang, tree-sitter, etc.
- Intuitive composition: Types compose naturally
(e.g.,
const char*becomesPointer(CType("char", ["const"]))) - Complete coverage: Represents everything C/C++ headers can express
Type Hierarchy¶
Type expressions form a recursive structure:
- :class:
CType- Base C type (int,unsigned long, etc.) - :class:
Pointer- Pointer to another type (int*,char**) - :class:
Array- Fixed or flexible array (int[10],char[]) - :class:
FunctionPointer- Function pointer type
Declaration Types¶
- :class:
Enum- Enumeration with named constants - :class:
Struct- Struct or union with fields - :class:
Function- Function declaration - :class:
Typedef- Type alias - :class:
Variable- Global variable - :class:
Constant- Compile-time constant or macro
Example¶
Parse a header and inspect declarations::
from headerkit.backends import get_backend
from headerkit.ir import Struct, Function
backend = get_backend()
header = backend.parse("struct Point { int x; int y; };", "test.h")
for decl in header.declarations:
if isinstance(decl, Struct):
print(f"Found struct: {decl.name}")
Header = SourceUnit
module-attribute
¶
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
|
Source code in headerkit/ir.py
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()
|
Source code in headerkit/ir.py
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
|
Source code in headerkit/ir.py
FunctionPointer
dataclass
¶
Function pointer or function prototype type.
Represents a pointer to a function (is_pointer=True) or an ISO C
0-star function prototype type (is_pointer=False) 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., |
None
|
qualifiers
|
list[str]
|
Pointer qualifiers (e.g., |
list()
|
is_pointer
|
bool
|
True for a 1-star function pointer ( Examples¶Simple function pointer:: With parameters:: Variadic function pointer:: |
True
|
Source code in headerkit/ir.py
Field
dataclass
¶
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
|
Source code in headerkit/ir.py
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
|
Source code in headerkit/ir.py
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
|
Source code in headerkit/ir.py
Enum
dataclass
¶
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
|
Source code in headerkit/ir.py
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
qualified_name
property
¶
The tag's full C++ spelling, or None if it is anonymous.
Struct
dataclass
¶
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
|
Source code in headerkit/ir.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 | |
has_body
property
¶
Return True if this record carries any body members beyond a bare forward declaration.
Function
dataclass
¶
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
|
Source code in headerkit/ir.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 | |
Typedef
dataclass
¶
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
|
Source code in headerkit/ir.py
Variable
dataclass
¶
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
|
Source code in headerkit/ir.py
Constant
dataclass
¶
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
|
Source code in headerkit/ir.py
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
|
Source code in headerkit/ir.py
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")
Source code in headerkit/ir.py
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 | |
name
property
¶
Human-readable name of this backend (e.g., "pycparser").
supports_macros
property
¶
Whether this backend can extract #define constants.
supports_cpp
property
¶
Whether this backend can parse C++ code.
follows_includes
property
¶
Whether this backend follows #include directives.
supports_compiler_args
property
¶
Whether this backend honors preprocessor/compiler flags (-I, -D, etc.).
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(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. |
Source code in headerkit/ir.py
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 | |