Expand description
Collect every function-unit (def/async def, method, nested def, lambda)
from a parsed Python Module.
§Design
- Build
SpanIndexonce per file; pass it by reference through the recursion. - Named units (
def/async def): anchor via pointer-arithmetic onname.value(the libcstName.value: &strborrows the original source buffer). - Lambda units: collect in pre-order; zip with
lambda_anchors(src)by index — the k-thlambdakeyword token (source order) corresponds to the k-thLambdanode encountered in pre-order. For this ordinal bijection to hold, the lambda-collection walk must be exhaustive: it visits EVERYLambdanode anywhere it can syntactically appear (a superset of the effect driver’s descent indetect/mod.rs— collection descends even into lazy generator-expression element bodies and decorator/param-default/with-item positions, because a lambda there is still its own tokenized unit).collectguards the bijection with adebug_assert_eq!against the tokenizer count, so any future drift fails loudly instead of silently mis-anchoring.
§Borrowed AST (lifetime)
Unlike syn/swc, libcst’s inflated tree borrows &'a str slices from the
source buffer; it is not owned. So a FnUnit cannot retain an owned body — it
borrows the body suite (or lambda body expression), parameters, and decorators
from the live Module. Collection and analysis therefore run in a single
borrowed pass (PythonFrontend::analyze keeps module + src alive while it
iterates), and analyze_unit emits owned Hotspots so nothing borrowed
outlives the pass.
Structs§
- FnUnit
- A single function-shaped scope collected from the source.
Enums§
- FnBody
- The body of a function-unit: a statement suite (
def/method), a single expression (lambda), or a module’s top-level statement list (the synthetic<module>unit). Borrowed from the parsedModule.
Functions§
- collect
- Collect every
def,async def, method, nesteddef, andlambdafrommodule. - module_
init_ unit - Build a synthetic
FnUnitrepresenting a module’s top-level initialisation code — the statements that execute when the module is first imported.