Skip to main content

Module ir

Module ir 

Source
Expand description

Structural-mode C frontend and the shared C-family CST walking machinery.

The file is parsed with the tree-sitter C grammar and the resulting error-tolerant concrete syntax tree is mapped onto the language-neutral SyntaxIrFile: a comment-free token stream plus a tree of IrNodes built from structurally meaningful grammar nodes only. Interior expression detail (member accesses, casts, non-assignment binary operators, parentheses) stays token-only under the nearest ancestor node. Statement wrappers add no node of their own when their inner expression already maps to a shape: f(); is one Shape::Call node, not an ExprStmt(Call) pair.

The walking machinery is language-parameterized through IrMapping and shared with the C++ structural frontend, which layers its own mapping table on top of the C one (cpp → c → core is the fixed dependency direction, so the shared code lives here).

§Granularity decisions specific to C

  • declaration maps to Shape::VarDecl uniformly — locals, file-scope variables and function prototypes alike. C declarations have no lexical marker separating those roles, and prototype-vs-variable disambiguation is a semantic judgement Structural mode does not make.
  • Macro invocations are structurally indistinguishable from call_expression (the grammar has no separate node for them), so they surface as Shape::Call; Shape::MacroCall is never produced.
  • Preprocessor conditionals (preproc_if, preproc_ifdef, …) become Shape::Native nodes and both branches stay in the IR unexpanded. #include and other non-defining directives produce tokens only.
  • Macro replacement text is a single opaque preproc_arg leaf in the grammar; it becomes one TokenKind::Unknown token.

§Degradation

Malformed regions and CST-depth truncation become Shape::Error nodes plus byte ranges in SyntaxIrFile::error_ranges. If the parser itself cannot be set up (grammar version mismatch) or returns no tree, the file degrades to an empty token stream and node tree with one error range spanning the whole file.

Structs§

CMapping
The C node-mapping table as an IrMapping.
CStructuralFrontend
The C Structural-mode frontend.

Enums§

Mapping
How one CST node maps onto the IR.

Constants§

STRUCTURAL_FRONTEND_VERSION
Version tag of this structural frontend, used as a fingerprint input. Bump it whenever a change alters the token stream or the IR tree for unchanged input.

Traits§

IrMapping
The per-language part of a C-family structural frontend.

Functions§

c_family_node_name
Recover a declared name where the C-family grammars provide one: the name field of record specifiers and macro definitions, or the identifier buried in a function definition’s declarator chain.
classify_c
The C node-mapping table, also the fallthrough table of the C++ frontend.
classify_token
The shared C-family token classification.
parse_to_ir
Parse source with grammar and map the tree onto the IR under mapping. This is the shared entry point of the C-family structural frontends.
record_mapping
Shape::Record when a record specifier carries a body; transparent in type-reference position (struct foo x; names a type, it defines nothing).