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 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
|
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
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | |
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
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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | |
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
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 | |
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
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 | |
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.
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
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | |