#pragma once #if !defined( RIPWIRE_INGEST_TU ) #error "ingest_binds.h is a SECTION of src/ingest.cpp's translation unit - include it only from ingest.cpp (see the ingest-family split note there)" #endif #include "infra/sortutil.h" // svLess: the byte order pruneRustTombstones sorts and searches its receiver roots in // ingest_binds.h — local binding capture, moved VERBATIM from ingest.cpp in the 2026-08-29 split: // receiver classification (the member-access vocabulary, classifyReceiver/receiverOf), the P2-D // local-variable TYPE binding capture (declarator/ctor/written-type readers), the L3 // fn-pointer/callback binding layer with both r9 noise gates (assignment + declaration arms, // FnBindGateState, the pending-resolve queues), shadow-scope declaration capture (lambdas, params, // scope spans), and the BindCtx visitors (bindsVisitNode/bindsFinalize) the side-capture stream // drives. Everything that binds a NAME inside a function body to a type or a function. Same // contract as every ingest_*.h: reopens `namespace rw` and the unnamed namespace inside it — one // TU, one unnamed namespace, internal linkage unchanged, zero new API surface — under the // RIPWIRE_INGEST_TU guard. namespace rw { namespace { // ── receiver capture: the member-access vocabulary, one declarative place ──────────────────────────── // The two shapes whose receiver we inspect: C++/ObjC `field_expression` (`.argument` / `.field`) and // Python `attribute` (`.object` / `.attribute`). Named here rather than re-spelled per call site because // the depth-2 chain walk below applies exactly the same three questions twice, one level apart. // Ruby: tree-sitter-ruby has no member-access node of its own — `recv.m(args)` IS the `call` node, with // `receiver:` / `method:` fields, and a receiver-less `m(args)` is the same node kind with no `receiver:` // field. So `call` is the member-access node for Ruby and a null receiver is the bare shape — receiverOf's // existing null-receiver return already reads that as RecvKind::None (test/rubyscopecheck.sh, Rule 1 arms). // DISCLOSED GAP, not an oversight: Kotlin is absent here. `A.f()` parses to a `navigation_expression` // (verified against the vendored grammar), which this function does not recognize, so EVERY Kotlin // call site — bare `f()` and explicitly-qualified `A.f()` alike — classifies RecvKind::None. The // practical cost: an explicit receiver that WOULD narrow a same-name collision (two Kotlin // classes/objects each defining `f`, called as `A.f()` vs `B.f()`) currently does not — both stay // candidates, same as a genuinely bare call. graph.h's JVM bridge doesn't cause this (it is real and // present for Kotlin-only collisions too, no Java involved); adding this is a real feature — a // navigation_expression arm here plus a Kotlin case in memberAccessReceiver/memberAccessField below // — not a bug fix, and is out of scope for this port. inline bool isMemberAccessNode( const char* t, Lang lang ) noexcept { if( lang == Lang::Cpp || lang == Lang::ObjC ) { return kindIs( t, "field_expression" ); } if( lang == Lang::Python ) { return kindIs( t, "attribute" ); } if( lang == Lang::Ruby ) { return kindIs( t, "call" ); } return false; } inline TSNode memberAccessReceiver( TSNode access, Lang lang ) noexcept { if( lang == Lang::Python ) { return fieldChild( access, NodeField::Object ); } if( lang == Lang::Ruby ) { return fieldChild( access, NodeField::Receiver ); } return fieldChild( access, NodeField::Argument ); } inline TSNode memberAccessField( TSNode access, Lang lang ) noexcept { if( lang == Lang::Python ) { return fieldChild( access, NodeField::Attribute ); } if( lang == Lang::Ruby ) { return fieldChild( access, NodeField::Method ); } return fieldChild( access, NodeField::Field ); } // The classified receiver of one call site. `var` is set for NamedVar / FieldOfVar, `field` for // FieldOfThis / FieldOfVar; both "" for None / ThisObj. `viaArrow`: the call's own member access was written // `->` (C++/ObjC) — set by receiverOf only, which is the one caller that holds that access. struct RecvShape { RecvKind kind = RecvKind::None; std::string var; std::string field; bool viaArrow = false; bool member = false; // FE-A: Go/JS/TS/Rust — the callee is a member access's field (model.h Reference::memberCall); // FE-B adds Java, Kotlin, C# and Swift, whose member calls recorded no shape at all std::string root; // FE-A: that access's receiver-chain root identifier, "" when not an identifier std::string path; // FE-B: the member names between root and the callee, '.'-joined (model.h Reference::memberPath) std::string ctor; // FE-B: the class a constructed receiver names (model.h Reference::memberCtor) }; // FE-A (model.h Reference::memberRoot): the ROOT identifier of a Go/JS/TS/Rust receiver chain — `crypto` for // `crypto.subtle`, `this` for `this.a`, the package alias for `pkg.F`. Walks only member accesses (`a.b.c` → // `a`); any other receiver node (a call, `new X()`, a subscript, a literal, a parenthesized expression) has no // root identifier and answers "". `self` in Rust and `this` in JS answer their own spelling, so a consumer can // tell them from a package or a global object. Bounded by the chain's depth, which the tree bounds. inline std::string memberChainRoot( TSNode recv, std::string_view src ) { TSNode node = recv; for( int depth = 0; depth < 64 && !ts_node_is_null( node ); ++depth ) { const char* t = ts_node_type( node ); if( kindIs( t, "identifier" ) || kindIs( t, "this" ) || kindIs( t, "self" ) || kindIs( t, "package_identifier" ) ) { return std::string( pattern::nodeText( node, src ) ); } if( kindIs( t, "member_expression" ) ) { node = fieldChild( node, NodeField::Object ); } else if( kindIs( t, "selector_expression" ) ) { node = fieldChild( node, NodeField::Operand ); } else if( kindIs( t, "field_expression" ) ) { node = fieldChild( node, NodeField::Value ); } else { return {}; } } return {}; } // FE-B (model.h Reference::memberPath / memberCtor): one receiver chain, walked from the call's immediate receiver down to // its root, for the languages whose member calls carry no RecvKind (JS/TS, Go, Java, Kotlin, C#, Swift). `root` keeps // memberChainRoot's answer for JS/TS and Go (FE-A reads it) and adds `super` (JS/TS/Java/Kotlin/Swift; C#'s `base`), // `this`/`self` spellings of the other languages, which FE-A never sees. `path` collects the member names passed on // the way down, in source order; `ctor` is the class a constructed receiver names (`new X( … )`, Go `X{ … }` or // `&X{ … }`), final segment, with root "". Any other node (a call, a subscript, a literal) ends the walk with root "" // and an empty path. Bounded by the chain's depth like memberChainRoot. struct MemberChain { std::string root; std::string path; std::string ctor; }; // the class a constructed receiver names, final segment; "" when `node` constructs nothing this rule reads inline std::string_view constructedClass( TSNode node, Lang lang, std::string_view src ) { const char* t = ts_node_type( node ); if( ( lang == Lang::JavaScript || lang == Lang::TypeScript ) && kindIs( t, "new_expression" ) ) { return finalTypeSegment( fieldChild( node, NodeField::Constructor ), src ); } if( ( lang == Lang::Java || lang == Lang::CSharp ) && kindIs( t, "object_creation_expression" ) ) { return finalTypeSegment( fieldChild( node, NodeField::Type ), src ); } if( lang == Lang::Go && kindIs( t, "composite_literal" ) ) { return finalTypeSegment( fieldChild( node, NodeField::Type ), src ); } if( lang == Lang::Go && kindIs( t, "unary_expression" ) && ts_node_named_child_count( node ) == 1 ) { const TSNode operand = ts_node_named_child( node, 0 ); return kindIs( ts_node_type( operand ), "composite_literal" ) ? finalTypeSegment( fieldChild( operand, NodeField::Type ), src ) : std::string_view{}; } return {}; } inline MemberChain memberChainOf( TSNode recv, Lang lang, std::string_view src ) { MemberChain out; std::vector names; // collected receiver-first (innermost member last), reversed below TSNode node = recv; for( int depth = 0; depth < 64 && !ts_node_is_null( node ); ++depth ) { const char* t = ts_node_type( node ); if( kindIs( t, "parenthesized_expression" ) && ts_node_named_child_count( node ) == 1 ) { node = ts_node_named_child( node, 0 ); continue; } if( kindIs( t, "identifier" ) || kindIs( t, "this" ) || kindIs( t, "self" ) || kindIs( t, "package_identifier" ) || kindIs( t, "super" ) || kindIs( t, "simple_identifier" ) || kindIs( t, "this_expression" ) || kindIs( t, "super_expression" ) || kindIs( t, "self_expression" ) || kindIs( t, "base_expression" ) ) { out.root = std::string( pattern::nodeText( node, src ) ); break; } TSNode next{}; TSNode name{}; if( kindIs( t, "member_expression" ) ) // JS/TS { next = fieldChild( node, NodeField::Object ); name = fieldChild( node, NodeField::Property ); } else if( kindIs( t, "selector_expression" ) ) // Go { next = fieldChild( node, NodeField::Operand ); name = fieldChild( node, NodeField::Field ); } else if( kindIs( t, "field_expression" ) ) // Rust (train 29: `c.x.m()` is not `c.m()`) { next = fieldChild( node, NodeField::Value ); name = fieldChild( node, NodeField::Field ); } else if( kindIs( t, "field_access" ) ) // Java { next = fieldChild( node, NodeField::Object ); name = fieldChild( node, NodeField::Field ); } else if( kindIs( t, "member_access_expression" ) ) // C# { next = fieldChild( node, NodeField::Expression ); name = fieldChild( node, NodeField::Name ); } else if( kindIs( t, "navigation_expression" ) && ts_node_named_child_count( node ) == 2 ) // Kotlin / Swift { next = ts_node_named_child( node, 0 ); const TSNode suffix = ts_node_named_child( node, 1 ); name = ts_node_named_child_count( suffix ) > 0 ? ts_node_named_child( suffix, ts_node_named_child_count( suffix ) - 1 ) : TSNode{}; } else { const std::string_view ctor = constructedClass( node, lang, src ); if( ctor.empty() ) { return MemberChain{}; // a call, subscript or literal: no root identifier and no path } out.ctor = std::string( ctor ); break; } if( ts_node_is_null( next ) || ts_node_is_null( name ) ) { return MemberChain{}; } names.push_back( pattern::nodeText( name, src ) ); node = next; } if( out.root.empty() && out.ctor.empty() ) { return MemberChain{}; } for( auto it = names.rbegin(); it != names.rend(); ++it ) { if( !out.path.empty() ) { out.path.push_back( '.' ); } out.path.append( *it ); } return out; } // FE-A: the member-access node kind and its receiver field for the languages whose receiverOf records no shape (Go, JS/TS, // Rust; and C, whose only member call is a call through a function-pointer field). inline TSNode memberOnlyReceiver( TSNode parent, Lang lang ) noexcept { const char* t = ts_node_type( parent ); if( ( lang == Lang::TypeScript || lang == Lang::JavaScript ) && kindIs( t, "member_expression" ) ) { return fieldChild( parent, NodeField::Object ); } if( lang == Lang::Go && kindIs( t, "selector_expression" ) ) { return fieldChild( parent, NodeField::Operand ); } if( lang == Lang::Rust && kindIs( t, "field_expression" ) ) { return fieldChild( parent, NodeField::Value ); } if( lang == Lang::Rust && kindIs( t, "scoped_identifier" ) ) { // a PATH call: `Type::f()`, `Self::f()`, `::f()` — never bare, even when rustQualifierOf leaves the // qualifier empty (the UFCS cast form), so FE-A must not read it as a receiverless call return fieldChild( parent, NodeField::Path ); } if( lang == Lang::C && kindIs( t, "field_expression" ) ) { return fieldChild( parent, NodeField::Argument ); // `ops->open( x )`: a call through a function-pointer FIELD } // FE-B: the implicit-receiver languages whose member calls recorded no shape, so a bare `flush()` and `w.flush()` // were one reference. A receiver here makes the call a member call; its absence keeps it bare (an implicit this). if( lang == Lang::Java && kindIs( t, "method_invocation" ) ) { return fieldChild( parent, NodeField::Object ); // null for a bare `flush()` } if( lang == Lang::CSharp && kindIs( t, "member_access_expression" ) ) { return fieldChild( parent, NodeField::Expression ); } if( lang == Lang::CSharp && kindIs( t, "member_binding_expression" ) ) { const TSNode cond = parentOf( parent ); // `w?.Bump()`: the conditional access holds the receiver return ( !ts_node_is_null( cond ) && kindIs( ts_node_type( cond ), "conditional_access_expression" ) && ts_node_named_child_count( cond ) > 0 ) ? ts_node_named_child( cond, 0 ) : TSNode{}; } if( ( lang == Lang::Kotlin || lang == Lang::Swift ) && kindIs( t, "navigation_suffix" ) ) { const TSNode nav = parentOf( parent ); return ( !ts_node_is_null( nav ) && kindIs( ts_node_type( nav ), "navigation_expression" ) && ts_node_named_child_count( nav ) > 0 ) ? ts_node_named_child( nav, 0 ) : TSNode{}; } return TSNode{}; } // One receiver NODE → its RecvShape. `allowChain` is the ONE-hop bound: true at the call's immediate // receiver (a member-access receiver descends exactly one level, re-asking the same questions of its // INNER receiver), false inside that descent — so a depth-3 chain's inner member-access classifies None // and the whole chain degrades to the honest §2a split. `test/chainguardcheck.sh` arm (h) pins the // bound, and the residual it leaves, as disclosed. // Ruby's receiver node kinds that are not an (identifier), #267: `self`, and a class/module CONSTANT. // A class/module RECEIVER is its own node kind — `Calc` is (constant), `Outer::Engine` and // `::Top` are (scope_resolution) — never an (identifier). Without this helper every `Cls.m(…)` call // classified None, receiverOf stamped it FieldOfVar with an empty recvVar, and resolve.h's Rule 2c // ("the receiver token IS the type") could not fire on the one Ruby call form that carries a type. // The type name is the FINAL constant segment (`Outer::Engine` → `Engine`, `::Top` → `Top`): the same // final-segment convention Rule 2's type bindings use (`ns::Foo` → `Foo`), and the one that meets // Symbol::scope, which is the IMMEDIATE enclosing name by design (ingest_sidecap.h). A // (scope_resolution) whose `name:` child is not a (constant) is not a constant receiver and falls // through to the honest ladder. `Outer::run( 1 )` never arrives as a scope_resolution at all — // tree-sitter-ruby parses it as an ordinary (call) with a (constant) receiver, exactly like // `Outer.run( 1 )`, so both spellings narrow through the (constant) arm. test/rubyrecvnarrowcheck.sh. // The FINAL constant segment a (constant) / (scope_resolution) names — `Calc` → `Calc`, `Outer::Engine` → `Engine` — // or empty when the node is a (scope_resolution) whose `name:` is not a (constant). Callers have checked the kind. inline std::string_view rubyFinalConstant( TSNode node, std::string_view src ) { if( kindIs( ts_node_type( node ), "constant" ) ) { return pattern::nodeText( node, src ); } return fieldChildTextOfKind( node, NodeField::Name, "constant", src ); } inline bool isRubyConstantNode( TSNode node ) noexcept { const char* t = ts_node_type( node ); return kindIs( t, "constant" ) || kindIs( t, "scope_resolution" ); } // An RSpec EXAMPLE GROUP call: describe/context (and the feature/example_group and x-/f- spellings), called bare or // on `RSpec`. 1 = a group, 2 = a SHARED group (shared_examples/_for, shared_context), 0 = neither. A shared group's // body runs inside whichever group INCLUDES it, so its described_class is not its lexical parent's. inline int rspecGroupKind( TSNode call, std::string_view src ) { const TSNode recv = fieldChild( call, NodeField::Receiver ); if( !ts_node_is_null( recv ) && !( kindIs( ts_node_type( recv ), "constant" ) && pattern::nodeText( recv, src ) == "RSpec" ) ) { return 0; } const TSNode method = fieldChild( call, NodeField::Method ); if( ts_node_is_null( method ) ) { return 0; } static constexpr std::string_view kGroups[] = { "describe", "context", "feature", "example_group", "xdescribe", "fdescribe", "xcontext", "fcontext" }; static constexpr std::string_view kShared[] = { "shared_examples", "shared_examples_for", "shared_context" }; const std::string_view m = pattern::nodeText( method, src ); if( std::ranges::find( kGroups, m ) != std::end( kGroups ) ) { return 1; } return std::ranges::find( kShared, m ) != std::end( kShared ) ? 2 : 0; } // One example group's FIRST description argument, as RSpec reads it: nullopt when it is nil or a String (the parent // group's described class stands), otherwise the answer — a constant's final segment, or empty for anything else // (`describe :sym`, a variable), which names no class this tool can resolve. inline std::optional rspecGroupArgument( TSNode call, std::string_view src ) { const TSNode args = fieldChild( call, NodeField::Arguments ); const TSNode first = ts_node_is_null( args ) ? args : ts_node_named_child( args, 0 ); if( ts_node_is_null( first ) || kindIs( ts_node_type( first ), "string" ) || kindIs( ts_node_type( first ), "nil" ) ) { return std::nullopt; } return isRubyConstantNode( first ) ? rubyFinalConstant( first, src ) : std::string_view {}; } // True when the file defines a METHOD named described_class — any `:described_class` symbol (`let( :described_class )`, // `subject( :described_class )`, `define_method( :described_class )`) or `def described_class` / `def self.described_class`. // A method reaches every example in its group, so the file's sites decline (floor (e)). Conservative on purpose, but a // whole word: `:described_class_name` and `my_described_class` are other names. inline bool rubyDefinesDescribedClassMethod( std::string_view src ) noexcept { static constexpr std::string_view kName = "described_class"; for( std::size_t at = src.find( kName ); at != std::string_view::npos; at = src.find( kName, at + kName.size() ) ) { const std::size_t end = at + kName.size(); if( end < src.size() && ( namesplit::isIdentChar( src[ end ] ) || src[ end ] == '?' || src[ end ] == '!' ) ) { continue; // `described_class_name`, `described_class?` — another name } const std::string_view before = src.substr( 0, at ); if( before.ends_with( ':' ) || before.ends_with( "def " ) || before.ends_with( "def self." ) ) { return true; } } return false; } // How a Ruby node kind takes part in LOCAL scoping — Ruby's own parse-time rule, read by rubyDescribedClassIsLocal and by // captureRubyBareCalls. A Closure (a block, a lambda) keeps its locals in but sees the enclosing ones; a Wall (a def, a // class, a module) sees no outer local; the Binds* kinds bind a local — the `left:` child, the `name:` child (not a // parameter's default value), any child identifier, the `pattern:` child (`for x in`, `in x`, `v => x`), a hash // pattern's key (`in { x: }` binds x; `in { k: x }` binds x), or the named groups of a regex literal matched with `=~` // (`/(?…)/ =~ s` — Ruby binds them only for a literal on the LEFT that holds no interpolation). enum class RubyLocalRole : std::uint8_t { Closure, Wall, BindsLeft, BindsName, BindsChildren, BindsPattern, BindsKey, BindsRegexGroups }; struct RubyLocalKind { std::string_view kind; RubyLocalRole role; }; inline constexpr RubyLocalKind kRubyLocalKinds[] = { { "block", RubyLocalRole::Closure }, { "do_block", RubyLocalRole::Closure }, { "lambda", RubyLocalRole::Closure }, { "method", RubyLocalRole::Wall }, { "singleton_method", RubyLocalRole::Wall }, { "class", RubyLocalRole::Wall }, { "singleton_class", RubyLocalRole::Wall }, { "module", RubyLocalRole::Wall }, { "assignment", RubyLocalRole::BindsLeft }, { "operator_assignment", RubyLocalRole::BindsLeft }, { "optional_parameter", RubyLocalRole::BindsName }, { "keyword_parameter", RubyLocalRole::BindsName }, { "block_parameters", RubyLocalRole::BindsChildren }, { "method_parameters", RubyLocalRole::BindsChildren }, { "lambda_parameters", RubyLocalRole::BindsChildren }, { "left_assignment_list", RubyLocalRole::BindsChildren }, { "destructured_left_assignment", RubyLocalRole::BindsChildren }, { "rest_assignment", RubyLocalRole::BindsChildren }, { "destructured_parameter", RubyLocalRole::BindsChildren }, { "splat_parameter", RubyLocalRole::BindsChildren }, { "hash_splat_parameter", RubyLocalRole::BindsChildren }, { "block_parameter", RubyLocalRole::BindsChildren }, { "exception_variable", RubyLocalRole::BindsChildren }, { "for", RubyLocalRole::BindsPattern }, { "in_clause", RubyLocalRole::BindsPattern }, { "match_pattern", RubyLocalRole::BindsPattern }, { "test_pattern", RubyLocalRole::BindsPattern }, { "array_pattern", RubyLocalRole::BindsChildren }, { "find_pattern", RubyLocalRole::BindsChildren }, { "as_pattern", RubyLocalRole::BindsName }, { "keyword_pattern", RubyLocalRole::BindsKey }, { "binary", RubyLocalRole::BindsRegexGroups }, }; inline std::optional rubyLocalRole( const char* t ) noexcept { const auto it = std::ranges::find( kRubyLocalKinds, std::string_view( t ), &RubyLocalKind::kind ); return it == std::end( kRubyLocalKinds ) ? std::nullopt : std::optional( it->role ); } // The one field a BindsLeft / BindsName / BindsPattern node binds through, indexed by RubyLocalRole; Count for the rest. inline constexpr NodeField kRubyBindingField[] = { NodeField::Count, NodeField::Count, NodeField::Left, NodeField::Name, NodeField::Count, NodeField::Pattern, NodeField::Count, NodeField::Count }; static_assert( std::size( kRubyBindingField ) == std::size_t( RubyLocalRole::BindsRegexGroups ) + 1, "one row per RubyLocalRole" ); // The `key:` of a hash pair or hash-pattern pair written with no value — `{ payload: }`, `in { x: }` — or null. Ruby // reads that key as a name: the hash shorthand reads `payload`, the pattern binds `x`. inline TSNode rubyShorthandKey( TSNode pair ) noexcept { const TSNode key = fieldChild( pair, NodeField::Key ); const bool bare = ts_node_is_null( fieldChild( pair, NodeField::Value ) ) && !ts_node_is_null( key ) && kindIs( ts_node_type( key ), "hash_key_symbol" ); return bare ? key : TSNode {}; } // `/(?…)/ =~ s` binds each named group `(?…)` as a local — Ruby's rule, for a regex LITERAL on the left of `=~` // that holds no interpolation. Each name is handed to `onName`; `(?<=` / `(? inline void rubyRegexMatchGroups( TSNode binary, std::string_view src, OnName&& onName ) { const TSNode left = fieldChild( binary, NodeField::Left ); if( ts_node_is_null( left ) || !kindIs( ts_node_type( left ), "regex" ) || nodeFieldText( binary, NodeField::Operator, src ) != "=~" ) { return; } for( std::uint32_t i = 0, cc = ts_node_named_child_count( left ); i < cc; ++i ) { if( kindIs( ts_node_type( ts_node_named_child( left, i ) ), "interpolation" ) ) { return; } } const std::string_view re = pattern::nodeText( left, src ); for( std::size_t at = re.find( "(?<" ); at != std::string_view::npos; at = re.find( "(?<", at + 3 ) ) { std::size_t end = at + 3; while( end < re.size() && namesplit::isIdentChar( re[ end ] ) ) { ++end; } if( end > at + 3 && end < re.size() && re[ end ] == '>' ) { onName( re.substr( at + 3, end - at - 3 ) ); } } } // Every local `n` (whose kind plays `role`) binds: `onNode( identifier )` for a binding spelled by an (identifier) child — // that node is the binding site, never a read — and `onText( name )` for one spelled by no identifier node (a hash // pattern's `key:` shorthand, a regex named group). The one reading of kRubyLocalKinds' Binds* roles. template inline void rubyForEachBinding( TSNode n, RubyLocalRole role, std::string_view src, OnNode&& onNode, OnText&& onText ) { const auto bindIdentifier = [ & ]( TSNode c ) { if( !ts_node_is_null( c ) && kindIs( ts_node_type( c ), "identifier" ) ) { onNode( c ); } }; if( const NodeField f = kRubyBindingField[ std::size_t( role ) ]; f != NodeField::Count ) { bindIdentifier( fieldChild( n, f ) ); } else if( role == RubyLocalRole::BindsChildren ) { for( std::uint32_t i = 0, cc = ts_node_named_child_count( n ); i < cc; ++i ) { bindIdentifier( ts_node_named_child( n, i ) ); } } else if( role == RubyLocalRole::BindsKey ) { bindIdentifier( fieldChild( n, NodeField::Value ) ); // `in { k: x }` if( const TSNode key = rubyShorthandKey( n ); !ts_node_is_null( key ) ) { onText( pattern::nodeText( key, src ) ); // `in { x: }` — the key is the local } } else if( role == RubyLocalRole::BindsRegexGroups ) { rubyRegexMatchGroups( n, src, onText ); } } // True when `n`, whose kind plays `role`, itself BINDS the local described_class. inline bool rubyNodeBindsDescribedClass( TSNode n, std::optional role, std::string_view src ) { if( !role ) { return false; } static constexpr std::string_view kName = "described_class"; bool binds = false; rubyForEachBinding( n, *role, src, [ & ]( TSNode c ) { binds = binds || pattern::nodeText( c, src ) == kName; }, [ & ]( std::string_view name ) { binds = binds || name == kName; } ); return binds; } // True when a child of `n` that ENDS before byte `at` — an earlier statement, a block's or a def's parameters — binds // described_class, searched without entering a nested block, lambda, def, class or module, whose locals never reach // past it. `stack` is the caller's scratch, so a hostile nesting depth costs heap, not stack. inline bool rubyEarlierChildBindsDescribedClass( TSNode n, std::uint32_t at, std::string_view src, std::vector& stack ) { stack.clear(); const std::uint32_t cc = ts_node_named_child_count( n ); for( std::uint32_t i = 0; i < cc && ts_node_end_byte( ts_node_named_child( n, i ) ) <= at; ++i ) { stack.push_back( ts_node_named_child( n, i ) ); } while( !stack.empty() ) { const TSNode cur = stack.back(); stack.pop_back(); const std::optional role = rubyLocalRole( ts_node_type( cur ) ); if( rubyNodeBindsDescribedClass( cur, role, src ) ) { return true; } if( role == RubyLocalRole::Closure || role == RubyLocalRole::Wall ) { continue; // a nested scope's locals never reach the site } const std::uint32_t kids = ts_node_named_child_count( cur ); for( std::uint32_t i = 0; i < kids; ++i ) { stack.push_back( ts_node_named_child( cur, i ) ); } } return false; } // True when the (identifier) `site` reads a LOCAL named described_class, not RSpec's method — Ruby's own rule: a local // is visible after its binding, in its own scope and in the blocks nested inside it. So the walk climbs from the site, // searching each enclosing node's earlier children, and stops after the first def, class or module. An enclosing // assignment counts too: in `described_class = described_class.m` the right side already reads the local. Floor (e). inline bool rubyDescribedClassIsLocal( TSNode site, std::string_view src ) { const std::uint32_t at = ts_node_start_byte( site ); std::vector stack; for( TSNode n = parentOf( site ); !ts_node_is_null( n ); n = parentOf( n ) ) { const std::optional role = rubyLocalRole( ts_node_type( n ) ); if( rubyNodeBindsDescribedClass( n, role, src ) || rubyEarlierChildBindsDescribedClass( n, at, src, stack ) ) { return true; } if( role == RubyLocalRole::Wall ) { break; // a def, class or module: no outer local reaches in } } return false; } // RSpec's `described_class` — a bare (identifier) receiver no binding names — read as the constant it IS. RSpec's // own rule (rspec-core 3.13, Metadata::ExampleGroupHash#described_class): a group's described class is its FIRST // description argument unless that is nil or a String; otherwise it is the parent group's. So the walk climbs the // enclosing calls whose BLOCK holds the site (the child it came from is a do_block/block), and the innermost example // group with a constant first argument answers. A string or absent argument passes outward; any other argument // (`describe :sym`, a variable) and a shared group stop the walk with no answer, and the site is left exactly as it // was. test/rubydescribedclasscheck.sh. rspecDescribingGroup is the climb: the group that answers, or a null node. inline TSNode rspecDescribingGroup( TSNode node, std::string_view src ) { TSNode prev = node; for( TSNode n = parentOf( node ); !ts_node_is_null( n ); prev = n, n = parentOf( n ) ) { const char* pt = ts_node_type( prev ); if( !kindIs( ts_node_type( n ), "call" ) || !( kindIs( pt, "do_block" ) || kindIs( pt, "block" ) ) ) { continue; // not a call, or the site is in its receiver/arguments rather than its block } const int group = rspecGroupKind( n, src ); if( group == 2 ) { return TSNode {}; // a shared group: its body runs in whichever group includes it } if( group == 1 && rspecGroupArgument( n, src ) ) { return n; } } return TSNode {}; } inline std::string_view rspecDescribedClass( TSNode node, std::string_view src ) { const TSNode group = rspecDescribingGroup( node, src ); return ts_node_is_null( group ) ? std::string_view {} : rspecGroupArgument( group, src ).value_or( std::string_view {} ); } // The described class AS WRITTEN (parser version 137): the describing group's constant argument whole // (`BankIntegration::Sync`, where rspecDescribedClass answers its final segment); empty when that answers none. inline std::string_view rspecDescribedPath( TSNode node, std::string_view src ) { const TSNode group = rspecDescribingGroup( node, src ); const TSNode args = ts_node_is_null( group ) ? group : fieldChild( group, NodeField::Arguments ); const TSNode first = ts_node_is_null( args ) ? args : ts_node_named_child( args, 0 ); return !ts_node_is_null( first ) && rubyIsConstantChain( first ) ? pattern::nodeText( first, src ) : std::string_view {}; } // ─── Ruby's BARE-WORD call (test/rubybarecallcheck.sh) ──────────────────────────────────────────────────────────────── // `full_name`, `render_profile`, the `items` of `items.sum`: an identifier with no receiver, no arguments and no // parentheses parses as a plain (identifier) — the node a local read is — so queries/ruby/tags.scm cannot capture it, // and one Ruby call site in five minted no reference at all. Ruby's parser tells the two apart LEXICALLY, and // captureRubyBareCalls applies that rule: an identifier is a local exactly when a binding of its name (kRubyLocalKinds' // Binds* roles) precedes it in its own scope or in an enclosing Closure's, up to the nearest Wall; every other bare // identifier is a method call on self (Prism: CallNode#variable_call?). It becomes a receiver-less call reference, the // shape resolve.h's Rule 1 and its base walk already read as an implicit-self send. // // The bindings land when the walk reaches the BINDING node, before its children, so `x = x` reads the local on its // right (Ruby's own reading) and a parameter's default value sees the parameters bound before it. A hash or keyword- // argument shorthand (`{ payload: }`) reads `payload` and is a call when no local has that name. Inside a block or // lambda with no parameter list, `it` and `_1`…`_9` are its implicit parameters (Ruby 3.4). // // RSpec: the let family DEFINES a method on its example group, visible to the whole group — before the line that // defines it, in nested groups, and through a `def` inside the group (a method calling a method) — and every example // group defines `subject`. Those names bind like locals over the group's block, stopped only by a class/module wall, so a // spec's `user` never reaches an application `def user`. They are not minted as definitions: a let is reachable only // from its group, and a definition application code could name would turn unique application calls into declines. // // Floors, pinned by the gate: `defined?( x )` calls nothing and is not walked; a shared context's lets are unknown in // the including file; run-time locals (`binding.local_variable_set`, `eval`) are invisible; and inside // `instance_eval`/`class_eval` blocks the call still resolves against the lexical class, as a parenthesised `m()` does. inline constexpr std::string_view kRubyLetFamily[] = { "let", "let!", "subject", "subject!", "let_it_be", "let_it_be_with_reload", "let_it_be_with_refind" }; // The local names visible at the walk's position: one frame per open Ruby scope, each name a stack of the frames that // bind it (deepest last), so a lookup is one hash probe whatever the scope's width. struct RubyLocalScopes { struct Frame { std::uint32_t endByte; // the scope node's end: the frame closes at the first node starting at or past it std::uint32_t wall; // index of the innermost Wall frame — this one, when it is a Wall std::uint32_t classWall; // index of the innermost class/module/`class <<`/file frame: where a let stops std::uint32_t firstBound; // this frame's names in `bound` start here bool implicitParams; // a block or lambda with no parameter list }; struct Entry { std::uint32_t depth; bool let; }; // one frame binding a name: a local, or a let std::vector frames; std::vector bound; HashMap> names; void push( std::uint32_t endByte, bool wall, bool classLike, bool implicitParams ) { const std::uint32_t depth = std::uint32_t( frames.size() ); const Frame outer = frames.empty() ? Frame {} : frames.back(); frames.push_back( Frame { endByte, wall ? depth : outer.wall, classLike ? depth : outer.classWall, std::uint32_t( bound.size() ), implicitParams } ); } // close every frame whose node ended at or before `atByte` — never the file's own void closeEndedBy( std::uint32_t atByte ) { while( frames.size() > 1 && frames.back().endByte <= atByte ) { for( std::size_t i = frames.back().firstBound; i < bound.size(); ++i ) { names.find( bound[ i ] )->second.pop_back(); // a name's deepest entry is the innermost frame's } bound.resize( frames.back().firstBound ); frames.pop_back(); } } void bind( std::string_view name, bool let ) { const std::uint32_t depth = std::uint32_t( frames.size() - 1 ); rw::SmallVec& entries = names[ name ]; if( entries.empty() || entries.back().depth != depth || entries.back().let != let ) { entries.push_back( Entry { depth, let } ); bound.push_back( name ); } } // a local up to the innermost wall, a let up to the innermost class wall, or a block's implicit parameter bool visible( std::string_view name ) const { const Frame& top = frames.back(); if( const auto it = names.find( name ); it != names.end() ) { for( auto e = it->second.rbegin(); e != it->second.rend() && e->depth >= top.classWall; ++e ) { if( e->let || e->depth >= top.wall ) { return true; } } } const bool implicitName = name == "it" || ( name.size() == 2 && name[ 0 ] == '_' && name[ 1 ] >= '1' && name[ 1 ] <= '9' ); // `it`, `_1`…`_9` return implicitName && std::any_of( frames.begin() + top.wall, frames.end(), []( const Frame& f ) { return f.implicitParams; } ); } }; // The first argument of a call when it is a plain symbol — `:user` → user — or empty. inline std::string_view rubyFirstSymbolArgument( TSNode call, std::string_view src ) { const TSNode args = fieldChild( call, NodeField::Arguments ); const TSNode first = ts_node_is_null( args ) ? args : ts_node_named_child( args, 0 ); if( ts_node_is_null( first ) || !kindIs( ts_node_type( first ), "simple_symbol" ) ) { return {}; } const std::string_view sym = pattern::nodeText( first, src ); return sym.substr( sym.empty() ? 0 : 1 ); } // The name one statement of an example group's body defines, or empty: a receiver-less let-family call, named by its // first simple_symbol argument — a nameless `subject { … }` defines `subject`. inline std::string_view rubyLetFamilyName( TSNode c, std::string_view src ) { if( !kindIs( ts_node_type( c ), "call" ) || !ts_node_is_null( fieldChild( c, NodeField::Receiver ) ) ) { return {}; } const std::string_view m = fieldChildTextOfKind( c, NodeField::Method, "identifier", src ); if( std::ranges::find( kRubyLetFamily, m ) == std::end( kRubyLetFamily ) ) { return {}; } const std::string_view name = rubyFirstSymbolArgument( c, src ); return name.empty() && m.starts_with( "subject" ) ? std::string_view( "subject" ) : name; } // The block a Ruby call is given — its last named child (tree-sitter-ruby's `block:` field) — or null. inline TSNode rubyCallBlock( TSNode call ) { const std::uint32_t cc = ts_node_named_child_count( call ); const TSNode last = cc == 0 ? TSNode {} : ts_node_named_child( call, cc - 1 ); return !ts_node_is_null( last ) && ( kindIs( ts_node_type( last ), "do_block" ) || kindIs( ts_node_type( last ), "block" ) ) ? last : TSNode {}; } // The block of an RSpec example-group call, or null. inline TSNode rspecGroupBlock( TSNode call, std::string_view src ) { return rspecGroupKind( call, src ) == 0 ? TSNode {} : rubyCallBlock( call ); } // Is `c` a child of `n` that NAMES something — a method, an alias — rather than reading a value? `call`'s `method:` is // the tags query's capture already; a def's `name:` and `object:`, a setter's name and alias/undef operands read nothing. inline bool rubyNamingChild( TSNode n, const char* t, TSNode c ) noexcept { if( kindIs( t, "call" ) ) { return ts_node_eq( c, fieldChild( n, NodeField::Method ) ); } if( kindIs( t, "method" ) || kindIs( t, "singleton_method" ) ) { return ts_node_eq( c, fieldChild( n, NodeField::Name ) ) || ts_node_eq( c, fieldChild( n, NodeField::Object ) ); } return kindIs( t, "setter" ) || kindIs( t, "alias" ) || kindIs( t, "undef" ); } // FE-B on #373: a Ruby receiver the code builds, typed in the reference's own fields (model.h rubyTypedRecvOf) inline void rubyTypeReceiver( RawRef& ref, std::string_view type, std::string_view via, bool factory ) { ref.recvVar.clear(); ref.setMemberCtor( type ); ref.setMemberVia( via ); ref.memberFactory = factory; } // ─── Ruby's TYPED receiver (test/rubytypedrecvcheck.sh, parser version 132) ─────────────────────────────────────────── // A receiver the code BUILDS is an instance of one class, and Ruby's method lookup on that class decides which `def` a call // on it reaches (graph.h rubyTypedReceiver). What builds one is read from what the expression IS: `Const.new( … )` and the // Active Record finders on a constant (kRubyTypedFinders — `User.find_by( … )` returns a User), `described_class.new` // inside an example group that describes a constant, and a FactoryBot build — `create`/`build`/`build_stubbed( :user, … )`, // receiver-less inside an example group or on `FactoryBot` anywhere — whose class only the tree's factory definitions // know (captureRubyFactories). A call's receiver is typed when it is one of those written in place, a local whose every // binding in its scope builds the same one, or the let/subject RSpec runs for that name there — the innermost definition, // a nested group's override included — where a group with no subject of its own has its parent's, and at the top // `described_class.new`. RubyBareCallWalk reads all of it on the walk it already makes, beside the locals it tracks, and // typeReceivers writes each type into its call reference (rubyTypeReceiver above; model.h rubyTypedRecvOf). Floors (test/rubytypedrecvcheck.sh): // control flow is not read (one binding of another shape anywhere in the scope, a parameter or a block parameter // included, leaves the local untyped); instance variables, return values and a shared context's lets are not typed. // RSpec's own builders are read the same way (test/rubyrspectargetcheck.sh, parser version 133): inside an example // group a receiver-less `expect( v )`, `allow( v )`, `expect_any_instance_of( K )`, `allow_any_instance_of( K )` and a // bare `is_expected` build one of RSpec's target classes (model.h kRspecTargets; `expect { … }` a BlockExpectationTarget), // so `expect( v ).to` is a call on RSpec's object. Not read: outside a group (a helper module a `config.include` mixes // in). A matcher builder (`receive`, `change`, …) is one of them too, and a call on a chain rooted at one — the `to` of // `change { }.from( a ).to( b )`, `.and_return` after `receive( :m ).with( 1 )` — is typed as the root, through at most // kRubyMatcherChainLinks links (rubyMatcherChainType, parser version 136). inline constexpr std::string_view kRubyTypedFinders[] = { "create", "create!", "find", "find_by", "find_by!", "find_or_create_by", "find_or_create_by!", "find_or_initialize_by", "find_sole_by", "first", "first!", "last", "last!", "new", "sole", "take", "take!" }; inline constexpr std::string_view kRubyFactoryBuilds[] = { "build", "build_stubbed", "create" }; inline constexpr std::size_t kRubyMatcherChainLinks = 8; // a chain is short; a hostile one costs a bounded walk // What one value builds: a class's constant as written, or a factory name (`factory`), and the method that built it. Empty // `type` = untyped. Views into the file's source. struct RubyValueType { std::string_view type; std::string_view via; bool factory = false; bool operator==( const RubyValueType& ) const = default; }; // Where a value is read: inside an example group's block (a receiver-less `create( :user )` is FactoryBot's there), and // the class `described_class` names at the site — empty when no constant-described group encloses it or the file // redefines the name. The walk keeps both per frame, so neither costs a climb up the tree. struct RubyValueSite { bool inGroup = false; std::string_view described; }; // What a FactoryBot build `v` to `m` builds — its factory, named by its first symbol argument — when `m` is a build // (kRubyFactoryBuilds). inline RubyValueType rubyFactoryBuildType( TSNode v, std::string_view m, std::string_view src ) { const bool build = std::ranges::find( kRubyFactoryBuilds, m ) != std::end( kRubyFactoryBuilds ); const std::string_view factory = build ? rubyFirstSymbolArgument( v, src ) : std::string_view {}; return factory.empty() ? RubyValueType {} : RubyValueType { factory, m, true }; } // What a receiver-less call `v` to `m` builds inside an example group: one of RSpec's targets (model.h kRspecTargets — // `expect { … }`, a block and no argument, is the block form), else a FactoryBot build (`create( :user )`). A bare word // (`is_expected`) is such a call with no arguments. inline RubyValueType rubyGroupBuildType( TSNode v, std::string_view m, std::string_view src ) { const auto target = std::ranges::find( kRspecTargets, m, &RspecTarget::builder ); if( target == std::end( kRspecTargets ) ) { return rubyFactoryBuildType( v, m, src ); } const bool block = kindIs( ts_node_type( v ), "call" ) && ts_node_is_null( fieldChild( v, NodeField::Arguments ) ) && !ts_node_is_null( rubyCallBlock( v ) ); return RubyValueType { m == "expect" && block ? kRspecBlockTarget : target->cls, m, false }; } // What a matcher chain `v` inside an example group builds: the class of the RSpec builder at its root — the receiver-less // call fewer than kRubyMatcherChainLinks receivers below `v`, so a call on `v` is at most that many links from it — whose // every link is RSpec's; untyped for any other root (a FactoryBot build's methods return anything). inline RubyValueType rubyMatcherChainType( TSNode v, std::string_view src ) { TSNode link = v; for( std::size_t depth = 0; depth < kRubyMatcherChainLinks && !ts_node_is_null( link ) && kindIs( ts_node_type( link ), "call" ); ++depth ) { const TSNode recv = fieldChild( link, NodeField::Receiver ); if( ts_node_is_null( recv ) ) { const RubyValueType root = rubyGroupBuildType( link, fieldChildTextOfKind( link, NodeField::Method, "identifier", src ), src ); return root.factory ? RubyValueType {} : root; } link = recv; } return {}; } // What a call `v` to `m` on `recv` builds, read at `site`: a FactoryBot build on `FactoryBot`, a finder on any other // constant — the constant AS WRITTEN (`OpenSSL::Cipher`): graph.h reads a qualified one only when the tree opens that // path — `described_class.new`, or a link of a matcher chain in an example group. inline RubyValueType rubyReceiverBuildType( TSNode v, TSNode recv, std::string_view m, std::string_view src, const RubyValueSite& site ) { const std::string_view on = isRubyConstantNode( recv ) ? rubyFinalConstant( recv, src ) : std::string_view {}; if( ( on == "FactoryBot" || on == "FactoryGirl" ) && std::ranges::find( kRubyFactoryBuilds, m ) != std::end( kRubyFactoryBuilds ) ) { return rubyFactoryBuildType( v, m, src ); } if( !on.empty() ) { const bool finder = std::ranges::find( kRubyTypedFinders, m ) != std::end( kRubyTypedFinders ); return finder ? RubyValueType { pattern::nodeText( recv, src ), m, false } : RubyValueType {}; } const bool describedNew = m == "new" && kindIs( ts_node_type( recv ), "identifier" ) && pattern::nodeText( recv, src ) == "described_class"; if( describedNew ) { return site.described.empty() ? RubyValueType {} : RubyValueType { site.described, m, false }; } return site.inGroup ? rubyMatcherChainType( v, src ) : RubyValueType {}; } // What the value expression `v` builds (the section note above), read at `site`. inline RubyValueType rubyValueType( TSNode v, std::string_view src, const RubyValueSite& site ) { const bool word = !ts_node_is_null( v ) && kindIs( ts_node_type( v ), "identifier" ); // a bare `is_expected` if( !word && ( ts_node_is_null( v ) || !kindIs( ts_node_type( v ), "call" ) ) ) { return {}; } const std::string_view m = word ? pattern::nodeText( v, src ) : fieldChildTextOfKind( v, NodeField::Method, "identifier", src ); const TSNode recv = word ? TSNode {} : fieldChild( v, NodeField::Receiver ); if( ts_node_is_null( recv ) ) { return site.inGroup ? rubyGroupBuildType( v, m, src ) : RubyValueType {}; } return rubyReceiverBuildType( v, recv, m, src, site ); } // What a let/subject call's block returns — its body's last statement — or null. inline TSNode rubyLetValue( TSNode letCall ) { const TSNode block = rubyCallBlock( letCall ); const TSNode body = ts_node_is_null( block ) ? block : fieldChild( block, NodeField::Body ); TSNode last {}; if( !ts_node_is_null( body ) ) { ChildCursor cursor( body ); // O(children): a body is as wide as its comments (src/infra/tschildren.h) forEachNamedChild( body, cursor.cur, [ & ]( TSNode c ) { last = kindIs( ts_node_type( c ), "comment" ) ? last : c; return true; } ); } return last; } // ─── Ruby's DECLARED calls (test/rubydeclrefcheck.sh, parser version 134) ───────────────────────────────────────────── // A Rails declaration names, by SYMBOL, a method the framework calls later on the class's instances: `before_action // :authenticate`, `after_save :reindex`, `validate :name_present`, an `if:` / `unless:` condition, `rescue_from …, with: // :not_found`. Nothing in the source calls the method, so each symbol is read as a call to it — a receiver-less call from // the class body, whose self's lookup is the class's (graph.h RubySelfReach) — when the macro sits at class-body position // (receiver-less, the innermost def-or-class wall a class or module body: a concern's `included do` block counts, a def // does not). `send`/`public_send`/`__send__`/`try`/`try!`/`method` with a literal symbol call that method on the same // receiver: the call's own reference, renamed (RubyBareCallWalk::sendTargets). Floors (test/rubydeclrefcheck.sh): option // values that are not calls (`only:`, `except:`, `on:`), `validates`' attribute names, `skip_*_action`, `set_callback`, a // string or computed name, and a callback macro inside a def make none. inline constexpr std::string_view kRubyCallbackMacros[] = { // ActionController, ActionMailer, AbstractController "before_action", "after_action", "around_action", "prepend_before_action", "prepend_after_action", "prepend_around_action", "append_before_action", "append_after_action", "append_around_action", "before_filter", "after_filter", "around_filter", // ActiveModel, ActiveRecord "before_validation", "after_validation", "before_save", "around_save", "after_save", "before_create", "around_create", "after_create", "before_update", "around_update", "after_update", "before_destroy", "around_destroy", "after_destroy", "after_commit", "after_rollback", "after_create_commit", "after_update_commit", "after_destroy_commit", "after_save_commit", "after_initialize", "after_find", "after_touch", "validate", // ActiveJob, ActionMailer delivery, ActionCable channels "before_enqueue", "around_enqueue", "after_enqueue", "before_perform", "around_perform", "after_perform", "after_discard", "before_deliver", "around_deliver", "after_deliver", "before_subscribe", "after_subscribe", "before_unsubscribe", "after_unsubscribe" }; inline constexpr std::string_view kRubySendCalls[] = { "__send__", "method", "public_send", "send", "try", "try!" }; // What a declaration's symbols name: a callback's positional symbols and its conditions, only the conditions (a // validation's positional symbols are attributes), or rescue_from's `with:` handler. enum class RubyDeclared : std::uint8_t { None, Callback, Conditions, Rescue }; inline RubyDeclared rubyDeclaredKind( std::string_view m ) noexcept { if( std::ranges::find( kRubyCallbackMacros, m ) != std::end( kRubyCallbackMacros ) ) { return RubyDeclared::Callback; } if( m == "validates" || m == "validates_with" || ( m.starts_with( "validates_" ) && m.ends_with( "_of" ) ) ) { return RubyDeclared::Conditions; } return m == "rescue_from" ? RubyDeclared::Rescue : RubyDeclared::None; } // Is the option key `key` (`if:` or the older `:if =>`) one whose value a `kind` declaration calls? inline bool rubyDeclaredOption( TSNode key, RubyDeclared kind, std::string_view src ) noexcept { std::string_view k = ts_node_is_null( key ) ? std::string_view {} : pattern::nodeText( key, src ); k = k.substr( k.starts_with( ':' ) ? 1 : 0 ); k = k.substr( 0, k.size() - ( k.ends_with( ':' ) ? 1 : 0 ) ); return k == "if" || k == "unless" || ( kind == RubyDeclared::Rescue && k == "with" ); } // Each method symbol a `kind` declaration's arguments name, in source order (the section note above), handed to `fn`. template inline void rubyForEachDeclaredSymbol( TSNode call, RubyDeclared kind, std::string_view src, Fn&& fn ) { const TSNode args = fieldChild( call, NodeField::Arguments ); if( ts_node_is_null( args ) ) { return; } const auto symbol = []( TSNode n ) { return kindIs( ts_node_type( n ), "simple_symbol" ); }; ChildCursor cursor( args ); // O(children) (src/infra/tschildren.h) forEachNamedChild( args, cursor.cur, [ & ]( TSNode a ) { const TSNode value = kindIs( ts_node_type( a ), "pair" ) && rubyDeclaredOption( fieldChild( a, NodeField::Key ), kind, src ) ? fieldChild( a, NodeField::Value ) : TSNode {}; if( kind == RubyDeclared::Callback && symbol( a ) ) { fn( a ); } else if( !ts_node_is_null( value ) && symbol( value ) ) { fn( value ); } else if( !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "array" ) ) { ChildCursor inner( value ); forEachNamedChild( value, inner.cur, [ & ]( TSNode e ) { if( symbol( e ) ) { fn( e ); } return true; } ); } return true; } ); } // ── Subscript = operator-method call (RefRole::Subscript; queries/{ruby,python}/tags.scm @reference.subscript) ─────────── // An element access `recv[ args ]` runs the receiver class's subscript OPERATOR METHOD. Which method is the language's // own spelling, kept HERE as data rather than per-language code: the read form, and the form an assignment's target runs. // C++ `operator[]` could ride the same row and the same graph.h proof (its Rules 1/2/2b type `this`, a local and a field); // it is not armed: its element accesses are mostly builtin arrays, pointers and std containers, whose answers this lane // keeps byte-identical. Every other indexed language has no user-definable subscript method (C, Go, Java, Bash, Lua) // or spells it otherwise (C# indexers `this[ ]`, Swift `subscript`, Rust `Index::index`, Kotlin `operator fun get`, // JS/TS proxies) and records nothing for it. struct SubscriptOperatorNames { Lang lang; std::string_view read; // `recv[ k ]` std::string_view write; // `recv[ k ] = v` }; inline constexpr SubscriptOperatorNames kSubscriptOperatorMethods[] = { { Lang::Python, "__getitem__", "__setitem__" }, { Lang::Ruby, "[]", "[]=" }, }; inline const SubscriptOperatorNames* subscriptOperatorNamesOf( Lang lang ) noexcept { const auto* const it = std::ranges::find( kSubscriptOperatorMethods, lang, &SubscriptOperatorNames::lang ); return it == std::ranges::end( kSubscriptOperatorMethods ) ? nullptr : it; } // The operator method element access `access` runs: the write form when it is an assignment's whole target (`h[ k ] = v`), // "" where no one name is right — a target inside a multiple assignment (`a[ 0 ], b = …`), a `del x[ k ]` (Python's // `__delitem__`, not read), an annotation (`x: Dict[ str, int ]`, a type, not a value). A compound assignment // (`h[ k ] += 1`, `h[ k ] ||= v`) runs both forms and is recorded as the read one, the setter capture's floor. inline std::string_view subscriptOperatorName( TSNode access, const SubscriptOperatorNames& names ) noexcept { const TSNode parent = parentOf( access ); if( ts_node_is_null( parent ) ) { return names.read; } const char* pt = ts_node_type( parent ); if( kindIs( pt, "assignment" ) ) { return ts_node_eq( fieldChild( parent, NodeField::Left ), access ) ? names.write : names.read; } const bool multiTarget = kindIs( pt, "pattern_list" ) || kindIs( pt, "tuple_pattern" ) || kindIs( pt, "list_pattern" ) || kindIs( pt, "left_assignment_list" ) || kindIs( pt, "destructured_left_assignment" ); const bool notValue = kindIs( pt, "delete_statement" ) || kindIs( pt, "type" ) || kindIs( pt, "generic_type" ) || ( kindIs( pt, "expression_list" ) && kindIs( ts_node_type( parentOf( parent ) ), "delete_statement" ) ); return multiTarget || notValue ? std::string_view {} : names.read; } // One call whose receiver is typed (RubyBareCallWalk): the call node's start (its reference's startByte) and method, // whether the receiver is an (identifier) — the reference's NamedVar — or a value, and what it builds; for a local, the // scope and name whose binding types are read once the walk has seen every binding (`local` non-empty, `type` filled by // typeReceivers). struct RubyTypedSite { std::uint32_t callStart; std::string_view method; bool identReceiver; RubyValueType type; std::uint32_t wall; std::string_view local; }; // One `send`-family call naming its method by a literal symbol (RubyBareCallWalk::sendTargets): the call node's start // and method, and the symbol. struct RubySendSite { std::uint32_t callStart; std::string_view method; TSNode symbol; std::size_t ref = 0; // the call's reference, when exactly one matched (`hits` == 1) std::uint32_t hits = 0; }; // One file's bare-word calls (see the section note above): one iterative pre-order walk in source order — an explicit // stack, no recursion, so a hostile nesting depth costs heap, not stack. class RubyBareCallWalk { public: RubyBareCallWalk( std::uint32_t fileId, std::string_view src, std::vector& refs ) : fileId_( fileId ), src_( src ), refs_( refs ) { scopes_.names.reserve( 64 ); stack_.reserve( 64 ); kids_.reserve( 16 ); } void run( TSNode root ); void typeReceivers( std::size_t firstRef, std::size_t endRef ); // after run: write each typed receiver into its call reference void sendTargets( std::size_t firstRef, std::size_t endRef ); // after typeReceivers: each `send( :m )`'s call to m private: struct Item { TSNode node; bool naming; // an identifier in a naming or binding position: it reads nothing bool groupBlock; // the block of an RSpec example-group call }; void visit( const Item& item, ChildCursor& cursor ); void enter( TSNode n, const char* t, RubyLocalRole role, bool groupBlock ); void bindGroupLets( TSNode block, bool groupBlock ); void pushChildren( TSNode n, const char* t, ChildCursor& cursor ); void emit( TSNode at, std::string_view name ); void noteFrame( bool groupBlock, TSNode block ); RubyValueSite valueSite() const; void noteLocalTypes( TSNode n, RubyLocalRole role ); void noteLetType( TSNode letCall, std::string_view name ); void noteImplicitSubject(); void noteTypedCall( TSNode call ); void noteTypedSubscript( TSNode access ); void noteTypedSend( std::uint32_t at, TSNode recv, std::string_view method ); void noteDeclaredCall( TSNode call ); void noteNamedReceiver( std::uint32_t at, std::string_view method, std::string_view name ); const RubyTypedSite* siteOf( const RawRef& r ) const; const std::string& typeKey( std::uint32_t frame, std::string_view name ); std::uint32_t fileId_; std::string_view src_; std::vector& refs_; RubyLocalScopes scopes_; std::vector stack_; std::vector kids_; std::vector binding_; // the visited node's binding identifiers: its children marked `naming` std::vector sendSites_; // typed receivers (the section note above): per open frame depth, a serial naming that frame and whether it is inside // an example group; "#" → what a let defines in that group / what every binding of a local in that // scope builds (a tombstone once two disagree); the group frames whose own subject is explicit; the typed call sites std::vector frameSerial_; std::vector frameInGroup_; std::vector frameDescribed_; // the class described_class names in the frame (rspecDescribedClass's rule) bool describedRedefined_ = false; // the file defines a method named described_class std::uint32_t nextSerial_ = 0; HashMap letTypes_; HashMap localTypes_; HashMap explicitSubjects_; std::vector typedSites_; std::string key_; }; inline void RubyBareCallWalk::run( TSNode root ) { ChildCursor cursor( root ); scopes_.push( ts_node_end_byte( root ), true, true, false ); describedRedefined_ = rubyDefinesDescribedClassMethod( src_ ); noteFrame( false, root ); stack_.push_back( Item { root, false, false } ); while( !stack_.empty() ) { const Item item = stack_.back(); stack_.pop_back(); visit( item, cursor ); } } inline void RubyBareCallWalk::visit( const Item& item, ChildCursor& cursor ) { const TSNode n = item.node; const char* t = ts_node_type( n ); scopes_.closeEndedBy( ts_node_start_byte( n ) ); if( kindIs( t, "identifier" ) ) { if( !item.naming ) { emit( n, pattern::nodeText( n, src_ ) ); } return; } if( kindIs( t, "unary" ) && pattern::nodeText( n, src_ ).starts_with( "defined?" ) ) { return; // floor (a): defined?( x ) asks, and calls nothing } if( const TSNode key = kindIs( t, "pair" ) ? rubyShorthandKey( n ) : TSNode {}; !ts_node_is_null( key ) ) { emit( key, pattern::nodeText( key, src_ ) ); // `{ payload: }` reads payload } if( kindIs( t, "call" ) ) { noteTypedCall( n ); noteDeclaredCall( n ); } else if( kindIs( t, "element_reference" ) ) { noteTypedSubscript( n ); } binding_.clear(); if( const std::optional role = rubyLocalRole( t ) ) { enter( n, t, *role, item.groupBlock ); } pushChildren( n, t, cursor ); } // a scope node opens its frame (and an example group's block binds its lets); a binding node binds its names inline void RubyBareCallWalk::enter( TSNode n, const char* t, RubyLocalRole role, bool groupBlock ) { if( role == RubyLocalRole::Wall || role == RubyLocalRole::Closure ) { const bool closure = role == RubyLocalRole::Closure; scopes_.push( ts_node_end_byte( n ), !closure, kindIs( t, "class" ) || kindIs( t, "module" ) || kindIs( t, "singleton_class" ), closure && ts_node_is_null( fieldChild( n, NodeField::Parameters ) ) ); noteFrame( groupBlock, n ); if( closure && !kindIs( t, "lambda" ) ) { bindGroupLets( n, groupBlock ); } return; } rubyForEachBinding( n, role, src_, [ & ]( TSNode c ) { binding_.push_back( c ); scopes_.bind( pattern::nodeText( c, src_ ), false ); }, [ & ]( std::string_view name ) { scopes_.bind( name, false ); } ); noteLocalTypes( n, role ); } // every let the block's statements define, and `subject` when the block is an example group's inline void RubyBareCallWalk::bindGroupLets( TSNode block, bool groupBlock ) { if( groupBlock ) { scopes_.bind( "subject", true ); noteImplicitSubject(); } const TSNode body = fieldChild( block, NodeField::Body ); for( std::uint32_t i = 0, cc = ts_node_is_null( body ) ? 0 : ts_node_named_child_count( body ); i < cc; ++i ) { if( const std::string_view name = rubyLetFamilyName( ts_node_named_child( body, i ), src_ ); !name.empty() ) { scopes_.bind( name, true ); noteLetType( ts_node_named_child( body, i ), name ); } } } inline void RubyBareCallWalk::pushChildren( TSNode n, const char* t, ChildCursor& cursor ) { const TSNode groupBlock = kindIs( t, "call" ) ? rspecGroupBlock( n, src_ ) : TSNode {}; collectChildren( n, cursor.cur, kids_ ); for( std::size_t i = kids_.size(); i > 0; --i ) { const TSNode c = kids_[ i - 1 ]; const bool naming = rubyNamingChild( n, t, c ) || std::ranges::any_of( binding_, [ & ]( TSNode b ) { return ts_node_eq( b, c ); } ); stack_.push_back( Item { c, naming, !ts_node_is_null( groupBlock ) && ts_node_eq( c, groupBlock ) } ); } } // receiver-less: RecvKind::None, the implicit-self shape Rule 1 reads inline void RubyBareCallWalk::emit( TSNode at, std::string_view name ) { if( !name.empty() && !scopes_.visible( name ) ) { refs_.push_back( rawRefAt( at, fileId_, Lang::Ruby, RefRole::Call, std::string( name ) ) ); } } // a new frame at the top of scopes_: its serial, whether it sits in an example group, and the class described_class names // in it — rspecDescribedClass's rule, kept per frame: an example group's block takes its group's constant argument, a // String or absent one passes the parent's through, any other argument and a shared group name none; any other frame // inherits its parent's inline void RubyBareCallWalk::noteFrame( bool groupBlock, TSNode block ) { const std::size_t depth = scopes_.frames.size() - 1; if( frameSerial_.size() <= depth ) { frameSerial_.resize( depth + 1 ); frameInGroup_.resize( depth + 1 ); frameDescribed_.resize( depth + 1 ); } const std::string_view parent = depth > 0 ? frameDescribed_[ depth - 1 ] : std::string_view {}; const TSNode group = groupBlock ? parentOf( block ) : TSNode {}; const int kind = ts_node_is_null( group ) ? 0 : rspecGroupKind( group, src_ ); const std::optional argument = kind == 1 ? rspecGroupArgument( group, src_ ) : std::nullopt; frameSerial_[ depth ] = nextSerial_++; frameInGroup_[ depth ] = char( groupBlock || ( depth > 0 && frameInGroup_[ depth - 1 ] != 0 ) ); frameDescribed_[ depth ] = kind == 2 ? std::string_view {} : argument ? *argument : parent; } // the value site at the walk's position: its group, and what described_class names there unless the file or a visible // local or let redefines it inline RubyValueSite RubyBareCallWalk::valueSite() const { const std::size_t top = scopes_.frames.size() - 1; const bool redefined = describedRedefined_ || scopes_.visible( "described_class" ); return RubyValueSite { frameInGroup_[ top ] != 0, redefined ? std::string_view {} : frameDescribed_[ top ] }; } inline const std::string& RubyBareCallWalk::typeKey( std::uint32_t frame, std::string_view name ) { key_.clear(); key_.append( std::to_string( frame ) ).push_back( '#' ); key_.append( name ); return key_; } // each local `n` binds, folded into its scope's type: what an assignment (or `||=`) builds; nothing, for every other binding inline void RubyBareCallWalk::noteLocalTypes( TSNode n, RubyLocalRole role ) { const std::uint32_t wall = frameSerial_[ scopes_.frames.back().wall ]; const TSNode left = role == RubyLocalRole::BindsLeft ? fieldChild( n, NodeField::Left ) : TSNode {}; const bool assigns = !ts_node_is_null( left ) && kindIs( ts_node_type( left ), "identifier" ) && ( kindIs( ts_node_type( n ), "assignment" ) || nodeFieldText( n, NodeField::Operator, src_ ) == "||=" ); const RubyValueType built = assigns ? rubyValueType( fieldChild( n, NodeField::Right ), src_, valueSite() ) : RubyValueType {}; const auto fold = [ & ]( std::string_view name, const RubyValueType& type ) { const auto [ it, inserted ] = localTypes_.try_emplace( typeKey( wall, name ), type ); if( !inserted && !( it->second == type ) ) { it->second = {}; // two bindings build different things: untyped in the whole scope } }; rubyForEachBinding( n, role, src_, [ & ]( TSNode c ) { fold( pattern::nodeText( c, src_ ), assigns && ts_node_eq( c, left ) ? built : RubyValueType {} ); }, [ & ]( std::string_view name ) { fold( name, {} ); } ); } // a let/subject statement of the group whose frame is on top: what its block builds; a named subject is the group's subject too inline void RubyBareCallWalk::noteLetType( TSNode letCall, std::string_view name ) { const std::uint32_t group = frameSerial_[ scopes_.frames.size() - 1 ]; const RubyValueType built = rubyValueType( rubyLetValue( letCall ), src_, valueSite() ); letTypes_[ typeKey( group, name ) ] = built; if( fieldChildTextOfKind( letCall, NodeField::Method, "identifier", src_ ).starts_with( "subject" ) ) { letTypes_[ typeKey( group, "subject" ) ] = built; explicitSubjects_[ group ] = built; } } // an example group's implicit subject: the nearest enclosing group's explicit one — RSpec looks it up through the parent // groups — else `described_class.new` inline void RubyBareCallWalk::noteImplicitSubject() { const std::size_t top = scopes_.frames.size() - 1; RubyValueType built = {}; bool found = false; for( std::size_t d = top; d > scopes_.frames[ top ].classWall && !found; --d ) { const auto it = explicitSubjects_.find( frameSerial_[ d - 1 ] ); found = it != explicitSubjects_.end(); built = found ? it->second : built; } const std::string_view described = found ? std::string_view {} : valueSite().described; letTypes_[ typeKey( frameSerial_[ top ], "subject" ) ] = described.empty() ? built : RubyValueType { described, "new", false }; } // a call whose receiver is typed: a value written in place, a let RSpec runs there, or a local of the scope (read at the end) inline void RubyBareCallWalk::noteTypedCall( TSNode call ) { noteTypedSend( ts_node_start_byte( call ), fieldChild( call, NodeField::Receiver ), fieldChildTextOfKind( call, NodeField::Method, "identifier", src_ ) ); } // an element access `recv[ k ]` sends its receiver the operator method its reference names (subscriptOperatorRef), typed // the way a call's receiver is inline void RubyBareCallWalk::noteTypedSubscript( TSNode access ) { const SubscriptOperatorNames* names = subscriptOperatorNamesOf( Lang::Ruby ); ASSUME( names != nullptr, "kSubscriptOperatorMethods carries a Ruby row" ); noteTypedSend( ts_node_start_byte( access ), fieldChild( access, NodeField::Object ), subscriptOperatorName( access, *names ) ); } // the typed-receiver record of one send at `at`: `method` on `recv` inline void RubyBareCallWalk::noteTypedSend( std::uint32_t at, TSNode recv, std::string_view method ) { if( ts_node_is_null( recv ) || method.empty() ) { return; } const bool ident = kindIs( ts_node_type( recv ), "identifier" ); if( !ident || !scopes_.visible( pattern::nodeText( recv, src_ ) ) ) { // a value — or a bare word no local or let binds, which is a call on self (`is_expected`) const RubyValueType built = rubyValueType( recv, src_, valueSite() ); if( !built.type.empty() ) { typedSites_.push_back( RubyTypedSite { at, method, ident, built, 0, {} } ); } return; } noteNamedReceiver( at, method, pattern::nodeText( recv, src_ ) ); } // a call whose receiver is the bare name `name`: the innermost definition of it visible here — a let, or a local of this // scope (whose type is read at the end, once every binding is seen) — decides, typed or not inline void RubyBareCallWalk::noteNamedReceiver( std::uint32_t at, std::string_view method, std::string_view name ) { const auto it = scopes_.names.find( name ); if( it == scopes_.names.end() ) { return; // no binding: a call on self, which no let or local types } const RubyLocalScopes::Frame& top = scopes_.frames.back(); const auto visible = std::find_if( it->second.rbegin(), it->second.rend(), [ & ]( const RubyLocalScopes::Entry& e ) { return e.depth < top.classWall || e.let || e.depth >= top.wall; } ); if( visible == it->second.rend() || visible->depth < top.classWall ) { return; } const bool local = !visible->let; const std::uint32_t frame = frameSerial_[ local ? top.wall : visible->depth ]; const auto& table = local ? localTypes_ : letTypes_; const auto found = table.find( typeKey( frame, name ) ); if( found != table.end() && !found->second.type.empty() ) { typedSites_.push_back( RubyTypedSite { at, method, true, found->second, frame, local ? name : std::string_view {} } ); } } // Each typed site's local read now that every binding is seen, then each written into the call reference the tags query // made for it — the one in [firstRef, endRef) at the call's start, with its method, whose receiver kind is the site's // (an identifier receiver is NamedVar, a value receiver FieldOfVar). inline void RubyBareCallWalk::typeReceivers( std::size_t firstRef, std::size_t endRef ) { for( RubyTypedSite& site : typedSites_ ) { if( !site.local.empty() ) { const auto it = localTypes_.find( typeKey( site.wall, site.local ) ); site.type = it == localTypes_.end() ? RubyValueType {} : it->second; } } std::erase_if( typedSites_, []( const RubyTypedSite& site ) { return site.type.type.empty(); } ); std::stable_sort( typedSites_.begin(), typedSites_.end(), []( const RubyTypedSite& a, const RubyTypedSite& b ) { return a.callStart < b.callStart; } ); for( std::size_t i = firstRef; i < endRef && i < refs_.size() && !typedSites_.empty(); ++i ) { if( const RubyTypedSite* site = siteOf( refs_[ i ] ) ) { rubyTypeReceiver( refs_[ i ], site->type.type, site->type.via, site->type.factory ); } } } // A declaration's method symbols (the section note above): a callback macro's at class-body position, each a call // reference at its symbol; a `send`-family call's symbol, recorded for sendTargets inline void RubyBareCallWalk::noteDeclaredCall( TSNode call ) { const std::string_view m = fieldChildTextOfKind( call, NodeField::Method, "identifier", src_ ); const bool bare = ts_node_is_null( fieldChild( call, NodeField::Receiver ) ); const RubyLocalScopes::Frame& top = scopes_.frames.back(); const RubyDeclared kind = bare && top.wall == top.classWall ? rubyDeclaredKind( m ) : RubyDeclared::None; if( kind != RubyDeclared::None ) { rubyForEachDeclaredSymbol( call, kind, src_, [ & ]( TSNode sym ) { const std::string_view text = pattern::nodeText( sym, src_ ); refs_.push_back( rawRefAt( sym, fileId_, Lang::Ruby, RefRole::Call, std::string( text.substr( text.empty() ? 0 : 1 ) ) ) ); } ); return; } if( !m.empty() && std::ranges::find( kRubySendCalls, m ) != std::end( kRubySendCalls ) ) { const TSNode args = fieldChild( call, NodeField::Arguments ); const TSNode first = ts_node_is_null( args ) ? args : ts_node_named_child( args, 0 ); if( !ts_node_is_null( first ) && kindIs( ts_node_type( first ), "simple_symbol" ) ) { sendSites_.push_back( RubySendSite { ts_node_start_byte( call ), m, first } ); } } } // Each `send`-family call's named method, as a call on the same receiver: a copy of the call's own reference — the one // in [firstRef, endRef) at the call's start that names it, typed receiver and all — renamed and placed at the symbol. Two // same-named calls starting at one byte (`a.send( :x ).send( :y )`) are ambiguous, and neither is copied. inline void RubyBareCallWalk::sendTargets( std::size_t firstRef, std::size_t endRef ) { std::stable_sort( sendSites_.begin(), sendSites_.end(), []( const RubySendSite& a, const RubySendSite& b ) { return a.callStart < b.callStart; } ); const auto byStart = []( const RubySendSite& site, std::uint32_t at ) { return site.callStart < at; }; for( std::size_t i = firstRef; i < endRef && i < refs_.size() && !sendSites_.empty(); ++i ) { const RawRef& r = refs_[ i ]; for( auto site = std::lower_bound( sendSites_.begin(), sendSites_.end(), r.startByte, byStart ); r.role == RefRole::Call && site != sendSites_.end() && site->callStart == r.startByte; ++site ) { site->ref = site->method == r.name ? i : site->ref; site->hits += site->method == r.name ? 1u : 0u; } } for( const RubySendSite& site : sendSites_ ) { if( site.hits == 1 ) { RawRef copy = refs_[ site.ref ]; const std::string_view text = pattern::nodeText( site.symbol, src_ ); copy.name.assign( text.substr( text.empty() ? 0 : 1 ) ); copy.startByte = ts_node_start_byte( site.symbol ); copy.line = ts_node_start_point( site.symbol ).row + 1; refs_.push_back( std::move( copy ) ); } } } // the typed site a call reference was made for: the one at its start whose method it names (a setter `x.name =` names // `name=`) and whose receiver kind it records — an (identifier) receiver is NamedVar, a value receiver FieldOfVar inline const RubyTypedSite* RubyBareCallWalk::siteOf( const RawRef& r ) const { const bool ident = r.recv == RecvKind::NamedVar; if( r.lang != Lang::Ruby || ( r.role != RefRole::Call && r.role != RefRole::Subscript ) || ( !ident && r.recv != RecvKind::FieldOfVar ) ) { return nullptr; } const std::string_view name = r.name; const std::string_view bare = name.ends_with( '=' ) ? name.substr( 0, name.size() - 1 ) : name; const auto byStart = []( const RubyTypedSite& site, std::uint32_t at ) { return site.callStart < at; }; for( auto site = std::lower_bound( typedSites_.begin(), typedSites_.end(), r.startByte, byStart ); site != typedSites_.end() && site->callStart == r.startByte; ++site ) { if( site->identReceiver == ident && ( site->method == name || site->method == bare ) ) { return &*site; } } return nullptr; } // One file's bare-word calls, and the type of each call receiver the file builds — written into the file's references // already in `refs` (the tags query's: the trailing run carrying this fileId); the walk's own references follow them. inline void captureRubyBareCalls( TSNode root, std::uint32_t fileId, std::string_view src, std::vector& refs ) { const std::size_t queryRefsEnd = refs.size(); std::size_t firstRef = queryRefsEnd; while( firstRef > 0 && refs[ firstRef - 1 ].fileId == fileId ) { --firstRef; } RubyBareCallWalk walk( fileId, src, refs ); walk.run( root ); walk.typeReceivers( firstRef, queryRefsEnd ); walk.sendTargets( firstRef, queryRefsEnd ); } // `described_class::Worker` — a constant path whose head is RSpec's described_class — spelled through the path the // described class is written as (`Log::Worker`, parser version 137); empty for any other head, and where the file // redefines described_class or a local of that name reaches the head. inline std::string rubyDescribedRelativePath( TSNode node, std::string_view src ) { TSNode head = node; for( TSNode scope = fieldChild( head, NodeField::Scope ); kindIs( ts_node_type( head ), "scope_resolution" ) && !ts_node_is_null( scope ); scope = fieldChild( head, NodeField::Scope ) ) { head = scope; // the leftmost segment, one step at a time: a deep chain costs a loop, never a frame } const bool described = kindIs( ts_node_type( head ), "identifier" ) && pattern::nodeText( head, src ) == "described_class" && !rubyDefinesDescribedClassMethod( src ) && !rubyDescribedClassIsLocal( head, src ); const std::string_view path = described ? rspecDescribedPath( head, src ) : std::string_view {}; if( path.empty() ) { return {}; } return std::string( path ).append( pattern::nodeText( node, src ).substr( ts_node_end_byte( head ) - ts_node_start_byte( node ) ) ); } // nullopt when the node is neither (classifyReceiver's shared arms decide it); otherwise the answer, which is empty for a // (scope_resolution) whose `name:` is not a (constant). inline std::optional classifyRubyReceiver( TSNode node, std::string_view src ) { const char* rt = ts_node_type( node ); if( kindIs( rt, "self" ) ) { return RecvShape { RecvKind::ThisObj, {}, {} }; // Ruby `self` — its own node kind, not an identifier } if( kindIs( rt, "identifier" ) && pattern::nodeText( node, src ) == "described_class" ) { const bool redefined = rubyDefinesDescribedClassMethod( src ) || rubyDescribedClassIsLocal( node, src ); const std::string_view cls = redefined ? std::string_view {} : rspecDescribedClass( node, src ); if( cls.empty() ) { return std::nullopt; // redefined, or no constant-described group encloses it: the identifier arm answers as before } return RecvShape { RecvKind::NamedVar, std::string( cls ), {} }; // the group's constant — Rule 2c fuel } if( !isRubyConstantNode( node ) ) { return std::nullopt; } const std::string_view v = rubyFinalConstant( node, src ); if( v.empty() ) { return RecvShape {}; } return RecvShape { RecvKind::NamedVar, std::string( v ), {} }; // Rule 2c fuel } inline RecvShape classifyReceiver( TSNode node, Lang lang, std::string_view src, bool allowChain ) { const char* rt = ts_node_type( node ); if( kindIs( rt, "this" ) ) { return { RecvKind::ThisObj, {}, {} }; // C++ `this` } if( lang == Lang::Ruby ) { if( std::optional ruby = classifyRubyReceiver( node, src ) ) { return std::move( *ruby ); // `self`, or a class/module constant receiver (classifyRubyReceiver) } } if( kindIs( rt, "identifier" ) ) { const std::string_view v = pattern::nodeText( node, src ); if( v.empty() ) { return {}; } if( lang == Lang::Python && v == "self" ) { return { RecvKind::ThisObj, {}, {} }; // Python `self` } return { RecvKind::NamedVar, std::string( v ), {} }; // `x` — Rule 2 fuel } if( lang == Lang::Python && kindIs( rt, "call" ) ) { // Phase 5: `super().m()` / `super(C, self).m()` — the receiver is a CALL of the identifier `super` const TSNode fn = fieldChild( node, NodeField::Function ); if( !ts_node_is_null( fn ) && kindIs( ts_node_type( fn ), "identifier" ) && pattern::nodeText( fn, src ) == "super" ) { return { RecvKind::SuperObj, {}, {} }; } return {}; // any other call receiver → not one-hop } if( allowChain && isMemberAccessNode( rt, lang ) ) { // depth 2: the receiver is ITSELF one member access — `this->FIELD.m()` / `base.FIELD.m()` const TSNode innerRecv = memberAccessReceiver( node, lang ); const TSNode innerField = memberAccessField( node, lang ); if( ts_node_is_null( innerRecv ) || ts_node_is_null( innerField ) ) { return {}; } // the intermediate must be a plain NAME — a template/computed/parenthesized form is not a field const char* ift = ts_node_type( innerField ); if( !kindIs( ift, "field_identifier" ) && !kindIs( ift, "identifier" ) ) { return {}; } const std::string_view fieldTxt = pattern::nodeText( innerField, src ); if( fieldTxt.empty() ) { return {}; } const RecvShape base = classifyReceiver( innerRecv, lang, src, false ); if( base.kind == RecvKind::ThisObj ) { return { RecvKind::FieldOfThis, {}, std::string( fieldTxt ) }; // `this->m_pool.run()` / `self.pool.acquire()` } if( base.kind == RecvKind::NamedVar ) { return { RecvKind::FieldOfVar, base.var, std::string( fieldTxt ) }; // `cfg.opts.enable()` } return {}; // depth-3 or richer base → not decidable in one hop; degrade to §2a } return {}; // parenthesized / subscripted / call receiver → not one-hop } // The C++ ELEMENT-ACCESS receiver mark (model.h Reference::memberPath, its C++ reading; parser version 165): a member call whose // receiver is ONE element access of a one-hop container — `m[ k ].empty()`, `v.at( i ).size()`, `v.front().size()`, // `v.back().size()`, `p->at( i ).size()` — records that container's shape as model.h kCppElementMark + "root" (a variable or field // NAME, `m`), "this.field" (`this->m[ k ]`) or "root.field" (`rule->bindings_[ k ]`, `r.vec_[ 0 ]`). graph.h StdMemberGate reads // the container's recorded written type for its element type (stdReceiverOf). "" for every other receiver: a two-level // access (`m[ a ][ b ]`), a dereference, a call result, a deeper chain, and an element access whose container is itself one. inline std::string cppElementContainer( TSNode recvNode, std::string_view src ) { const char* rt = ts_node_type( recvNode ); TSNode container = {}; if( kindIs( rt, "subscript_expression" ) ) { container = fieldChild( recvNode, NodeField::Argument ); } else if( kindIs( rt, "call_expression" ) ) { const TSNode fn = fieldChild( recvNode, NodeField::Function ); if( !ts_node_is_null( fn ) && kindIs( ts_node_type( fn ), "field_expression" ) ) { const std::string_view accessor = nodeFieldText( fn, NodeField::Field, src ); if( accessor == "at" || accessor == "front" || accessor == "back" ) { container = memberAccessReceiver( fn, Lang::Cpp ); } } } if( ts_node_is_null( container ) ) { return {}; } const RecvShape shape = classifyReceiver( container, Lang::Cpp, src, /*allowChain=*/true ); std::string out; if( shape.kind == RecvKind::NamedVar && !shape.var.empty() ) { out.assign( kCppElementMark ).append( shape.var ); } else if( shape.kind == RecvKind::FieldOfThis && !shape.field.empty() ) { out.assign( kCppElementMark ).append( "this." ).append( shape.field ); } else if( shape.kind == RecvKind::FieldOfVar && !shape.var.empty() && !shape.field.empty() ) { out.assign( kCppElementMark ).append( shape.var ).push_back( '.' ); out.append( shape.field ); } return out; } // TS/JS literal-receiver kinds (issue #163). A member call whose receiver type the syntax already // proves is a built-in; it must not take the bare-name ladder. Object literals, identifier // receivers, this, casts, and element-returning links stay RecvKind::None. Do NOT widen // isMemberAccessNode — every recv==None guard would then see every TS/JS member call. inline RecvKind jsLiteralKindOf( const char* t ) noexcept { if( kindIs( t, "string" ) || kindIs( t, "template_string" ) ) { return RecvKind::LitString; } if( kindIs( t, "array" ) ) { return RecvKind::LitArray; } if( kindIs( t, "regex" ) ) { return RecvKind::LitRegex; } if( kindIs( t, "number" ) ) { return RecvKind::LitNumber; } if( kindIs( t, "true" ) || kindIs( t, "false" ) ) { return RecvKind::LitBoolean; } return RecvKind::None; } struct JsLitChainRow { RecvKind from; const char* method; RecvKind to; }; // Certain (from, method) → result kind. Anything not listed ends certainty (find/at/pop/shift/reduce, // subscript, non-null !, casts). Keep this table the single place a chain link is decided. inline RecvKind jsLitChainResult( RecvKind from, std::string_view method ) noexcept { static constexpr JsLitChainRow kRows[] = { { RecvKind::LitString, "charAt", RecvKind::LitString }, { RecvKind::LitString, "concat", RecvKind::LitString }, { RecvKind::LitString, "normalize", RecvKind::LitString }, { RecvKind::LitString, "padEnd", RecvKind::LitString }, { RecvKind::LitString, "padStart", RecvKind::LitString }, { RecvKind::LitString, "repeat", RecvKind::LitString }, { RecvKind::LitString, "replace", RecvKind::LitString }, { RecvKind::LitString, "replaceAll", RecvKind::LitString }, { RecvKind::LitString, "slice", RecvKind::LitString }, { RecvKind::LitString, "split", RecvKind::LitArray }, { RecvKind::LitString, "substr", RecvKind::LitString }, { RecvKind::LitString, "substring", RecvKind::LitString }, { RecvKind::LitString, "toLocaleLowerCase", RecvKind::LitString }, { RecvKind::LitString, "toLocaleUpperCase", RecvKind::LitString }, { RecvKind::LitString, "toLowerCase", RecvKind::LitString }, { RecvKind::LitString, "toString", RecvKind::LitString }, { RecvKind::LitString, "toUpperCase", RecvKind::LitString }, { RecvKind::LitString, "trim", RecvKind::LitString }, { RecvKind::LitString, "trimEnd", RecvKind::LitString }, { RecvKind::LitString, "trimLeft", RecvKind::LitString }, { RecvKind::LitString, "trimRight", RecvKind::LitString }, { RecvKind::LitString, "trimStart", RecvKind::LitString }, { RecvKind::LitString, "valueOf", RecvKind::LitString }, { RecvKind::LitArray, "concat", RecvKind::LitArray }, { RecvKind::LitArray, "copyWithin", RecvKind::LitArray }, { RecvKind::LitArray, "fill", RecvKind::LitArray }, { RecvKind::LitArray, "filter", RecvKind::LitArray }, { RecvKind::LitArray, "flat", RecvKind::LitArray }, { RecvKind::LitArray, "flatMap", RecvKind::LitArray }, { RecvKind::LitArray, "join", RecvKind::LitString }, { RecvKind::LitArray, "map", RecvKind::LitArray }, { RecvKind::LitArray, "reverse", RecvKind::LitArray }, { RecvKind::LitArray, "slice", RecvKind::LitArray }, { RecvKind::LitArray, "sort", RecvKind::LitArray }, { RecvKind::LitArray, "toLocaleString", RecvKind::LitString }, { RecvKind::LitArray, "toReversed", RecvKind::LitArray }, { RecvKind::LitArray, "toSorted", RecvKind::LitArray }, { RecvKind::LitArray, "toSpliced", RecvKind::LitArray }, { RecvKind::LitArray, "toString", RecvKind::LitString }, { RecvKind::LitArray, "with", RecvKind::LitArray }, { RecvKind::LitRegex, "test", RecvKind::LitBoolean }, { RecvKind::LitNumber, "toExponential", RecvKind::LitString }, { RecvKind::LitNumber, "toFixed", RecvKind::LitString }, { RecvKind::LitNumber, "toPrecision", RecvKind::LitString }, { RecvKind::LitNumber, "toString", RecvKind::LitString }, { RecvKind::LitBoolean, "toString", RecvKind::LitString }, }; for( const JsLitChainRow& row : kRows ) { if( row.from == from && method == row.method ) { return row.to; } } return RecvKind::None; } inline RecvKind classifyJsTsLiteralRecv( TSNode node, std::string_view src, int depth ) noexcept { if( depth > 16 || ts_node_is_null( node ) ) { return RecvKind::None; } const char* t = ts_node_type( node ); if( kindIs( t, "parenthesized_expression" ) ) { return classifyJsTsLiteralRecv( ts_node_named_child( node, 0 ), src, depth + 1 ); } // A SIGNED numeric literal — (-1).toFixed(), (+2).toFixed() — is a number receiver: the grammar spells the sign as a // unary operator over a `number` node, so without this it classified as no literal at all and the call could bind an // unrelated in-repo toFixed. Only + and - over a number: `!1`, `typeof 1` and `-x` are not number literals. if( kindIs( t, "unary_expression" ) ) { const TSNode sign = fieldChild( node, NodeField::Operator ); const TSNode arg = fieldChild( node, NodeField::Argument ); if( !ts_node_is_null( sign ) && !ts_node_is_null( arg ) && kindIs( ts_node_type( arg ), "number" ) ) { const std::string_view op = pattern::nodeText( sign, src ); if( op == "-" || op == "+" ) { return RecvKind::LitNumber; } } return RecvKind::None; } if( kindIs( t, "as_expression" ) || kindIs( t, "satisfies_expression" ) || kindIs( t, "type_assertion" ) || kindIs( t, "non_null_expression" ) || kindIs( t, "subscript_expression" ) ) { return RecvKind::None; } const RecvKind lit = jsLiteralKindOf( t ); if( lit != RecvKind::None ) { return lit; } if( !kindIs( t, "call_expression" ) ) { return RecvKind::None; } const TSNode fn = fieldChild( node, NodeField::Function ); if( ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "member_expression" ) ) { return RecvKind::None; } const TSNode prop = fieldChild( fn, NodeField::Property ); const TSNode obj = fieldChild( fn, NodeField::Object ); if( ts_node_is_null( prop ) || ts_node_is_null( obj ) ) { return RecvKind::None; } const RecvKind inner = classifyJsTsLiteralRecv( obj, src, depth + 1 ); if( inner == RecvKind::None ) { return RecvKind::None; } return jsLitChainResult( inner, pattern::nodeText( prop, src ) ); } // The node a call's @name hangs under once the C++ template-argument wrappers are stepped over. For // `x.m()` that is the name's own parent. `x.m()` puts ONE wrapper between them — field_expression // field: (template_method name: m arguments: …) — and the disambiguated `x.template m()` a second — // field: (dependent_name (template_method …)) — so the climb steps over exactly those two kinds, in that // order, and a name under any other parent keeps the parent it had. Both kinds are tree-sitter-cpp's (the // CUDA grammar is generated from it); no other grammar names either, so the climb is inert everywhere else. // A template_method that is not a member callee (a qualified definition's declarator) climbs to a parent // that is no member access, which every caller already reads as "not a member call". inline TSNode calleeAccessParent( TSNode nameNode ) noexcept { TSNode parent = parentOf( nameNode ); if( ts_node_is_null( parent ) || !kindIs( ts_node_type( parent ), "template_method" ) ) { return parent; } parent = parentOf( parent ); if( !ts_node_is_null( parent ) && kindIs( ts_node_type( parent ), "dependent_name" ) ) { parent = parentOf( parent ); } return parent; } // P2-D RECEIVER capture: classify the call-site receiver of `recv.method()` / `recv->method()` so // resolve.h can narrow before the ambiguous §2a name spray. `nameNode` is the @name capture (the called // identifier). When it is the `.field`/`.attribute` of a member-access node, inspect that node's // receiver (`.argument` in C++ `field_expression`, `.object` in Python `attribute`): // `this`/`self` → ThisObj (the enclosing class is definitive — Rule 1) // a bare `(identifier)` → NamedVar, recvVar = the variable text (the var's type pins the method — Rule 2) // ONE more member access whose OWN receiver is `this`/`self` or a bare identifier → FieldOfThis / // FieldOfVar, carrying the intermediate field name (`this->m_pool.run()` → field "m_pool"; // `cfg.opts.enable()` → var "cfg", field "opts"). NO resolve rule consumes these yet: the receiver // kind exists so the five `recv == RecvKind::None` guard sites stop misclassifying a chained // receiver as a BARE name — Rule 1's bareCish arm wrong-narrowed `this->m_pool.run()` to the // caller's own class, and shadow suppression deleted `this->m_cfg.enable()` under a local named // `enable` (docs/EVALS.md §4 "Receiver-guard misfires"; the names carried here make a future // chain-resolution rule resolve-side only, with no second re-parse). // `super()` / `super(C, self)` (Python) → SuperObj (Phase 5: resolves through the bases only) // anything else (a depth-3 chain, `(expr)`, subscripts, a non-super call in the chain, …) → FieldOfVar // with an EMPTY recvVar — "a member access, receiver undecidable" (Phase 5; was None, which the five // bare-name guard sites misread as a BARE call). The bound is ONE intermediate hop, deliberately: past // that the receiver is too rich to decide syntactically. `test/chainguardcheck.sh` arm (h) pins the // bound: the depth-3 call now takes the honest ladder, never Rule 1's enclosing-class pin. // Pure-syntactic, deterministic, allocation-light: at most two short identifier copies, and none at all // for the None/ThisObj shapes that dominate. // TS/JS: a member_expression whose object is a certain literal (or a chain that stays certain) returns // LitString/LitArray/LitRegex/LitNumber/LitBoolean instead of None. isMemberAccessNode stays false for // TS/JS so every other member call is still RecvKind::None. // // The parent it inspects is calleeAccessParent's, not the raw parent: a C++ member call with explicit // template arguments puts a template_method (and, behind `template`, a dependent_name) between the name and // its field_expression. Read through the raw parent, `other.f()` classified None — a BARE call — and the // enclosing-class rule pinned it to the caller's own same-named method (test/cppqualcheck.sh §12 (d)). inline RecvShape receiverOf( TSNode nameNode, Lang lang, std::string_view src ) { const TSNode parent = calleeAccessParent( nameNode ); if( ts_node_is_null( parent ) ) { return {}; } if( const TSNode obj = memberOnlyReceiver( parent, lang ); !ts_node_is_null( obj ) ) { if( lang == Lang::TypeScript || lang == Lang::JavaScript ) { const RecvKind lit = classifyJsTsLiteralRecv( obj, src, 0 ); if( lit != RecvKind::None ) { return { lit, {}, {} }; } } RecvShape member; // recv stays None (non-literal TS/JS, every Go/Rust member call); FE-A marks the shape member.member = true; if( lang == Lang::C ) { member.root = memberChainRoot( obj, src ); // FE-A's root only: no receiver-evidence rule reads C's (Rust: train 29, below) return member; } MemberChain chain = memberChainOf( obj, lang, src ); // FE-B: root + path + constructed class member.root = std::move( chain.root ); member.path = std::move( chain.path ); member.ctor = std::move( chain.ctor ); return member; } if( !isMemberAccessNode( ts_node_type( parent ), lang ) ) { return {}; } const TSNode recvNode = memberAccessReceiver( parent, lang ); if( ts_node_is_null( recvNode ) ) { return {}; } RecvShape rs = classifyReceiver( recvNode, lang, src, /*allowChain=*/ true ); // `p->m()` against `p.m()`: on a std smart pointer member only `->` reaches the pointee (Rule 2b, fieldnarrowcheck arm p) rs.viaArrow = ( lang == Lang::Cpp || lang == Lang::ObjC ) && nodeFieldText( parent, NodeField::Operator, src ) == "->"; if( rs.kind == RecvKind::None ) { // Phase 5 (docs/EVALS.md "Phase 5", kParserVer 77): a member access whose receiver is too rich to // classify (a depth-3 chain, a call other than `super()`, a subscript, a literal, a parenthesized // expression) is still a MEMBER ACCESS — never a bare name. Stamped FieldOfVar with an EMPTY recvVar, // the convention stampMemberReceiver already uses for Read/Write refs, so the `recv == None` guard // sites (Rule 1's bare arm, shadow suppression, the external-name veto's bare arms) read None as // "truly bare". Found by the veto's own precision listing: `self.to_cartesian().sum()` was read as a // bare builtin `sum(…)` and refused. Closes the depth-3 residual test/chainguardcheck.sh arm (h) pinned. rs.kind = RecvKind::FieldOfVar; if( lang == Lang::Cpp ) { rs.path = cppElementContainer( recvNode, src ); // `m[ k ].empty()`: the container's shape (parser version 165) } } return rs; } // The constant a Ruby call's receiver names, AS WRITTEN, when that differs from `final` — the final segment recvVar keeps // (parser version 137: `Stripe::Customer` is not the tree's Customer unless the tree opens that path, graph.h // RubyClassObjects). A qualified constant's whole path; the path through the described class for `described_class` and // for `described_class::Worker`; empty for a final segment alone, and for a path with any other head that is no constant // (`self.class::Foo`). inline std::string rubyWrittenPathOfReceiver( TSNode recv, std::string_view final, std::string_view src ); inline std::string rubyReceiverWrittenPath( TSNode nameNode, std::string_view final, std::string_view src ) { const TSNode call = calleeAccessParent( nameNode ); return rubyWrittenPathOfReceiver( ts_node_is_null( call ) || !kindIs( ts_node_type( call ), "call" ) ? TSNode {} : fieldChild( call, NodeField::Receiver ), final, src ); } // rubyReceiverWrittenPath's answer for the receiver node itself (an element access's `object:` reads it too) inline std::string rubyWrittenPathOfReceiver( TSNode recv, std::string_view final, std::string_view src ) { std::string written; if( ts_node_is_null( recv ) ) { return written; } if( kindIs( ts_node_type( recv ), "identifier" ) && pattern::nodeText( recv, src ) == "described_class" ) { // The group's path. Also written where classifyRubyReceiver declined (a local or redefined described_class), but // never read there: rubyConstantReceiver (graph.h) reads it only beside a constant recvVar, and a declined one keeps // `described_class` (test/rubydescribedclasscheck.sh qloc_spec). Not writing it moves extraction output (kParserVer). written = rspecDescribedPath( recv, src ); } else if( isRubyConstantNode( recv ) ) { written = rubyIsConstantChain( recv ) ? std::string( pattern::nodeText( recv, src ) ) : rubyDescribedRelativePath( recv, src ); } return written == final ? std::string {} : written; } // The RefRole::Subscript reference of element access `access` (whose receiver is `recv`), or nullopt where it can name no // class: a language with no operator row, a form subscriptOperatorName names nothing for, a receiver whose class no rule // can type — a literal (`{ a: 1 }[ :a ]`, `[ 1, 2 ][ 0 ]`), a call result in Python, another element access. The // receiver is read as a member call's is (classifyReceiver), so graph.h's receiver rules read the same fields; a Ruby // receiver too rich for them stays FieldOfVar, which the typing walk may still type (`Report.new[ :x ]`, // RubyBareCallWalk::noteTypedSend). inline std::optional subscriptOperatorRef( TSNode access, TSNode recv, std::uint32_t fileId, Lang lang, std::string_view src ) { const SubscriptOperatorNames* names = subscriptOperatorNamesOf( lang ); const std::string_view op = names == nullptr ? std::string_view {} : subscriptOperatorName( access, *names ); if( op.empty() || ts_node_is_null( recv ) ) { return std::nullopt; } RecvShape rs = classifyReceiver( recv, lang, src, /*allowChain=*/ true ); if( rs.kind == RecvKind::None && !langFacts( lang ).keepsUnshapedSubscript ) { return std::nullopt; } RawRef r; r.fileId = fileId; r.startByte = ts_node_start_byte( access ); r.line = ts_node_start_point( access ).row + 1; r.lang = lang; r.role = RefRole::Subscript; r.name = op; r.recv = rs.kind == RecvKind::None ? RecvKind::FieldOfVar : rs.kind; // receiverOf's "too rich" member shape r.recvVar = rs.var; r.setFieldName( std::move( rs.field ) ); if( langFacts( lang ).constantPathReceiver && r.recv == RecvKind::NamedVar ) { r.setFieldName( rubyWrittenPathOfReceiver( recv, r.recvVar, src ) ); // a constant's path as written (parser version 137) } ENSURES( r.recv != RecvKind::None, "a subscript always has a receiver: graph.h's receiver rules read it as a member call" ); return r; } // Java method references are not ordinary member-access nodes. The pinned grammar's first named // child is the receiver and deliberately uses the SAME `identifier` node for a simple type and a // variable. Preserve that uncertainty for graph.h: simple and dotted type candidates are retained // as JavaTypeCandidate with the receiver text (`Widget`, `Outer.Inner`, `com.example.Widget`); // `this`, `super`, arbitrary expressions, and `Type::new` remain unresolved here. The resolver // proves a type receiver from indexed class names and lexical shadowing at the site — it does // not require every dotted segment to be a class (package prefixes are not classes). // The member-name query already excludes `new`. inline RecvShape javaMethodReferenceReceiver( TSNode roleNode, std::string_view src ) { RecvShape out; if( ts_node_is_null( roleNode ) || !kindIs( ts_node_type( roleNode ), "method_reference" ) ) { return out; } out.kind = RecvKind::JavaTypeCandidate; if( ts_node_named_child_count( roleNode ) == 0 ) { return out; } const TSNode receiver = ts_node_named_child( roleNode, 0 ); if( kindIs( ts_node_type( receiver ), "identifier" ) ) { out.var = std::string( nodeTextOf( receiver, src ) ); } else if( kindIs( ts_node_type( receiver ), "field_access" ) ) { out.var = std::string( nodeTextOf( receiver, src ) ); } return out; } // ── P2-D Rule 2 LOCAL-VARIABLE TYPE BINDING capture (`Foo x;` → x:Foo) ─────────────────────────────── // Walk a node subtree and emit one RawBind per local variable whose TYPE is syntactically decidable, so a // later `x.m()`/`x->m()` can narrow to `typeName::m`. Pure-syntactic, deterministic, allocation-light: // it reads exactly the declaration/assignment shapes ground-truthed from the grammars (see the gate fixtures). // * The recorded typeName is the WRITTEN type's final segment (`ns::Foo` → `Foo`). Rule 2 matches it as the scope // of the called method, so an inferred type from a constructor-call (`auto x = Foo()`) only narrows if `Foo` // defines the method; an ASSIGNMENT's inferred type is dropped in buildGraph unless a class of that name exists. // * Only the named-receiver shape is useful downstream, so only bare-identifier targets are recorded // (member targets `self.x`/`obj.f` are not — `receiverOf` doesn't capture those as recvVar either). // the innermost bare `(identifier)` reached by unwrapping pointer/reference/parenthesized declarators — // the actual variable name of a C++ declarator. "" if the declarator isn't a single named variable. inline std::string_view declaratorVarName( TSNode decl, std::string_view src ) { for( int guard = 0; guard < 8 && !ts_node_is_null( decl ); ++guard ) { const char* dt = ts_node_type( decl ); if( kindIs( dt, "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( decl ), b = ts_node_end_byte( decl ); return ( a <= b && b <= src.size() ) ? src.substr( a, b - a ) : std::string_view{}; } // unwrap a pointer/reference/parenthesized declarator to its inner `declarator` child const TSNode inner = fieldChild( decl, NodeField::Declarator ); if( ts_node_is_null( inner ) ) { return {}; } decl = inner; } return {}; } // member-variable round (card A3): the variable name of a PARAMETER declarator, which is declaratorVarName's // answer plus one shape it refuses on purpose: a reference_declarator holds its inner declarator as an UNNAMED // child (`Counter& c` — the `&` is the only anonymous sibling), so the `declarator` field probe is null there. // Unwrapped HERE, for the ParamType record alone — widening declaratorVarName itself would mint Rule-2 Type // records for `Foo& x = …` locals too, which Rule 2's flat per-function table would leak past their scope // (ParamType records reach Rule 2 only through the lexical lookup, resolve.h buildScopedRecvDecls). inline std::string_view paramDeclaratorVarName( TSNode decl, std::string_view src ) { if( !ts_node_is_null( decl ) && kindIs( ts_node_type( decl ), "reference_declarator" ) && ts_node_is_null( fieldChild( decl, NodeField::Declarator ) ) && ts_node_named_child_count( decl ) > 0 ) { decl = ts_node_named_child( decl, 0 ); } return declaratorVarName( decl, src ); } // the node a C++ type or constructor NAME ends in, read through the grammar's own fields — a qualified name's `name`, // then a template-id's `name`: `Vec` and `ll::Vec` end in `Vec`, `Outer::Inner` in `Inner`. The // spelling is never cut as text: finalSegment() truncates at the FIRST `<`, which read `Outer::Inner` as `Outer` // (test/narrowcheck.sh arm 41). Every other kind is its own last name. Each step moves to a child, so the walk ends; a // missing `name` field (error recovery) reads as a null node, whose text is "". inline TSNode lastNameNode( TSNode n ) noexcept { while( !ts_node_is_null( n ) ) { const char* t = ts_node_type( n ); if( !kindIs( t, "qualified_identifier" ) && !kindIs( t, "template_type" ) && !kindIs( t, "template_function" ) ) { break; } n = fieldChild( n, NodeField::Name ); } return n; } // the type NAME of a constructor-style RHS value node: `Foo()` (call_expression) or `new Foo()` // (new_expression). Last name of the callee/constructor identifier. "" if the value isn't a // plain constructor call. A plain FUNCTION call is not told apart: `auto x = makeFoo()` records `makeFoo`, which names // no class and never narrows (graph.h's varType note) — but it still conflicts with the variable's other declarations // in Rule 2's flat table, and a conflict tombstones the variable. The assignment `x = makeFoo()` records it too; // assignedTypeOf marks that one, and buildGraph keeps it only when a class names it (resolve.h assignmentNamesNoClass). inline TSNode ctorNameNode( TSNode value ) { if( ts_node_is_null( value ) ) { return TSNode{}; } const char* vt = ts_node_type( value ); TSNode idn {}; if( kindIs( vt, "call_expression" ) ) { // C++/TS `Foo()` idn = fieldChild( value, NodeField::Function ); } else if( kindIs( vt, "new_expression" ) ) { // C++/TS `new Foo()` idn = fieldChild( value, NodeField::Constructor ); } if( ts_node_is_null( idn ) ) { return TSNode{}; } const char* it = ts_node_type( idn ); // An unqualified template-id callee (`Vec()`) is refused, a STATED FLOOR (test/narrowcheck.sh arm 39d): the same // spelling is every cast helper, and accepting it records `dyn_cast` / `cast` as the type of `auto *CI = // dyn_cast( I )` — a name that conflicts with the declaration's written type (`const ConstantInt *CI = // dyn_cast( V )`) or with a second declaration of the variable, and the conflict tombstones it. Measured on // integration/train-3 (llvm-project 4d5358b1d, 2026-09-17): accepting it moves 463 call sites, 324 of them edges lost // and 2 gained; rocksdb moves none. (On main, before #278 dropped an ASSIGNMENT's callee name, `Spec = // cast( F )` tombstoned too: 994 moved, 779 lost.) The class a cast names // is its template ARGUMENT, which a name-only record cannot tell from a constructor's. The QUALIFIED spelling // (`llvm::cast( x )`) still records its last name the same way, as it did before this floor was written. if( !kindIs( it, "identifier" ) && !kindIs( it, "type_identifier" ) && !kindIs( it, "qualified_identifier" ) && !kindIs( it, "scoped_identifier" ) ) { return TSNode{}; } return idn; } inline std::string ctorTypeOf( TSNode value, std::string_view src ) { return finalSegment( nodeTextOf( lastNameNode( ctorNameNode( value ) ), src ) ); // a null node reads "", and "" splits to "" } // the written type name of a `type:`-field type node — a `type_identifier`, a qualified one or a template-id — as its // last name (lastNameNode: `Vec` → `Vec`, `Outer::Inner` → `Inner`). "" for `auto`/decltype/dependent // types — those fall back to constructor inference. inline std::string writtenTypeOf( TSNode typeNode, std::string_view src ) { if( ts_node_is_null( typeNode ) ) { return {}; } const char* tt = ts_node_type( typeNode ); if( kindIs( tt, "type_identifier" ) || kindIs( tt, "qualified_identifier" ) || kindIs( tt, "template_type" ) ) { return finalSegment( nodeTextOf( lastNameNode( typeNode ), src ) ); } return {}; // auto / decltype / dependent — type not directly written → try the initializer } // Rule 2's qualifier guard (2026-09-16, test/narrowcheck.sh arms 17-24): the text of a type or constructor NAME node // when it is QUALIFIED — a qualified name on its way to the last name has a scope (`std::map`, `ext::Widget`, // `Outer::Inner`; a leading global `::` alone has none) — else "". writtenTypeOf/ctorTypeOf keep the last name // alone, and Rule 2 matches it against class names that carry no namespace, so `const std::map& ref` read as // `map` and narrowed to an unrelated in-repo `map` (measured on a private C++ corpus). The whole text rides the // Type/ParamType record in RawBind::importedName; Rule 2 refuses to narrow on a `std::` one (resolve.h namesStdType) // and keeps every other. A template ARGUMENT's `::` qualifies nothing: `Vec` is unqualified (arm 40). inline std::string qualifiedNameText( TSNode nameNode, std::string_view src ) { bool scoped = false; for( TSNode n = nameNode; !scoped && !ts_node_is_null( n ) && kindIs( ts_node_type( n ), "qualified_identifier" ); n = fieldChild( n, NodeField::Name ) ) { scoped = !ts_node_is_null( fieldChild( n, NodeField::Scope ) ); } if( !scoped ) { return {}; } std::string_view text = nodeTextOf( nameNode, src ); while( !text.empty() && ( text.front() == ' ' || text.front() == ':' ) ) { text.remove_prefix( 1 ); // a leading `::` names the global namespace — not a qualifier } return std::string( text ); } // one declaration's recorded type: the name Rule 2 matches (the final segment) and, when the type was written // QUALIFIED, its whole text — carried together so no emitter can record one without the other — and whether an // ASSIGNMENT's callee supplied it rather than a declaration (assignedTypeOf). struct DeclType { std::string name; std::string qualified; bool isFromAssignment = false; bool directDeclarator = false; // CPP-PTR-ALIAS: the variable IS the written type or a reference to it (model.h kDirectDeclaratorBit) }; // a type or constructor NAME node's DeclType inline DeclType declTypeOfName( TSNode typeNode, std::string_view src ) { return DeclType{ writtenTypeOf( typeNode, src ), qualifiedNameText( typeNode, src ) }; } // a declarator's recorded type: the declaration's WRITTEN type when it has one, else the type of the constructor its // initializer calls (`auto x = Foo()`, `auto x = ns::Foo()`) inline DeclType declaredTypeOf( const DeclType& written, TSNode value, std::string_view src ) { if( !written.name.empty() ) { return written; } return DeclType{ ctorTypeOf( value, src ), qualifiedNameText( ctorNameNode( value ), src ) }; } // a C++ ASSIGNMENT's recorded type (`x = Foo()`, `x = new Foo()`): the callee's, marked as an assignment's. The grammar // cannot tell a constructor from a function here — `t = llvm::cast( y )` reads `cast` — and an assignment // declares nothing, so graph.h keeps the record only when a class of that name exists (resolve.h assignmentNamesNoClass). inline DeclType assignedTypeOf( TSNode value, std::string_view src ) { DeclType type = declaredTypeOf( DeclType{}, value, src ); type.isFromAssignment = true; return type; } // ── L3 fn-pointer/callback binding capture helpers ─────────────────────────────────────────────────── // the bound-function TARGET of an initializer/assignment RHS value node, for a var→FUNCTION binding: // `&alpha` / `&ns::alpha` (address-of) → "alpha" / "ns::alpha"; `alpha` / `ns::alpha` (bare) → same; // `[](){...}` (lambda) → kFnBindLambdaTarget. "" for everything else (a call, a literal, arithmetic — // not a recognizable single function). `wasBareIdent` reports the bare-IDENTIFIER shape so the caller can // apply the primitive-type noise gate (`int a = b;` is almost never a function copy; `H h = beta;` through // a typedef legitimately is). inline std::string fnBindTargetOf( TSNode value, std::string_view src, bool& wasBareIdent ) { wasBareIdent = false; if( ts_node_is_null( value ) ) { return {}; } const char* vt = ts_node_type( value ); if( kindIs( vt, "lambda_expression" ) ) { return std::string( kFnBindLambdaTarget ); } TSNode idn = value; bool addressOf = false; if( kindIs( vt, "pointer_expression" ) ) { // only the ADDRESS-OF form — `*p` is also a pointer_expression, and a dereference names no function. const TSNode op = ts_node_child( value, 0 ); if( ts_node_is_null( op ) || !kindIs( ts_node_type( op ), "&" ) ) { return {}; } idn = fieldChild( value, NodeField::Argument ); if( ts_node_is_null( idn ) ) { return {}; } addressOf = true; } const char* it = ts_node_type( idn ); if( !kindIs( it, "identifier" ) && !kindIs( it, "qualified_identifier" ) ) { return {}; } const std::uint32_t a = ts_node_start_byte( idn ), b = ts_node_end_byte( idn ); if( a > b || b > src.size() ) { return {}; } wasBareIdent = !addressOf && kindIs( it, "identifier" ); return std::string( src.substr( a, b - a ) ); } // the shape a possibly fn-pointer declarator chain (`(*fn)()` → "fn") presents, descending through // function/parenthesized/pointer/reference declarators. `sawFn` reports crossing a function_declarator — // the explicit fn-pointer syntax that licenses a bare-identifier initializer even under a primitive written // type (`void (*fn)() = handler;`); `sawRef` a reference_declarator (`H& r = fn;`), where the reference // ALIASES its initializer, so the caller must treat the bound-to variable as ESCAPED (clobbered) and never // emit a positive for the alias; `sawPtr` a pointer_declarator, which is what separates the two shapes the // type node alone cannot tell apart — `void (*fp)()` (a fn-pointer VARIABLE: sawFn AND sawPtr) from // `void fp()` (a function DECLARATION: sawFn alone), which declares no variable at all. declaratorVarName // (Rule 2) is NOT reused: parenthesized_declarator and reference_declarator carry their inner declarator as // an UNNAMED child, which a field-only unwrap cannot reach. An array_declarator bails — an ARRAY of fn // pointers is table territory, never a single-var binding (its indexed call must stay unresolved). struct FnBindDeclShape { std::string_view name; bool sawFn = false; bool sawPtr = false; bool sawRef = false; }; inline FnBindDeclShape fnDeclaratorShape( TSNode decl, std::string_view src ) { FnBindDeclShape shape; for( int guard = 0; guard < 10 && !ts_node_is_null( decl ); ++guard ) { const char* dt = ts_node_type( decl ); if( kindIs( dt, "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( decl ), b = ts_node_end_byte( decl ); shape.name = ( a <= b && b <= src.size() ) ? src.substr( a, b - a ) : std::string_view{}; return shape; } if( kindIs( dt, "array_declarator" ) ) { return shape; } const bool isRef = ( kindIs( dt, "reference_declarator" ) ); shape.sawFn = shape.sawFn || kindIs( dt, "function_declarator" ); shape.sawPtr = shape.sawPtr || kindIs( dt, "pointer_declarator" ); shape.sawRef = shape.sawRef || isRef; TSNode inner = fieldChild( decl, NodeField::Declarator ); if( ts_node_is_null( inner ) && ( isRef || kindIs( dt, "parenthesized_declarator" ) ) ) { // the parenthesized/reference inner declarator is an UNNAMED child — take the first named one if( ts_node_named_child_count( decl ) > 0 ) { inner = ts_node_named_child( decl, 0 ); } } if( ts_node_is_null( inner ) ) { return shape; } decl = inner; } return shape; } // tree-sitter-cpp MIS-PARSES a raw fn-pointer declaration inside a function body — // `void (*fn)() = α` — as an assignment_expression whose LEFT is // call_expression( function: call_expression( function: primitive_type, arguments: ((*fn)) ), arguments: () ) // (the C grammar parses the same statement as a true declaration; only C++ takes the expression branch — // ground-truthed with an AST dump against the vendored grammars, 2026-08-08). Decode the variable name from // that shape. The inner callee must be a PRIMITIVE type — `void(...)` is never callable, so the shape is // unambiguous evidence of a declaration; an identifier callee (`H (*g)()`, but equally REAL code // `foo(*p)() = x;` assigning through a call result) stays undecoded — conservative, no false binding. inline std::string_view misparsedFnPtrDeclVar( TSNode lhs, std::string_view src ) { if( ts_node_is_null( lhs ) || !kindIs( ts_node_type( lhs ), "call_expression" ) ) { return {}; } const TSNode inner = fieldChild( lhs, NodeField::Function ); if( ts_node_is_null( inner ) || !kindIs( ts_node_type( inner ), "call_expression" ) ) { return {}; } const TSNode ty = fieldChild( inner, NodeField::Function ); if( ts_node_is_null( ty ) || !kindIs( ts_node_type( ty ), "primitive_type" ) ) { return {}; } const TSNode args = fieldChild( inner, NodeField::Arguments ); if( ts_node_is_null( args ) || ts_node_named_child_count( args ) != 1 ) { return {}; } const TSNode pe = ts_node_named_child( args, 0 ); if( !kindIs( ts_node_type( pe ), "pointer_expression" ) ) { return {}; } const TSNode op = ts_node_child( pe, 0 ); if( ts_node_is_null( op ) || !kindIs( ts_node_type( op ), "*" ) ) { return {}; } const TSNode idn = fieldChild( pe, NodeField::Argument ); if( ts_node_is_null( idn ) || !kindIs( ts_node_type( idn ), "identifier" ) ) { return {}; } const std::uint32_t a = ts_node_start_byte( idn ), b = ts_node_end_byte( idn ); return ( a <= b && b <= src.size() ) ? src.substr( a, b - a ) : std::string_view{}; } // L3: an assignment whose RHS is not a recognizable single function — a CLOBBER site, emitted as a // kFnBindClobberTarget record at the end of the walk IF the var has a fn binding in the same file. struct FnBindClobber { std::string var; std::uint32_t startByte = 0; }; // ── L3 VALUE-ASSIGNMENT NOISE GATE (r9 fix round) ──────────────────────────────────────────────────── // The DECLARATION arm gates its bare-identifier initializer on the written type (`int a = b;` is a copy, // not a function). The ASSIGNMENT arm carries no type node at all, so before this gate EVERY `x = y;` with // a bare-identifier RHS minted an FnAssign — `std::string line; line = zzz;` included. Two measured harms, // both from a binding that names no function anybody could call: shadowSuppressedSite VETOES local-shadow // suppression for any name carrying an L3 binding (calls THROUGH a bound variable must keep resolving), so // the local handed its every read/write back to the function it shadows; and buildFnPtrBindTables' file- // scope sweep keys FnAssign records by VAR NAME ALONE across the whole corpus, so one bogus record // TOMBSTONED a genuine, never-clobbered file-scope binding of the same name in an unrelated file. // The gate asks the file's own declarations of that name what the variable IS: // * PROVEN VALUE — declared with a concrete written type that is no function-pointer alias → the // assignment is a copy: no positive, a CLOBBER instead (exactly what `fn = getHandler()` records), // inert unless the name holds a real binding here and correctly tombstoning it when it does; // * FN-CAPABLE — a fn-pointer declarator (`void (*fp)()`) or a fn-pointer alias type (`H fp;`) → mint, // and this wins over any value evidence for the same name (recall-safe when a file reuses a name); // * NEITHER — `auto`, `decltype`, a template type, or a name this file never declares (a global, a // member, an extern): UNKNOWN, and unknown MINTS, exactly as before. When in doubt, don't gate. // File-scoped and name-based, like every other L3 table: evidence from one function's declarations reaches // another's assignment of the same name. That over-approximation runs toward NOT minting a binding, which // is the side that can only cost a disclosed edge, never invent one. Address-of (`fn = &beta`) and lambda // RHS forms are self-evidencing and never reach the gate. // Each fact carries the DEFINITION it was declared inside, so one file declaring `std::string run;` in one // function and `void (*run)();` in another gets both answers right; a file-scope declaration carries {0,0} // and applies everywhere. This is a function-granular scope, deliberately coarser than the shadow spans in // model.h — it decides what a NAME can hold, not which sites a declaration claims. struct FnBindVarTypeFact { std::string var; std::string typeName; // final segment of a WRITTEN type name; "" for the unnamed kinds std::uint32_t scopeStart = 0; // the enclosing definition's byte span; {0,0} = file scope std::uint32_t scopeEnd = 0; bool concreteType = false; // the type node's spelling FIXES the type (never auto/decltype/template) bool fnPtrVariable = false; // the declarator chain is a function POINTER, not a plain value }; // a bare-identifier assignment held back until the walk ends, when the file's full declaration evidence — // including declarations the DFS has not reached yet — decides whether it mints a binding or a clobber. // Deferring is what keeps the verdict independent of walk order, and so of the AST's shape. struct PendingFnBindAssign { std::string var; std::string target; std::uint32_t startByte = 0; }; // a bare-identifier DECLARATION initializer held back the same way, and for one reason only: the fn-pointer // ALIAS table is not complete until the walk ends, and a `typedef void (*H)();` written BELOW `H fp = beta;` // is the difference between a binding and a copy. Unlike its assignment sibling this record carries its own // verdict material — the declaration's WRITTEN TYPE, read straight off the node — because a declaration IS // the variable and needs no file-wide fact lookup to say what it holds. struct PendingFnBindDecl { std::string var; std::string target; std::string typeName; // final segment of the written type; "" for the unnamed concrete kinds std::uint32_t startByte = 0; bool concreteType = false; // the spelling FIXES the type (never auto/decltype/template) }; // the gate's whole per-file state: the declared-variable type facts, the file's function-pointer type // aliases, and the assignments AND declarations held back until both of those are complete. One object // because they have no independent life — filled by one walk and spent together the moment it ends. struct FnBindGateState { std::vector facts; std::vector pending; std::vector pendingDecl; HashMap aliases; }; // true when a `type:` field node is a CONCRETE written type — one whose spelling alone fixes what the // variable is. `name` receives the final segment for the NAMED kinds (the only ones a typedef can make a // function pointer); the built-in and class/enum-body kinds can never be one and leave it empty. Everything // else — `auto`, `decltype`, and any type carrying TEMPLATE ARGUMENTS — is dependent, not concrete: the // spelling of `std::function` says nothing about callability the way `std::string` does. inline bool concreteWrittenType( TSNode typeNode, std::string_view src, std::string& name ) { name.clear(); if( ts_node_is_null( typeNode ) ) { return false; } const char* tt = ts_node_type( typeNode ); if( kindIs( tt, "primitive_type" ) || kindIs( tt, "sized_type_specifier" ) || kindIs( tt, "struct_specifier" ) || kindIs( tt, "class_specifier" ) || kindIs( tt, "union_specifier" ) || kindIs( tt, "enum_specifier" ) ) { return true; } if( !kindIs( tt, "type_identifier" ) && !kindIs( tt, "qualified_identifier" ) && !kindIs( tt, "scoped_type_identifier" ) ) { return false; } const std::string_view text = nodeTextOf( typeNode, src ); if( text.empty() || text.find( '<' ) != std::string_view::npos ) { return false; } name = finalSegment( text ); return true; } // the TYPE-ALIAS name a node declares for a FUNCTION-POINTER type — `typedef void (*H)();` and // `using H = void(*)();` both yield "H"; every other typedef/alias yields "". This is the one piece of // evidence that separates a callable alias from an ordinary class name, both of which reach a declaration // as a bare `type_identifier`. Same-file only, which is the disclosed limit: an alias declared in a header // is invisible to a per-file parse, so a variable of that type stays UNKNOWN — and unknown still mints. inline std::string_view fnPtrAliasName( TSNode n, const char* t, std::string_view src ) { if( kindIs( t, "alias_declaration" ) ) { const TSNode desc = fieldChild( n, NodeField::Type ); if( ts_node_is_null( desc ) ) { return {}; } const TSNode abst = fieldChild( desc, NodeField::Declarator ); if( ts_node_is_null( abst ) || !kindIs( ts_node_type( abst ), "abstract_function_declarator" ) ) { return {}; } const TSNode nm = fieldChild( n, NodeField::Name ); return ts_node_is_null( nm ) ? std::string_view{} : nodeTextOf( nm, src ); } if( !kindIs( t, "type_definition" ) ) { return {}; } // O(children): the declarator-field scan rides the cursor's own O(1) field name instead of // `ts_node_field_name_for_child( n, i )`, which restarts the child iterator (src/infra/tschildren.h). // A typedef's width is input-controlled — a comment sits between `typedef` and the declarator as a // direct child like any other extra (test/childwalkscalecheck.sh, arm B7). std::string_view found; ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* fname = ts_tree_cursor_current_field_name( &cursor.cur ); if( fname == nullptr || !kindIs( fname, "declarator" ) ) { return true; } TSNode d = c; bool crossed = false; for( int guard = 0; guard < 10 && !ts_node_is_null( d ); ++guard ) { const char* dt = ts_node_type( d ); if( kindIs( dt, "type_identifier" ) ) { found = crossed ? nodeTextOf( d, src ) : std::string_view{}; return false; // the indexed loop returned from here } if( kindIs( dt, "function_declarator" ) ) { crossed = true; } TSNode inner = fieldChild( d, NodeField::Declarator ); if( ts_node_is_null( inner ) && ts_node_named_child_count( d ) > 0 ) { inner = ts_node_named_child( d, 0 ); } d = inner; } return true; } ); return found; } // the byte span of the DEFINITION a node sits inside — a function body or a lambda, whichever encloses it // first. {0,0} at file/namespace/class scope, which the gate reads as "applies everywhere": a file-scope // variable IS in scope in every function below it. inline std::pair enclosingDefSpan( TSNode n ) { TSNode p = parentOf( n ); for( int guard = 0; guard < 128 && !ts_node_is_null( p ); ++guard ) { const char* pt = ts_node_type( p ); if( kindIs( pt, "function_definition" ) || kindIs( pt, "lambda_expression" ) ) { return { ts_node_start_byte( p ), ts_node_end_byte( p ) }; } p = parentOf( p ); } return { 0u, 0u }; } // record one declaration node's type facts — one per DECLARED VARIABLE. Covers the three shapes that // declare a name a later `x = y;` can target: a block/file `declaration`, a function `parameter_declaration` // (a value parameter reassigned from another parameter is the same copy), and a `field_declaration`. inline void collectFnBindTypeFacts( TSNode n, const char* t, std::string_view src, std::vector& facts ) { if( !kindIs( t, "declaration" ) && !kindIs( t, "parameter_declaration" ) && !kindIs( t, "optional_parameter_declaration" ) && !kindIs( t, "field_declaration" ) ) { return; } std::string typeName; const bool concrete = concreteWrittenType( fieldChild( n, NodeField::Type ), src, typeName ); const auto [ scopeStart, scopeEnd ] = enclosingDefSpan( n ); // O(children), on the cursor's O(1) field name — see fnPtrAliasName above and arm B7. ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* fname = ts_tree_cursor_current_field_name( &cursor.cur ); if( fname == nullptr || !kindIs( fname, "declarator" ) ) { return true; } TSNode d = c; if( kindIs( ts_node_type( d ), "init_declarator" ) ) { d = fieldChild( d, NodeField::Declarator ); } const FnBindDeclShape shape = fnDeclaratorShape( d, src ); if( shape.name.empty() || ( shape.sawFn && !shape.sawPtr ) ) { return true; // nameless, an array of pointers, or a plain function DECLARATION — no variable here } facts.push_back( { std::string( shape.name ), typeName, scopeStart, scopeEnd, concrete, shape.sawFn && shape.sawPtr } ); return true; } ); } // the gate's whole per-node collection: a declaration's variable type facts AND, from the same node, any // function-pointer type alias it declares. `cFamily` is taken rather than checked at the call site so the // walk carries ONE unconditional line for the evidence — a `declaration` node feeds both the L3 capture // arms and the type facts here, and a typedef/alias node reaches neither of those arms. inline void collectFnBindGateEvidence( TSNode n, const char* t, std::string_view src, bool cFamily, FnBindGateState& gate ) { if( !cFamily ) { return; } collectFnBindTypeFacts( n, t, src, gate.facts ); if( const std::string_view alias = fnPtrAliasName( n, t, src ); !alias.empty() ) { gate.aliases.try_emplace( std::string( alias ), 1 ); } } // the gate's verdict for one assignment: true ⇒ the file PROVED this name is a value variable where the // assignment sits, so it is a copy and mints no binding. Only facts whose definition span CONTAINS the // assignment count (plus file-scope ones, which contain everything); among those, fn-pointer evidence wins // outright, and with no fact at all the name is unknown and the answer is false (mint, exactly as before). inline bool fnBindProvenValueVar( std::string_view var, std::uint32_t startByte, const std::vector& facts, const HashMap& fnAliases ) { bool proven = false; for( const FnBindVarTypeFact& f : facts ) { if( f.var != var ) { continue; } if( f.scopeEnd != 0u && ( startByte < f.scopeStart || startByte >= f.scopeEnd ) ) { continue; // declared inside a definition this assignment is not in — a different variable } const bool aliasTyped = !f.typeName.empty() && fnAliases.find( f.typeName ) != fnAliases.end(); if( f.fnPtrVariable || aliasTyped ) { return false; } proven = proven || ( f.concreteType && !aliasTyped ); } return proven; } // where a bind-record SITS: its own position (for enclosing-def attribution) plus, on VarDecl records, // the declaring BLOCK's byte range — the shadow scope model.h's suppressShadowedReferences tests sites // against ({0,0} on every other kind: contains nothing, inert by construction). struct BindSite { std::uint32_t startByte = 0; std::uint32_t spanStart = 0; std::uint32_t spanEnd = 0; }; // the ONE bind-record emitter. A nameless declarator records nothing. kind=VarDecl is the r9 shadow- // evidence record: typeName stays EMPTY on it (shadow evidence, not narrowing fuel — nothing downstream // ever reads a type off it), and so may an ImplBlock's (an implementor with no name), so the empty-typeName refusal applies to every OTHER kind, where it is // load-bearing for Rule 2 (an undecidable type must degrade to §2a, not mint a half-record). The DeclType's // qualified text rides importedName (see qualifiedNameText) — non-empty only on a declaration's Type/ParamType record. inline void pushTypedBind( std::uint32_t fileId, Lang lang, std::string_view var, const DeclType& type, BindSite site, LocalBindKind kind, std::vector& binds ) { if( var.empty() || ( type.name.empty() && kind != LocalBindKind::VarDecl && kind != LocalBindKind::ImplBlock ) ) { return; } RawBind b; b.fileId = fileId; b.startByte = site.startByte; b.lang = lang; b.kind = kind; b.spanStart = site.spanStart; b.spanEnd = site.spanEnd; b.var.assign( var ); b.typeName = type.name; b.importedName = type.qualified; b.isFromAssignment = type.isFromAssignment; b.unwrapDepth = ( type.directDeclarator && lang == Lang::Cpp && ( kind == LocalBindKind::Type || kind == LocalBindKind::ParamType ) ) ? kDirectDeclaratorBit : 0; binds.push_back( b ); } // the same record with no qualified text — every emitter but a declaration's Type/ParamType inline void pushRawBind( std::uint32_t fileId, Lang lang, std::string_view var, std::string typeName, BindSite site, LocalBindKind kind, std::vector& binds ) { pushTypedBind( fileId, lang, var, DeclType{ std::move( typeName ), std::string{} }, site, kind, binds ); } // emit a Rule-2 binding from one declared variable: prefer the WRITTEN type; else infer from a // constructor-style initializer (`auto x = Foo()`). Records nothing when neither is decidable. inline void emitBind( std::uint32_t fileId, Lang lang, std::string_view var, std::string typeName, std::uint32_t startByte, std::vector& binds ) { pushRawBind( fileId, lang, var, std::move( typeName ), BindSite{ startByte, 0u, 0u }, LocalBindKind::Type, binds ); } // the scope a `declaration` node's names shadow within: the byte span, plus whether that span came from a // PLAIN BLOCK (the only kind the declaration-point narrowing below applies to). {0,0} when nothing encloses // (file/namespace/class scope): such a record can contain no site and is inert by construction. struct ShadowScope { std::uint32_t start = 0; std::uint32_t end = 0; bool plainBlock = false; }; // r9 shadow fix round (A5, iteration 3): the byte span a declaration's names are scoped to. A declaration // in a control statement's HEADER — for-init (`for (int run = 0; ...)`), if/while/switch condition // (`if (int run = f())`) — scopes to THAT STATEMENT's full span (C++: the variable lives for the whole // statement, else-branch included), NOT the enclosing block: the header declaration is a SIBLING of the // statement's body, so the plain compound_statement walk of iteration 2 leaked the scope past the loop and // ate every genuine call after it. A header declaration reaches its control statement BEFORE any // compound_statement (bodies ARE compound_statements, and C++ forbids a declaration as a braceless body), // so "first ancestor of either kind wins" needs no field tracking — a body declaration hits the body block // first, a header declaration the statement first. That same discrimination is what plainBlock reports. inline ShadowScope enclosingShadowScope( TSNode n ) { TSNode p = parentOf( n ); for( int guard = 0; guard < 128 && !ts_node_is_null( p ); ++guard ) { const char* pt = ts_node_type( p ); if( kindIs( pt, "compound_statement" ) ) { return { ts_node_start_byte( p ), ts_node_end_byte( p ), true }; } // Java (and Go/Rust) spell a plain brace block `block`. C++ hits compound_statement // first, so this arm is inert there. Checked before lambda/method so a body local // stays in its block rather than the whole callable. if( kindIs( pt, "block" ) ) { return { ts_node_start_byte( p ), ts_node_end_byte( p ), true }; } if( kindIs( pt, "for_statement" ) || kindIs( pt, "for_range_loop" ) || kindIs( pt, "if_statement" ) || kindIs( pt, "while_statement" ) || kindIs( pt, "switch_statement" ) || kindIs( pt, "enhanced_for_statement" ) || kindIs( pt, "catch_clause" ) || kindIs( pt, "switch_block" ) || kindIs( pt, "try_with_resources_statement" ) ) { return { ts_node_start_byte( p ), ts_node_end_byte( p ), false }; } // Java parameters and inferred lambda names: the callable is the scope when no // block sits between the declaration and it. C++ lambda bodies are // compound_statement, so a C++ local still hits that first. if( kindIs( pt, "lambda_expression" ) || kindIs( pt, "method_declaration" ) || kindIs( pt, "constructor_declaration" ) ) { return { ts_node_start_byte( p ), ts_node_end_byte( p ), false }; } p = parentOf( p ); } return { 0u, 0u, false }; } // r9 shadow fix round (A5, iteration 4): where an ordinary block declaration's names START shadowing. // Iteration 2 started every span at the BLOCK's opening brace, which silently ate a genuine call written // ABOVE the shadowing local (`key(); int key = 0;` lost the call — verifier attack4, a recall loss, not the // disclosed over-suppression). THE DECLARATION POINT SHIPPED HERE IS THE END BYTE OF THE COMPLETE // DECLARATOR, which is C++ [basic.scope.pdecl] exactly: the locus of a declarator is immediately after the // complete declarator and before its initializer, and a structured binding's is immediately after its // identifier-list — the outermost declarator's end byte is both. So `int a = probe(), probe = 0, b = probe;` // keeps the call in a's initializer and suppresses b's read, and `int probe = probe;` suppresses its own // initializer (which IS the new local, indeterminate value and all). The point itself is exact — a byte // offset the grammar hands us, not an approximation — so what remains is the floor that was always there // and is now simply visible ABOVE the point too: a pre-declaration site is only KEPT, never resolved, so if // the name there denotes an OUTER local rather than the indexed symbol, --uses still name-matches it (the // header's own "reference-name-based" disclosure). The one declaration this cannot narrow is a declarator // tree emitShadowVarDecls refuses (`std::string key( tok );`, the most-vexing parse), which records no // evidence at all and is the disclosed floor already. // Applies ONLY to a plain block: a control-statement header declaration, and every whole-scope shape // (definition/lambda/catch parameters, captures, range-for variables), is in scope from the START of its // scope, so narrowing those would re-mint the false positives iterations 1-3 removed. inline std::uint32_t shadowSpanStart( const ShadowScope& scope, TSNode completeDeclarator ) { if( !scope.plainBlock || ts_node_is_null( completeDeclarator ) ) { return scope.start; } const std::uint32_t point = ts_node_end_byte( completeDeclarator ); return point > scope.start ? point : scope.start; } // the declarator one wrapping declarator holds: its `declarator` field, or, for the three kinds whose grammar rule // names no field — reference_declarator (`& d`), parenthesized_declarator (`( d )`) and attributed_declarator // (`d [[maybe_unused]]`) — its first named child. Null for a leaf (an identifier, a qualified name). inline TSNode innerDeclaratorOf( TSNode decl ) { const TSNode inner = fieldChild( decl, NodeField::Declarator ); if( !ts_node_is_null( inner ) ) { return inner; } const char* dt = ts_node_type( decl ); const bool holdsUnnamed = kindIs( dt, "reference_declarator" ) || kindIs( dt, "parenthesized_declarator" ) || kindIs( dt, "attributed_declarator" ); return ( holdsUnnamed && ts_node_named_child_count( decl ) > 0 ) ? ts_node_named_child( decl, 0 ) : TSNode{}; } // r9 shadow fix round (A5): every VARIABLE name a declarator declares → one VarDecl record each, carrying // the declaring block's span. Handles the shapes the verifier refuted the first landing on: // * reference_declarator / parenthesized_declarator hold their inner declarator as an UNNAMED child // (no `declarator` field — same grammar fact fnDeclaratorVarName already works around), so a // field-only unwrap missed `const T& key` entirely — pass-by-const-ref, the most idiomatic C++ // parameter shape; attributed_declarator (`int run [[maybe_unused]] = 0;`) is the same fact and // recorded nothing until 2026-09-17 (test/shadowcheck.sh arm q8) — all three unwrap in innerDeclaratorOf; // * structured_binding_declarator (`auto& [key, w]`) declares SEVERAL names — one record per identifier; // * a plain function declarator still yields NOTHING (`void helper();` in a body and the most-vexing- // parse `Foo x();` declare a FUNCTION, whose calls must never be suppressed), while a // function_declarator whose inner is PARENTHESIZED is a fn-POINTER variable and stays a variable. // Conservative by construction: an unrecognized shape captures nothing (under-suppression, the disclosed // floor — e.g. the ctor-style most-vexing `std::string key( tok );`, which parses as a function decl). inline void emitShadowVarDecls( std::uint32_t fileId, Lang lang, TSNode decl, std::string_view src, BindSite site, std::vector& binds ) { for( int guard = 0; guard < 8 && !ts_node_is_null( decl ); ++guard ) { const char* dt = ts_node_type( decl ); if( kindIs( dt, "identifier" ) ) { pushRawBind( fileId, lang, nodeTextOf( decl, src ), std::string{}, site, LocalBindKind::VarDecl, binds ); return; } if( kindIs( dt, "structured_binding_declarator" ) ) { const std::uint32_t cc = ts_node_named_child_count( decl ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode c = ts_node_named_child( decl, i ); if( kindIs( ts_node_type( c ), "identifier" ) ) { pushRawBind( fileId, lang, nodeTextOf( c, src ), std::string{}, site, LocalBindKind::VarDecl, binds ); } } return; } const TSNode inner = innerDeclaratorOf( decl ); if( ts_node_is_null( inner ) ) { return; } if( kindIs( dt, "function_declarator" ) && !kindIs( ts_node_type( inner ), "parenthesized_declarator" ) ) { return; // a FUNCTION's name, not a variable's } decl = inner; } } // one DECLARATOR → both records: the Rule-2 var→type binding and the r9 VarDecl shadow record(s). The two // name reads stay separate on purpose — declaratorVarName descends into a function declarator (harmless // for narrowing), emitShadowVarDecls refuses it (load-bearing for suppression). // C++: the variable a declarator names under `&` / `&&` — and under `*` too when `throughPointer` (`const auto& c`, `auto* p`); // null for anything else (an array, a function declarator, a pointer when not asked for) inline TSNode cppDeclaratorName( TSNode d, bool throughPointer ) { for( int guard = 0; guard < 4 && !ts_node_is_null( d ); ++guard ) { const char* t = ts_node_type( d ); if( kindIs( t, "identifier" ) ) { return d; } if( !kindIs( t, "reference_declarator" ) && !( throughPointer && kindIs( t, "pointer_declarator" ) ) ) { return {}; } d = ts_node_named_child_count( d ) > 0 ? ts_node_named_child( d, ts_node_named_child_count( d ) - 1 ) : TSNode{}; } return {}; } // CPP-PTR-ALIAS: true when a C++ declarator names the variable itself or a reference to it (`T x`, `const T& x`, `T&& x`) — so // `x->m()` on a smart-pointer type reaches the pointee — and false through a pointer, array or function declarator (`T* x`, // `T x[ 4 ]`, `T (*x)()`), where `->` reaches the written type itself. An unreadable shape is false. inline bool cppDirectDeclarator( TSNode d ) { return !ts_node_is_null( cppDeclaratorName( d, /*throughPointer=*/false ) ); } inline void emitDeclBinds( std::uint32_t fileId, Lang lang, TSNode declNode, std::string_view src, DeclType type, BindSite site, std::vector& binds ) { type.directDeclarator = cppDirectDeclarator( declNode ); const std::string_view var = declaratorVarName( declNode, src ); if( var.empty() && !type.name.empty() ) { // member-variable round (card A3): a REFERENCE local (`const Symbol& s = ing.symbols[ i ];`) is the one // typed declaration Rule 2's flat table refuses (declaratorVarName cannot see through the unnamed reference // child). Recorded as a ParamType fact, so `s.name` resolves in the field use-site index and `s.m()` narrows // through Rule 2's LEXICAL lookup (resolve.h buildScopedRecvDecls) — only where this declaration is in scope. pushTypedBind( fileId, lang, paramDeclaratorVarName( declNode, src ), type, BindSite{ site.startByte, 0u, 0u }, LocalBindKind::ParamType, binds ); } else { pushTypedBind( fileId, lang, var, type, BindSite{ site.startByte, 0u, 0u }, LocalBindKind::Type, binds ); } emitShadowVarDecls( fileId, lang, declNode, src, site, binds ); } // one parameter_list → VarDecl records for its named parameters, scoped to the owning BODY's span. Shared // by the function-definition and lambda arms below (their parameter semantics are identical: names local // to the body). inline void emitShadowParamDecls( TSNode params, std::uint32_t fileId, Lang lang, std::string_view src, BindSite bodySite, std::vector& binds ) { const std::uint32_t cc = ts_node_child_count( params ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode p = ts_node_child( params, i ); const char* pt = ts_node_type( p ); if( !kindIs( pt, "parameter_declaration" ) && !kindIs( pt, "optional_parameter_declaration" ) ) { continue; // commas, `...`, attribute nodes — nothing declared } bodySite.startByte = ts_node_start_byte( p ); const TSNode declarator = fieldChild( p, NodeField::Declarator ); emitShadowVarDecls( fileId, lang, declarator, src, bodySite, binds ); // member-variable round (card A3): the parameter's WRITTEN type as a ParamType record (`Counter& c` → // c:Counter), read by the field use-site index and Rule 2's lexical lookup — see LocalBindKind::ParamType. `auto`, templated // and decltype types write nothing (writtenTypeOf's own refusal), and pushRawBind drops the record. DeclType written = declTypeOfName( fieldChild( p, NodeField::Type ), src ); written.directDeclarator = cppDirectDeclarator( declarator ); pushTypedBind( fileId, lang, paramDeclaratorVarName( declarator, src ), std::move( written ), BindSite{ ts_node_start_byte( p ), 0u, 0u }, LocalBindKind::ParamType, binds ); } } // A5 fix round: one LAMBDA's shadow-evidence names — parameters and capture-list names, all scoped to the // lambda BODY's span. Lambdas are expressions, not definitions, so the definition arm below never sees // them (the r9 sweep's A01 query is exactly a lambda parameter shadowing an indexed function). A simple // capture (`[run]`) re-binds an outer VARIABLE (a function cannot be captured, so the name always denotes // a variable) and an init-capture (`[trim = expr]`, node lambda_capture_initializer) introduces a NEW // name — both are VarDecl evidence for the body span. inline void captureLambdaShadowDecls( TSNode n, std::uint32_t fileId, Lang lang, std::string_view src, BindSite bodySite, std::vector& binds ) { const TSNode d = fieldChild( n, NodeField::Declarator ); // abstract_function_declarator if( !ts_node_is_null( d ) ) { const TSNode params = fieldChild( d, NodeField::Parameters ); if( !ts_node_is_null( params ) ) { emitShadowParamDecls( params, fileId, lang, src, bodySite, binds ); } } const TSNode caps = fieldChild( n, NodeField::Captures ); // lambda_capture_specifier if( ts_node_is_null( caps ) ) { return; } // O(captures): a capture list's width is input-set like every other list — a comment run between two // captures is spliced into its child array (src/infra/tschildren.h), and 16 000 of them measured // 1.25 s of plain map on a one-line file before this became a cursor (arm B12). ChildCursor capsCursor( caps ); forEachNamedChild( caps, capsCursor.cur, [ & ]( TSNode c ) { const char* ct = ts_node_type( c ); TSNode ident {}; if( kindIs( ct, "identifier" ) ) { ident = c; // simple capture `[run]` / `[&run]` (the `&` is an anonymous sibling) } else if( kindIs( ct, "lambda_capture_initializer" ) && ts_node_named_child_count( c ) > 0 ) { const TSNode nm = ts_node_named_child( c, 0 ); // `[trim = expr]` — the FIRST named child is the introduced name if( kindIs( ts_node_type( nm ), "identifier" ) ) { ident = nm; } } if( !ts_node_is_null( ident ) ) { bodySite.startByte = ts_node_start_byte( c ); pushRawBind( fileId, lang, nodeTextOf( ident, src ), std::string{}, bodySite, LocalBindKind::VarDecl, binds ); } return true; } ); } // a function DEFINITION's parameter_list, reached through its own declarator chain (`char* f(...)` / // `T& f(...)` unwrap to the function_declarator). Null when the shape isn't a plain definition — // walking only THIS chain (never bare parameter_declaration nodes) is what keeps a PROTOTYPE's // parameters and a fn-pointer TYPE's parameter list out of shadow evidence. The `T&` / `T&&` step goes // through innerDeclaratorOf: a reference_declarator holds the function declarator by no field, and a // field-only walk stopped there, so every definition returning a reference recorded no parameter at all // (2026-09-17, test/narrowcheck.sh arms 61-63, test/shadowcheck.sh arm am). inline TSNode fnDefParameterList( TSNode fnDef ) { TSNode decl = fieldChild( fnDef, NodeField::Declarator ); for( int guard = 0; guard < 8 && !ts_node_is_null( decl ) && !kindIs( ts_node_type( decl ), "function_declarator" ); ++guard ) { decl = innerDeclaratorOf( decl ); } if( ts_node_is_null( decl ) || !kindIs( ts_node_type( decl ), "function_declarator" ) ) { return TSNode{}; } return fieldChild( decl, NodeField::Parameters ); } // r9 shadow suppression (A5 fix round): the local-declaring shapes that live OUTSIDE `declaration` nodes // (the Rule-2 branch never sees them), dispatched on the caller's already-read node type `t`: // * a range-for's loop variable (`for( auto& s : v )`, incl. structured bindings) — scoped to the WHOLE // loop statement (iteration 3, unified with enclosingShadowScope's control-statement rule); // * a C++/ObjC function DEFINITION's named parameters — scoped to the definition BODY's span. Walking // only the definition node's own declarator chain (never bare parameter_declaration nodes) is what // keeps two non-scopes out: a PROTOTYPE's parameters (`void f(int run);` binds nothing anywhere) and a // fn-pointer type's parameter list (`void (*cb)(int run)` — those names are part of a TYPE, in no // scope at all); // * a LAMBDA's parameters and capture-list names — captureLambdaShadowDecls above; // * a CATCH clause's parameter — a local of its handler block (iteration 3, the noted 3b gap). // Gates the language and node type ITSELF, so captureBindings calls it unconditionally — the shapes are // disjoint from every branch of the Rule-2 chain there. // Phase 4b (Rule 2c's shadow veto, docs/EVALS.md): a Python function DEFINITION's parameter NAMES as VarDecl // evidence with an EMPTY span — the `{0,0}` "contains nothing" shape model.h's Binding documents — so they // reach buildFieldNarrowTables' localNameSet (the veto Rules 2b/2c share: a parameter named like a class is a // variable, not the class) and NOTHING else: the r9 shadow suppression tests span containment and is // registered and measured for C++/ObjC only, so the Python call graph moves through Rule 2c alone. `self` // and `cls` are recorded like any other name (no class is spelled that way; a special case would be a // second rule to keep in step). Plain, typed, defaulted and splat parameters; tuple patterns bind nothing. // Java issue #74: a parameter or local named like a class is a value receiver for // Identifier::method, but only where Java lexical scope makes that binding active. // Class fields keep empty spans and are copied onto contained methods in graph.h. // Locals start at the declarator (plain block) or the enclosing statement; parameters // and inferred lambda names cover the callable body. inline bool javaKindIsFieldDecl( const char* t ) noexcept { return kindIs( t, "field_declaration" ) || kindIs( t, "constant_declaration" ); } inline bool javaKindIsCallable( const char* t ) noexcept { return kindIs( t, "method_declaration" ) || kindIs( t, "constructor_declaration" ) || kindIs( t, "lambda_expression" ); } inline BindSite javaShadowSite( TSNode declNode, TSNode nameNode ) noexcept { const std::uint32_t start = ts_node_start_byte( nameNode ); TSNode p = parentOf( declNode ); for( int guard = 0; guard < 128 && !ts_node_is_null( p ); ++guard ) { const char* pt = ts_node_type( p ); if( javaKindIsFieldDecl( pt ) ) { return { start, 0u, 0u }; } if( kindIs( pt, "block" ) || kindIs( pt, "for_statement" ) || kindIs( pt, "enhanced_for_statement" ) || kindIs( pt, "switch_block" ) || kindIs( pt, "catch_clause" ) || kindIs( pt, "try_with_resources_statement" ) ) { break; } if( javaKindIsCallable( pt ) ) { const TSNode body = fieldChild( p, NodeField::Body ); if( ts_node_is_null( body ) ) { return { start, ts_node_start_byte( p ), ts_node_end_byte( p ) }; } return { start, ts_node_start_byte( body ), ts_node_end_byte( body ) }; } p = parentOf( p ); } const ShadowScope scope = enclosingShadowScope( declNode ); return { start, shadowSpanStart( scope, declNode ), scope.end }; } inline void emitJavaShadowName( std::uint32_t fileId, Lang lang, TSNode declNode, TSNode nameNode, std::string_view src, std::vector& binds ) { if( ts_node_is_null( nameNode ) || !kindIs( ts_node_type( nameNode ), "identifier" ) ) { return; } pushRawBind( fileId, lang, nodeTextOf( nameNode, src ), std::string{}, javaShadowSite( declNode, nameNode ), LocalBindKind::VarDecl, binds ); } inline void captureJavaShadowDecls( TSNode n, const char* t, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { // A catch parameter and a try-with-resources `resource` declare a name the same way (a `name:` field; a resource // that only NAMES an existing variable has none, and emits nothing). Their scope is the catch clause or the whole // try-with-resources statement, which javaShadowSite reaches through enclosingShadowScope. if( kindIs( t, "formal_parameter" ) || kindIs( t, "spread_parameter" ) || kindIs( t, "variable_declarator" ) || kindIs( t, "catch_formal_parameter" ) || kindIs( t, "resource" ) ) { emitJavaShadowName( fileId, lang, n, fieldChild( n, NodeField::Name ), src, binds ); return; } // `for ( T x : xs )` — the loop itself declares x, so the NAME is passed as the declaration node: its parent is the // loop, whose span is x's scope. Passing the statement would start the walk at the enclosing block and widen the // veto to every reference after the loop (test/javamethodrefcheck.sh forEachAfterFn). if( kindIs( t, "enhanced_for_statement" ) ) { const TSNode name = fieldChild( n, NodeField::Name ); emitJavaShadowName( fileId, lang, name, name, src, binds ); return; } // `Widget -> ...` — the inferred parameter is the `parameters:` identifier of the lambda. if( kindIs( t, "lambda_expression" ) ) { const TSNode params = fieldChild( n, NodeField::Parameters ); if( !ts_node_is_null( params ) && kindIs( ts_node_type( params ), "identifier" ) ) { emitJavaShadowName( fileId, lang, params, params, src, binds ); } return; } // `(Widget) -> ...` / `(a, b) -> ...` — inferred_parameters holds identifier children. if( kindIs( t, "inferred_parameters" ) ) { const std::uint32_t cc = ts_node_named_child_count( n ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode c = ts_node_named_child( n, i ); if( kindIs( ts_node_type( c ), "identifier" ) ) { emitJavaShadowName( fileId, lang, c, c, src, binds ); } } } } // issue #287 round 2 (Rule 2d soundness, review rv-p6.md HIGH finding): collect every bare NAME a Python // assignment-target-shaped node binds, at any depth through tuple/list unpacking and a starred target — // NEVER through an attribute or subscript target (`obj.tm = x` / `d["tm"] = x` mutate an object; they do // not rebind a NAME). `target` and every node reached through it come straight from the parse tree — a // shape this function does not recognise (an attribute, a subscript, a malformed unpacking) is refused by // simply not collecting it, the same graceful skip every sibling capture in this file already uses for an // unexpected grammar shape, not a promise this function makes about what parsed source contains. inline void collectPythonNameTargets( TSNode target, std::vector& out ) { if( ts_node_is_null( target ) ) { return; } const char* tt = ts_node_type( target ); if( kindIs( tt, "identifier" ) ) { out.push_back( target ); return; } if( kindIs( tt, "pattern_list" ) || kindIs( tt, "tuple_pattern" ) || kindIs( tt, "list_pattern" ) || kindIs( tt, "expression_list" ) ) { const std::uint32_t cc = ts_node_named_child_count( target ); for( std::uint32_t i = 0; i < cc; ++i ) { collectPythonNameTargets( ts_node_named_child( target, i ), out ); } return; } if( kindIs( tt, "list_splat_pattern" ) || kindIs( tt, "dictionary_splat_pattern" ) ) { if( ts_node_named_child_count( target ) > 0 ) { collectPythonNameTargets( ts_node_named_child( target, 0 ), out ); // `*rest` / `**rest` — unwrap to the bound name } return; } // attribute / subscript / anything else this shape doesn't recognise — not a bare-name rebind. } // issue #287 round 2 (Rule 2d soundness, review rv-p6.md HIGH finding). Rule 2d's `hasLocal` guard // (graph.h ExternalVeto::hasLocal) only sees evidence from a Binding, and Python previously recorded // NONE for a plain reassignment — only Import (file scope) and a parameter's VarDecl // (capturePythonParamShadowDecls, just below) ever reached it. So `import target_mod as tm` followed by // `tm = {"run": …}` inside the SAME function left the reassignment invisible: Rule 2d saw no local // evidence for `tm` and confidently bound `tm.run(1)` to the import's module after the name had stopped // meaning that. This closes the gap: every Python name-REBINDING form emits a `LocalBindKind::VarDecl` // RawBind — the exact "veto evidence only, empty span" shape `capturePythonParamShadowDecls` already // uses — so a reassignment and a parameter shadow read as the identical fact to Rule 2d's guard. // // SCOPE, and why `startByte` alone decides it. `fromSymbol = bindSweep.find( fileId, startByte )` // (ingest_model.h emitBindings) is the SAME byte-to-enclosing-def sweep every other bind kind already // rides — nothing new to plumb. A form physically INSIDE a function attributes to that function, so // `hasLocal` refuses only THAT function's uses of the name — sound, because a plain Python assignment // with no `global`/`nonlocal` is function-local. A form at true module top level gets NO enclosing def // (`fromSymbol==kNoNode`) — `buildFieldNarrowTables` already skips `fromSymbol==kNoNode` bindings for // `hasLocal` on purpose, so these never reach it; `graph.h`'s `pythonModuleRebind` table (built in // `buildExternalVetoTables`) reads them instead and vetoes the WHOLE FILE's use of the name, because a // module-global rebind can reach every function that reads it. `global NAME` / `nonlocal NAME` get that // SAME file-wide treatment ON PURPOSE even when the statement sits inside a function — such a statement // can rewrite the file's own module global — so it is deliberately recorded at `startByte=0` (guaranteed // to resolve to no enclosing def) rather than at its own position, which would otherwise only veto the // one function that happens to declare it. // // NEVER GUESS, never over-claim either: comprehension-scoped targets (`[x for x in …]` is a // `for_in_clause`, not the `for_statement` this function matches) do NOT leak into the enclosing scope in // Python 3 and are correctly never visited here. A nested `def NAME` / `class NAME` shadows NAME in its // ENCLOSING scope for the rest of that scope (Python binds the def statement's own name where the def // STATEMENT sits, not inside the function it defines) — attributed one byte BEFORE the nested def/class's // own span starts, so the fact lands on the enclosing scope rather than inside the newly-declared one // (verified empirically with `--match`, not merely reasoned about: a byte at the def/class node's own // start resolves INSIDE that new symbol's own span, one byte earlier does not). // Is `name` declared `global`/`nonlocal` anywhere in the function that encloses `n` — searched over that // function's OWN body only, never crossing into a nested def/class/lambda's body (a nested scope's // global/nonlocal statements bind there, not here)? Python's `global`/`nonlocal` statement scopes the // WHOLE enclosing function regardless of where in the function body it textually sits, so a reassignment // anywhere in that function to a name the function has declared global/nonlocal never creates a local // shadow — it rewrites the OUTER binding, same as the declaration statement itself (see the SCOPE doc // above capturePythonRebindShadowDecls). `n` with no enclosing function_definition is module scope, where // `global`/`nonlocal` cannot appear — always false there. inline bool pythonNameIsGlobalOrNonlocalHere( TSNode n, std::string_view name, std::string_view src ) { const bool nGiven = !ts_node_is_null( n ); EXPECTS( nGiven, "n is the rebind/for/named_expression/as_pattern node the caller is currently visiting — " "the tree walker never dispatches on a null node" ); TSNode fn = parentOf( n ); while( !ts_node_is_null( fn ) && !kindIs( ts_node_type( fn ), "function_definition" ) ) { fn = parentOf( fn ); } const bool fnIsNullOrFnDef = ts_node_is_null( fn ) || kindIs( ts_node_type( fn ), "function_definition" ); ASSUME( fnIsNullOrFnDef, "the loop above exits only when fn is null (walked off the tree) or a function_definition" ); if( ts_node_is_null( fn ) ) { return false; // module scope — global/nonlocal cannot appear there } const TSNode body = fieldChild( fn, NodeField::Body ); if( ts_node_is_null( body ) ) { return false; } std::vector stack{ body }; while( !stack.empty() ) { const TSNode cur = stack.back(); stack.pop_back(); const char* ct = ts_node_type( cur ); if( kindIs( ct, "global_statement" ) || kindIs( ct, "nonlocal_statement" ) ) { const std::uint32_t cc = ts_node_named_child_count( cur ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode id = ts_node_named_child( cur, i ); if( kindIs( ts_node_type( id ), "identifier" ) && nodeTextOf( id, src ) == name ) { return true; } } continue; } if( kindIs( ct, "function_definition" ) || kindIs( ct, "class_definition" ) || kindIs( ct, "lambda" ) ) { continue; // nested scope — never crossed } const std::uint32_t cc = ts_node_named_child_count( cur ); for( std::uint32_t i = 0; i < cc; ++i ) { stack.push_back( ts_node_named_child( cur, i ) ); } } return false; } // A Python match-statement pattern or a PEP 695 `type` statement binds at most ONE name at the node itself. Grammar shapes (read off the // tree): a capture pattern is a `case_pattern` holding a dotted_name of ONE identifier (`case Base:`), a keyword pattern's value is the same // dotted_name (`Cls(x=Base)`), `*Base` and `**Base` are a splat_pattern, and the `as` target of `case P as Base` is the identifier that // follows the case_pattern inside the `as_pattern` (no `alias` field there, unlike `with ... as`). A dotted_name of two or more // identifiers (`case Color.RED`) is a value pattern and binds nothing. `type Base = ...` / `type Base[T] = ...` binds the left side's // first identifier. inline TSNode pythonIdentifierChild( TSNode n, std::uint32_t i ) // the named child at i when it is an identifier, else null { const bool ok = !ts_node_is_null( n ) && i < ts_node_named_child_count( n ) && kindIs( ts_node_type( ts_node_named_child( n, i ) ), "identifier" ); return ok ? ts_node_named_child( n, i ) : TSNode{}; } inline TSNode pythonSoleName( TSNode dotted ) // a dotted_name of exactly one identifier, else null { const bool one = !ts_node_is_null( dotted ) && kindIs( ts_node_type( dotted ), "dotted_name" ) && ts_node_named_child_count( dotted ) == 1; return one ? pythonIdentifierChild( dotted, 0 ) : TSNode{}; } inline TSNode pythonTypeAliasName( TSNode n ) // `type Base = ...` / `type Base[T] = ...`: type > identifier | generic_type > identifier { const TSNode left = fieldChild( n, NodeField::Left ); const TSNode head = !ts_node_is_null( left ) && ts_node_named_child_count( left ) > 0 ? ts_node_named_child( left, 0 ) : TSNode{}; return !ts_node_is_null( head ) && kindIs( ts_node_type( head ), "generic_type" ) ? pythonIdentifierChild( head, 0 ) : pythonIdentifierChild( left, 0 ); } inline TSNode pythonPatternTarget( TSNode n, const char* t ) { const std::uint32_t cc = ts_node_named_child_count( n ); if( kindIs( t, "case_pattern" ) ) { return cc == 1 ? pythonSoleName( ts_node_named_child( n, 0 ) ) : TSNode{}; } if( kindIs( t, "keyword_pattern" ) ) { return cc == 2 ? pythonSoleName( ts_node_named_child( n, 1 ) ) : TSNode{}; } if( kindIs( t, "splat_pattern" ) ) { return cc == 1 ? pythonIdentifierChild( n, 0 ) : TSNode{}; } if( kindIs( t, "as_pattern" ) && ts_node_is_null( fieldChild( n, NodeField::Alias ) ) ) // the with/except form has an alias field { return cc > 0 ? pythonIdentifierChild( n, cc - 1 ) : TSNode{}; } return kindIs( t, "type_alias_statement" ) ? pythonTypeAliasName( n ) : TSNode{}; } // a Python name node as a VarDecl at byte `at` (veto evidence: the match/`type`/type-parameter records below); a null node records nothing inline void pushPythonNodeDecl( TSNode id, std::uint32_t at, std::uint32_t fileId, std::string_view src, std::vector& binds ) { if( !ts_node_is_null( id ) ) { pushRawBind( fileId, Lang::Python, nodeTextOf( id, src ), std::string{}, BindSite{ at, 0u, 0u }, LocalBindKind::VarDecl, binds ); } } // the name a match pattern or `type` statement binds, as a VarDecl at the statement (veto evidence like capturePythonRebindShadowDecls'), // or at byte 0 when the enclosing function declares it global/nonlocal inline void capturePythonPatternDecls( TSNode n, std::uint32_t fileId, std::string_view src, std::vector& binds ) { const TSNode id = pythonPatternTarget( n, ts_node_type( n ) ); if( ts_node_is_null( id ) ) { return; } const std::uint32_t at = pythonNameIsGlobalOrNonlocalHere( n, nodeTextOf( id, src ), src ) ? 0u : ts_node_start_byte( n ); pushPythonNodeDecl( id, at, fileId, src, binds ); } // PEP 695 `class C[Base](Base)` / `def f[Base]()`: each type parameter is a name the base clause can resolve to (a TypeVar, so CPython // raises TypeError and no derivation exists). One `type` per name; a bound `Base: int` wraps its name in a constrained_type, `*Ts` / `**P` // in a splat_type. A def's parameters are visible inside it (a VarDecl at the list); a class's are visible to its own base clause, which // the scope chain reads from OUTSIDE the class, so it is filed one byte before the class like the class's own name. inline TSNode pythonTypeParamName( TSNode param ) // `type` > identifier | constrained_type > type > identifier | splat_type > identifier { TSNode c = param; for( int depth = 0; depth < 4 && !ts_node_is_null( c ) && !kindIs( ts_node_type( c ), "identifier" ); ++depth ) { c = ts_node_named_child_count( c ) > 0 ? ts_node_named_child( c, 0 ) : TSNode{}; } return !ts_node_is_null( c ) && kindIs( ts_node_type( c ), "identifier" ) ? c : TSNode{}; } inline void capturePythonTypeParamDecls( TSNode n, std::uint32_t fileId, std::string_view src, std::vector& binds ) { const bool isClass = kindIs( ts_node_type( n ), "class_definition" ); const TSNode list = isClass || kindIs( ts_node_type( n ), "function_definition" ) ? fieldChild( n, NodeField::TypeParameters ) : TSNode{}; if( ts_node_is_null( list ) ) { return; } const std::uint32_t own = ts_node_start_byte( n ); const std::uint32_t at = isClass ? ( own > 0 ? own - 1 : 0 ) : ts_node_start_byte( list ); for( std::uint32_t i = 0, k = ts_node_named_child_count( list ); i < k; ++i ) { pushPythonNodeDecl( pythonTypeParamName( ts_node_named_child( list, i ) ), at, fileId, src, binds ); } } // is the def/class statement `n` at MODULE scope but inside a compound statement (`if`/`try`/`with`/`for`/`while`/`match`, any // clause), so it may not run? False for a statement directly in the module (a decorator wrapping it changes nothing) and for one // inside a def or class (not module scope). Read by the kPythonCondDefPrefix record below. inline bool pythonDefIsConditionalAtModule( TSNode n ) { TSNode p = parentOf( n ); if( !ts_node_is_null( p ) && kindIs( ts_node_type( p ), "decorated_definition" ) ) { p = parentOf( p ); } if( ts_node_is_null( p ) || kindIs( ts_node_type( p ), "module" ) ) { return false; } for( ; !ts_node_is_null( p ); p = parentOf( p ) ) { const char* pt = ts_node_type( p ); if( kindIs( pt, "function_definition" ) || kindIs( pt, "class_definition" ) || kindIs( pt, "lambda" ) ) { return false; } if( kindIs( pt, "module" ) ) { return true; } } return false; } // the kPythonCondDefPrefix record (`?name`) of a def/class statement `n` that may not run (pythonDefIsConditionalAtModule), at the // def's own VarDecl site; nothing for any other statement inline void pushPythonCondDefMark( TSNode n, std::string_view name, std::uint32_t fileId, Lang lang, BindSite at, std::vector& binds ) { if( pythonDefIsConditionalAtModule( n ) ) { pushRawBind( fileId, lang, std::string{ kPythonCondDefPrefix }.append( name ), std::string{}, at, LocalBindKind::VarDecl, binds ); } } // A Python lambda parameter or comprehension target is local to that lambda / comprehension: filed as a VarDecl `~name` // (kPythonLocalScopePrefix) whose span is the lambda / comprehension (src/regindex.h reads it as "name is another variable here"). inline void pushPythonLocalScopeDecl( TSNode id, TSNode scope, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { if( !ts_node_is_null( id ) && kindIs( ts_node_type( id ), "identifier" ) ) { pushRawBind( fileId, lang, std::string{ kPythonLocalScopePrefix }.append( nodeTextOf( id, src ) ), std::string{}, BindSite{ ts_node_start_byte( scope ), ts_node_start_byte( scope ), ts_node_end_byte( scope ) }, LocalBindKind::VarDecl, binds ); } } inline void capturePythonLocalScopeDecls( TSNode n, const char* t, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { if( kindIs( t, "for_in_clause" ) ) { std::vector targets; collectPythonNameTargets( fieldChild( n, NodeField::Left ), targets ); for( const TSNode& id : targets ) { pushPythonLocalScopeDecl( id, parentOf( n ), fileId, lang, src, binds ); } return; } if( !kindIs( t, "lambda" ) ) { return; } const TSNode params = fieldChild( n, NodeField::Parameters ); const std::uint32_t cc = ts_node_is_null( params ) ? 0u : ts_node_named_child_count( params ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode p = ts_node_named_child( params, i ); const char* pt = ts_node_type( p ); if( kindIs( pt, "default_parameter" ) ) { pushPythonLocalScopeDecl( fieldChild( p, NodeField::Name ), n, fileId, lang, src, binds ); } else if( kindIs( pt, "list_splat_pattern" ) || kindIs( pt, "dictionary_splat_pattern" ) ) { pushPythonLocalScopeDecl( ts_node_named_child( p, 0 ), n, fileId, lang, src, binds ); } else { pushPythonLocalScopeDecl( p, n, fileId, lang, src, binds ); } } } inline void capturePythonRebindShadowDecls( TSNode n, const char* t, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { std::vector targets; // reused scratch, cleared before each shape below // `ownStart` is the fallback attribution (this statement's own position, function-local veto evidence); // a name this function has declared global/nonlocal gets the SAME file-wide startByte=0 treatment the // declaration statement itself uses below, because the reassignment rewrites the outer binding, not a // local one — see pythonNameIsGlobalOrNonlocalHere. const auto emitAll = [ & ]( TSNode site, std::uint32_t ownStart ) { for( const TSNode& id : targets ) { const std::uint32_t at = pythonNameIsGlobalOrNonlocalHere( site, nodeTextOf( id, src ), src ) ? 0u : ownStart; pushRawBind( fileId, lang, nodeTextOf( id, src ), std::string{}, BindSite{ at, 0u, 0u }, LocalBindKind::VarDecl, binds ); } }; if( kindIs( t, "assignment" ) || kindIs( t, "augmented_assignment" ) ) { targets.clear(); collectPythonNameTargets( fieldChild( n, NodeField::Left ), targets ); emitAll( n, ts_node_start_byte( n ) ); return; } if( kindIs( t, "for_statement" ) ) { targets.clear(); collectPythonNameTargets( fieldChild( n, NodeField::Left ), targets ); emitAll( n, ts_node_start_byte( n ) ); return; } if( kindIs( t, "named_expression" ) ) // walrus `(tm := …)` — the grammar allows only a bare name here { const TSNode nm = fieldChild( n, NodeField::Name ); if( !ts_node_is_null( nm ) && kindIs( ts_node_type( nm ), "identifier" ) ) { const std::uint32_t ownStart = ts_node_start_byte( n ); const std::uint32_t at = pythonNameIsGlobalOrNonlocalHere( n, nodeTextOf( nm, src ), src ) ? 0u : ownStart; pushRawBind( fileId, lang, nodeTextOf( nm, src ), std::string{}, BindSite{ at, 0u, 0u }, LocalBindKind::VarDecl, binds ); } return; } if( kindIs( t, "as_pattern" ) ) // `with X as tm:` AND `except X as tm:` share this ONE grammar node { const TSNode alias = fieldChild( n, NodeField::Alias ); const bool hasTgt = !ts_node_is_null( alias ) && kindIs( ts_node_type( alias ), "as_pattern_target" ) && ts_node_named_child_count( alias ) > 0; const TSNode target = hasTgt ? ts_node_named_child( alias, 0 ) : alias; // grammar wraps the alias in `as_pattern_target` if( !ts_node_is_null( target ) && kindIs( ts_node_type( target ), "identifier" ) ) { const std::uint32_t ownStart = ts_node_start_byte( n ); const std::uint32_t at = pythonNameIsGlobalOrNonlocalHere( n, nodeTextOf( target, src ), src ) ? 0u : ownStart; pushRawBind( fileId, lang, nodeTextOf( target, src ), std::string{}, BindSite{ at, 0u, 0u }, LocalBindKind::VarDecl, binds ); } return; } if( kindIs( t, "delete_statement" ) ) // `del tm` (single target) / `del tm, other` (wrapped in expression_list) { targets.clear(); const std::uint32_t cc = ts_node_named_child_count( n ); for( std::uint32_t i = 0; i < cc; ++i ) { collectPythonNameTargets( ts_node_named_child( n, i ), targets ); } emitAll( n, ts_node_start_byte( n ) ); return; } if( kindIs( t, "global_statement" ) || kindIs( t, "nonlocal_statement" ) ) { const std::uint32_t cc = ts_node_named_child_count( n ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode id = ts_node_named_child( n, i ); if( kindIs( ts_node_type( id ), "identifier" ) ) { // deliberate startByte=0 — file-wide, regardless of physical nesting; see the doc above. pushRawBind( fileId, lang, nodeTextOf( id, src ), std::string{}, BindSite{ 0u, 0u, 0u }, LocalBindKind::VarDecl, binds ); } } return; } if( kindIs( t, "function_definition" ) || kindIs( t, "class_definition" ) ) { const TSNode nm = fieldChild( n, NodeField::Name ); if( !ts_node_is_null( nm ) && kindIs( ts_node_type( nm ), "identifier" ) ) { const std::uint32_t at = ts_node_start_byte( n ); // the def/class node's OWN start — see the doc above pushRawBind( fileId, lang, nodeTextOf( nm, src ), std::string{}, BindSite{ at > 0 ? at - 1 : 0, 0u, 0u }, LocalBindKind::VarDecl, binds ); pushPythonCondDefMark( n, nodeTextOf( nm, src ), fileId, lang, BindSite{ at > 0 ? at - 1 : 0, 0u, 0u }, binds ); } return; } } inline void capturePythonParamShadowDecls( TSNode n, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { const TSNode params = fieldChild( n, NodeField::Parameters ); if( ts_node_is_null( params ) ) { return; } const std::uint32_t cc = ts_node_child_count( params ); for( std::uint32_t i = 0; i < cc; ++i ) { const TSNode p = ts_node_child( params, i ); const char* pt = ts_node_type( p ); TSNode ident{}; if( kindIs( pt, "identifier" ) ) { ident = p; } else if( kindIs( pt, "default_parameter" ) || kindIs( pt, "typed_default_parameter" ) ) { ident = fieldChild( p, NodeField::Name ); } else if( kindIs( pt, "typed_parameter" ) || kindIs( pt, "list_splat_pattern" ) || kindIs( pt, "dictionary_splat_pattern" ) ) { ident = ts_node_named_child( p, 0 ); } if( ts_node_is_null( ident ) || !kindIs( ts_node_type( ident ), "identifier" ) ) { continue; // commas, separators, tuple patterns — nothing this veto can name } pushRawBind( fileId, lang, nodeTextOf( ident, src ), std::string{}, BindSite{ ts_node_start_byte( p ), 0u, 0u }, LocalBindKind::VarDecl, binds ); } } // MF7 (PY-MOD-CALL-SITES): a module-scope from-import that may not run (inside an `if`/`try`/`with`/loop clause, // pythonDefIsConditionalAtModule) marks each name it binds `?:y` (kPythonCondImportPrefix); the import's own Import binding is filed // elsewhere. A star import binds no name and files none. inline void capturePythonCondImportMarks( TSNode n, std::uint32_t fileId, std::string_view src, std::vector& binds ) { if( !kindIs( ts_node_type( n ), "import_from_statement" ) || !pythonDefIsConditionalAtModule( n ) ) { return; } const std::uint32_t at = ts_node_start_byte( n ); const TSNode module = fieldChild( n, NodeField::ModuleName ); ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const TSNode nm = kindIs( ts_node_type( c ), "aliased_import" ) ? fieldChild( c, NodeField::Alias ) : c; if( !ts_node_eq( c, module ) && !ts_node_is_null( nm ) && ( kindIs( ts_node_type( nm ), "identifier" ) || kindIs( ts_node_type( nm ), "dotted_name" ) ) ) { pushRawBind( fileId, Lang::Python, std::string{ kPythonCondImportPrefix }.append( nodeTextOf( nm, src ) ), std::string{}, BindSite{ at, 0u, 0u }, LocalBindKind::VarDecl, binds ); } return true; } ); } inline void captureShadowScopeDecls( TSNode n, const char* t, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& binds ) { if( lang == Lang::Python ) { if( kindIs( t, "function_definition" ) ) { capturePythonParamShadowDecls( n, fileId, lang, src, binds ); // Phase 4b: veto evidence only (empty span) } capturePythonRebindShadowDecls( n, t, fileId, lang, src, binds ); // issue #287 round 2: rebind veto evidence capturePythonCondImportMarks( n, fileId, src, binds ); // MF7: `?:y` for a from-import that may not run capturePythonLocalScopeDecls( n, t, fileId, lang, src, binds ); // lambda parameters / comprehension targets (`~name`) capturePythonPatternDecls( n, fileId, src, binds ); // ind4b fix 4: match captures and `type` statements capturePythonTypeParamDecls( n, fileId, src, binds ); // ind4b fix 5: PEP 695 type parameters return; } if( lang != Lang::Cpp && lang != Lang::ObjC ) { return; } const bool isRangeFor = kindIs( t, "for_range_loop" ); const bool isLambda = !isRangeFor && kindIs( t, "lambda_expression" ); const bool isCatch = !isRangeFor && !isLambda && kindIs( t, "catch_clause" ); const bool isFnDef = !isRangeFor && !isLambda && !isCatch && kindIs( t, "function_definition" ); if( !isRangeFor && !isLambda && !isCatch && !isFnDef ) { return; // every other node type declares nothing this capture owns } const TSNode body = fieldChild( n, NodeField::Body ); if( ts_node_is_null( body ) ) { return; // a body-less shape scopes nothing (declaration-only lambda/definition never parses so) } const BindSite bodySite{ ts_node_start_byte( n ), ts_node_start_byte( body ), ts_node_end_byte( body ) }; if( isRangeFor ) { // iteration 3, unified with enclosingShadowScope's control-statement rule: the loop variable scopes // to the WHOLE for_range_loop statement (its own span), not merely the body. const BindSite loopSite{ ts_node_start_byte( n ), ts_node_start_byte( n ), ts_node_end_byte( n ) }; const TSNode loopDeclarator = fieldChild( n, NodeField::Declarator ); emitShadowVarDecls( fileId, lang, loopDeclarator, src, loopSite, binds ); // member-variable round (card A3): the loop variable's WRITTEN type (`for( const Symbol& s : v )` → // s:Symbol) as a ParamType record for the field use-site index and Rule 2's lexical lookup — the single // most common typed receiver shape in this repo's own source (`s.name`), and `auto` writes nothing, as for // parameters. pushTypedBind( fileId, lang, paramDeclaratorVarName( loopDeclarator, src ), declTypeOfName( fieldChild( n, NodeField::Type ), src ), BindSite{ ts_node_start_byte( n ), 0u, 0u }, LocalBindKind::ParamType, binds ); return; } if( isLambda ) { captureLambdaShadowDecls( n, fileId, lang, src, bodySite, binds ); return; } // a catch parameter is a local of its HANDLER block (iteration 3, the noted 3b gap) — its // parameter_list is a direct field; a definition's sits behind the declarator chain // (fnDefParameterList above), which is what keeps prototypes and fn-pointer TYPE params out. const TSNode params = isCatch ? fieldChild( n, NodeField::Parameters ) : fnDefParameterList( n ); if( !ts_node_is_null( params ) ) { emitShadowParamDecls( params, fileId, lang, src, bodySite, binds ); } } // emit one L3 var→function RawBind (kind FnDecl/FnAssign) — emitBind's record shape with the kind stamped // after the push, so the two emitters share ONE body instead of cloning it. inline void emitFnBind( std::uint32_t fileId, Lang lang, std::string_view var, std::string target, std::uint32_t startByte, LocalBindKind kind, std::vector& binds ) { const std::size_t before = binds.size(); emitBind( fileId, lang, var, std::move( target ), startByte, binds ); if( binds.size() > before ) { binds.back().kind = kind; } } // L3 capture over one C-family `declaration` node: one FnDecl record per init_declarator whose RHS names a // function (`&alpha` / `beta` / a lambda). `&name` and lambdas are self-evidencing and emit at once; // a BARE-IDENTIFIER initializer is the one shape a fn-pointer bind shares with a plain value copy, so it // goes to the VALUE-INITIALIZATION NOISE GATE below (`pending`) unless the declarator itself spells a fn // pointer, which settles it on the spot. // A reference declarator (`H& r = fn;`) emits NO positive and clobbers the bound-to var (A5 escape guard). inline void captureFnBindDecl( TSNode n, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& fnPos, std::vector& fnUnk, std::vector& pending ) { const TSNode typeNode = fieldChild( n, NodeField::Type ); std::string writtenType; const bool concrete = concreteWrittenType( typeNode, src, writtenType ); // O(children), on the cursor's O(1) field name — see fnPtrAliasName above and arm B7. ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* fname = ts_tree_cursor_current_field_name( &cursor.cur ); if( fname == nullptr || !kindIs( fname, "declarator" ) ) { return true; } if( !kindIs( ts_node_type( c ), "init_declarator" ) ) { return true; // no initializer → no binding fact here (a later assignment carries its own) } const auto [ var, sawFnDecl, sawPtrDecl, sawRef ] = fnDeclaratorShape( fieldChild( c, NodeField::Declarator ), src ); const TSNode valueNode = fieldChild( c, NodeField::Value ); if( sawRef ) { // A5 escape guard: `H& r = fn;` / `auto& r = fn;` ALIASES fn — a write through r retargets fn // invisibly, so the bound-to variable is clobbered (toward tombstone, never toward resolve) and // the alias itself gets NO positive (its target can change under it the same way). if( !ts_node_is_null( valueNode ) && kindIs( ts_node_type( valueNode ), "identifier" ) ) { const std::string_view aliased = nodeTextOf( valueNode, src ); if( !aliased.empty() ) { fnUnk.push_back( { std::string( aliased ), ts_node_start_byte( n ) } ); } } return true; } bool bareIdent = false; std::string target = fnBindTargetOf( valueNode, src, bareIdent ); if( bareIdent && !target.empty() && !( sawFnDecl && sawPtrDecl ) ) { // the declarator does not itself spell a fn pointer, so only the WRITTEN TYPE can tell a bind // from a copy — and that answer needs the file's complete alias table. Hold it. pending.push_back( { std::string( var ), std::move( target ), writtenType, ts_node_start_byte( n ), concrete } ); return true; } emitFnBind( fileId, lang, var, std::move( target ), ts_node_start_byte( n ), LocalBindKind::FnDecl, fnPos ); return true; } ); } // A5 escape guard over one `pointer_expression`: `&fn` ANYWHERE makes the variable mutable through the // pointer (`indirect_mutate(&fn)` retargets it behind the resolver's back), so any address-of over a bare // identifier records a CLOBBER for that identifier — toward tombstone, never toward resolve. A by-value use // (`takes_fn(fn)`, `other = fn`) copies the pointer and cannot mutate the variable, so it does NOT clobber. // The `&alpha` inside a positive binding RHS also lands here (clobbering the FUNCTION's name as a "var") — // harmless-conservative: it only matters if a same-named variable holds a binding in this file, and then // refusing to resolve it is the safe side. Dereferences (`*p`) are excluded by the operator check. inline void captureFnBindEscape( TSNode n, std::string_view src, std::vector& fnUnk ) { const TSNode op = ts_node_child( n, 0 ); if( ts_node_is_null( op ) || !kindIs( ts_node_type( op ), "&" ) ) { return; } const TSNode idn = fieldChild( n, NodeField::Argument ); if( ts_node_is_null( idn ) || !kindIs( ts_node_type( idn ), "identifier" ) ) { return; } const std::string_view var = nodeTextOf( idn, src ); if( !var.empty() ) { fnUnk.push_back( { std::string( var ), ts_node_start_byte( n ) } ); } } // L3 capture over one C-family `assignment_expression`: a recognizable RHS emits an FnAssign record; any // other RHS on a bare-identifier LHS (`fn = getHandler()`, `fn = nullptr`, `n += 1`) records a CLOBBER // candidate, emitted as a tombstone at the end of the walk IF the var has a fn binding in the same file. // A BARE-IDENTIFIER RHS (`fn = beta;`) is neither yet: it is the one shape a plain value copy shares with a // genuine fn-pointer rebind, and the assignment node carries no type to tell them apart — so it is held in // `pending` for the end-of-walk value-assignment noise gate above, which asks the file's own declarations. // The second branch decodes the C++-grammar MIS-PARSE of a raw fn-pointer declaration (`void (*fn)() = // α` — see misparsedFnPtrDeclVar): the shape itself proves a fn-pointer declarator, so a // bare-identifier RHS is captured immediately there — the "type" IS the evidence, no gate needed. inline void captureFnBindAssign( TSNode n, std::uint32_t fileId, Lang lang, std::string_view src, std::vector& fnPos, std::vector& fnUnk, std::vector& pending ) { const TSNode lhs = fieldChild( n, NodeField::Left ); const TSNode rhs = fieldChild( n, NodeField::Right ); if( !ts_node_is_null( lhs ) && kindIs( ts_node_type( lhs ), "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( lhs ), b = ts_node_end_byte( lhs ); if( a <= b && b <= src.size() ) { const std::string_view var = src.substr( a, b - a ); bool bareIdent = false; std::string target = fnBindTargetOf( rhs, src, bareIdent ); if( target.empty() ) { fnUnk.push_back( { std::string( var ), ts_node_start_byte( n ) } ); } else if( bareIdent ) { pending.push_back( { std::string( var ), std::move( target ), ts_node_start_byte( n ) } ); } else { emitFnBind( fileId, lang, var, std::move( target ), ts_node_start_byte( n ), LocalBindKind::FnAssign, fnPos ); } } } else if( const std::string_view dvar = misparsedFnPtrDeclVar( lhs, src ); !dvar.empty() ) { bool bareIdent = false; std::string target = fnBindTargetOf( rhs, src, bareIdent ); if( !target.empty() ) { emitFnBind( fileId, lang, dvar, std::move( target ), ts_node_start_byte( n ), LocalBindKind::FnDecl, fnPos ); } else { fnUnk.push_back( { std::string( dvar ), ts_node_start_byte( n ) } ); } } } // decide every bare-identifier assignment the walk held back, against the file's COMPLETE declaration // evidence. A name the file proved to be a VALUE variable where the assignment sits records NOTHING — not a // positive, and deliberately not a clobber either: a clobber is a statement ABOUT a function pointer ("this // one is no longer trustworthy"), and the end-of-walk sweep promotes it to a real FnAssign tombstone as // soon as any same-named var in the file holds a binding. That tombstone reads as a binding to every // consumer — it re-vetoed the very shadow suppression this gate exists to restore. A copy into a string is // evidence in NEITHER direction. Everything else mints its FnAssign exactly as it did before the gate. inline void resolvePendingFnBindAssigns( std::uint32_t fileId, Lang lang, FnBindGateState& gate, std::vector& fnPos ) { for( PendingFnBindAssign& p : gate.pending ) { if( !fnBindProvenValueVar( p.var, p.startByte, gate.facts, gate.aliases ) ) { emitFnBind( fileId, lang, p.var, std::move( p.target ), p.startByte, LocalBindKind::FnAssign, fnPos ); } } } // ── L3 VALUE-INITIALIZATION NOISE GATE (r9 fix round, DECLARATION arm) ─────────────────────────────── // The sibling gate above answers "what is this VARIABLE?" from the file's declarations because an // assignment node carries no type. A declaration carries one, so this arm asks the stronger question // directly of the node in front of it: does the WRITTEN TYPE prove a value? // * a CONCRETE type that is no fn-pointer alias — `std::string tag = zzz;`, `Box b = other;`, // `int a = b;` — is a copy. No binding. Before this gate only the PRIMITIVE half of that was caught, // so a CLASS-typed copy minted an FnDecl, and shadowSuppressedSite (model.h) VETOES local-shadow // suppression for any name carrying an L3 binding — the local handed its every read/write site back to // the function it shadows. That is the same harm, and the same mechanism, as the assignment arm's. // * a fn-pointer DECLARATOR (`void (*fp)() = handler;`) never reaches here at all: the shape is its own // evidence and captureFnBindDecl emits it on the spot. // * a same-file fn-pointer ALIAS (`typedef void (*H)(); H fp = beta;`) mints — the alias table is why // these records are deferred to the end of the walk rather than judged where they are written. // * everything else is UNKNOWN and unknown MINTS: `auto fp = f;` (the idiomatic form), `decltype(...)`, // and any template/dependent type. Refusing to guess is what keeps this gate from costing recall. // DISCLOSED BLIND SPOT, pinned by test/fnptrcheck.sh arm (t): the alias evidence is SAME-FILE, so a // `typedef void (*H)();` living in a HEADER leaves `H fp = beta;` indistinguishable from a value copy and // its edge is gated away. It cost ZERO edges on the two corpora this round measured (this repo, 1093 files // / 10771 edges, full map byte-identical; a 2376-file ObjC++ tree, 39741 edges, every callee row identical // and only `unresolved=` moving 2577 → 2509) — but that is a measurement, not a proof. Widening the alias // evidence corpus-wide is the fix if a corpus ever pays for it. inline void resolvePendingFnBindDecls( std::uint32_t fileId, Lang lang, FnBindGateState& gate, std::vector& fnPos ) { for( PendingFnBindDecl& p : gate.pendingDecl ) { const bool aliasTyped = !p.typeName.empty() && gate.aliases.find( p.typeName ) != gate.aliases.end(); const bool provenValue = p.concreteType && !aliasTyped; if( !provenValue ) { emitFnBind( fileId, lang, p.var, std::move( p.target ), p.startByte, LocalBindKind::FnDecl, fnPos ); } } } // P2-D Rule 2 local var→type bindings + the L3 fn-pointer capture. One visitor on the shared pre-order // stream (streamSideCaptures below) — the pass used to own an identical walk of its own, which is what the // fusion removed. Its state outlives a single node (the L3 clobber sweep needs the whole file's positives), // so it rides in a context the driver holds by reference; bindsFinalize spends it when the stream ends. // FE-B (review B4b): one type parameter a generic declares (``, `[T any]`, `[T]`) and the declaration it belongs to // (its owner's bytes). A written type that spells one of these inside the owner is the parameter, never a same-named class. struct TypeParamScope { std::string_view name; std::uint32_t start = 0; std::uint32_t end = 0; bool declares( std::string_view spelled, std::uint32_t at ) const noexcept { return name == spelled && start <= at && at < end; } }; struct BindCtx { std::uint32_t fileId = 0; Lang lang {}; std::string_view src; std::vector* binds = nullptr; std::vector typeParams; // FE-B: filled as the pre-order walk meets each parameter list // (L) fix round 4: the blocks captureUnparsedBlocks has read, as (start, end) — the enclosing ones of the walk's position std::vector> macroBlocks; // L3 fn-pointer buffers. Positives collect here (not straight into binds) so the end-of-walk clobber // sweep can ask "does this var have a fn binding in this file?" — a clobbering assignment // (`fn = getHandler()`) matters only then, which keeps a fn-binding-free file contributing ZERO new // records (the whole feature inert there). std::vector fnPos; std::vector fnUnk; FnBindGateState fnGate; // value-assignment noise-gate evidence — filled by the stream, spent at the end bool cFamilyFn = false; // lane std-namesake: every name this Rust file writes a member access on (`m` in `m.get( k )`, in code and in a macro's // token tree) — views into src. pruneRustTombstones keeps a standard-type tombstone only for one of these. std::vector rustReceiverRoots; }; // ── FE-B receiver-evidence capture (model.h LocalBindKind::RecvType / MemberType / MethodAlias) ────────────────────────── // the text of an identifier in any grammar's spelling (`identifier`, `type_identifier`, `simple_identifier`); "" for a null // node or any other kind inline constexpr std::array kIdentifierKinds = { "identifier", "type_identifier", "simple_identifier" }; inline std::string_view identifierText( TSNode n, std::string_view src ) { const std::string_view kind = ts_node_is_null( n ) ? std::string_view{} : std::string_view( ts_node_type( n ) ); return std::ranges::find( kIdentifierKinds, kind ) != kIdentifierKinds.end() ? nodeTextOf( n, src ) : std::string_view{}; } // The class a written TYPE names, final segment — "" for anything that names no single class (an array, a union, a // string annotation, a generic's argument). Unwraps the annotation wrappers each grammar puts around the type. // `qualified` (optional): the written type WHOLE when the class is reached through a Python `mod.T` (attribute) or a TS `ns.T` // (nested_type_identifier) — the qualifier ReceiverEvidence::build checks against the file's imports (lane h1-annot); else // untouched. Go's `pkg.T` rides goQualifiedWhole, and the JVM / C# / Kotlin / Swift qualified forms are not reported (a floor). inline std::string_view annotatedClass( TSNode node, std::string_view src, std::string_view* qualified = nullptr ) { for( int guard = 0; guard < 8 && !ts_node_is_null( node ); ++guard ) { if( const std::string_view name = identifierText( node, src ); !name.empty() ) { return name; } const char* t = ts_node_type( node ); if( kindIs( t, "type" ) || kindIs( t, "type_annotation" ) || kindIs( t, "pointer_type" ) || kindIs( t, "parenthesized_type" ) ) { node = ts_node_named_child_count( node ) == 1 ? ts_node_named_child( node, 0 ) : TSNode{}; } else if( kindIs( t, "qualified_type" ) ) // Go `pkg.T` { node = fieldChild( node, NodeField::Name ); } else if( kindIs( t, "attribute" ) || kindIs( t, "nested_type_identifier" ) ) // Python `mod.T`, TS `ns.T` { if( qualified != nullptr && qualified->empty() ) { *qualified = nodeTextOf( node, src ); // the outermost qualified spelling, `a.b.T` whole } node = fieldChild( node, kindIs( t, "attribute" ) ? NodeField::Attribute : NodeField::Name ); } else if( kindIs( t, "generic_type" ) || kindIs( t, "generic_name" ) ) // TS `T` / Go `T[U]` / Java / C# `T`: the class { node = ts_node_named_child_count( node ) > 0 ? ts_node_named_child( node, 0 ) : TSNode{}; } else if( kindIs( t, "nullable_type" ) || kindIs( t, "optional_type" ) ) // C# / Kotlin `T?`, Swift `T?`: the class { node = ts_node_named_child_count( node ) > 0 ? ts_node_named_child( node, 0 ) : TSNode{}; } else if( kindIs( t, "user_type" ) || kindIs( t, "scoped_type_identifier" ) ) // Kotlin / Swift `a.B`, Java `a.B`: the last { // name segment, never a type argument TSNode last{}; for( std::uint32_t i = 0, k = ts_node_named_child_count( node ); i < k; ++i ) { const TSNode c = ts_node_named_child( node, i ); if( kindIs( ts_node_type( c ), "type_identifier" ) ) { last = c; } } node = last; } else if( kindIs( t, "qualified_name" ) ) // C# `a.B` { node = fieldChild( node, NodeField::Name ); } else { return {}; // an array, a function type, C#'s implicit `var`, a predefined/primitive type: no single class } } return {}; } inline void pushEvidenceBind( BindCtx& cx, LocalBindKind kind, std::string_view var, std::string_view type, std::string_view imported, std::uint32_t at, bool flag ) { EXPECTS( kind == LocalBindKind::RecvType || kind == LocalBindKind::MemberType || kind == LocalBindKind::MethodAlias || kind == LocalBindKind::NameAlias || kind == LocalBindKind::StaticMember, "only the receiver-evidence kinds ride this emitter" ); if( type.empty() || ( var.empty() && !( kind == LocalBindKind::RecvType && flag ) ) ) { return; // a nameless record is kept only for a method's own class (Go `func (T) m()`, JS `T.prototype.m = …`) } RawBind b; b.fileId = cx.fileId; b.startByte = at; b.lang = cx.lang; b.kind = kind; b.isFromAssignment = flag; b.var.assign( var ); b.typeName.assign( type ); b.importedName.assign( imported ); cx.binds->push_back( b ); } // the class a constructor-shaped initializer names — JS/TS `new Foo( … )`, Python `Foo( … )`, Go `Foo{ … }` / `&Foo{ … }` inline std::string_view constructedBy( TSNode value, Lang lang, std::string_view src ) { if( ts_node_is_null( value ) ) { return {}; } if( lang == Lang::Python ) { if( !kindIs( ts_node_type( value ), "call" ) ) { return {}; } const TSNode fn = fieldChild( value, NodeField::Function ); return ( !ts_node_is_null( fn ) && kindIs( ts_node_type( fn ), "identifier" ) ) ? nodeTextOf( fn, src ) : std::string_view{}; } if( lang == Lang::Kotlin || lang == Lang::Swift ) { // `Store( … )` is spelled like a function call: a capitalized callee is read as the class it constructs (the house // convention of both languages). A lower-case factory (`make()`) names no class, and a capitalized name that is no // class in the tree proves nothing downstream (ReceiverEvidence finds no `Name::member`). if( !kindIs( ts_node_type( value ), "call_expression" ) || ts_node_named_child_count( value ) == 0 ) { return {}; } const TSNode callee = ts_node_named_child( value, 0 ); const std::string_view name = kindIs( ts_node_type( callee ), "simple_identifier" ) ? nodeTextOf( callee, src ) : std::string_view{}; return ( !name.empty() && name.front() >= 'A' && name.front() <= 'Z' ) ? name : std::string_view{}; } return constructedClass( value, lang, src ); } // lane receiver-fixes (PY-CTOR-ANNOT): a Python constructor-shaped call through a module, `mod.X( … )` / `a.b.X( … )` — the final // name, with the callee's whole spelling in `whole` (the written-qualifier check ReceiverEvidence::build applies to `x: mod.T` // decides it: an in-tree module passes, `requests.Session()` does not). "" for every other value, a bare `X( … )` included // (constructedBy reads that one). inline std::string_view pyQualifiedConstructed( TSNode value, std::string_view src, std::string_view& whole ) { if( ts_node_is_null( value ) || !kindIs( ts_node_type( value ), "call" ) ) { return {}; } const TSNode fn = fieldChild( value, NodeField::Function ); if( ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "attribute" ) ) { return {}; } for( TSNode obj = fieldChild( fn, NodeField::Object ); ; obj = fieldChild( obj, NodeField::Object ) ) // a dotted NAME only { if( ts_node_is_null( obj ) ) { return {}; } if( kindIs( ts_node_type( obj ), "identifier" ) ) { break; } if( !kindIs( ts_node_type( obj ), "attribute" ) ) { return {}; // `f().X()`, `x[0].X()`: no module spelling } } const TSNode attr = fieldChild( fn, NodeField::Attribute ); if( ts_node_is_null( attr ) || !kindIs( ts_node_type( attr ), "identifier" ) ) { return {}; } whole = nodeTextOf( fn, src ); return nodeTextOf( attr, src ); } // lane receiver-fixes (PY-CTOR-ANNOT): the one type an OPTIONAL Python annotation names — `T | None`, `None | T` (a union_type // or, as the grammar may read it, a binary `|`), `Optional[ T ]` / `typing.Optional[ T ]` — else the annotation as written. A // union of two non-None types names no single class (annotatedClass reads such a node as ""). inline TSNode pyOptionalInner( TSNode type, std::string_view src ) { TSNode n = type; if( !ts_node_is_null( n ) && kindIs( ts_node_type( n ), "type" ) && ts_node_named_child_count( n ) == 1 ) { n = ts_node_named_child( n, 0 ); } if( ts_node_is_null( n ) ) { return type; } const auto isNone = []( TSNode x ) noexcept { if( !ts_node_is_null( x ) && kindIs( ts_node_type( x ), "type" ) && ts_node_named_child_count( x ) == 1 ) { x = ts_node_named_child( x, 0 ); } return !ts_node_is_null( x ) && kindIs( ts_node_type( x ), "none" ); }; const char* t = ts_node_type( n ); bool union_ = kindIs( t, "union_type" ); if( kindIs( t, "binary_operator" ) ) { for( std::uint32_t i = 0, k = ts_node_child_count( n ); i < k && !union_; ++i ) { union_ = kindIs( ts_node_type( ts_node_child( n, i ) ), "|" ); } } if( union_ && ts_node_named_child_count( n ) == 2 ) { const TSNode a = ts_node_named_child( n, 0 ); const TSNode b = ts_node_named_child( n, 1 ); return isNone( b ) && !isNone( a ) ? a : isNone( a ) && !isNone( b ) ? b : TSNode{}; } if( union_ ) { return TSNode{}; } if( ( kindIs( t, "generic_type" ) || kindIs( t, "subscript" ) ) && ts_node_named_child_count( n ) == 2 ) { const std::string_view head = nodeTextOf( ts_node_named_child( n, 0 ), src ); TSNode arg = ts_node_named_child( n, 1 ); if( !ts_node_is_null( arg ) && kindIs( ts_node_type( arg ), "type_parameter" ) ) { arg = ts_node_named_child_count( arg ) == 1 ? ts_node_named_child( arg, 0 ) : TSNode{}; } if( head == "Optional" || head == "typing.Optional" ) { return arg; } } return type; } // FE-B (review B4b): the names one type parameter declares, visible over [start, end): the leading identifier(s) after any // annotation, modifier or variance (Java/C#/Kotlin/Swift/TS `T`, Go `A, B any`), or Python 3.12's `type`-wrapped name. inline void noteTypeParameter( BindCtx& cx, TSNode param, std::uint32_t start, std::uint32_t end ) { bool named = false; for( std::uint32_t i = 0, k = ts_node_named_child_count( param ); i < k; ++i ) { TSNode c = ts_node_named_child( param, i ); if( !named && kindIs( ts_node_type( c ), "type" ) && ts_node_named_child_count( c ) == 1 ) { c = ts_node_named_child( c, 0 ); // Python `def f[T]( … )` } if( const std::string_view name = identifierText( c, cx.src ); !name.empty() ) { cx.typeParams.push_back( { name, start, end } ); named = true; } else if( named ) { return; // the bound / constraint after the names } } } // a parameter LIST — the generic declaration that owns it is its parent, and the parameters are visible over all of it // (the pre-order walk meets the list before the owner's parameters, fields and body) inline void noteTypeParameters( BindCtx& cx, TSNode list ) { const TSNode owner = parentOf( list ); if( ts_node_is_null( owner ) ) { return; } const std::uint32_t start = ts_node_start_byte( owner ); const std::uint32_t end = ts_node_end_byte( owner ); if( cx.lang == Lang::Python ) { noteTypeParameter( cx, list, start, end ); // Python's `type_parameter` IS the list: `[A, B]` → one `type` per name return; } for( std::uint32_t i = 0, k = ts_node_named_child_count( list ); i < k; ++i ) { const TSNode p = ts_node_named_child( list, i ); if( kindIs( ts_node_type( p ), "type_parameter" ) || kindIs( ts_node_type( p ), "type_parameter_declaration" ) ) { noteTypeParameter( cx, p, start, end ); } } } // Go `func ( b Box[T] ) m()`: the receiver's type arguments are the method's type parameters inline void noteGoReceiverTypeParameters( BindCtx& cx, TSNode method ) { std::vector pending{ fieldChild( method, NodeField::Receiver ) }; for( int guard = 0; guard < 64 && !pending.empty(); ++guard ) { const TSNode n = pending.back(); pending.pop_back(); if( ts_node_is_null( n ) ) { continue; } if( kindIs( ts_node_type( n ), "type_arguments" ) ) { for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { TSNode a = ts_node_named_child( n, i ); if( kindIs( ts_node_type( a ), "type_elem" ) && ts_node_named_child_count( a ) == 1 ) { a = ts_node_named_child( a, 0 ); } if( const std::string_view name = identifierText( a, cx.src ); !name.empty() ) { cx.typeParams.push_back( { name, ts_node_start_byte( method ), ts_node_end_byte( method ) } ); } } continue; } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { pending.push_back( ts_node_named_child( n, i ) ); } } } // is `name`, written at byte `at`, a type parameter of a generic that encloses it inline bool namesTypeParameter( const BindCtx& cx, std::string_view name, std::uint32_t at ) noexcept { return std::ranges::any_of( cx.typeParams, [ & ]( const TypeParamScope& p ) { return p.declares( name, at ); } ); } // the class a written type names at byte `at` — "" when it names none, or names a type parameter (` … Tank t`); // `qualified` as annotatedClass's inline std::string_view writtenClass( const BindCtx& cx, TSNode type, std::uint32_t at, std::string_view* qualified = nullptr ) { const std::string_view cls = annotatedClass( type, cx.src, qualified ); return ( !cls.empty() && namesTypeParameter( cx, cls, at ) ) ? std::string_view{} : cls; } // FE-B: Java's `var` is inference, not a class: the written type, unless it is `var`, else what the initializer constructs. // A written type that is a type parameter names no class, and the initializer does not override it. inline std::string_view declaredOrConstructed( const BindCtx& cx, TSNode type, TSNode value, std::uint32_t at ) { const std::string_view written = annotatedClass( type, cx.src ); if( !written.empty() && !( written == "var" && cx.lang == Lang::Java ) ) { return namesTypeParameter( cx, written, at ) ? std::string_view{} : written; } return constructedBy( value, cx.lang, cx.src ); } // ── scopes (review B4a) ───────────────────────────────────────────────────────────────────────────────────────────── // A local's record carries the bytes it is visible in (RawBind spanStart/spanEnd; {0,0} = anywhere in its definition), so // receiverevidence.h visibleLocal can tell which binding of a name a call site sees. A binding whose class is unknown is // still recorded (a TOMBSTONE): it hides the field or the outer local of its name over its span and proves nothing. // a name recorded at `at` and visible over `scope` ({0,0} when there is none: the whole definition) — the whole of it, or // (`fromHere`, a block's declaration) from `at` to its end inline BindSite siteOver( std::uint32_t at, TSNode scope, bool fromHere ) noexcept { if( ts_node_is_null( scope ) ) { return { at, 0u, 0u }; } return { at, fromHere ? at : ts_node_start_byte( scope ), ts_node_end_byte( scope ) }; } inline BindSite siteIn( std::uint32_t at, TSNode scope ) noexcept { return siteOver( at, scope, false ); } inline BindSite siteFrom( std::uint32_t at, TSNode scope ) noexcept { return siteOver( at, scope, true ); } // one local binding of `var`, naming class `cls` ("" = unknown), visible over `site`'s span. `flag`: Go's method receiver. // one site-spanned evidence record of `kind` (RecvType, lane L6's DispatchType): `var` names `type` over `site` inline void pushSiteEvidence( BindCtx& cx, LocalBindKind kind, std::string_view var, std::string_view type, BindSite site, bool flag, std::string_view imported ) { RawBind b; b.fileId = cx.fileId; b.startByte = site.startByte; b.lang = cx.lang; b.kind = kind; b.isFromAssignment = flag; b.spanStart = site.spanStart; b.spanEnd = site.spanEnd; b.var.assign( var ); b.typeName.assign( type ); b.importedName.assign( imported ); cx.binds->push_back( b ); } inline void pushLocalEvidence( BindCtx& cx, std::string_view var, std::string_view cls, BindSite site, bool flag = false, std::string_view whole = {} ) { if( var.empty() ) { return; } pushSiteEvidence( cx, LocalBindKind::RecvType, var, cls, site, flag, cls.empty() ? std::string_view{} : whole ); // Go `pkg.T`: the qualifier ReceiverEvidence::build checks } // the same for a name NODE (an identifier of any grammar's spelling; anything else records nothing) inline void pushLocalEvidence( BindCtx& cx, TSNode name, std::string_view cls, BindSite site ) { pushLocalEvidence( cx, identifierText( name, cx.src ), cls, site ); // "" records nothing } // every identifier `node` holds at any depth (a destructuring / tuple pattern's names), each as a tombstone over `site` inline void pushPatternTombstones( BindCtx& cx, TSNode node, BindSite site ) { std::vector pending{ node }; for( int guard = 0; guard < 256 && !pending.empty(); ++guard ) { const TSNode n = pending.back(); pending.pop_back(); if( ts_node_is_null( n ) ) { continue; } const char* t = ts_node_type( n ); if( kindIs( t, "identifier" ) || kindIs( t, "simple_identifier" ) || kindIs( t, "shorthand_property_identifier_pattern" ) ) { pushLocalEvidence( cx, nodeTextOf( n, cx.src ), {}, site ); continue; } if( kindIs( t, "type_identifier" ) || kindIs( t, "user_type" ) || kindIs( t, "type_annotation" ) || kindIs( t, "predefined_type" ) ) { continue; } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { pending.push_back( ts_node_named_child( n, i ) ); } } } // a field whose class is unknown (its written type is a type parameter): a tombstone, so it hides a base class's field inline void pushMemberTombstone( BindCtx& cx, std::string_view var, std::uint32_t at ) { if( var.empty() ) { return; } RawBind b; b.fileId = cx.fileId; b.startByte = at; b.lang = cx.lang; b.kind = LocalBindKind::MemberType; b.var.assign( var ); cx.binds->push_back( b ); } // FE-B (review B3): one declarator's evidence — `name` declared with the class its written `type` names, else the class its // initializer `value` constructs. A parameter or local whose class is unknown (`var s = make()`) is still a DECLARATION: // recorded with no class (a tombstone), so it hides a same-named field (receiverevidence.h declaresLocal) and proves nothing. // A local is visible over `site` (review B4a); a field (MemberType) belongs to its class. inline void pushDeclaratorEvidence( BindCtx& cx, LocalBindKind kind, TSNode type, TSNode name, TSNode value, BindSite site ) { if( ts_node_is_null( name ) || !( kindIs( ts_node_type( name ), "identifier" ) || kindIs( ts_node_type( name ), "simple_identifier" ) ) ) { return; } const std::string_view cls = declaredOrConstructed( cx, type, value, site.startByte ); if( kind == LocalBindKind::RecvType ) { pushLocalEvidence( cx, name, cls, site ); } else if( !cls.empty() ) { pushEvidenceBind( cx, kind, nodeTextOf( name, cx.src ), cls, {}, site.startByte, false ); } else { pushMemberTombstone( cx, nodeTextOf( name, cx.src ), site.startByte ); } } // the first (or, with `last`, the last) named child of `node` whose kind is one of `kinds`; null when none — Kotlin's grammar // carries no field names and Swift's reuses `name` for the parameter, its label and its type inline TSNode namedChildOfKind( TSNode node, std::initializer_list kinds, bool last = false ) { TSNode hit{}; for( std::uint32_t i = 0, k = ts_node_named_child_count( node ); i < k; ++i ) { const TSNode c = ts_node_named_child( node, i ); if( std::ranges::find( kinds, std::string_view( ts_node_type( c ) ) ) != kinds.end() ) { hit = c; if( !last ) { break; } } } return hit; } // ── call-result typing (train 29 lane call-result-typing; test/receiverevidencecheck.sh (L)) ───────────────────────────────── // A local bound to a call's result records LocalBindKind::CallResult; each declared result of a definition records // LocalBindKind::ReturnType. graph.h's resolve loop joins the two through the call's OWN committed edge (one proven target), // so this capture decides nothing about the callee: it only states what the source writes. // The NAME node a call Reference sits on — the callee's last identifier: `f` in `f()`, `F` in Go `pkg.F()`, `m` in `x.m()`, // `load` in Rust `Book::load()`. Null for any other callee shape (a generic instantiation, a closure, an index): no record. inline TSNode calleeNameNode( TSNode call ) { if( kindIs( ts_node_type( call ), "method_invocation" ) ) // Java `Configs.make()` / `make()` { return fieldChild( call, NodeField::Name ); } TSNode fn = fieldChild( call, NodeField::Function ); if( ts_node_is_null( fn ) && kindIs( ts_node_type( call ), "call_expression" ) && ts_node_named_child_count( call ) > 0 ) { fn = ts_node_named_child( call, 0 ); // Kotlin: the callee is the first child } for( int guard = 0; guard < 4 && !ts_node_is_null( fn ); ++guard ) { const char* t = ts_node_type( fn ); if( kindIs( t, "identifier" ) || kindIs( t, "field_identifier" ) || kindIs( t, "property_identifier" ) || kindIs( t, "simple_identifier" ) ) { return fn; } if( kindIs( t, "selector_expression" ) || kindIs( t, "field_expression" ) ) // Go `pkg.F` / `x.m`, Rust `x.m` { fn = fieldChild( fn, NodeField::Field ); } else if( kindIs( t, "scoped_identifier" ) || kindIs( t, "qualified_identifier" ) ) // Rust `Type::f`, C++ `ns::f` { fn = fieldChild( fn, NodeField::Name ); } else if( kindIs( t, "member_expression" ) ) // TS `x.m` { fn = fieldChild( fn, NodeField::Property ); } else if( kindIs( t, "navigation_expression" ) && ts_node_named_child_count( fn ) == 2 ) // Kotlin `X.m`: its suffix { const TSNode suffix = ts_node_named_child( fn, 1 ); fn = ts_node_named_child_count( suffix ) > 0 ? ts_node_named_child( suffix, ts_node_named_child_count( suffix ) - 1 ) : TSNode{}; } else { return {}; } } return {}; } // One CallResult: `var` binds result `index` of `call` after `depth` unwrap operators, visible over `site`. A blank `_`, a // non-identifier name or a callee with no name node records nothing. // `written`: the qualifier a Rust path call is written with (rustCallQualifier; model.h LocalBindKind::CallResult) inline void pushCallResult( BindCtx& cx, TSNode var, TSNode call, std::uint8_t index, std::uint8_t depth, BindSite site, const std::string& written = {} ) { const TSNode callee = ts_node_is_null( call ) ? TSNode{} : calleeNameNode( call ); const std::string_view name = ts_node_is_null( var ) ? std::string_view{} : identifierText( var, cx.src ); if( ts_node_is_null( callee ) || name.empty() || name == "_" ) { return; } // startByte: the call Reference's own byte (the call's start) — graph.h joins on it and the callee name cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( call ), .lang = cx.lang, .kind = LocalBindKind::CallResult, .resultIndex = index, .unwrapDepth = depth, .spanStart = site.spanStart, .spanEnd = site.spanEnd, .var = rw::InternedText( name ), .importedName = rw::InternedText( written ), .typeName = rw::InternedText( nodeTextOf( callee, cx.src ) ) } ); } // One ReturnType of the definition starting at `defStart`: position `index`, class `cls` ("" records nothing), the qualified // text whole (`os.File`, `std::fs::File`; "" when unqualified) and `depth` Result/Option layers above the class. // `spelledSelf` (Rust): the result is written `Self` — carried on isFromAssignment (model.h Binding: kind==ReturnType) inline void pushReturnType( BindCtx& cx, std::uint32_t defStart, std::uint8_t index, std::string_view cls, std::string_view whole, std::uint8_t depth, bool spelledSelf = false ) { if( !cls.empty() ) // Symbol::sigStartByte joins startByte (the RubySingletonDef pattern) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = defStart, .lang = cx.lang, .kind = LocalBindKind::ReturnType, .isFromAssignment = spelledSelf, .resultIndex = index, .unwrapDepth = depth, .importedName = rw::InternedText( whole ), .typeName = rw::InternedText( cls ) } ); } } // ── type imports (fix round 1): what a callee's file means by a bare result type name ───────────────────────────────────── // One TypeImport per name an import binds (model.h LocalBindKind::TypeImport). graph.h reads them to resolve a declared // result type in its callee's file: a name imported from outside names no in-tree class, however it is spelled. inline void pushTypeImport( BindCtx& cx, std::uint32_t at, std::string_view var, std::string_view item, std::string_view path ) { if( !var.empty() && !path.empty() ) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = at, .lang = cx.lang, .kind = LocalBindKind::TypeImport, .var = rw::InternedText( var ), .importedName = rw::InternedText( path ), .typeName = rw::InternedText( item ) } ); } } // a written path as one spelling: every space and line break removed (`crate :: a` → `crate::a`, `std ::map` → `std::map`) inline std::string withoutBlanks( std::string text ) { std::erase_if( text, []( char c ) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } ); return text; } inline std::string importPathText( TSNode n, std::string_view src ) { return withoutBlanks( std::string( ts_node_is_null( n ) ? std::string_view{} : nodeTextOf( n, src ) ) ); } // Java `import a.b.C;` / `import a.b.*;` and Kotlin `import a.b.C [as D]` / `import a.b.*`. A Java `import static a.B.N;` // (fix round 4) imports every static member named N — a member TYPE among them, and JLS 6.4.1 lets it shadow the package's // types — so it is recorded like a single-type import (its path names a class's member: never an in-tree top-level class); // `import static a.B.*;` is an on-demand row like `import a.B.*;`. inline void captureJvmTypeImport( BindCtx& cx, TSNode n, std::uint32_t at ) { TSNode path{}; TSNode alias{}; bool wildcard = false; ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* t = ts_node_type( c ); if( kindIs( t, "asterisk" ) || kindIs( t, "wildcard_import" ) ) { wildcard = true; } else if( kindIs( t, "import_alias" ) ) { alias = namedChildOfKind( c, { "type_identifier", "simple_identifier" } ); } else if( ts_node_is_null( path ) && ts_node_is_named( c ) && ( kindIs( t, "scoped_identifier" ) || kindIs( t, "identifier" ) ) ) { path = c; } return true; } ); if( ts_node_is_null( path ) ) { return; } const std::string whole = importPathText( path, cx.src ); if( wildcard ) { pushTypeImport( cx, at, "*", {}, whole ); return; } const std::string_view item = namesplit::afterLast( whole, "." ); pushTypeImport( cx, at, ts_node_is_null( alias ) ? item : nodeTextOf( alias, cx.src ), item, whole ); } // ── lexical scopes (fix round 2): where a class a bare result type names is nested ───────────────────────────────────────── // One Namespace per C++ `namespace` / TS `namespace` / `module` body (model.h LocalBindKind::Namespace): the index has no // symbol for them, yet a class inside one is out of scope outside it (receiverevidence.h CallResultTypes::visibleAt). inline void pushNamespace( BindCtx& cx, TSNode n, TSNode name, TSNode body ) { if( !ts_node_is_null( body ) ) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( n ), .lang = cx.lang, .kind = LocalBindKind::Namespace, .spanStart = ts_node_start_byte( body ), .spanEnd = ts_node_end_byte( body ), .var = rw::InternedText( importPathText( name, cx.src ) ) } ); } } // The nodes that carry a call-result scope fact, per family (data rows, read by bindsVisitNode): a namespace body the index // has no symbol for, C++'s using-directives / using-declarations, (fix round 3) every declaration that binds a type name to // another type — each family's alias forms (model.h LocalBindKind::TypeAlias) — and (fix round 4) every declaration that // binds a type name the index keeps NO class symbol for (Shadow: a Java record / @interface; C++ a union outside a // declaration, a bodiless struct / class / union), and the regions the parser cannot read as declarations (Unparsed: a C++ // statement, parse error or broken declaration where a declaration must stand — a macro invocation; UnparsedBlocks: a Rust // nested fn, whose enclosing blocks' statement-position macros may declare items it sees). enum class ScopeFactRole : std::uint8_t { Scope, Alias, Shadow, Unparsed, UnparsedBlocks }; struct ScopeFactNode { CallResultFamily family; std::string_view kind; ScopeFactRole role; }; inline constexpr ScopeFactNode kScopeFactNodes[] = { { CallResultFamily::Cpp, "namespace_definition", ScopeFactRole::Scope }, { CallResultFamily::Cpp, "using_declaration", ScopeFactRole::Scope }, { CallResultFamily::Cpp, "alias_declaration", ScopeFactRole::Alias }, { CallResultFamily::Cpp, "type_definition", ScopeFactRole::Alias }, { CallResultFamily::Cpp, "namespace_alias_definition", ScopeFactRole::Alias }, { CallResultFamily::Cpp, "union_specifier", ScopeFactRole::Shadow }, { CallResultFamily::Cpp, "struct_specifier", ScopeFactRole::Shadow }, { CallResultFamily::Cpp, "class_specifier", ScopeFactRole::Shadow }, { CallResultFamily::Cpp, "expression_statement", ScopeFactRole::Unparsed }, { CallResultFamily::Cpp, "ERROR", ScopeFactRole::Unparsed }, { CallResultFamily::Cpp, "declaration", ScopeFactRole::Unparsed }, { CallResultFamily::Cpp, "field_declaration", ScopeFactRole::Unparsed }, { CallResultFamily::Cpp, "function_definition", ScopeFactRole::Unparsed }, { CallResultFamily::Cpp, "field_declaration_list", ScopeFactRole::UnparsedBlocks }, { CallResultFamily::TypeScript, "internal_module", ScopeFactRole::Scope }, { CallResultFamily::TypeScript, "module", ScopeFactRole::Scope }, { CallResultFamily::TypeScript, "type_alias_declaration", ScopeFactRole::Alias }, { CallResultFamily::TypeScript, "import_alias", ScopeFactRole::Alias }, { CallResultFamily::Rust, "type_item", ScopeFactRole::Alias }, { CallResultFamily::Rust, "function_item", ScopeFactRole::UnparsedBlocks }, { CallResultFamily::Jvm, "type_alias", ScopeFactRole::Alias }, { CallResultFamily::Jvm, "record_declaration", ScopeFactRole::Shadow }, { CallResultFamily::Jvm, "annotation_type_declaration", ScopeFactRole::Shadow }, }; // the scope-fact row for node kind `t` in language `lang`; nullptr when none inline const ScopeFactNode* scopeFactRow( Lang lang, const char* t ) { const auto row = std::ranges::find_if( kScopeFactNodes, [ & ]( const ScopeFactNode& r ) { return r.family == callResultFamilyOf( lang ) && r.kind == t; } ); return row == std::end( kScopeFactNodes ) ? nullptr : &*row; } // the C++ scope a declaration `n` is in force over: a namespace body, a class body (only when `classScope` admits one), a // function body (fix round 4: a local class's member result is declared inside it), or the whole file (0 .. 0xFFFFFFFF) inline std::pair cppDeclScope( TSNode n, bool classScope ) { for( TSNode up = parentOf( n ); !ts_node_is_null( up ); up = parentOf( up ) ) { const char* ut = ts_node_type( up ); if( kindIs( ut, "translation_unit" ) ) { return { 0u, 0xFFFFFFFFu }; } const TSNode owner = parentOf( up ); if( kindIs( ut, "declaration_list" ) && !ts_node_is_null( owner ) && kindIs( ts_node_type( owner ), "namespace_definition" ) ) { return { ts_node_start_byte( up ), ts_node_end_byte( up ) }; } if( kindIs( ut, "field_declaration_list" ) ) { return classScope ? std::pair{ ts_node_start_byte( up ), ts_node_end_byte( up ) } : std::pair{ 0u, 0u }; } if( kindIs( ut, "compound_statement" ) ) { return { ts_node_start_byte( up ), ts_node_end_byte( up ) }; // fix round 4: in force in that body (a local class's members) } } return { 0u, 0u }; } // fix round 3: one TypeAlias per name an alias declaration binds (model.h). The name and the target per form: C++ // `using A = T;` (name / type) and `namespace a = b;` (name / the last named child), `typedef T A, *PA;` (every // declarator's identifier / type); Rust `type A = T;` and TS `type A = T` (name / type or value); Kotlin `typealias A = T` // and TS `import A = n.T` (the first identifier / the last named child). A C++ row carries its scope's span. inline void captureTypeAlias( BindCtx& cx, TSNode n, const char* t ) { std::uint32_t spanStart = 0; std::uint32_t spanEnd = 0; if( callResultFamilyOf( cx.lang ) == CallResultFamily::Cpp ) { std::tie( spanStart, spanEnd ) = cppDeclScope( n, true ); if( spanEnd == 0 ) { return; } } const auto push = [ & ]( std::string_view name, TSNode target ) { if( !name.empty() ) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( n ), .lang = cx.lang, .kind = LocalBindKind::TypeAlias, .spanStart = spanStart, .spanEnd = spanEnd, .var = rw::InternedText( name ), .importedName = rw::InternedText( importPathText( target, cx.src ) ) } ); } }; if( kindIs( t, "type_definition" ) ) { const TSNode type = fieldChild( n, NodeField::Type ); ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { // each declarator (`A`, `*PA`, `(*Fn)( int )`): its innermost identifier TSNode d = c; for( int guard = 0; guard < 8 && ts_node_is_named( c ) && !ts_node_eq( c, type ) && !ts_node_is_null( d ) && !kindIs( ts_node_type( d ), "type_identifier" ); ++guard ) { d = fieldChild( d, NodeField::Declarator ); } if( ts_node_is_named( c ) && !ts_node_eq( c, type ) && !ts_node_is_null( d ) && kindIs( ts_node_type( d ), "type_identifier" ) ) { push( nodeTextOf( d, cx.src ), type ); } return true; } ); return; } TSNode name = fieldChild( n, NodeField::Name ); TSNode target = fieldChild( n, NodeField::Type ); if( ts_node_is_null( target ) ) { target = fieldChild( n, NodeField::Value ); } if( ts_node_is_null( name ) ) // Kotlin `typealias`, TS `import A = n.T`: no fields { name = namedChildOfKind( n, { "type_identifier", "identifier", "simple_identifier" } ); } if( ts_node_is_null( target ) ) { target = ts_node_named_child_count( n ) > 1 ? namedChildOfKind( n, { "user_type", "nullable_type", "function_type", "parenthesized_type", "nested_identifier", "identifier", "namespace_identifier", "nested_namespace_specifier", "type_identifier" }, true ) : TSNode{}; } push( ts_node_is_null( name ) ? std::string_view{} : nodeTextOf( name, cx.src ), target ); } // fix round 4: a type declaration the index keeps no class symbol for, or a C++ bodiless (forward) declaration — a TypeAlias // row marked "shadow" / "forward" (model.h kTypeBind* constants): var = the name it binds, C++ rows with their scope's span. A C++ // union inside a `declaration` (`union U {…} u;`) and every struct / class with a body are class symbols (queries/cpp/tags.scm); // a bodiless one counts only as a declaration of its own (namespace, file, class or block scope) — an elaborated type in a // declaration, a parameter, a friend or a template declares nothing this reader follows. // one TypeAlias row binding `var` with no alias target, marked `mark` (model.h kTypeBind* constants), over `span` (C++) inline void pushTypeBind( BindCtx& cx, std::uint32_t at, std::pair span, std::string_view var, std::string_view mark ) { RawBind row{ .fileId = cx.fileId, .startByte = at, .lang = cx.lang, .kind = LocalBindKind::TypeAlias, .spanStart = span.first, .spanEnd = span.second }; row.var.assign( var ); row.typeName.assign( mark ); cx.binds->push_back( row ); } inline void captureShadowDecl( BindCtx& cx, TSNode n, const char* t ) { const TSNode name = fieldChild( n, NodeField::Name ); if( ts_node_is_null( name ) || !( kindIs( ts_node_type( name ), "identifier" ) || kindIs( ts_node_type( name ), "type_identifier" ) ) ) { return; } std::string_view mark = kTypeBindShadow; std::uint32_t spanStart = 0; std::uint32_t spanEnd = 0; if( callResultFamilyOf( cx.lang ) == CallResultFamily::Cpp ) { const TSNode parent = parentOf( n ); const char* pt = ts_node_is_null( parent ) ? "" : ts_node_type( parent ); const bool bodiless = ts_node_is_null( fieldChild( n, NodeField::Body ) ); constexpr std::string_view kDeclScopes[] = { "declaration_list", "translation_unit", "compound_statement" }; // a statement of its own const bool own = std::ranges::find( kDeclScopes, std::string_view( pt ) ) != std::end( kDeclScopes ) || ( kindIs( pt, "field_declaration" ) && ts_node_is_null( fieldChild( parent, NodeField::Declarator ) ) ); if( bodiless ? !own : !kindIs( t, "union_specifier" ) || kindIs( pt, "declaration" ) ) { return; } mark = bodiless ? kTypeBindForward : kTypeBindShadow; std::tie( spanStart, spanEnd ) = cppDeclScope( n, true ); if( spanEnd == 0 ) { return; } } pushTypeBind( cx, ts_node_start_byte( n ), { spanStart, spanEnd }, nodeTextOf( name, cx.src ), mark ); } // fix round 4: a region the parser cannot read as declarations may declare ANY name — an "unparsed" TypeAlias row (var "!", // model.h kTypeBind* constants). C++: a statement, a parse error, or a declaration holding one, standing where only declarations // may (a namespace / file / class body) and spelling at least one identifier — a macro invocation (`DECL(S)`, // `REGISTER(A, B);`, `DECLARE_ALL()`) or (fix round 5) a type-less function-style declaration (cppMacroShape Invocation), // with its scope's span; a class declaration only a macro can have written (DeclaresClass) is an "unrecorded" row per name // its head spells instead. Blocks (captureUnparsedBlocks): a Rust fn in a fn body — nested, or a method of an `impl` // there — and a C++ local class see the items of every block enclosing them, and a statement there may be a macro that // declares one — one row per such block, each block read once per file (BindCtx::macroBlocks: its ancestors, outermost first). // does the region `n` spell an identifier (a pre-order walk of its subtree, stopped at the first identifier leaf) inline bool spellsIdentifier( TSNode n ) { ChildCursor walk( n ); for( bool down = true;; ) { const TSNode c = ts_tree_cursor_current_node( &walk.cur ); if( down && ts_tree_cursor_goto_first_child( &walk.cur ) ) { continue; } if( ts_node_child_count( c ) == 0 && std::string_view( ts_node_type( c ) ).ends_with( "identifier" ) ) { return true; } down = ts_tree_cursor_goto_next_sibling( &walk.cur ); while( !down && ts_tree_cursor_goto_parent( &walk.cur ) ) { down = ts_tree_cursor_goto_next_sibling( &walk.cur ); } if( !down ) { return false; } } } // fix round 5: a C++ declaration tree-sitter-cpp reads WELL-FORMED that only a macro can have written — no error node marks // it. DeclaresClass: a function definition whose declarator is a bare name (`class API S {…}`, `struct API S final {…}`, // `API struct S {…}`, `MKSTRUCT S {…}`: a function needs a parameter list), an elaborated class-key declaring a bare name // (`struct API S;` — also `struct stat st;`, which hedges the spelling `st` with it). Invocation: a type-less function-style // declaration that is no constructor (`MK(S);`, `DISALLOW_COPY(X);` in a class body: a constructor bears its class's name). enum class CppMacroShape : std::uint8_t { None, DeclaresClass, Invocation }; inline CppMacroShape cppMacroShape( TSNode n, const char* t, TSNode scope, std::string_view src ) { const TSNode decl = fieldChild( n, NodeField::Declarator ); const char* dt = ts_node_is_null( decl ) ? "" : ts_node_type( decl ); const bool bareName = kindIs( dt, "identifier" ) || kindIs( dt, "field_identifier" ); const TSNode type = fieldChild( n, NodeField::Type ); if( kindIs( t, "function_definition" ) || !ts_node_is_null( type ) ) { constexpr std::string_view kClassKeys[] = { "struct_specifier", "class_specifier", "union_specifier" }; const bool elaborated = !ts_node_is_null( type ) && std::ranges::find( kClassKeys, std::string_view( ts_node_type( type ) ) ) != std::end( kClassKeys ) && ts_node_is_null( fieldChild( type, NodeField::Body ) ); return bareName && ( kindIs( t, "function_definition" ) || elaborated ) ? CppMacroShape::DeclaresClass : CppMacroShape::None; } const TSNode callee = kindIs( dt, "function_declarator" ) ? fieldChild( decl, NodeField::Declarator ) : TSNode{}; if( ts_node_is_null( callee ) || !kindIs( ts_node_type( callee ), "identifier" ) ) { return CppMacroShape::None; // a destructor, a conversion operator, a qualified name } const TSNode owner = kindIs( ts_node_type( scope ), "field_declaration_list" ) ? parentOf( scope ) : TSNode{}; TSNode cls = ts_node_is_null( owner ) ? TSNode{} : fieldChild( owner, NodeField::Name ); if( !ts_node_is_null( cls ) && kindIs( ts_node_type( cls ), "template_type" ) ) { cls = fieldChild( cls, NodeField::Name ); } return ts_node_is_null( cls ) || nodeTextOf( callee, src ) != nodeTextOf( cls, src ) ? CppMacroShape::Invocation : CppMacroShape::None; } // one "unrecorded" row per identifier the head of `n` spells (every child but its body): the class it declares is among them inline void pushUnrecordedNames( BindCtx& cx, TSNode n ) { const TSNode body = fieldChild( n, NodeField::Body ); ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { if( ts_node_eq( c, body ) ) { return true; } ChildCursor walk( c ); for( bool down = true;; ) { const TSNode leaf = ts_tree_cursor_current_node( &walk.cur ); if( down && ts_tree_cursor_goto_first_child( &walk.cur ) ) { continue; } if( ts_node_child_count( leaf ) == 0 && std::string_view( ts_node_type( leaf ) ).ends_with( "identifier" ) ) { pushTypeBind( cx, ts_node_start_byte( n ), { 0u, 0u }, nodeTextOf( leaf, cx.src ), kTypeBindUnrecorded ); } down = ts_tree_cursor_goto_next_sibling( &walk.cur ); while( !down && ts_tree_cursor_goto_parent( &walk.cur ) ) { down = ts_tree_cursor_goto_next_sibling( &walk.cur ); } if( !down ) { return true; } } } ); } inline void captureCppUnparsed( BindCtx& cx, TSNode n, const char* t ) { TSNode scope = parentOf( n ); while( !ts_node_is_null( scope ) && kindIs( ts_node_type( scope ), "template_declaration" ) ) { scope = parentOf( scope ); // a template's declaration stands in the template's scope } const char* pt = ts_node_is_null( scope ) ? "" : ts_node_type( scope ); constexpr std::string_view kDeclOnly[] = { "declaration_list", "translation_unit", "field_declaration_list" }; // no statement stands here if( std::ranges::find( kDeclOnly, std::string_view( pt ) ) == std::end( kDeclOnly ) ) { return; } const CppMacroShape shape = cppMacroShape( n, t, scope, cx.src ); if( shape == CppMacroShape::DeclaresClass ) { pushUnrecordedNames( cx, n ); return; } const bool function = kindIs( t, "function_definition" ); // its body's own errors are the body's const bool broken = kindIs( t, "ERROR" ) || kindIs( t, "expression_statement" ) || ( !function && ts_node_has_error( n ) ) || shape == CppMacroShape::Invocation; if( !broken || !spellsIdentifier( n ) ) { return; } const auto span = cppDeclScope( n, true ); if( span.second != 0 ) { pushTypeBind( cx, ts_node_start_byte( n ), span, "!", kTypeBindUnparsed ); } } // may the statement `c` of an enclosing block be a macro invocation that declares an item there — Rust: a statement- or // item-position macro; C++ (fix round 5): any expression statement, parse error or broken declaration (a macro reads as a call) inline bool rustMacroStatement( TSNode c ) { const TSNode inner = kindIs( ts_node_type( c ), "expression_statement" ) && ts_node_named_child_count( c ) > 0 ? ts_node_named_child( c, 0 ) : c; return kindIs( ts_node_type( inner ), "macro_invocation" ); } inline bool cppMacroStatement( TSNode c ) { return ( kindIs( ts_node_type( c ), "ERROR" ) || kindIs( ts_node_type( c ), "expression_statement" ) || ts_node_has_error( c ) ) && spellsIdentifier( c ); } // the blocks whose items a nested declaration sees, per family (data rows): Rust — every block enclosing a fn (a nested fn, // or (fix round 5) a method of an `impl` written in a fn body), up to its module; C++ (fix round 5) — every function body // enclosing a class body (a local class's members). A C++ row carries the block's span (its members are inside it). struct UnparsedBlockRule { CallResultFamily family; std::string_view block; std::string_view stop; bool spanned; bool ( *mayDeclare )( TSNode ); }; inline constexpr UnparsedBlockRule kUnparsedBlockRules[] = { { CallResultFamily::Rust, "block", "mod_item", false, &rustMacroStatement }, { CallResultFamily::Cpp, "compound_statement", "translation_unit", true, &cppMacroStatement }, }; inline void captureUnparsedBlocks( BindCtx& cx, TSNode n ) { const auto rule = std::ranges::find( kUnparsedBlockRules, callResultFamilyOf( cx.lang ), &UnparsedBlockRule::family ); if( rule == std::end( kUnparsedBlockRules ) ) { return; } const std::uint32_t at = ts_node_start_byte( n ); auto& seen = cx.macroBlocks; while( !seen.empty() && seen.back().second <= at ) { seen.pop_back(); // blocks the walk has left (the walk is in source order) } std::vector blocks; // the enclosing blocks not yet read, innermost first; the walk stops at the module for( TSNode up = parentOf( n ); !ts_node_is_null( up ) && ts_node_type( up ) != rule->stop; up = parentOf( up ) ) { if( ts_node_type( up ) == rule->block && std::ranges::none_of( seen, [ & ]( const auto& b ) { return b.first == ts_node_start_byte( up ); } ) ) { blocks.push_back( up ); } } ChildCursor cursor( n ); for( auto b = blocks.rbegin(); b != blocks.rend(); ++b ) { seen.emplace_back( ts_node_start_byte( *b ), ts_node_end_byte( *b ) ); bool macro = false; forEachChild( *b, cursor.cur, [ & ]( TSNode c ) { macro = rule->mayDeclare( c ); return !macro; } ); if( macro ) { pushTypeBind( cx, ts_node_start_byte( *b ), rule->spanned ? std::pair{ ts_node_start_byte( *b ), ts_node_end_byte( *b ) } : std::pair{ 0u, 0u }, "!", kTypeBindUnparsed ); } } } // A TS `namespace` / `module` body, a C++ `namespace` body (an `inline` namespace is transparent: its members are the // enclosing namespace's, so none is recorded), and a C++ using-directive / using-declaration at namespace, file or (fix // round 4) block scope as a TypeImport spanning the scope it is declared in — what a callee's bare result type names // (receiverevidence.h CallResultTypes::visibleAt / cppUnqualifiedClass): a block's is in force for a local class's members // only. One in a class is a member re-export (resolve.h buildUsingReexports) and is not recorded. inline void captureScopeFact( BindCtx& cx, TSNode n, const char* t ) { if( kindIs( t, "internal_module" ) || kindIs( t, "module" ) ) { pushNamespace( cx, n, fieldChild( n, NodeField::Name ), fieldChild( n, NodeField::Body ) ); return; } if( kindIs( t, "namespace_definition" ) ) { const bool isInline = ts_node_child_count( n ) > 0 && kindIs( ts_node_type( ts_node_child( n, 0 ) ), "inline" ); if( !isInline ) { pushNamespace( cx, n, fieldChild( n, NodeField::Name ), fieldChild( n, NodeField::Body ) ); } return; } const auto [ spanStart, spanEnd ] = cppDeclScope( n, false ); // one cursor pass (a using's child list is input-sized: every comment in it is a child — test/childwalkscalecheck.sh // B22), keeping the `namespace` keyword and the last named child that is not a comment (the name used) bool directive = false; TSNode path{}; ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* ct = ts_node_type( c ); directive = directive || ( !ts_node_is_named( c ) && kindIs( ct, "namespace" ) ); path = ts_node_is_named( c ) && !kindIs( ct, "comment" ) ? c : path; return true; } ); std::string whole = importPathText( path, cx.src ); if( whole.starts_with( "::" ) ) { whole.erase( 0, 2 ); } if( spanEnd == 0 || whole.empty() ) { return; } const std::string_view item = directive ? std::string_view{} : namesplit::afterLast( whole, "::" ); cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( n ), .lang = cx.lang, .kind = LocalBindKind::TypeImport, .spanStart = spanStart, .spanEnd = spanEnd, .var = rw::InternedText( directive ? std::string_view( "*" ) : std::string_view( item ) ), .importedName = rw::InternedText( whole ), .typeName = rw::InternedText( item ) } ); } // Rust `use` trees, flattened: `use a::b::C;`, `use a::{b::C, D as E, self, f::*};` — one TypeImport per name bound, the // path rooted at `prefix` (`a::` inside a group). `self` in a group binds the group's own module. inline void captureRustUseTree( BindCtx& cx, std::uint32_t at, TSNode node, const std::string& prefix, int depth ) { if( ts_node_is_null( node ) || depth > 8 ) { return; } const char* t = ts_node_type( node ); if( kindIs( t, "use_list" ) ) { for( std::uint32_t i = 0, k = ts_node_named_child_count( node ); i < k; ++i ) { captureRustUseTree( cx, at, ts_node_named_child( node, i ), prefix, depth + 1 ); } return; } if( kindIs( t, "scoped_use_list" ) ) { const TSNode p = fieldChild( node, NodeField::Path ); captureRustUseTree( cx, at, namedChildOfKind( node, { "use_list" } ), ts_node_is_null( p ) ? prefix : prefix + importPathText( p, cx.src ) + "::", depth + 1 ); return; } if( kindIs( t, "use_wildcard" ) ) { const TSNode p = ts_node_named_child_count( node ) > 0 ? ts_node_named_child( node, 0 ) : TSNode{}; std::string glob = prefix + importPathText( p, cx.src ); if( glob.ends_with( "::" ) ) { glob.resize( glob.size() - 2 ); } pushTypeImport( cx, at, "*", {}, glob ); return; } if( kindIs( t, "use_as_clause" ) ) { const std::string whole = prefix + importPathText( fieldChild( node, NodeField::Path ), cx.src ); pushTypeImport( cx, at, importPathText( fieldChild( node, NodeField::Alias ), cx.src ), namesplit::afterLast( whole, "::" ), whole ); return; } const std::string text = importPathText( node, cx.src ); // an identifier, `crate`/`self`/`super`, or a scoped path if( text == "self" && prefix.size() > 2 ) { const std::string module = prefix.substr( 0, prefix.size() - 2 ); pushTypeImport( cx, at, namesplit::afterLast( module, "::" ), namesplit::afterLast( module, "::" ), module ); return; } const std::string whole = prefix + text; pushTypeImport( cx, at, namesplit::afterLast( whole, "::" ), namesplit::afterLast( whole, "::" ), whole ); } // Is `name` one of the type parameters the identifiers under `node` declare (Go `[T any]`, a generic receiver's `Ref[T]`)? // does the subtree `node` hold an identifier (or, with `typeNames`, a type_identifier) spelled `name`? A subtree of more than // `budget` nodes answers `pastBudget` — false where a miss is the safe answer, true where a hit is (lane std-namesake) inline bool subtreeHoldsName( TSNode node, std::string_view name, std::string_view src, bool typeNames, int budget, bool pastBudget ) { std::vector pending; if( !ts_node_is_null( node ) ) { pending.push_back( node ); } for( int guard = 0; guard < budget && !pending.empty(); ++guard ) { const TSNode n = pending.back(); pending.pop_back(); const char* t = ts_node_type( n ); if( ( kindIs( t, "identifier" ) || ( typeNames && kindIs( t, "type_identifier" ) ) ) && nodeTextOf( n, src ) == name ) { return true; } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { pending.push_back( ts_node_named_child( n, i ) ); } } return !pending.empty() && pastBudget; } inline bool declaresTypeName( TSNode node, std::string_view name, std::string_view src ) { return subtreeHoldsName( node, name, src, /*typeNames=*/true, 256, /*pastBudget=*/false ); } // The declarations whose type parameters scope their members (data row: grammar node kinds; a C++ class template's are its // `template_declaration` parent's). Only these are asked — never a container such as a translation unit, whose child list // is input-sized (test/childwalkscalecheck.sh). inline constexpr std::string_view kGenericOwnerKinds[] = { "class_declaration", "interface_declaration", "object_declaration", "record_declaration", "enum_declaration", "abstract_class_declaration", "class", "impl_item", "trait_item", "struct_item", "enum_item", "template_declaration", }; // does the parameter LIST `list` declare the type parameter `name` — each parameter's own name (its first identifier: `T` in // `<@A T extends Config>`, Rust `T: Bound`, C++ `class T`), never a bound inline bool namesTypeParam( TSNode list, std::string_view name, std::string_view src ) { bool hit = false; if( ts_node_is_null( list ) ) { return false; } ChildCursor cursor( list ); forEachChild( list, cursor.cur, [ & ]( TSNode p ) { TSNode id = kindIs( ts_node_type( p ), "identifier" ) || kindIs( ts_node_type( p ), "type_identifier" ) ? p : TSNode{}; for( std::uint32_t i = 0, k = ts_node_is_null( id ) && ts_node_is_named( p ) ? std::min( ts_node_named_child_count( p ), 4u ) : 0u; i < k && ts_node_is_null( id ); ++i ) { const TSNode c = ts_node_named_child( p, i ); id = kindIs( ts_node_type( c ), "identifier" ) || kindIs( ts_node_type( c ), "type_identifier" ) ? c : TSNode{}; } hit = hit || ( !ts_node_is_null( id ) && nodeTextOf( id, src ) == name ); return !hit; } ); return hit; } // fix round 3: is `name` a type parameter of a generic that ENCLOSES `fn` — a class / interface / object / impl / trait // (`class Box { S get() }`, `impl H { fn load() -> S }`) or a C++ class template (`template struct H { // S load(); }`)? Such a result names the parameter, never a class of that name. The callee's own parameters are each // reader's own check. inline bool enclosingGenericDeclares( TSNode fn, std::string_view name, std::string_view src ) { TSNode a = ts_node_is_null( fn ) ? TSNode{} : parentOf( fn ); for( int guard = 0; guard < 64 && !ts_node_is_null( a ); ++guard, a = parentOf( a ) ) { const std::string_view kind = ts_node_type( a ); if( std::ranges::find( kGenericOwnerKinds, kind ) == std::end( kGenericOwnerKinds ) ) { continue; } if( kind == "template_declaration" ? namesTypeParam( fieldChild( a, NodeField::Parameters ), name, src ) : namesTypeParam( fieldChild( a, NodeField::TypeParameters ), name, src ) || namesTypeParam( namedChildOfKind( a, { "type_parameters", "type_parameter_list" } ), name, src ) ) { return true; } } return false; } // Go: a generic method receiver's type arguments (`[T]` in `func (r Ref[T]) Get() T`) — the names it declares; null otherwise inline TSNode goReceiverTypeArguments( TSNode fn ) { std::vector pending; if( const TSNode recv = fieldChild( fn, NodeField::Receiver ); !ts_node_is_null( recv ) ) { pending.push_back( recv ); } for( int guard = 0; guard < 64 && !pending.empty(); ++guard ) { const TSNode n = pending.back(); pending.pop_back(); if( kindIs( ts_node_type( n ), "type_arguments" ) ) { return n; } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { pending.push_back( ts_node_named_child( n, i ) ); } } return {}; } // Go: the class one written result type names — `T`, `*T`, `pkg.T`, `*pkg.T` (`whole` = "pkg.T" for the qualified one), and // a generic instantiation's own class (`Set[T]`, `*pkg.Set[string]`: a value of `Set[..]` has Set's methods whatever the // arguments are) — or "" for anything else (a slice, map, channel, function, interface literal) or a type parameter of // the definition (`fn`'s own `[T any]` or its receiver's `Ref[T]`). inline std::string_view goResultClass( TSNode type, TSNode fn, std::string_view src, std::string_view& whole ) { whole = {}; for( int guard = 0; guard < 5 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "pointer_type" ) || kindIs( t, "parenthesized_type" ) ) { type = ts_node_named_child_count( type ) == 1 ? ts_node_named_child( type, 0 ) : TSNode{}; continue; } if( kindIs( t, "generic_type" ) ) { type = fieldChild( type, NodeField::Type ); // `Set[T]` / `pkg.Set[T]`: the instantiated class, never an argument continue; } if( kindIs( t, "type_identifier" ) ) { const std::string_view name = nodeTextOf( type, src ); if( declaresTypeName( fieldChild( fn, NodeField::TypeParameters ), name, src ) || declaresTypeName( goReceiverTypeArguments( fn ), name, src ) ) { return {}; } return name; } if( kindIs( t, "qualified_type" ) ) { const TSNode name = fieldChild( type, NodeField::Name ); if( ts_node_is_null( name ) ) { return {}; } whole = nodeTextOf( type, src ); return nodeTextOf( name, src ); } return {}; } return {}; } // Go: one ReturnType per declared result position of a function or method — a single `T` is position 0; a parameter list // counts ONE position per NAME (grouped `(a, b *T, o *U)` is three) and one per unnamed type (`(T, error)`). inline void captureGoReturnTypes( BindCtx& cx, TSNode fn ) { const TSNode result = fieldChild( fn, NodeField::Result ); if( ts_node_is_null( result ) ) { return; } const std::uint32_t defStart = ts_node_start_byte( fn ); std::string_view whole; if( !kindIs( ts_node_type( result ), "parameter_list" ) ) { const std::string_view cls = goResultClass( result, fn, cx.src, whole ); pushReturnType( cx, defStart, 0, cls, whole, 0 ); return; } std::uint32_t position = 0; for( std::uint32_t i = 0, k = ts_node_named_child_count( result ); i < k && position < kWholeResult; ++i ) { const TSNode decl = ts_node_named_child( result, i ); if( !kindIs( ts_node_type( decl ), "parameter_declaration" ) ) { continue; } const std::string_view cls = goResultClass( fieldChild( decl, NodeField::Type ), fn, cx.src, whole ); std::uint32_t names = 0; ChildCursor cursor( decl ); forEachChild( decl, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); names += ( field != nullptr && kindIs( field, "name" ) ) ? 1u : 0u; ( void )c; return true; } ); for( std::uint32_t j = 0, n = names == 0 ? 1u : names; j < n && position < kWholeResult; ++j, ++position ) { pushReturnType( cx, defStart, std::uint8_t( position ), cls, whole, 0 ); } } } // Go: the import qualifier of a written type (`os` in `*os.File`), so graph.h can refuse a class an outside package names // (receiverevidence.h ReceiverEvidence::build). "" for an unqualified type. A generic instantiation keeps its class's // qualifier (`addrs.Set[addrs.Module]` → "addrs.Set"): annotatedClass already reads it as `Set`, and without the qualifier // every in-tree `Set` was a candidate (terraform's `d.suppressedModules.Add` split onto collections.Set.Add). inline std::string_view goQualifiedWhole( TSNode type, std::string_view src ) { for( int guard = 0; guard < 5 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "pointer_type" ) || kindIs( t, "parenthesized_type" ) ) { type = ts_node_named_child_count( type ) == 1 ? ts_node_named_child( type, 0 ) : TSNode{}; continue; } if( kindIs( t, "generic_type" ) ) { type = fieldChild( type, NodeField::Type ); // the instantiated class, never a type argument continue; } return kindIs( t, "qualified_type" ) ? nodeTextOf( type, src ) : std::string_view{}; } return {}; } // C++: a written qualified type as one spelling — `qualifiedNameText`'s (a leading global `::` dropped) without blanks, so // `::std::map`, `std ::map` and `std::map` all read `std::map`; "" for an unqualified name inline std::string cppQualifiedSpelling( TSNode type, std::string_view src ) { return withoutBlanks( qualifiedNameText( type, src ) ); } // C++ (train 29 call-result typing): the class a function's written result names — `T`, `T&`, `T*`, `const T&`, `ns::T` // — with `arrow` set for a std smart pointer / optional (externalnames.h kCppStdArrowTemplates: the class is its `->` // pointee, never its `.` members) and `whole` the qualified spelling (`ext::Config`; "" when written bare), which graph.h // resolves to one in-tree class or refuses. "" for `auto`, a `std::` class (however spelled), any other template // (`std::reference_wrapper`, `std::vector`) or a template parameter of the enclosing template. inline std::string_view cppResultClass( TSNode fn, std::string_view src, bool& arrow, std::string& whole ) { arrow = false; whole.clear(); const TSNode type = fieldChild( fn, NodeField::Type ); if( ts_node_is_null( type ) ) { return {}; } const TSNode tmpl = ancestorOfKind( fn, { "template_declaration" } ); const auto param = [ & ]( std::string_view name ) { return ( !ts_node_is_null( tmpl ) && ts_node_eq( parentOf( fn ), tmpl ) && declaresTypeName( fieldChild( tmpl, NodeField::Parameters ), name, src ) ) || enclosingGenericDeclares( fn, name, src ); // fix round 3: a class template's parameter (`template struct H { S load(); }`) }; const char* t = ts_node_type( type ); if( kindIs( t, "type_identifier" ) ) { const std::string_view name = nodeTextOf( type, src ); return param( name ) ? std::string_view{} : name; } if( !kindIs( t, "qualified_identifier" ) && !kindIs( t, "template_type" ) ) { return {}; } const TSNode tt = kindIs( t, "template_type" ) ? type : namedChildOfKind( type, { "template_type" }, true ); if( ts_node_is_null( tt ) ) { whole = cppQualifiedSpelling( type, src ); // `ns::T` return whole.starts_with( "std::" ) ? std::string_view{} : nodeTextOf( lastNameNode( type ), src ); } // `std::unique_ptr`: a std arrow template whose ONE argument is a plain class const TSNode name = fieldChild( tt, NodeField::Name ); const TSNode args = namedChildOfKind( tt, { "template_argument_list" } ); if( ts_node_is_null( name ) || ts_node_is_null( args ) || ts_node_named_child_count( args ) != 1 || !cppQualifiedSpelling( type, src ).starts_with( "std::" ) || !std::binary_search( std::begin( externalnames::kCppStdArrowTemplates ), std::end( externalnames::kCppStdArrowTemplates ), nodeTextOf( name, src ) ) ) { return {}; } const TSNode arg = ts_node_named_child( args, 0 ); const TSNode cls = kindIs( ts_node_type( arg ), "type_descriptor" ) ? fieldChild( arg, NodeField::Type ) : arg; if( ts_node_is_null( cls ) || !( kindIs( ts_node_type( cls ), "type_identifier" ) || kindIs( ts_node_type( cls ), "qualified_identifier" ) ) ) { return {}; } const std::string_view inner = nodeTextOf( lastNameNode( cls ), src ); whole = cppQualifiedSpelling( cls, src ); if( inner.empty() || whole.starts_with( "std::" ) || param( inner ) ) { whole.clear(); return {}; } arrow = true; return inner; } // C++: the ReturnType of a function definition or declaration (`Config make();` in a header is the symbol a call may bind // when no definition is indexed): position 0; unwrapDepth 1 marks a std arrow template's pointee (reached by `->` only). inline void captureCppReturnType( BindCtx& cx, TSNode fn ) { TSNode d = fieldChild( fn, NodeField::Declarator ); for( int guard = 0; guard < 4 && !ts_node_is_null( d ) && ( kindIs( ts_node_type( d ), "pointer_declarator" ) || kindIs( ts_node_type( d ), "reference_declarator" ) ); ++guard ) { d = ts_node_named_child_count( d ) > 0 ? ts_node_named_child( d, ts_node_named_child_count( d ) - 1 ) : TSNode{}; } if( ts_node_is_null( d ) || !kindIs( ts_node_type( d ), "function_declarator" ) ) { return; // a variable, not a function } bool arrow = false; std::string whole; const std::string_view cls = cppResultClass( fn, cx.src, arrow, whole ); pushReturnType( cx, ts_node_start_byte( fn ), 0, cls, whole, arrow ? 1 : 0 ); } // C++: the variable a declarator names under `&` / `*` / `&&` (`const auto& c`, `auto* p`); null for anything else inline TSNode cppDeclaredName( TSNode d ) { return cppDeclaratorName( d, /*throughPointer=*/true ); } // (L) Java `var x = m()`: the callee's declared result types x (a written type is Rule-2/FE-B's own record) inline void pushJavaVarCallResult( BindCtx& cx, TSNode type, TSNode declarator, BindSite site ) { const TSNode value = fieldChild( declarator, NodeField::Value ); if( !ts_node_is_null( type ) && nodeTextOf( type, cx.src ) == "var" && !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "method_invocation" ) ) { pushCallResult( cx, fieldChild( declarator, NodeField::Name ), value, 0, 0, site ); } } // Java / Kotlin / TS: the class one written result type names — a plain type name (`Config`), TS `Promise` as one LAYER // (an `await` at the call site unwraps it), a qualified name (`java.io.File`, `ns.T`) returned with `whole` set (kept only // when its qualifier names an in-tree package: never, for these languages today — a floor). "" for anything else (a generic // such as Optional / List, an array, a union) or a type parameter of `fn`. inline std::string_view declaredResultClass( TSNode type, TSNode fn, std::string_view src, std::uint8_t& depth, std::string_view& whole ) { whole = {}; for( int guard = 0; guard < 6 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "type_annotation" ) || kindIs( t, "parenthesized_type" ) || ( kindIs( t, "user_type" ) && ts_node_named_child_count( type ) == 1 ) ) { type = ts_node_named_child_count( type ) == 1 ? ts_node_named_child( type, 0 ) : TSNode{}; continue; } if( kindIs( t, "generic_type" ) ) { const TSNode head = fieldChild( type, NodeField::Name ); const TSNode args = namedChildOfKind( type, { "type_arguments" } ); if( ts_node_is_null( head ) || ts_node_is_null( args ) || ts_node_named_child_count( args ) != 1 || nodeTextOf( head, src ) != "Promise" ) { return {}; } ++depth; // TS `Promise`: one `await` type = ts_node_named_child( args, 0 ); continue; } if( kindIs( t, "type_identifier" ) ) { const std::string_view name = nodeTextOf( type, src ); return declaresTypeName( fieldChild( fn, NodeField::TypeParameters ), name, src ) || declaresTypeName( namedChildOfKind( fn, { "type_parameters" } ), name, src ) || enclosingGenericDeclares( fn, name, src ) ? std::string_view{} : name; } if( kindIs( t, "scoped_type_identifier" ) || kindIs( t, "nested_type_identifier" ) || kindIs( t, "user_type" ) ) { whole = nodeTextOf( type, src ); // qualified: refused unless its qualifier is an in-tree package const TSNode last = namedChildOfKind( type, { "type_identifier" }, true ); return ts_node_is_null( last ) ? std::string_view{} : nodeTextOf( last, src ); } return {}; } return {}; } // Java / Kotlin / TS: the declared result of a method or function, as one ReturnType at position 0. Kotlin writes it after // the parameter list with no field; Java's `type` field; TS's `return_type`. inline void captureDeclaredReturnType( BindCtx& cx, TSNode fn ) { TSNode type = fieldChild( fn, NodeField::ReturnType ); if( ts_node_is_null( type ) && kindIs( ts_node_type( fn ), "method_declaration" ) ) { type = fieldChild( fn, NodeField::Type ); } if( ts_node_is_null( type ) && kindIs( ts_node_type( fn ), "function_declaration" ) ) // Kotlin: `fun f( … ): T` { bool afterParams = false; for( std::uint32_t i = 0, k = ts_node_named_child_count( fn ); i < k; ++i ) { const TSNode c = ts_node_named_child( fn, i ); const char* ct = ts_node_type( c ); if( kindIs( ct, "function_value_parameters" ) ) { afterParams = true; } else if( afterParams && ( kindIs( ct, "user_type" ) || kindIs( ct, "nullable_type" ) ) ) { type = c; break; } else if( afterParams ) { break; } } } if( ts_node_is_null( type ) ) { return; } std::uint8_t depth = 0; std::string_view whole; const std::string_view cls = declaredResultClass( type, fn, cx.src, depth, whole ); pushReturnType( cx, ts_node_start_byte( fn ), 0, cls, whole, depth ); } // FE-B (review B3, B4): Java — a typed parameter (its method's or lambda's), each declarator of a local (from the // declaration to its block's end) or field declaration, and every other binding form: an enhanced-for variable, an // inferred lambda parameter, a catch parameter, a try resource, and the pattern variables of `instanceof` and `case` // (visible in their enclosing block — `if (!(o instanceof T t)) return;` keeps `t` after the `if`, so the block is the // superset; their class is not recorded, since an `else` branch does not see them). inline void captureJavaDeclEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { const auto callable = [ & ] { return ancestorOfKind( n, { "method_declaration", "constructor_declaration", "compact_constructor_declaration", "lambda_expression" } ); }; const auto block = [ & ] { return ancestorOfKind( n, { "block", "switch_block_statement_group", "switch_rule", "lambda_expression", "method_declaration" } ); }; if( kindIs( t, "formal_parameter" ) ) { pushDeclaratorEvidence( cx, LocalBindKind::RecvType, fieldChild( n, NodeField::Type ), fieldChild( n, NodeField::Name ), TSNode{}, siteIn( at, callable() ) ); return; } if( kindIs( t, "enhanced_for_statement" ) ) { const TSNode name = fieldChild( n, NodeField::Name ); pushDeclaratorEvidence( cx, LocalBindKind::RecvType, fieldChild( n, NodeField::Type ), name, TSNode{}, siteIn( ts_node_is_null( name ) ? at : ts_node_start_byte( name ), n ) ); return; } if( kindIs( t, "lambda_expression" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Parameters ), {}, siteIn( at, n ) ); // `x -> …` (an identifier only) return; } if( kindIs( t, "inferred_parameters" ) ) { pushPatternTombstones( cx, n, siteIn( at, parentOf( n ) ) ); // `( a, b ) -> …` return; } if( kindIs( t, "catch_formal_parameter" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Name ), {}, siteIn( at, parentOf( n ) ) ); return; } if( kindIs( t, "resource" ) ) { pushDeclaratorEvidence( cx, LocalBindKind::RecvType, fieldChild( n, NodeField::Type ), fieldChild( n, NodeField::Name ), fieldChild( n, NodeField::Value ), siteIn( at, ancestorOfKind( n, { "try_with_resources_statement" } ) ) ); return; } if( kindIs( t, "instanceof_expression" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Name ), {}, siteIn( at, block() ) ); // a record pattern: its components below return; } if( kindIs( t, "type_pattern" ) || kindIs( t, "record_pattern_component" ) ) { pushLocalEvidence( cx, namedChildOfKind( n, { "identifier" }, true ), {}, siteIn( at, block() ) ); return; } if( !kindIs( t, "local_variable_declaration" ) && !kindIs( t, "field_declaration" ) ) { return; } const bool field = kindIs( t, "field_declaration" ); const LocalBindKind kind = field ? LocalBindKind::MemberType : LocalBindKind::RecvType; const TSNode type = fieldChild( n, NodeField::Type ); const BindSite site = field ? BindSite{ at, 0u, 0u } : siteFrom( at, parentOf( n ) ); for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode d = ts_node_named_child( n, i ); if( kindIs( ts_node_type( d ), "variable_declarator" ) ) { pushDeclaratorEvidence( cx, kind, type, fieldChild( d, NodeField::Name ), fieldChild( d, NodeField::Value ), site ); if( !field ) { pushJavaVarCallResult( cx, type, d, site ); } } } } // FE-B (review B3, B4): C# — a typed parameter (its method's, local function's or lambda's), each declarator of a local // (from the declaration to its block's end) or field declaration, a property, and every other binding form: a foreach // variable, an implicit lambda parameter, a catch variable, a query range variable, a deconstruction, and the variables a // pattern or an `out var` declares (visible in their enclosing block, a superset, with no class recorded). inline void captureCSharpDeclEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { const auto block = [ & ] { return ancestorOfKind( n, { "block", "switch_section", "switch_expression_arm", "lambda_expression", "arrow_expression_clause" } ); }; if( kindIs( t, "parameter" ) ) { const TSNode callable = ancestorOfKind( n, { "method_declaration", "constructor_declaration", "destructor_declaration", "operator_declaration", "conversion_operator_declaration", "indexer_declaration", "local_function_statement", "lambda_expression", "anonymous_method_expression", "accessor_declaration" } ); pushDeclaratorEvidence( cx, LocalBindKind::RecvType, fieldChild( n, NodeField::Type ), fieldChild( n, NodeField::Name ), TSNode{}, siteIn( at, callable ) ); return; } if( kindIs( t, "implicit_parameter" ) ) { pushLocalEvidence( cx, nodeTextOf( n, cx.src ), {}, siteIn( at, parentOf( n ) ) ); // `x => …` return; } if( kindIs( t, "foreach_statement" ) ) { const TSNode left = fieldChild( n, NodeField::Left ); if( !ts_node_is_null( left ) && kindIs( ts_node_type( left ), "identifier" ) ) { pushLocalEvidence( cx, left, writtenClass( cx, fieldChild( n, NodeField::Type ), at ), siteIn( ts_node_start_byte( left ), n ) ); } else { pushPatternTombstones( cx, left, siteIn( at, n ) ); // `foreach ( var ( a, b ) in … )` } return; } if( kindIs( t, "declaration_pattern" ) || kindIs( t, "declaration_expression" ) || kindIs( t, "recursive_pattern" ) || kindIs( t, "var_pattern" ) ) { const TSNode name = fieldChild( n, NodeField::Name ); pushPatternTombstones( cx, ts_node_is_null( name ) && kindIs( t, "var_pattern" ) ? n : name, siteIn( at, block() ) ); return; } if( kindIs( t, "tuple_pattern" ) ) { pushPatternTombstones( cx, n, siteIn( at, block() ) ); // `var ( a, b ) = p;` return; } if( kindIs( t, "catch_declaration" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Name ), {}, siteIn( at, parentOf( n ) ) ); return; } if( kindIs( t, "from_clause" ) || kindIs( t, "join_clause" ) || kindIs( t, "let_clause" ) || kindIs( t, "query_continuation" ) || kindIs( t, "join_into_clause" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Name ), {}, siteIn( at, ancestorOfKind( n, { "query_expression" } ) ) ); return; } if( kindIs( t, "property_declaration" ) ) { pushDeclaratorEvidence( cx, LocalBindKind::MemberType, fieldChild( n, NodeField::Type ), fieldChild( n, NodeField::Name ), fieldChild( n, NodeField::Value ), BindSite{ at, 0u, 0u } ); return; } if( !kindIs( t, "variable_declaration" ) ) { return; } // a field's declaration sits in a field_declaration, a local's in a local_declaration_statement (or a using/for), whose // block is where it is visible TSNode up = parentOf( n ); const bool field = !ts_node_is_null( up ) && kindIs( ts_node_type( up ), "field_declaration" ); if( !ts_node_is_null( up ) && kindIs( ts_node_type( up ), "local_declaration_statement" ) ) { up = parentOf( up ); } const LocalBindKind kind = field ? LocalBindKind::MemberType : LocalBindKind::RecvType; const BindSite site = field ? BindSite{ at, 0u, 0u } : siteFrom( at, up ); const TSNode type = fieldChild( n, NodeField::Type ); for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode d = ts_node_named_child( n, i ); if( kindIs( ts_node_type( d ), "variable_declarator" ) ) { // the initializer is the declarator's last named child, when it has one besides the name (no field names it) const TSNode name = fieldChild( d, NodeField::Name ); const TSNode value = ts_node_named_child_count( d ) > 1 ? ts_node_named_child( d, ts_node_named_child_count( d ) - 1 ) : TSNode{}; pushDeclaratorEvidence( cx, kind, type, name, ts_node_eq( value, name ) ? TSNode{} : value, site ); } } } // FE-B (review B3, B4): Kotlin — a typed parameter (its function's or lambda's; `val` constructor parameters are // properties), a local or property declaration (a property in a class body, a local elsewhere, visible from the declaration // to its block's end), and every other variable a declaration binds: a for variable, a lambda parameter, a destructuring // entry, a `when ( val x = … )` subject, a catch parameter. inline void captureKotlinDeclEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( kindIs( t, "parameter" ) || kindIs( t, "class_parameter" ) ) { const bool property = kindIs( t, "class_parameter" ); if( property && ts_node_is_null( namedChildOfKind( n, { "binding_pattern_kind" } ) ) ) { return; // a plain constructor parameter, not a property } const TSNode callable = ancestorOfKind( n, { "function_declaration", "anonymous_function", "secondary_constructor", "setter", "lambda_literal" } ); pushDeclaratorEvidence( cx, property ? LocalBindKind::MemberType : LocalBindKind::RecvType, namedChildOfKind( n, { "user_type", "nullable_type" }, true ), namedChildOfKind( n, { "simple_identifier" }, true ), TSNode{}, property ? BindSite{ at, 0u, 0u } : siteIn( at, callable ) ); return; } if( kindIs( t, "catch_block" ) ) { pushLocalEvidence( cx, namedChildOfKind( n, { "simple_identifier" } ), {}, siteIn( at, n ) ); return; } if( kindIs( t, "variable_declaration" ) ) { // a declaration's own variable is read with the declaration below; here, every other place a variable is bound TSNode up = parentOf( n ); if( !ts_node_is_null( up ) && kindIs( ts_node_type( up ), "multi_variable_declaration" ) ) { up = parentOf( up ); } if( ts_node_is_null( up ) || kindIs( ts_node_type( up ), "property_declaration" ) ) { return; } TSNode scope = up; // a for loop if( kindIs( ts_node_type( up ), "lambda_parameters" ) || kindIs( ts_node_type( up ), "when_subject" ) ) { scope = parentOf( up ); // the lambda / the when expression } else if( !kindIs( ts_node_type( up ), "for_statement" ) ) { scope = ancestorOfKind( n, { "for_statement", "lambda_literal", "when_expression", "function_declaration" } ); } pushLocalEvidence( cx, namedChildOfKind( n, { "simple_identifier" } ), writtenClass( cx, namedChildOfKind( n, { "user_type", "nullable_type" } ), at ), siteIn( at, scope ) ); return; } if( !kindIs( t, "property_declaration" ) ) { return; } const TSNode up = parentOf( n ); const bool field = !ts_node_is_null( up ) && ( kindIs( ts_node_type( up ), "class_body" ) || kindIs( ts_node_type( up ), "enum_class_body" ) ); const BindSite site = field ? BindSite{ at, 0u, 0u } : siteFrom( at, up ); const TSNode var = namedChildOfKind( n, { "variable_declaration" } ); if( !ts_node_is_null( var ) ) { pushDeclaratorEvidence( cx, field ? LocalBindKind::MemberType : LocalBindKind::RecvType, namedChildOfKind( var, { "user_type", "nullable_type" } ), namedChildOfKind( var, { "simple_identifier" } ), namedChildOfKind( n, { "call_expression" } ), site ); if( !field && ts_node_is_null( namedChildOfKind( var, { "user_type", "nullable_type" } ) ) ) // (L) `val x = f()` { pushCallResult( cx, namedChildOfKind( var, { "simple_identifier" } ), namedChildOfKind( n, { "call_expression" } ), 0, 0, site ); } return; } const TSNode multi = namedChildOfKind( n, { "multi_variable_declaration" } ); // `val ( a, b ) = p` for( std::uint32_t i = 0, k = ts_node_is_null( multi ) || field ? 0u : ts_node_named_child_count( multi ); i < k; ++i ) { const TSNode v = ts_node_named_child( multi, i ); pushLocalEvidence( cx, namedChildOfKind( v, { "simple_identifier" } ), writtenClass( cx, namedChildOfKind( v, { "user_type", "nullable_type" } ), at ), site ); } } // Swift: where the names a pattern (or an if/guard/while condition) binds are visible — the statement that binds them, // the rest of the enclosing block for `guard` and for a declaration inline BindSite swiftBindingSite( TSNode n, std::uint32_t at ) { const TSNode s = ancestorOfKind( n, { "for_statement", "if_statement", "while_statement", "repeat_while_statement", "switch_entry", "catch_block", "lambda_literal", "guard_statement", "property_declaration", "function_declaration", "init_declaration" } ); if( !ts_node_is_null( s ) && ( kindIs( ts_node_type( s ), "guard_statement" ) || kindIs( ts_node_type( s ), "property_declaration" ) ) ) { return { at, ts_node_start_byte( s ), ts_node_end_byte( parentOf( s ) ) }; } return siteIn( at, s ); } // FE-B (review B3, B4): Swift — a typed parameter (its function's), a local or property declaration (a property in a // class body, a local elsewhere, visible from the declaration to its block's end), a closure parameter, and every name a // pattern binds: for-in, if/guard/while `let`, `case let`, catch, a tuple declaration. inline void captureSwiftDeclEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( kindIs( t, "parameter" ) ) { // `label p: T`: the LAST identifier is the parameter's own name const TSNode callable = ancestorOfKind( n, { "function_declaration", "init_declaration", "subscript_declaration", "lambda_literal" } ); pushDeclaratorEvidence( cx, LocalBindKind::RecvType, namedChildOfKind( n, { "user_type", "optional_type" }, true ), namedChildOfKind( n, { "simple_identifier" }, true ), TSNode{}, siteIn( at, callable ) ); return; } if( kindIs( t, "lambda_parameter" ) ) { pushLocalEvidence( cx, namedChildOfKind( n, { "simple_identifier" } ), writtenClass( cx, namedChildOfKind( n, { "user_type", "optional_type" } ), at ), siteIn( at, ancestorOfKind( n, { "lambda_literal" } ) ) ); return; } if( kindIs( t, "pattern" ) ) { const TSNode up = parentOf( n ); if( !ts_node_is_null( up ) && kindIs( ts_node_type( up ), "property_declaration" ) && !ts_node_is_null( namedChildOfKind( n, { "simple_identifier" } ) ) ) { return; // a plain declaration: read with its type below } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode c = ts_node_named_child( n, i ); if( kindIs( ts_node_type( c ), "simple_identifier" ) ) { pushLocalEvidence( cx, c, {}, swiftBindingSite( n, at ) ); } } return; } if( kindIs( t, "if_statement" ) || kindIs( t, "guard_statement" ) || kindIs( t, "while_statement" ) ) { // `if let x = …`: the bound name is the statement's own `bound_identifier` child const BindSite site = kindIs( t, "guard_statement" ) ? BindSite{ at, at, ts_node_end_byte( parentOf( n ) ) } : siteIn( at, n ); ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); if( field != nullptr && kindIs( field, "bound_identifier" ) ) { pushLocalEvidence( cx, c, {}, site ); } return true; } ); return; } if( !kindIs( t, "property_declaration" ) ) { return; } const TSNode up = parentOf( n ); const bool field = !ts_node_is_null( up ) && ( kindIs( ts_node_type( up ), "class_body" ) || kindIs( ts_node_type( up ), "enum_class_body" ) ); const TSNode pattern = fieldChild( n, NodeField::Name ); const TSNode ann = namedChildOfKind( n, { "type_annotation" } ); pushDeclaratorEvidence( cx, field ? LocalBindKind::MemberType : LocalBindKind::RecvType, ts_node_is_null( ann ) ? TSNode{} : namedChildOfKind( ann, { "user_type", "optional_type" } ), ts_node_is_null( pattern ) ? TSNode{} : namedChildOfKind( pattern, { "simple_identifier" } ), fieldChild( n, NodeField::Value ), field ? BindSite{ at, 0u, 0u } : siteFrom( at, up ) ); } // FE-B (review B3): a declaration's evidence in Java, C#, Kotlin and Swift — a typed parameter, a typed or constructed // local (RecvType), a typed or constructed field / property (MemberType); (review B4) every other binding form, and the // type parameters a generic declares. The base resolver bound these receivers by name alone; FE-B proves a member call // only through evidence, so the declarations are read here. The four languages share this head (type parameters, JVM // imports and packages, a declared return type); each one's own declaration reader is its row of the per-TU hook table // (receiverEvidenceHookOf: lane rust-receivers registered them there, which removed the `cx.lang ==` chain this ended in). // False when the node was handled here whole. inline bool captureTypedDeclShared( BindCtx& cx, TSNode n, const char* t ) { if( kindIs( t, "type_parameters" ) || kindIs( t, "type_parameter_list" ) ) { noteTypeParameters( cx, n ); return false; } if( ( kindIs( t, "import_declaration" ) || kindIs( t, "import_header" ) ) && callResultFamilyOf( cx.lang ) == CallResultFamily::Jvm ) { captureJvmTypeImport( cx, n, ts_node_start_byte( n ) ); // (L) fix round 1: what a bare result type names in this file return false; } if( ( kindIs( t, "package_declaration" ) || kindIs( t, "package_header" ) ) && callResultFamilyOf( cx.lang ) == CallResultFamily::Jvm ) { // (L) fix round 2 (D3): the file's declared package — a Namespace with no span (it scopes nothing lexically): which // packages this tree declares, whatever directory holds them, so an import of any other package is from outside const TSNode path = namedChildOfKind( n, { "scoped_identifier", "identifier" } ); cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( n ), .lang = cx.lang, .kind = LocalBindKind::Namespace, .var = rw::InternedText( importPathText( path, cx.src ) ) } ); return false; } if( kindIs( t, "method_declaration" ) || kindIs( t, "function_declaration" ) ) { captureDeclaredReturnType( cx, n ); // (L) call-result typing: Java's `type`, Kotlin's type after the parameters (C#/Swift: none read) } return true; } // each language's row: the shared head, then its own declaration reader (`at` = the node's start byte, as each reader expects) inline void captureJavaEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( captureTypedDeclShared( cx, n, t ) ) { captureJavaDeclEvidence( cx, n, t, at ); } } inline void captureCSharpEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( captureTypedDeclShared( cx, n, t ) ) { captureCSharpDeclEvidence( cx, n, t, at ); } } inline void captureKotlinEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( captureTypedDeclShared( cx, n, t ) ) { captureKotlinDeclEvidence( cx, n, t, at ); } } inline void captureSwiftEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( captureTypedDeclShared( cx, n, t ) ) { captureSwiftDeclEvidence( cx, n, t, at ); } } // JS/TS (review B4a): where a declarator's names are visible — a `let`/`const` from the declaration to the end of its block // (or loop / switch / function), a `var` anywhere in its function ({0,0}) inline BindSite jsDeclaratorSite( TSNode declarator ) { const std::uint32_t at = ts_node_start_byte( declarator ); const TSNode decl = parentOf( declarator ); if( ts_node_is_null( decl ) || !kindIs( ts_node_type( decl ), "lexical_declaration" ) ) { return { at, 0u, 0u }; } TSNode scope = parentOf( decl ); for( int guard = 0; guard < 64 && !ts_node_is_null( scope ); ++guard ) { const char* st = ts_node_type( scope ); if( jsFunctionScope( scope ) || kindIs( st, "program" ) || kindIs( st, "statement_block" ) || kindIs( st, "for_statement" ) || kindIs( st, "for_in_statement" ) || kindIs( st, "switch_body" ) || kindIs( st, "class_static_block" ) ) { return siteFrom( at, scope ); } scope = parentOf( scope ); } return { at, 0u, 0u }; } // each name a JS/TS binding pattern binds (never an initializer's or a type's), as a tombstone over `site` inline void pushJsPatternTombstones( BindCtx& cx, TSNode pattern, BindSite site ) { for( const std::string& name : jsPatternNames( pattern, cx.src ) ) { pushLocalEvidence( cx, name, {}, site ); } } // Lane h1-annot: Python's in-place operators — `x op= v` calls the in-place method on the left (`x.__iadd__( v )`) and REBINDS // `x` to what it returns; a class with no in-place method gets `x = x op v` instead (the binary method, `__add__`), which also // rebinds. DATA: one row per operator. struct PythonInPlaceOp { std::string_view op; std::string_view inPlace; std::string_view binary; }; inline constexpr std::array kPythonInPlaceMethods = { { { "+=", "__iadd__", "__add__" }, { "-=", "__isub__", "__sub__" }, { "*=", "__imul__", "__mul__" }, { "@=", "__imatmul__", "__matmul__" }, { "/=", "__itruediv__", "__truediv__" }, { "//=", "__ifloordiv__", "__floordiv__" }, { "%=", "__imod__", "__mod__" }, { "**=", "__ipow__", "__pow__" }, { "<<=", "__ilshift__", "__lshift__" }, { ">>=", "__irshift__", "__rshift__" }, { "&=", "__iand__", "__and__" }, { "^=", "__ixor__", "__xor__" }, { "|=", "__ior__", "__or__" }, } }; // can a Python block NOT fall off its end (fix round 1: an implicit `return None` is not self)? Its last statement is a // `return` / `raise`, a `with` whose body cannot, or an `if` with an `else` where no branch can. Anything else (a loop, a // `try`, a `match`) is read as "may fall through" — the hedge. Bounded by `depth`. inline bool pythonBlockTerminates( TSNode block, int depth = 0 ) { if( ts_node_is_null( block ) || depth > 32 ) { return false; } TSNode last{}; for( std::uint32_t i = ts_node_named_child_count( block ); i > 0; --i ) { const TSNode c = ts_node_named_child( block, i - 1 ); if( !kindIs( ts_node_type( c ), "comment" ) ) { last = c; break; } } if( ts_node_is_null( last ) ) { return false; } const char* t = ts_node_type( last ); if( kindIs( t, "return_statement" ) || kindIs( t, "raise_statement" ) ) { return true; } if( kindIs( t, "with_statement" ) ) { return pythonBlockTerminates( fieldChild( last, NodeField::Body ), depth + 1 ); } if( !kindIs( t, "if_statement" ) ) { return false; } bool hasElse = false; for( std::uint32_t i = 0, k = ts_node_named_child_count( last ); i < k; ++i ) { const TSNode c = ts_node_named_child( last, i ); const char* ct = ts_node_type( c ); TSNode branch{}; if( kindIs( ct, "block" ) ) { branch = c; } else if( kindIs( ct, "elif_clause" ) || kindIs( ct, "else_clause" ) ) { hasElse = hasElse || kindIs( ct, "else_clause" ); for( std::uint32_t j = 0, m = ts_node_named_child_count( c ); j < m; ++j ) { if( kindIs( ts_node_type( ts_node_named_child( c, j ) ), "block" ) ) { branch = ts_node_named_child( c, j ); } } if( ts_node_is_null( branch ) ) { return false; } } else { continue; // the condition } if( !pythonBlockTerminates( branch, depth + 1 ) ) { return false; } } return hasElse; } // does `def` (a Python function_definition) return its first parameter BARE on every path — every `return` returns it, the // body cannot fall off its end (pythonBlockTerminates; fix round 1), no `yield`; // a nested def, lambda or class is another scope and is not read. Bounded: a body past 4096 nodes proves nothing. inline bool pythonReturnsFirstParameter( TSNode def, std::string_view src ) { const TSNode params = fieldChild( def, NodeField::Parameters ); TSNode first = ts_node_is_null( params ) || ts_node_named_child_count( params ) == 0 ? TSNode{} : ts_node_named_child( params, 0 ); if( !ts_node_is_null( first ) && kindIs( ts_node_type( first ), "typed_parameter" ) && ts_node_named_child_count( first ) > 0 ) { first = ts_node_named_child( first, 0 ); // `self: Self` } const std::string_view self = identifierText( first, src ); if( self.empty() ) { return false; } const TSNode body = fieldChild( def, NodeField::Body ); if( !pythonBlockTerminates( body ) ) { return false; // `if o: return self` then an implicit `return None`: not self on every path } std::vector pending{ body }; std::size_t returns = 0; for( std::size_t visited = 0; !pending.empty(); ++visited ) { const TSNode n = pending.back(); pending.pop_back(); if( visited >= 4096 ) { return false; } if( ts_node_is_null( n ) ) { continue; } const char* t = ts_node_type( n ); if( kindIs( t, "function_definition" ) || kindIs( t, "lambda" ) || kindIs( t, "class_definition" ) ) { continue; } if( kindIs( t, "yield" ) ) { return false; // a generator: the call returns a generator object, never self } if( kindIs( t, "return_statement" ) ) { if( ts_node_named_child_count( n ) != 1 || identifierText( ts_node_named_child( n, 0 ), src ) != self ) { return false; // `return`, `return 7`, `return super().__ior__( o )`: not provably self } ++returns; continue; } for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { pending.push_back( ts_node_named_child( n, i ) ); } } return returns > 0; } // the class an operator method's result provably is (model.h LocalBindKind::OperatorResult): "self" — every return returns // the first parameter bare, or the return annotation is `Self` (the receiver's own class, a subclass's too); the method's own // class name — the return annotation names it (`-> Color`, `-> "Color"`); "" — neither (a body that may return anything). inline std::string_view pythonOperatorResult( TSNode def, std::string_view src ) { if( pythonReturnsFirstParameter( def, src ) ) { return "self"; } TSNode ret = fieldChild( def, NodeField::ReturnType ); if( !ts_node_is_null( ret ) && kindIs( ts_node_type( ret ), "type" ) && ts_node_named_child_count( ret ) == 1 ) { ret = ts_node_named_child( ret, 0 ); } std::string_view named = identifierText( ret, src ); if( named.empty() && !ts_node_is_null( ret ) && kindIs( ts_node_type( ret ), "string" ) ) { named = nodeTextOf( ret, src ); // a forward reference `-> "Color"`: the name between the quotes named = named.size() >= 2 && ( named.front() == '"' || named.front() == '\'' ) && named.back() == named.front() ? named.substr( 1, named.size() - 2 ) : std::string_view{}; } if( named.empty() ) { return {}; } if( named == "Self" ) { return "self"; } const TSNode owner = ancestorOfKind( def, { "class_definition", "function_definition", "lambda" } ); const bool own = !ts_node_is_null( owner ) && kindIs( ts_node_type( owner ), "class_definition" ) && nodeFieldText( owner, NodeField::Name, src ) == named; return own ? named : std::string_view{}; } // Lane h1-annot (model.h LocalBindKind::AugAssign / OperatorResult): `x op= v` on a bare name records the operator; an // operator method whose result is provably its receiver's class records so inside its body. ReceiverEvidence::build decides. inline void capturePythonInPlaceFacts( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( kindIs( t, "augmented_assignment" ) ) { const auto op = std::ranges::find( kPythonInPlaceMethods, nodeFieldText( n, NodeField::Operator, cx.src ), &PythonInPlaceOp::op ); const std::string_view var = identifierText( fieldChild( n, NodeField::Left ), cx.src ); if( op != kPythonInPlaceMethods.end() && !var.empty() ) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = at, .lang = cx.lang, .kind = LocalBindKind::AugAssign, .var = rw::InternedText( var ), .importedName = rw::InternedText( op->binary ), .typeName = rw::InternedText( op->inPlace ) } ); } return; } if( !kindIs( t, "function_definition" ) ) { return; } const std::string_view name = nodeFieldText( n, NodeField::Name, cx.src ); const TSNode body = fieldChild( n, NodeField::Body ); bool operatorMethod = false; // one the table's augmented assignments call: in-place, or the binary fallback for( const PythonInPlaceOp& o : kPythonInPlaceMethods ) { operatorMethod = operatorMethod || o.inPlace == name || o.binary == name; } if( ts_node_is_null( body ) || !operatorMethod ) { return; } if( const std::string_view result = pythonOperatorResult( n, cx.src ); !result.empty() ) { cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( body ), .lang = cx.lang, .kind = LocalBindKind::OperatorResult, .var = rw::InternedText( name ), .typeName = rw::InternedText( result ) } ); } } // FE-B (review B4a): Python binds a name for the whole function wherever it assigns it (no block scope), so every // rebinding form records the name with no span: a `for` target, a `with … as` / `except … as` / `case … as` name, a // `match` capture, a walrus name, and an assignment the base Type record does not already type (an annotation is read below, a constructor call by // the base pass). A lambda's parameters and a comprehension's targets are visible inside it only; an untyped parameter // hides an enclosing function's binding of its name. inline void capturePythonBindingTombstones( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { std::vector targets; const auto tombstones = [ & ]( TSNode target, BindSite site ) { targets.clear(); collectPythonNameTargets( target, targets ); for( const TSNode& id : targets ) { pushLocalEvidence( cx, id, {}, site ); } }; if( kindIs( t, "for_statement" ) ) { tombstones( fieldChild( n, NodeField::Left ), BindSite{ at, 0u, 0u } ); } else if( kindIs( t, "for_in_clause" ) ) { tombstones( fieldChild( n, NodeField::Left ), siteIn( at, parentOf( n ) ) ); } else if( kindIs( t, "named_expression" ) ) { tombstones( fieldChild( n, NodeField::Name ), BindSite{ at, 0u, 0u } ); } else if( kindIs( t, "as_pattern" ) ) { // `with X as n:` / `except X as n:` (an `alias` field), `case P() as n:` (the last identifier) TSNode alias = fieldChild( n, NodeField::Alias ); if( ts_node_is_null( alias ) ) { alias = namedChildOfKind( n, { "identifier" }, true ); } tombstones( !ts_node_is_null( alias ) && kindIs( ts_node_type( alias ), "as_pattern_target" ) && ts_node_named_child_count( alias ) > 0 ? ts_node_named_child( alias, 0 ) : alias, BindSite{ at, 0u, 0u } ); } else if( kindIs( t, "case_pattern" ) || kindIs( t, "keyword_pattern" ) || kindIs( t, "splat_pattern" ) ) { // a `match` capture: a bare name (`case n:`, `[n, *rest]`, `P(x=n)`); a dotted name is a value pattern, a class // pattern's class sits under class_pattern for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode c = ts_node_named_child( n, i ); const char* ct = ts_node_type( c ); if( kindIs( ct, "dotted_name" ) && ts_node_named_child_count( c ) == 1 ) { pushLocalEvidence( cx, ts_node_named_child( c, 0 ), {}, BindSite{ at, 0u, 0u } ); } else if( kindIs( t, "splat_pattern" ) && kindIs( ct, "identifier" ) ) { pushLocalEvidence( cx, c, {}, BindSite{ at, 0u, 0u } ); } } } else if( kindIs( t, "lambda" ) ) { const TSNode params = fieldChild( n, NodeField::Parameters ); for( std::uint32_t i = 0, k = ts_node_is_null( params ) ? 0u : ts_node_named_child_count( params ); i < k; ++i ) { const TSNode p = ts_node_named_child( params, i ); pushLocalEvidence( cx, kindIs( ts_node_type( p ), "identifier" ) ? p : fieldChild( p, NodeField::Name ), {}, siteIn( at, n ) ); } } else if( kindIs( t, "parameters" ) ) { // an untyped parameter (a typed one is read with its annotation): `x`, `x=1`, `*xs`, `**kw` for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode p = ts_node_named_child( n, i ); const char* pt = ts_node_type( p ); const TSNode id = kindIs( pt, "identifier" ) ? p : kindIs( pt, "default_parameter" ) ? fieldChild( p, NodeField::Name ) : ( kindIs( pt, "list_splat_pattern" ) || kindIs( pt, "dictionary_splat_pattern" ) ) && ts_node_named_child_count( p ) > 0 ? ts_node_named_child( p, 0 ) : TSNode{}; pushLocalEvidence( cx, id, {}, BindSite{ ts_node_start_byte( p ), 0u, 0u } ); } } else if( kindIs( t, "assignment" ) && ts_node_is_null( fieldChild( n, NodeField::Type ) ) ) { const TSNode lhs = fieldChild( n, NodeField::Left ); const TSNode rhs = fieldChild( n, NodeField::Right ); const bool typedByBase = !ts_node_is_null( lhs ) && kindIs( ts_node_type( lhs ), "identifier" ) && !ts_node_is_null( rhs ) && kindIs( ts_node_type( rhs ), "call" ) && !ts_node_is_null( fieldChild( rhs, NodeField::Function ) ) && kindIs( ts_node_type( fieldChild( rhs, NodeField::Function ) ), "identifier" ); std::string_view whole; // PY-CTOR-ANNOT: `x = mod.X( … )` is recorded by captureReceiverEvidence (typed, or its own tombstone) const bool qualifiedCtor = !ts_node_is_null( lhs ) && kindIs( ts_node_type( lhs ), "identifier" ) && !pyQualifiedConstructed( rhs, cx.src, whole ).empty(); if( !typedByBase && !qualifiedCtor ) { tombstones( lhs, BindSite{ at, 0u, 0u } ); } } } // FE-B (review B4a): Go — where a name declared inside a function is visible: a short variable declaration in a block from // there to the block's end; in an if / for / switch header, that whole statement inline BindSite goShortVarSite( TSNode decl, std::uint32_t at ) { const TSNode up = parentOf( decl ); if( ts_node_is_null( up ) ) { return { at, 0u, 0u }; } const char* ut = ts_node_type( up ); if( kindIs( ut, "block" ) ) { return siteFrom( at, up ); } if( kindIs( ut, "for_clause" ) ) { return siteIn( at, parentOf( up ) ); } if( kindIs( ut, "if_statement" ) || kindIs( ut, "expression_switch_statement" ) || kindIs( ut, "type_switch_statement" ) ) { return siteIn( at, up ); } return siteFrom( at, ancestorOfKind( decl, { "block" } ) ); } // each identifier an expression_list holds (Go's left-hand sides), recorded naming `cls` over `site` inline void pushGoNames( BindCtx& cx, TSNode list, std::string_view cls, BindSite site ) { for( std::uint32_t i = 0, k = ts_node_is_null( list ) ? 0u : ts_node_named_child_count( list ); i < k; ++i ) { pushLocalEvidence( cx, ts_node_named_child( list, i ), cls, site ); // `_` is an identifier too: harmless, never a receiver } } // FE-B: Go's receiver evidence — typed parameters (the method receiver names the method's class), typed / constructed // locals, struct field types; (review B4) every other name a function binds (a range / type-switch / receive variable, an // untyped short declaration, a reassignment), and the type parameters a generic declares. inline void captureGoEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { const std::string_view src = cx.src; if( kindIs( t, "type_parameter_list" ) ) { noteTypeParameters( cx, n ); } else if( kindIs( t, "method_declaration" ) ) { noteGoReceiverTypeParameters( cx, n ); captureGoReturnTypes( cx, n ); } else if( kindIs( t, "function_declaration" ) ) { captureGoReturnTypes( cx, n ); } else if( kindIs( t, "parameter_declaration" ) ) { const TSNode list = parentOf( n ); const TSNode method = ts_node_is_null( list ) ? TSNode{} : parentOf( list ); const bool receiver = !ts_node_is_null( method ) && kindIs( ts_node_type( method ), "method_declaration" ) && ts_node_eq( fieldChild( method, NodeField::Receiver ), list ); const std::string_view type = writtenClass( cx, fieldChild( n, NodeField::Type ), at ); const std::string_view whole = goQualifiedWhole( fieldChild( n, NodeField::Type ), src ); const BindSite site = siteIn( at, ancestorOfKind( n, { "function_declaration", "method_declaration", "func_literal" } ) ); bool named = false; ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); if( field != nullptr && kindIs( field, "name" ) && kindIs( ts_node_type( c ), "identifier" ) ) { named = true; pushLocalEvidence( cx, nodeTextOf( c, src ), type, site, receiver, whole ); } return true; } ); if( !named && receiver ) { pushEvidenceBind( cx, LocalBindKind::RecvType, {}, type, {}, at, true ); // `func (T) m()`: the class alone } } else if( kindIs( t, "var_spec" ) ) { std::string_view type = writtenClass( cx, fieldChild( n, NodeField::Type ), at ); const TSNode values = fieldChild( n, NodeField::Value ); if( type.empty() && ts_node_is_null( fieldChild( n, NodeField::Type ) ) && !ts_node_is_null( values ) && ts_node_named_child_count( values ) == 1 ) { type = constructedBy( ts_node_named_child( values, 0 ), cx.lang, src ); } const std::string_view whole = goQualifiedWhole( fieldChild( n, NodeField::Type ), src ); const BindSite site = siteFrom( at, ancestorOfKind( n, { "block" } ) ); ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); if( field != nullptr && kindIs( field, "name" ) && kindIs( ts_node_type( c ), "identifier" ) ) { pushLocalEvidence( cx, nodeTextOf( c, src ), type, site, false, whole ); } return true; } ); } else if( kindIs( t, "short_var_declaration" ) ) { // `x := T{}` names T; `a, b := …`, `x := f()` name no class. (A plain assignment `x = …` keeps x's declared type: // Go's static typing, so it records nothing.) const TSNode left = fieldChild( n, NodeField::Left ); const TSNode right = fieldChild( n, NodeField::Right ); const bool one = !ts_node_is_null( left ) && !ts_node_is_null( right ) && ts_node_named_child_count( left ) == 1 && ts_node_named_child_count( right ) == 1; const auto site = goShortVarSite( n, at ); pushGoNames( cx, left, one ? constructedBy( ts_node_named_child( right, 0 ), cx.lang, src ) : std::string_view{}, site ); // (L) `x := f()` / `a, b := f()`: one CallResult per name, by position — the resolve loop types each from f's results const TSNode call = ( !ts_node_is_null( right ) && ts_node_named_child_count( right ) == 1 ) ? ts_node_named_child( right, 0 ) : TSNode{}; if( !ts_node_is_null( call ) && kindIs( ts_node_type( call ), "call_expression" ) ) { for( std::uint32_t i = 0, k = ts_node_named_child_count( left ); i < k && i < kWholeResult; ++i ) { pushCallResult( cx, ts_node_named_child( left, i ), call, std::uint8_t( i ), 0, site ); } } } else if( ( kindIs( t, "range_clause" ) || kindIs( t, "receive_statement" ) ) && jsHasToken( n, ":=" ) ) { // `for k, v := range xs` (the loop) / `case v := <-ch:` (the case); with `=` they assign names declared elsewhere pushGoNames( cx, fieldChild( n, NodeField::Left ), {}, siteIn( at, ancestorOfKind( n, { "for_statement", "communication_case" } ) ) ); } else if( kindIs( t, "type_switch_statement" ) ) { pushGoNames( cx, fieldChild( n, NodeField::Alias ), {}, siteIn( at, n ) ); // `switch v := x.(type)` } else if( kindIs( t, "field_declaration" ) ) { const std::string_view written = annotatedClass( fieldChild( n, NodeField::Type ), src ); const std::string_view whole = goQualifiedWhole( fieldChild( n, NodeField::Type ), src ); // `f *os.File`: the qualifier build() checks const bool param = !written.empty() && namesTypeParameter( cx, written, at ); const std::string_view type = param ? std::string_view{} : written; bool named = false; ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); if( field != nullptr && kindIs( field, "name" ) && kindIs( ts_node_type( c ), "field_identifier" ) ) { named = true; if( param ) { pushMemberTombstone( cx, nodeTextOf( c, src ), at ); // `item T` in `Box[T any]` } else { pushEvidenceBind( cx, LocalBindKind::MemberType, nodeTextOf( c, src ), type, whole, at, false ); } } return true; } ); if( !named && !param ) { pushEvidenceBind( cx, LocalBindKind::MemberType, type, type, whole, at, true ); // an embedded field: its methods are promoted } } } // ── Rust (train 29 call-result typing; the shared Rust local-evidence path the rust-receivers lane extends) ────────────────── // The wrapper ROWS a Rust type is read through are model.h's family table (kRustTypeWrappers, typeWrapOf): Box transparent, // Result / Option one LAYER each (an unwrap operator at the call site: `?`, `.unwrap()`, `.expect(..)`), Rc / Arc / RefCell / // Cell / Mutex / RwLock HIDE their argument (their own members answer a call written on them). Matched on the wrapper's last // segment, so `std::result::Result`, `io::Result` and a crate's one-argument `Result` alias all read as Result. A generic // no row names (Vec, HashMap, an in-tree `Stack`) is its own head class, resolved like any name in graph.h. A reader of a // WRITTEN local, parameter or field type (rustWrittenClass) takes the class only at depth 0: a Layer there means it is NOT a T. // the type an `impl` block names (`Book` in `impl Book` / `impl Store` / `impl Trait for Book`) — what `Self` means in it inline std::string_view rustImplSelf( TSNode n, std::string_view src ) { const TSNode impl = ancestorOfKind( n, { "impl_item" } ); TSNode type = ts_node_is_null( impl ) ? TSNode{} : fieldChild( impl, NodeField::Type ); if( !ts_node_is_null( type ) && kindIs( ts_node_type( type ), "generic_type" ) ) { type = fieldChild( type, NodeField::Type ); } return ( !ts_node_is_null( type ) && kindIs( ts_node_type( type ), "type_identifier" ) ) ? nodeTextOf( type, src ) : std::string_view{}; } // Rust: the class one written type names after its transparent wrappers (`&T`, `&mut T`, `Box`), counting the LAYERS it // passes (`Result`, `Option`) into `depth`: `T`, `Self` (the impl's type), or a path's last segment with `whole` // set to the path as written (`crate::model::Config`, `std::fs::File`), which graph.h resolves to an in-tree module's // class or refuses (receiverevidence.h rustResultClass). `tuple` receives a tuple type left at the core (positions are the // caller's). "" for anything else, or a type parameter of `fn`. `spelledSelf` receives whether the class came // from a written `Self` (fix round 1: graph.h types such a call's local only through a qualifier that resolves to it). inline std::string_view rustResultClass( TSNode type, TSNode fn, std::string_view src, std::uint8_t& depth, std::string_view& whole, TSNode* tuple, bool& spelledSelf ) { whole = {}; spelledSelf = false; for( int guard = 0; guard < 8 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "reference_type" ) ) { type = fieldChild( type, NodeField::Type ); continue; } if( kindIs( t, "generic_type" ) ) { const TSNode head = fieldChild( type, NodeField::Type ); std::string_view name = ts_node_is_null( head ) ? std::string_view{} : nodeTextOf( head, src ); if( const std::size_t sep = name.rfind( "::" ); sep != std::string_view::npos ) { name.remove_prefix( sep + 2 ); // `std::result::Result` / `io::Result`: the wrapper's last segment } const TypeWrap wrap = typeWrapOf( CallResultFamily::Rust, name ); const TSNode args = namedChildOfKind( type, { "type_arguments" } ); if( wrap == TypeWrap::None ) { type = head; // rust-receivers: a generic no row names is its own head class (`Stack` is a Stack) continue; } if( wrap == TypeWrap::Hides || ts_node_is_null( args ) || ts_node_named_child_count( args ) == 0 ) { return {}; } depth += wrap == TypeWrap::Layer ? 1u : 0u; type = ts_node_named_child( args, 0 ); continue; } if( kindIs( t, "tuple_type" ) ) { if( tuple != nullptr ) { *tuple = type; } return {}; } if( kindIs( t, "type_identifier" ) && nodeTextOf( type, src ) != "Self" ) { const std::string_view name = nodeTextOf( type, src ); const TSNode params = ts_node_is_null( fn ) ? TSNode{} : fieldChild( fn, NodeField::TypeParameters ); // a null node has no tree return declaresTypeName( params, name, src ) || enclosingGenericDeclares( fn, name, src ) ? std::string_view{} : name; } // `Self` (tree-sitter-rust spells it a type_identifier in a type, `fn new() -> Self`): the impl's type, "" in a trait if( kindIs( t, "self" ) || ( ( kindIs( t, "identifier" ) || kindIs( t, "type_identifier" ) ) && nodeTextOf( type, src ) == "Self" ) ) { spelledSelf = true; return rustImplSelf( fn, src ); } if( kindIs( t, "scoped_type_identifier" ) ) { const std::string_view text = nodeTextOf( type, src ); const TSNode name = fieldChild( type, NodeField::Name ); if( ts_node_is_null( name ) ) { return {}; } whole = text; // graph.h resolves the path (crate / self / super / an in-tree crate or module) or refuses it return nodeTextOf( name, src ); } return {}; } return {}; } // the same reading where whether the class came from a written `Self` does not matter (a written local / parameter / field) inline std::string_view rustResultClass( TSNode type, TSNode fn, std::string_view src, std::uint8_t& depth, std::string_view& whole, TSNode* tuple ) { bool spelledSelf = false; return rustResultClass( type, fn, src, depth, whole, tuple, spelledSelf ); } inline std::string_view rustDispatchTrait( TSNode type, TSNode owner, std::string_view src, std::string_view& whole, std::string_view& generic ); // Rust: one ReturnType per declared result of a fn — the whole value (kWholeResult) or, for a tuple at the core // (`Result<(Book, Ctx)>`), one per element by position; each element is read through the transparent wrappers only. // lane rust-self-chain: is `fn` written `async fn` (its call yields a future of the declared result: kAwaitResultBit) inline bool rustFnIsAsync( TSNode fn ) { for( std::uint32_t i = 0, k = ts_node_child_count( fn ); i < k; ++i ) { const TSNode c = ts_node_child( fn, i ); if( !kindIs( ts_node_type( c ), "function_modifiers" ) ) { continue; } for( std::uint32_t j = 0, m = ts_node_child_count( c ); j < m; ++j ) { if( kindIs( ts_node_type( ts_node_child( c, j ) ), "async" ) ) { return true; } } } return false; } inline void captureRustReturnTypes( BindCtx& cx, TSNode fn ) { const TSNode ret = fieldChild( fn, NodeField::ReturnType ); if( ts_node_is_null( ret ) ) { return; } const std::uint32_t defStart = ts_node_start_byte( fn ); const std::uint8_t awaitBit = rustFnIsAsync( fn ) ? kAwaitResultBit : 0; // the declared result is the awaited future's std::uint8_t depth = 0; std::string_view whole; TSNode tuple{}; bool spelledSelf = false; const std::string_view cls = rustResultClass( ret, fn, cx.src, depth, whole, &tuple, spelledSelf ); if( ts_node_is_null( tuple ) ) { if( cls.empty() && depth == 0 && langFacts( cx.lang ).returnTypedDispatchLet ) { // lane iface-peel: `-> &dyn T` / `-> Box` / `-> impl T` — the trait the result dispatches through. A generic // `-> R` bounded by T (rustDispatchTrait's `generic`) is not read: the CALLER picks R, so it is no implementor set. std::string_view traitWhole; std::string_view generic; const std::string_view trait = rustDispatchTrait( ret, fn, cx.src, traitWhole, generic ); if( !trait.empty() && generic.empty() ) { pushReturnType( cx, defStart, kWholeResult, trait, traitWhole, kDispatchResultBit | awaitBit ); } return; } pushReturnType( cx, defStart, kWholeResult, cls, whole, depth | awaitBit, spelledSelf ); return; } for( std::uint32_t i = 0, k = ts_node_named_child_count( tuple ); i < k && i < kWholeResult; ++i ) { std::uint8_t inner = 0; bool elemSelf = false; const std::string_view elem = rustResultClass( ts_node_named_child( tuple, i ), fn, cx.src, inner, whole, nullptr, elemSelf ); if( inner == 0 ) // an element that is itself a Result/Option layer needs an operator the tuple pattern cannot apply { pushReturnType( cx, defStart, std::uint8_t( i ), elem, whole, depth | awaitBit, elemSelf ); } } } // rust-receivers fix round 1: the qualifier a Rust path call is WRITTEN with — `crate::other::Path` in // `crate::other::Path::new( p )`, `fs::File`, `Self` — blanks removed; "" for any other call (a method call, a bare call). // The call Reference keeps only the last segment (ingest_names.h rustQualifierOf); graph.h resolves this whole path like a // written type before a callee's `-> Self` may type the local (receiverevidence.h rustQualifiedTypeOwns). inline std::string rustCallQualifier( TSNode call, std::string_view src ) { const TSNode fn = fieldChild( call, NodeField::Function ); const TSNode path = ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "scoped_identifier" ) ? TSNode{} : fieldChild( fn, NodeField::Path ); return ts_node_is_null( path ) ? std::string{} : withoutBlanks( std::string( nodeTextOf( path, src ) ) ); } // `x.unwrap()` / `x.expect( … )`: the `x` it strips one layer from; null for any other call. inline TSNode rustUnwrapMethodReceiver( TSNode call, std::string_view src ) { const TSNode fn = fieldChild( call, NodeField::Function ); if( ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "field_expression" ) ) { return {}; } const TSNode field = fieldChild( fn, NodeField::Field ); const std::string_view name = ts_node_is_null( field ) ? std::string_view{} : nodeTextOf( field, src ); return name == "unwrap" || name == "expect" ? fieldChild( fn, NodeField::Value ) : TSNode{}; } // Rust: the call a `let` value binds, through the unwrap operators applied to it — each `?` and each `.unwrap()` / // `.expect( … )` counts one into `depth`; any other method on the result (`.into_other()`, `.iter()`) is not stripped, so // that call is the one recorded (its own result: one level further, a floor). Null when the value is no call. // lane rust-self-chain: an `.await` applied to the call FIRST (`f().await`, `f().await?`) sets kAwaitResultBit in `depth`; an // await anywhere else (`f()?.await`, two awaits) records nothing. inline TSNode rustUnwrappedCall( TSNode value, std::uint8_t& depth, std::string_view src ) { depth = 0; for( int guard = 0; guard < 8 && !ts_node_is_null( value ); ++guard ) { const char* t = ts_node_type( value ); const bool oneChild = ts_node_named_child_count( value ) == 1; if( oneChild && kindIs( t, "await_expression" ) ) { // the await is the innermost operator this reads: the awaited value itself must be the call (an unwrap there is // that call, not a layer) depth |= kAwaitResultBit; const TSNode awaitedValue = ts_node_named_child( value, 0 ); return kindIs( ts_node_type( awaitedValue ), "call_expression" ) ? awaitedValue : TSNode{}; } if( oneChild && kindIs( t, "try_expression" ) ) { ++depth; value = ts_node_named_child( value, 0 ); continue; } if( !kindIs( t, "call_expression" ) ) { return {}; } const TSNode unwrapped = rustUnwrapMethodReceiver( value, src ); if( ts_node_is_null( unwrapped ) ) { return value; } ++depth; value = unwrapped; } return {}; } // ── lane rust-self-chain: a member call on another call's result (model.h LocalBindKind::RecvCall) ──────────────────────────── // The call a Rust member call's receiver evaluates, through parentheses, the `?` operators (`tries`) and an `.await` applied to it // first (`awaited`: `x.f().await?.g()`); null when the receiver is no call (a name, a field, a literal, a macro) or an operator was // applied before the await (`x.f()?.await.g()`), and for a call that is no member call (`f( x )`, a path call: its receiver is no // value). inline TSNode rustReceiverCall( TSNode call, std::uint8_t& tries, bool& awaited ) { tries = 0; awaited = false; const TSNode fn = fieldChild( call, NodeField::Function ); if( ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "field_expression" ) ) { return {}; } TSNode value = fieldChild( fn, NodeField::Value ); for( int guard = 0; guard < 16 && !ts_node_is_null( value ); ++guard ) { const char* t = ts_node_type( value ); if( kindIs( t, "call_expression" ) ) { return value; } if( ts_node_named_child_count( value ) != 1 || awaited ) { return {}; // two awaits, or a `?` applied before the await } if( kindIs( t, "try_expression" ) && tries < kUnwrapLayersMask ) { ++tries; } else if( kindIs( t, "await_expression" ) ) { awaited = true; } else if( !kindIs( t, "parenthesized_expression" ) ) { return {}; } value = ts_node_named_child( value, 0 ); } return {}; } // Rust: one RecvCall per member call whose receiver is another call — `.route( a )` and `.layer( b )` in // `Router::new().route( a ).layer( b )`. Its hop depth counts the calls below it down to the chain's root (rustReceiverCall // walked again from the inner call: the depth the inner call's own record carries, plus one); a chain deeper than a u8 holds, // or an inner call with no name node (a generic instantiation `x.f::()`, a closure call), records nothing. inline void captureRustRecvCall( BindCtx& cx, TSNode call ) { std::uint8_t tries = 0; bool awaited = false; const TSNode inner = rustReceiverCall( call, tries, awaited ); const TSNode outerName = ts_node_is_null( inner ) ? TSNode{} : calleeNameNode( call ); const TSNode innerName = ts_node_is_null( inner ) ? TSNode{} : calleeNameNode( inner ); if( ts_node_is_null( outerName ) || ts_node_is_null( innerName ) ) { return; } std::uint32_t depth = 1; std::uint8_t ignoredTries = 0; bool ignoredAwaited = false; for( TSNode below = rustReceiverCall( inner, ignoredTries, ignoredAwaited ); !ts_node_is_null( below ) && depth < kWholeResult; below = rustReceiverCall( below, ignoredTries, ignoredAwaited ) ) { ++depth; } if( depth >= kWholeResult ) { return; } const std::uint32_t innerStart = ts_node_start_byte( inner ); cx.binds->push_back( RawBind{ .fileId = cx.fileId, .startByte = ts_node_start_byte( call ), .lang = cx.lang, .kind = LocalBindKind::RecvCall, .isFromAssignment = awaited, .resultIndex = std::uint8_t( depth ), .unwrapDepth = tries, .spanStart = innerStart, .spanEnd = innerStart, .var = rw::InternedText( nodeTextOf( innerName, cx.src ) ), .typeName = rw::InternedText( nodeTextOf( outerName, cx.src ) ) } ); } // Rust: where a `let` name is visible — from the end of its statement (`let c = c.f();` reads the OUTER c) to its block's end inline BindSite rustLetSite( TSNode decl, std::uint32_t at ) { auto site = siteFrom( ts_node_end_byte( decl ), ancestorOfKind( decl, { "block" } ) ); // the statement's end .. the block's end site.startByte = at; // the record itself sits at the statement (enclosing-def attribution) return site; } // rust-receivers: the item whose generics a binding's written type may name — the nearest ancestor of `kinds`, else the node // itself (a `let` or closure parameter in a `const` / `static` initializer: no fn; the enclosing impls' generics still count) inline TSNode rustGenericsOwner( TSNode n, std::initializer_list kinds ) { const TSNode owner = ancestorOfKind( n, kinds ); return ts_node_is_null( owner ) ? n : owner; } // rust-receivers: the class a WRITTEN Rust type names for a local, a parameter or a field — rustResultClass's reading (the // wrapper rows, `Self`, a generic's head, a path's last segment with `whole` set to the path) taken at depth 0 only: under a // Result / Option layer the binding is not a T, and names no class. `owner` is the item whose generics the type may name. inline std::string_view rustWrittenClass( TSNode type, TSNode owner, std::string_view src, std::string_view& whole ) { std::uint8_t depth = 0; const std::string_view cls = rustResultClass( type, owner, src, depth, whole, nullptr ); if( depth != 0 ) { whole = {}; return {}; } return cls; } // lane L6 (interface-typed dispatch): the marker traits a generic bound may add beside the one trait it dispatches through — // a bound `R: Renderer + Send + Clone` still dispatches through Renderer: the implementors of Renderer are a SUPERSET of what // may run there, which is the hedge's direction. A bound with two traits outside this row names no one trait (a floor). inline constexpr std::array kRustMarkerTraits = { "Send", "Sync", "Unpin", "Sized", "Copy", "Clone", "Debug", "Default", "Eq", "PartialEq", "Hash", "Ord", "PartialOrd", "Display", "Any", }; // lane L6: the trait a written trait reference names — `Renderer`, `render::Renderer` (`whole` the path), `Renderer` (its // head); "" for anything else (a function trait `Fn( u32 )`, a lifetime, a `?Sized`). inline std::string_view rustTraitNamed( TSNode trait, std::string_view src, std::string_view& whole ) { whole = {}; for( int guard = 0; guard < 4 && !ts_node_is_null( trait ); ++guard ) { const char* t = ts_node_type( trait ); if( kindIs( t, "generic_type" ) ) { trait = fieldChild( trait, NodeField::Type ); continue; } if( kindIs( t, "type_identifier" ) ) { return nodeTextOf( trait, src ); } if( kindIs( t, "scoped_type_identifier" ) && !ts_node_is_null( fieldChild( trait, NodeField::Name ) ) ) { whole = nodeTextOf( trait, src ); return nodeTextOf( fieldChild( trait, NodeField::Name ), src ); } return {}; } return {}; } // lane L6: the ONE non-marker trait among the bounds `generic` is given by `owner`'s type parameters (``) and its `where` // clause (`where R: T`); "" when none, or two or more (each named in either place counts). inline std::string_view rustBoundTrait( TSNode owner, std::string_view generic, std::string_view src, std::string_view& whole ) { whole = {}; std::string_view one; int traits = 0; const auto boundsOf = [ & ]( TSNode left, TSNode bounds ) { if( ts_node_is_null( left ) || ts_node_is_null( bounds ) || nodeTextOf( left, src ) != generic ) { return; } for( std::uint32_t i = 0, k = ts_node_named_child_count( bounds ); i < k; ++i ) { const TSNode b = ts_node_named_child( bounds, i ); std::string_view path; const std::string_view name = rustTraitNamed( b, src, path ); if( kindIs( ts_node_type( b ), "lifetime" ) || std::ranges::find( kRustMarkerTraits, name ) != kRustMarkerTraits.end() ) { continue; } ++traits; one = name; whole = path; } }; const TSNode params = ts_node_is_null( owner ) ? TSNode{} : fieldChild( owner, NodeField::TypeParameters ); for( std::uint32_t i = 0, k = ts_node_is_null( params ) ? 0u : ts_node_named_child_count( params ); i < k; ++i ) { const TSNode p = ts_node_named_child( params, i ); if( kindIs( ts_node_type( p ), "constrained_type_parameter" ) ) { boundsOf( fieldChild( p, NodeField::Left ), namedChildOfKind( p, { "trait_bounds" } ) ); } } const TSNode where = ts_node_is_null( owner ) ? TSNode{} : namedChildOfKind( owner, { "where_clause" } ); for( std::uint32_t i = 0, k = ts_node_is_null( where ) ? 0u : ts_node_named_child_count( where ); i < k; ++i ) { const TSNode p = ts_node_named_child( where, i ); if( kindIs( ts_node_type( p ), "where_predicate" ) ) { boundsOf( fieldChild( p, NodeField::Left ), namedChildOfKind( p, { "trait_bounds" } ) ); } } if( traits != 1 || one.empty() ) { whole = {}; return {}; } return one; } // lane L6: the TRAIT a written Rust type dispatches through, after the transparent wrappers alone (`&`, `&mut`, Box): `dyn T`, // `impl T`, or a type parameter of `owner` bounded by one trait (rustBoundTrait). `whole` the trait's path when qualified; // `generic` the type parameter's name for a bound, else "". "" for anything else — a Layer or a Hides wrapper // (`Option>`, `Rc`: kRustTypeWrappers), a `dyn A + B` the grammar does not give one trait field. inline std::string_view rustDispatchTrait( TSNode type, TSNode owner, std::string_view src, std::string_view& whole, std::string_view& generic ) { whole = {}; generic = {}; for( int guard = 0; guard < 8 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "reference_type" ) ) { type = fieldChild( type, NodeField::Type ); continue; } if( kindIs( t, "generic_type" ) ) { const TSNode head = fieldChild( type, NodeField::Type ); std::string_view name = ts_node_is_null( head ) ? std::string_view{} : nodeTextOf( head, src ); if( const std::size_t sep = name.rfind( "::" ); sep != std::string_view::npos ) { name.remove_prefix( sep + 2 ); } const TSNode args = namedChildOfKind( type, { "type_arguments" } ); if( typeWrapOf( CallResultFamily::Rust, name ) != TypeWrap::Transparent || ts_node_is_null( args ) || ts_node_named_child_count( args ) == 0 ) { return {}; } type = ts_node_named_child( args, 0 ); continue; } if( kindIs( t, "dynamic_type" ) || kindIs( t, "abstract_type" ) ) { return rustTraitNamed( fieldChild( type, NodeField::Trait ), src, whole ); } if( kindIs( t, "type_identifier" ) ) { const std::string_view name = rustBoundTrait( owner, nodeTextOf( type, src ), src, whole ); generic = name.empty() ? std::string_view{} : nodeTextOf( type, src ); return name; } return {}; } return {}; } // lane L6: one Rust binding typed only as SOME implementor of a trait (model.h LocalBindKind::DispatchType): `var` over `site` // (a parameter or local) or, `field` set, a struct's field (fromSymbol the struct). `generic` the bound's type parameter. inline void pushRustDispatch( BindCtx& cx, std::string_view var, std::string_view trait, std::string_view whole, std::string_view generic, BindSite site, bool field ) { const std::string bound = generic.empty() ? std::string() : std::string( generic ).append( ": " ).append( whole.empty() ? trait : whole ); // bound= text pushSiteEvidence( cx, LocalBindKind::DispatchType, var, trait, site, field, generic.empty() ? whole : std::string_view( bound ) ); } // rust-receivers: the class a `let` VALUE constructs — a struct literal `T { .. }` / `Self { .. }` / `m::T { .. }`, or // `T::default()` on a type path (Default's contract: the value is a T); `whole` the path when qualified. "" for anything // else — `T::new( .. )` is a CALL, typed (or not) by its declared result through the call-result rows. inline std::string_view rustConstructedClass( TSNode value, TSNode owner, std::string_view src, std::string_view& whole ) { whole = {}; std::uint8_t depth = 0; if( ts_node_is_null( value ) ) { return {}; } if( kindIs( ts_node_type( value ), "struct_expression" ) ) { return rustResultClass( fieldChild( value, NodeField::Name ), owner, src, depth, whole, nullptr ); } const TSNode fn = kindIs( ts_node_type( value ), "call_expression" ) ? fieldChild( value, NodeField::Function ) : TSNode{}; if( ts_node_is_null( fn ) || !kindIs( ts_node_type( fn ), "scoped_identifier" ) || nodeTextOf( fieldChild( fn, NodeField::Name ), src ) != "default" ) { return {}; } const TSNode path = fieldChild( fn, NodeField::Path ); // `Cfg` (identifier), `Self`, `model::Cfg` (scoped_identifier) if( ts_node_is_null( path ) ) { return {}; } const char* pt = ts_node_type( path ); if( kindIs( pt, "self" ) || nodeTextOf( path, src ) == "Self" ) { return rustImplSelf( owner, src ); } if( kindIs( pt, "identifier" ) ) { const std::string_view name = nodeTextOf( path, src ); const TSNode params = ts_node_is_null( owner ) ? TSNode{} : fieldChild( owner, NodeField::TypeParameters ); return declaresTypeName( params, name, src ) || enclosingGenericDeclares( owner, name, src ) ? std::string_view{} : name; } if( kindIs( pt, "scoped_identifier" ) && !ts_node_is_null( fieldChild( path, NodeField::Name ) ) ) { whole = nodeTextOf( path, src ); // graph.h resolves the path or refuses it, as for a written `m::T` return nodeTextOf( fieldChild( path, NodeField::Name ), src ); } return {}; } // ── lane std-namesake (STD-RECEIVER-NAMESAKE): a written Rust type that spells a STANDARD type ─────────────────────────────── // The SPINE of a written type whose members are the standard library's own (externalnames.h kRustStdReceiverTypes): each type // name the spelling passes, as written, joined by '<' — `Option` -> "Option", `&mut std::sync::Mutex` -> "std::sync::Mutex", // `Arc>` -> "Arc "str", `&[T]` / `[T; 4]` -> "[]". "" for // every other type: a name outside the tables, a type parameter of `owner` or of a generic enclosing it, a wrapper whose // argument is no std receiver (`Arc`), a tuple, a pointer, a `dyn` / `impl`. The spine is a SPELLING, never a proof: // graph.h proves each name is the standard type at the binding's file (receiverevidence.h rustStdSpineHead) before it reads one. inline bool rustStdSpineInto( TSNode type, TSNode owner, std::string_view src, std::string& spine ) { for( int guard = 0; guard < 8 && !ts_node_is_null( type ); ++guard ) { const char* t = ts_node_type( type ); if( kindIs( t, "reference_type" ) ) { type = fieldChild( type, NodeField::Type ); continue; } if( kindIs( t, "array_type" ) ) { spine += "[]"; // `[T]` and `[T; N]`: a slice's or an array's own members return true; } if( kindIs( t, "primitive_type" ) ) { if( nodeTextOf( type, src ) != "str" ) { return false; } spine += "str"; return true; } const bool generic = kindIs( t, "generic_type" ); const TSNode head = generic ? fieldChild( type, NodeField::Type ) : type; if( ts_node_is_null( head ) || !( kindIs( ts_node_type( head ), "type_identifier" ) || kindIs( ts_node_type( head ), "scoped_type_identifier" ) ) ) { return false; } const std::string_view written = nodeTextOf( head, src ); const std::size_t sep = written.rfind( "::" ); const std::string_view name = sep == std::string_view::npos ? written : written.substr( sep + 2 ); if( written.find_first_of( " \t\r\n<" ) != std::string_view::npos ) { return false; // `::Out`, a path the grammar spread over lines: not a plain path to a type } if( sep == std::string_view::npos && ( declaresTypeName( ts_node_is_null( owner ) ? TSNode{} : fieldChild( owner, NodeField::TypeParameters ), name, src ) || enclosingGenericDeclares( owner, name, src ) ) ) { return false; // a type parameter spelled like a std type } if( externalnames::inSortedTable( externalnames::kRustStdReceiverTypes, name ) ) { spine += written; return true; } const TSNode args = generic ? namedChildOfKind( type, { "type_arguments" } ) : TSNode{}; TSNode arg{}; for( std::uint32_t i = 0, k = ts_node_is_null( args ) ? 0u : ts_node_named_child_count( args ); i < k && ts_node_is_null( arg ); ++i ) { if( const TSNode c = ts_node_named_child( args, i ); !kindIs( ts_node_type( c ), "lifetime" ) ) { arg = c; } } if( !externalnames::inSortedTable( externalnames::kRustStdDerefTypes, name ) || ts_node_is_null( arg ) ) { return false; } spine += written; spine += '<'; type = arg; } return false; } inline std::string rustStdSpine( TSNode type, TSNode owner, std::string_view src ) { std::string spine; return rustStdSpineInto( type, owner, src, spine ) ? spine : std::string{}; } // std-namesake: one Rust local or parameter NAME written as a standard type — the tombstone it always was (it names no in-tree // class) carrying the spine as its importedName; false (nothing recorded) for a pattern that is no plain name or a type with no spine inline bool pushRustStdBinding( BindCtx& cx, TSNode pattern, const std::string& spine, BindSite site ) { if( spine.empty() || ts_node_is_null( pattern ) || !kindIs( ts_node_type( pattern ), "identifier" ) ) { return false; } pushSiteEvidence( cx, LocalBindKind::RecvType, nodeTextOf( pattern, cx.src ), {}, site, false, spine ); return true; } // does the pattern (or condition) subtree `node` hold the identifier `name` — a name it may bind? (a subtree past the budget may) inline bool rustSubtreeNames( TSNode node, std::string_view name, std::string_view src ) { return subtreeHoldsName( node, name, src, /*typeNames=*/false, 512, /*pastBudget=*/true ); } // What one binder around a use says about the plain name: undecided (look further out), or decided, with the name's WRITTEN // type — null for every doubt (a binder with no written type, a pattern that binds the name another way). struct RustBinderLook { bool decided = false; TSNode type{}; }; inline RustBinderLook rustBinderDecided( TSNode type ) { return RustBinderLook{ true, type }; } // a statement before the use, in a block or a let chain: `let name: T` / `let name` / `let name = ..` decide; a pattern that holds // the name, or a statement macro that names it (`tokio::pin!( name )` rebinds it), decides null inline RustBinderLook rustStatementBinds( TSNode prev, std::string_view name, std::string_view src ) { const char* pt = ts_node_type( prev ); if( kindIs( pt, "let_declaration" ) || kindIs( pt, "let_condition" ) ) { const TSNode pattern = fieldChild( prev, NodeField::Pattern ); if( !ts_node_is_null( pattern ) && kindIs( ts_node_type( pattern ), "identifier" ) && nodeTextOf( pattern, src ) == name ) { return rustBinderDecided( kindIs( pt, "let_declaration" ) ? fieldChild( prev, NodeField::Type ) : TSNode{} ); } return rustSubtreeNames( pattern, name, src ) ? rustBinderDecided( {} ) : RustBinderLook{}; } // an expression statement holds the macro const TSNode inner = kindIs( pt, "expression_statement" ) && ts_node_named_child_count( prev ) > 0 ? ts_node_named_child( prev, 0 ) : prev; const bool macroNames = kindIs( ts_node_type( inner ), "macro_invocation" ) && nodeTextOf( inner, src ).find( name ) != std::string_view::npos; return macroNames ? rustBinderDecided( {} ) : RustBinderLook{}; } // the parameters of a closure or fn `up`: a typed `name: T` decides T, a pattern holding the name null; an fn item sees no outer // local, so it decides null when no parameter is the name inline RustBinderLook rustParamsBind( TSNode up, std::string_view name, std::string_view src ) { const TSNode params = fieldChild( up, NodeField::Parameters ); for( std::uint32_t i = 0, k = ts_node_is_null( params ) ? 0u : ts_node_named_child_count( params ); i < k; ++i ) { const TSNode p = ts_node_named_child( params, i ); const bool typed = kindIs( ts_node_type( p ), "parameter" ); const TSNode pattern = typed ? fieldChild( p, NodeField::Pattern ) : p; if( typed && !ts_node_is_null( pattern ) && kindIs( ts_node_type( pattern ), "identifier" ) && nodeTextOf( pattern, src ) == name ) { return rustBinderDecided( fieldChild( p, NodeField::Type ) ); } if( rustSubtreeNames( pattern, name, src ) ) { return rustBinderDecided( {} ); } } return kindIs( ts_node_type( up ), "function_item" ) ? rustBinderDecided( {} ) : RustBinderLook{}; } // what the ancestor `up` of a use (`cur` its child on the way up) binds of the name: a block's or let chain's earlier statements // (`budget` statements over the whole walk), a `for` / `match` pattern, an `if let` / `while let` condition over the body, a // closure's or fn's parameters inline RustBinderLook rustAncestorBinds( TSNode up, TSNode cur, std::string_view name, std::string_view src, int& budget ) { const char* ut = ts_node_type( up ); if( kindIs( ut, "block" ) || kindIs( ut, "let_chain" ) ) { for( TSNode prev = ts_node_prev_named_sibling( cur ); !ts_node_is_null( prev ); prev = ts_node_prev_named_sibling( prev ) ) { if( --budget < 0 ) { return rustBinderDecided( {} ); } if( const RustBinderLook look = rustStatementBinds( prev, name, src ); look.decided ) { return look; } } return {}; } if( kindIs( ut, "for_expression" ) || kindIs( ut, "match_arm" ) ) { return rustSubtreeNames( fieldChild( up, NodeField::Pattern ), name, src ) ? rustBinderDecided( {} ) : RustBinderLook{}; } if( kindIs( ut, "if_expression" ) || kindIs( ut, "while_expression" ) ) { const TSNode cond = fieldChild( up, NodeField::Condition ); const bool binds = !ts_node_is_null( cond ) && !ts_node_eq( cond, cur ) && ( kindIs( ts_node_type( cond ), "let_condition" ) || kindIs( ts_node_type( cond ), "let_chain" ) ) && rustSubtreeNames( cond, name, src ); return binds ? rustBinderDecided( {} ) : RustBinderLook{}; // a pattern of the condition binds the name over the body the use is in } if( kindIs( ut, "closure_expression" ) || kindIs( ut, "function_item" ) ) { return rustParamsBind( up, name, src ); } return {}; } // The WRITTEN type of the plain name `name` as Rust's lexical scoping finds it at `use`: the nearest enclosing binder, innermost // first — an earlier `let name: T` of an enclosing block, a typed closure or fn parameter. Null for every doubt: a binder with no // written type, a pattern that binds the name another way (a destructuring `let`, a `for` / `match` / `if let` pattern around the // use, an earlier condition of the same let chain), a statement macro before the use that names it (`tokio::pin!( name )` // rebinds it), a name no enclosing fn binds (a static, a const), or a walk past its budget. inline TSNode rustWrittenTypeOfName( TSNode use, std::string_view name, std::string_view src ) { int budget = 256; // sibling statements read, over the whole walk (each step back is a walk from the parent) for( TSNode cur = use, up = parentOf( use ); !ts_node_is_null( up ); cur = up, up = parentOf( up ) ) { if( const RustBinderLook look = rustAncestorBinds( up, cur, name, src, budget ); look.decided ) { return look.type; } } return {}; } // std-namesake: the payload name of `if let Some( x ) = v` / `while let Ok( x ) = &v` (a let_condition `cond`), where `v` is a // plain name whose WRITTEN type (rustWrittenTypeOfName) is `Option

` / `Result` and P (E for `Err`) spells a standard // type: x records the spine "Option<" + P's — the wrapper's name rides it so graph.h proves `Option` is std's too (an in-tree // `enum Option` with its own `Some` proves nothing). false (nothing recorded) for every other shape; the caller's tombstones stand. inline bool pushRustPayloadBinding( BindCtx& cx, TSNode cond, BindSite site ) { const std::string_view src = cx.src; const TSNode pattern = fieldChild( cond, NodeField::Pattern ); TSNode value = fieldChild( cond, NodeField::Value ); if( ts_node_is_null( pattern ) || !kindIs( ts_node_type( pattern ), "tuple_struct_pattern" ) || ts_node_named_child_count( pattern ) != 2 ) { return false; } const TSNode variant = fieldChild( pattern, NodeField::Type ); const TSNode payload = ts_node_named_child( pattern, 1 ); if( !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "reference_expression" ) ) { value = fieldChild( value, NodeField::Value ); // `= &v` / `= &mut v`: the payload is a reference to the same type } if( ts_node_is_null( variant ) || ts_node_is_null( value ) || !kindIs( ts_node_type( variant ), "identifier" ) || !kindIs( ts_node_type( payload ), "identifier" ) || !kindIs( ts_node_type( value ), "identifier" ) ) { return false; } const std::string_view variantName = nodeTextOf( variant, src ); const std::string_view wrapper = variantName == "Some" ? "Option" : ( variantName == "Ok" || variantName == "Err" ) ? "Result" : std::string_view{}; TSNode type = wrapper.empty() ? TSNode{} : rustWrittenTypeOfName( cond, nodeTextOf( value, src ), src ); while( !ts_node_is_null( type ) && kindIs( ts_node_type( type ), "reference_type" ) ) { type = fieldChild( type, NodeField::Type ); } if( ts_node_is_null( type ) || !kindIs( ts_node_type( type ), "generic_type" ) ) { return false; } const TSNode owner = rustGenericsOwner( cond, { "function_item" } ); std::string spine; TSNode wrapperArg{}; if( !rustStdSpineInto( type, owner, src, spine ) || namesplit::afterLast( spine, "::" ) != wrapper ) { return false; // the scrutinee's own type must spell the wrapper the variant belongs to } const TSNode args = namedChildOfKind( type, { "type_arguments" } ); std::uint32_t want = variantName == "Err" ? 1u : 0u; for( std::uint32_t i = 0, k = ts_node_is_null( args ) ? 0u : ts_node_named_child_count( args ); i < k && ts_node_is_null( wrapperArg ); ++i ) { if( const TSNode c = ts_node_named_child( args, i ); !kindIs( ts_node_type( c ), "lifetime" ) && want-- == 0 ) { wrapperArg = c; } } spine += '<'; return !ts_node_is_null( wrapperArg ) && rustStdSpineInto( wrapperArg, owner, src, spine ) && pushRustStdBinding( cx, payload, spine, site ); } // rust-receivers: one name a Rust binding declares — typed (`cls`, `whole` its qualified spelling) or, when no class is // named, the tombstones of every name `pattern` holds inline void pushRustBinding( BindCtx& cx, TSNode pattern, std::string_view cls, std::string_view whole, BindSite site ) { if( !cls.empty() && !ts_node_is_null( pattern ) && kindIs( ts_node_type( pattern ), "identifier" ) ) { pushLocalEvidence( cx, nodeTextOf( pattern, cx.src ), cls, site, false, whole ); return; } pushPatternTombstones( cx, pattern, site ); } // lane L6: a parameter or `let` NAME (a plain identifier pattern) whose written type names no class but dispatches through a trait // (rustDispatchTrait) records a DispatchType in place of its tombstone; false (nothing recorded) for any other binding. inline bool pushRustDispatchBinding( BindCtx& cx, TSNode pattern, std::string_view cls, TSNode type, TSNode owner, BindSite site ) { if( !cls.empty() || ts_node_is_null( pattern ) || !kindIs( ts_node_type( pattern ), "identifier" ) ) { return false; } std::string_view whole, generic; const std::string_view trait = rustDispatchTrait( type, owner, cx.src, whole, generic ); if( trait.empty() ) { return false; } pushRustDispatch( cx, nodeTextOf( pattern, cx.src ), trait, whole, generic, site, false ); return true; } // a parameter's or `let`'s names under their WRITTEN type: the class it names (rustWrittenClass), else (lane L6) the trait it // dispatches through, else the pattern's tombstones inline void pushRustWrittenBinding( BindCtx& cx, TSNode pattern, TSNode type, TSNode owner, BindSite site ) { std::string_view whole; const std::string_view cls = rustWrittenClass( type, owner, cx.src, whole ); if( pushRustDispatchBinding( cx, pattern, cls, type, owner, site ) ) { return; } // std-namesake: a type that names no class (`Option`, `&str`, `Arc>`) may still spell a standard one if( !cls.empty() || !pushRustStdBinding( cx, pattern, rustStdSpine( type, owner, cx.src ), site ) ) { pushRustBinding( cx, pattern, cls, whole, site ); } } // rust-receivers: a `let`'s names — `let x: T` typed by its written type, `let x = T { .. }` / `T::default()` by the class // the value constructs, `let x = f()?` by the callee's declared result (a CallResult per bound name, a tuple by position) inline void captureRustLet( BindCtx& cx, TSNode n, std::uint32_t at ) { const TSNode pattern = fieldChild( n, NodeField::Pattern ); const auto site = rustLetSite( n, at ); const TSNode owner = rustGenericsOwner( n, { "function_item" } ); std::string_view whole; if( const TSNode type = fieldChild( n, NodeField::Type ); !ts_node_is_null( type ) ) { pushRustWrittenBinding( cx, pattern, type, owner, site ); // `let x: T = …` (L6: `let x: &dyn T = …`) return; } // `let x = T { .. }` / `T::default()`: the class the value constructs, as the binding's site spells it. `T::default()` // is a call too, and its CallResult rides beside it: where the spelling resolves to no class here (the record is // then an empty fact), the callee's declared result may still type it (graph.h's resolve loop fills an empty fact). pushRustBinding( cx, pattern, rustConstructedClass( fieldChild( n, NodeField::Value ), owner, cx.src, whole ), whole, site ); std::uint8_t depth = 0; const TSNode call = rustUnwrappedCall( fieldChild( n, NodeField::Value ), depth, cx.src ); if( ts_node_is_null( call ) || ts_node_is_null( pattern ) ) { return; } if( kindIs( ts_node_type( pattern ), "identifier" ) ) { pushCallResult( cx, pattern, call, kWholeResult, depth, site, rustCallQualifier( call, cx.src ) ); } else if( kindIs( ts_node_type( pattern ), "tuple_pattern" ) ) { std::uint32_t position = 0; // every element counts, `_` included: walk ALL children, skipping the punctuation for( std::uint32_t i = 0, k = ts_node_child_count( pattern ); i < k && position < kWholeResult; ++i ) { const TSNode c = ts_node_child( pattern, i ); const char* ct = ts_node_type( c ); if( kindIs( ct, "(" ) || kindIs( ct, ")" ) || kindIs( ct, "," ) ) { continue; } if( kindIs( ct, "identifier" ) ) { pushCallResult( cx, c, call, std::uint8_t( position ), depth, site, rustCallQualifier( call, cx.src ) ); } ++position; } } } // rust-receivers: a parameter `b: &Book` names the class it is written as (a fn's or a closure's); any other pattern: tombstones inline void captureRustParameter( BindCtx& cx, TSNode n, std::uint32_t at ) { pushRustWrittenBinding( cx, fieldChild( n, NodeField::Pattern ), fieldChild( n, NodeField::Type ), rustGenericsOwner( n, { "function_item", "function_signature_item" } ), siteIn( at, ancestorOfKind( n, { "function_item", "closure_expression" } ) ) ); } // rust-receivers: a struct's named field `book: Book` (MemberType on the struct, so `self.book.m()` narrows through it); a // field whose type names no class is a tombstone. An enum variant's fields are no struct's: nothing. inline void captureRustField( BindCtx& cx, TSNode n, std::uint32_t at ) { const TSNode list = parentOf( n ); const TSNode item = ts_node_is_null( list ) ? TSNode{} : parentOf( list ); const TSNode name = fieldChild( n, NodeField::Name ); if( ts_node_is_null( item ) || !kindIs( ts_node_type( item ), "struct_item" ) || ts_node_is_null( name ) ) { return; } std::string_view whole; const std::string_view cls = rustWrittenClass( fieldChild( n, NodeField::Type ), item, cx.src, whole ); std::string_view traitWhole, generic; const std::string_view trait = cls.empty() ? rustDispatchTrait( fieldChild( n, NodeField::Type ), item, cx.src, traitWhole, generic ) : std::string_view{}; if( !trait.empty() ) { pushMemberTombstone( cx, nodeTextOf( name, cx.src ), at ); // the field names no class (memberClass), as before pushRustDispatch( cx, nodeTextOf( name, cx.src ), trait, traitWhole, generic, BindSite{ at, 0u, 0u }, true ); // L6 } else if( cls.empty() ) { pushMemberTombstone( cx, nodeTextOf( name, cx.src ), at ); if( const std::string spine = rustStdSpine( fieldChild( n, NodeField::Type ), item, cx.src ); !spine.empty() ) { cx.binds->back().importedName.assign( spine ); // std-namesake: the field's tombstone carries the standard type it spells } } else { pushEvidenceBind( cx, LocalBindKind::MemberType, nodeTextOf( name, cx.src ), cls, whole, at, false ); } } // FE-B for Rust (train 29): every name a function binds is a tombstone over its span — `let` patterns, parameters, closure // parameters, `for` / `if let` / `while let` / `match` patterns — so a typed binding never leaks to a shadowing one; a `let` // whose value is a call (through `?` / `.unwrap()` / `.expect()`) records one CallResult per bound name (a tuple pattern by // position), which graph.h's resolve loop types from the callee's declared result; each fn records its ReturnTypes. // std-namesake: `if let Some( x ) = v` under a written `v: Option>` records x's spine (pushRustPayloadBinding); any other // let condition's names are tombstones inline void captureRustLetCondition( BindCtx& cx, TSNode n, std::uint32_t at ) { const BindSite site = siteIn( at, ancestorOfKind( n, { "if_expression", "while_expression", "let_chain" } ) ); if( !pushRustPayloadBinding( cx, n, site ) ) { pushPatternTombstones( cx, fieldChild( n, NodeField::Pattern ), site ); } } // std-namesake: the name a member access is written on (BindCtx::rustReceiverRoots) — `m` in `m.get( k )` / `m.len`, and the // same inside a macro's arguments (`assert!( m.get( k ).is_some() )`), where the grammar keeps tokens: NAME then `.` inline void noteRustReceiverRoots( BindCtx& cx, TSNode n, const char* t ) { if( kindIs( t, "field_expression" ) ) { if( const TSNode value = fieldChild( n, NodeField::Value ); !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "identifier" ) ) { cx.rustReceiverRoots.push_back( nodeTextOf( value, cx.src ) ); } return; } if( !kindIs( t, "token_tree" ) ) { return; } // O(children): one cursor pass, the previous token carried (an indexed ts_node_child walk is quadratic on a long tree) std::string_view before; // the identifier just before this token, "" when the previous token was none ChildCursor tokens( n ); forEachChild( n, tokens.cur, [ & ]( TSNode c ) { const char* ct = ts_node_type( c ); if( !before.empty() && kindIs( ct, "." ) ) { cx.rustReceiverRoots.push_back( before ); } before = kindIs( ct, "identifier" ) ? nodeTextOf( c, cx.src ) : std::string_view{}; return true; } ); } // `|a, (b, c)|`: each pattern's names are tombstones; a typed `|b: &Book|` is a `parameter`, recorded by its own arm. // Moved out of captureRustEvidence by train 1117d2 (the composed std-namesake and rust-self-chain arms took it past qd's bar). inline void captureRustClosureParameters( BindCtx& cx, TSNode n, std::uint32_t at ) { const BindSite site = siteIn( at, parentOf( n ) ); for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { if( const TSNode c = ts_node_named_child( n, i ); !kindIs( ts_node_type( c ), "parameter" ) ) { pushPatternTombstones( cx, c, site ); } } } inline void captureRustEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( kindIs( t, "function_item" ) ) { captureRustReturnTypes( cx, n ); } else if( kindIs( t, "call_expression" ) ) { captureRustRecvCall( cx, n ); // lane rust-self-chain: a member call on another call's result } else if( kindIs( t, "use_declaration" ) ) { captureRustUseTree( cx, at, fieldChild( n, NodeField::Argument ), std::string{}, 0 ); // (L) fix round 1: `use` names } else if( kindIs( t, "let_declaration" ) ) { captureRustLet( cx, n, at ); } else if( kindIs( t, "parameter" ) ) { captureRustParameter( cx, n, at ); } else if( kindIs( t, "field_declaration" ) ) { captureRustField( cx, n, at ); } else if( kindIs( t, "closure_parameters" ) ) { captureRustClosureParameters( cx, n, at ); } else if( kindIs( t, "for_expression" ) ) { pushPatternTombstones( cx, fieldChild( n, NodeField::Pattern ), siteIn( at, n ) ); } else if( kindIs( t, "let_condition" ) ) { captureRustLetCondition( cx, n, at ); } else if( kindIs( t, "match_arm" ) ) { pushPatternTombstones( cx, fieldChild( n, NodeField::Pattern ), siteIn( at, n ) ); } else { noteRustReceiverRoots( cx, n, t ); // std-namesake } } // std-namesake: a tombstone that carries a standard type's spine (pushRustStdBinding) is read only where its name is written as a // member access's root in this file (`roots`: BindCtx::rustReceiverRoots); any other becomes the plain tombstone it was before the // lane, which pruneRustTombstones then prunes like any other. Run before it. inline void dropUnreadRustSpines( std::vector& binds, std::size_t first, std::vector& roots ) { std::sort( roots.begin(), roots.end(), rw::sortutil::svLess ); // an explicit byte order (portablebuildcheck #6) for( std::size_t i = first; i < binds.size(); ++i ) { RawBind& b = binds[ i ]; if( b.kind == LocalBindKind::RecvType && b.typeName.empty() && !b.importedName.empty() && !std::binary_search( roots.begin(), roots.end(), std::string_view( b.var ), rw::sortutil::svLess ) ) { b.importedName.assign( std::string_view{} ); // not `= {}`: that picks the `const char*` assignment with a null pointer } } } // (L) fix round 2 (memory): a Rust tombstone — the untyped RecvType of a `let`, parameter, closure, `for`, `if let` or match // arm name — only ever HIDES a typed fact of the same name where both are visible: a call-typed one (CallResult) or, since // rust-receivers, a written or constructed one (a RecvType WITH a class: a typed parameter, `let x: T`, `let x = T { .. }`); // Rust receivers read no other local fact (Rust is no class-name-receiver language and records no module alias). So a file // keeps a tombstone only when one of its typed records binds the same name over a span that overlaps the tombstone's, and // drops the rest before they are cached or merged (nushell: 98 740 tombstones → 21 091 at train 29). inline void pruneRustTombstones( std::vector& binds, std::size_t first ) { struct Typed { std::string var; // owned: remove_if below moves the records the names came from std::uint32_t start = 0; std::uint32_t end = 0; bool operator<( const Typed& o ) const { return var < o.var; } }; std::vector typed; for( std::size_t i = first; i < binds.size(); ++i ) { // a kept std tombstone is a fact a shadowing tombstone must hide, as a typed record is if( binds[ i ].kind == LocalBindKind::CallResult || ( binds[ i ].kind == LocalBindKind::RecvType && ( !binds[ i ].typeName.empty() || !binds[ i ].importedName.empty() ) ) ) { typed.push_back( { binds[ i ].var, binds[ i ].spanStart, binds[ i ].spanEnd } ); } } std::sort( typed.begin(), typed.end() ); const auto hidesNothing = [ & ]( const RawBind& b ) { if( b.kind != LocalBindKind::RecvType || !b.typeName.empty() || !b.importedName.empty() ) { return false; } const auto [ lo, hi ] = std::equal_range( typed.begin(), typed.end(), Typed{ b.var, 0, 0 } ); return std::none_of( lo, hi, [ & ]( const Typed& t ) { return b.spanStart < t.end && t.start < b.spanEnd; } ); }; binds.erase( std::remove_if( binds.begin() + std::ptrdiff_t( first ), binds.end(), hidesNothing ), binds.end() ); } // FE-B: JS/TS receiver evidence — constructed / typed locals and parameters, `this.x` and TS field types, `static` members, // prototype methods; (review B4) every other name a function binds (an untyped parameter, a destructuring, a for-in/of // variable, a catch parameter, a reassignment), each visible where the language scopes it, and TS type parameters. inline void captureJsEvidence( BindCtx& cx, TSNode n, const char* t, std::uint32_t at ) { if( kindIs( t, "function_declaration" ) || kindIs( t, "method_definition" ) || kindIs( t, "function_signature" ) ) { captureDeclaredReturnType( cx, n ); // (L) call-result typing: a TS return annotation (JS writes none) } const Lang lang = cx.lang; const std::string_view src = cx.src; if( kindIs( t, "type_parameters" ) ) { noteTypeParameters( cx, n ); } else if( kindIs( t, "variable_declarator" ) ) { const TSNode name = fieldChild( n, NodeField::Name ); const BindSite site = jsDeclaratorSite( n ); if( ts_node_is_null( name ) || !kindIs( ts_node_type( name ), "identifier" ) ) { pushJsPatternTombstones( cx, name, site ); // `const { a, b } = o`, `const [ a ] = xs` return; } const TSNode value = fieldChild( n, NodeField::Value ); if( lang == Lang::JavaScript ) { pushLocalEvidence( cx, name, constructedBy( value, lang, src ), site ); return; } // TS records a written or constructed class as a Type record already (the base pass: the annotation's first // type_identifier, else ctorTypeOf); here, a declaration that names none hides an outer binding of its name, and one // whose written type is a type parameter ties that Type record (unknown) const TSNode ann = fieldChild( n, NodeField::Type ); TSNode written{}; if( !ts_node_is_null( ann ) ) { ChildCursor annCursor( ann ); forEachChild( ann, annCursor.cur, [ & ]( TSNode c ) { if( !kindIs( ts_node_type( c ), "type_identifier" ) ) { return true; } written = c; return false; } ); } if( !ts_node_is_null( written ) ) { if( namesTypeParameter( cx, nodeTextOf( written, src ), at ) ) { pushLocalEvidence( cx, name, {}, BindSite{ at, 0u, 0u } ); } } else { if( ctorTypeOf( value, src ).empty() ) { pushLocalEvidence( cx, name, {}, site ); } // (L) `const x = f()` / `const x = await f()`: the callee's declared result types x (an await unwraps a Promise) const bool awaited = !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "await_expression" ) && ts_node_named_child_count( value ) == 1; const TSNode call = awaited ? ts_node_named_child( value, 0 ) : value; if( !ts_node_is_null( call ) && kindIs( ts_node_type( call ), "call_expression" ) && callResultFamilyOf( cx.lang ) != CallResultFamily::None ) { pushCallResult( cx, name, call, 0, awaited ? 1 : 0, site ); // TS only: JavaScript declares no result type } } } else if( kindIs( t, "formal_parameters" ) ) { const BindSite site = siteIn( at, parentOf( n ) ); // the function, arrow or method the list belongs to for( std::uint32_t i = 0, k = ts_node_named_child_count( n ); i < k; ++i ) { const TSNode p = ts_node_named_child( n, i ); const TSNode pattern = lang == Lang::TypeScript ? fieldChild( p, NodeField::Pattern ) : TSNode{}; if( !ts_node_is_null( pattern ) && kindIs( ts_node_type( pattern ), "identifier" ) ) { std::string_view whole; // `s: ns.T`: the qualifier ReceiverEvidence::build checks (h1-annot) const std::string_view cls = writtenClass( cx, fieldChild( p, NodeField::Type ), at, &whole ); pushLocalEvidence( cx, nodeTextOf( pattern, src ), cls, site, false, whole ); } else { pushJsPatternTombstones( cx, p, site ); } } } else if( kindIs( t, "arrow_function" ) ) { pushLocalEvidence( cx, fieldChild( n, NodeField::Parameter ), {}, siteIn( at, n ) ); // `x => …` (an identifier only) } else if( kindIs( t, "for_in_statement" ) ) { const bool lexical = jsHasToken( n, "let" ) || jsHasToken( n, "const" ); pushJsPatternTombstones( cx, fieldChild( n, NodeField::Left ), lexical ? siteIn( at, n ) : BindSite{ at, 0u, 0u } ); } else if( kindIs( t, "catch_clause" ) ) { pushJsPatternTombstones( cx, fieldChild( n, NodeField::Parameter ), siteIn( at, n ) ); } else if( kindIs( t, "method_definition" ) ) { // `static m () {}`: the member's side of the lookup, recorded inside its body so it attributes to the member const TSNode name = fieldChild( n, NodeField::Name ); const TSNode body = fieldChild( n, NodeField::Body ); bool isStatic = false; for( std::uint32_t i = 0, k = ts_node_child_count( n ); i < k && !isStatic; ++i ) { isStatic = kindIs( ts_node_type( ts_node_child( n, i ) ), "static" ); } if( isStatic && !ts_node_is_null( name ) && !ts_node_is_null( body ) ) { pushEvidenceBind( cx, LocalBindKind::StaticMember, nodeTextOf( name, src ), "static", {}, ts_node_start_byte( body ), false ); } } else if( lang == Lang::TypeScript && kindIs( t, "public_field_definition" ) ) { // TS-FIELD-SELF: a `#private` field declares its type as a public one does (its name, `#f`, is how `this.#f` spells the path) const TSNode name = fieldChild( n, NodeField::Name ); if( !ts_node_is_null( name ) && ( kindIs( ts_node_type( name ), "property_identifier" ) || kindIs( ts_node_type( name ), "private_property_identifier" ) ) ) { std::string_view whole; // `s: ns.T`: the qualifier ReceiverEvidence::build checks (h1-annot) const std::string_view written = annotatedClass( fieldChild( n, NodeField::Type ), src, &whole ); if( !written.empty() && namesTypeParameter( cx, written, at ) ) { pushMemberTombstone( cx, nodeTextOf( name, src ), at ); // `item: T` in `class Box` return; } const std::string_view type = written.empty() ? constructedBy( fieldChild( n, NodeField::Value ), lang, src ) : written; pushEvidenceBind( cx, LocalBindKind::MemberType, nodeTextOf( name, src ), type, written.empty() ? std::string_view{} : whole, at, false ); } } else if( kindIs( t, "assignment_expression" ) || kindIs( t, "augmented_assignment_expression" ) ) { const TSNode lhs = fieldChild( n, NodeField::Left ); if( ts_node_is_null( lhs ) ) { return; } if( kindIs( ts_node_type( lhs ), "identifier" ) ) { // a JS rebinding can change the class (no span) — `x += 1` too (h1-annot: JS has no operator methods, so `+=` // leaves a primitive; `x ||= new T()` names T); a TS one keeps the declared type if( lang == Lang::JavaScript ) { pushLocalEvidence( cx, lhs, constructedBy( fieldChild( n, NodeField::Right ), lang, src ), BindSite{ at, 0u, 0u } ); } return; } if( !kindIs( ts_node_type( lhs ), "member_expression" ) || kindIs( t, "augmented_assignment_expression" ) ) { return; // a member's `+=` keeps its field's class, as a plain `this.x = f()` does (pushEvidenceBind records no "") } const TSNode obj = fieldChild( lhs, NodeField::Object ); const TSNode prop = fieldChild( lhs, NodeField::Property ); if( !ts_node_is_null( obj ) && !ts_node_is_null( prop ) && kindIs( ts_node_type( obj ), "this" ) && ( kindIs( ts_node_type( prop ), "property_identifier" ) || kindIs( ts_node_type( prop ), "private_property_identifier" ) ) ) { pushEvidenceBind( cx, LocalBindKind::MemberType, nodeTextOf( prop, src ), constructedBy( fieldChild( n, NodeField::Right ), lang, src ), {}, at, false ); } // `Foo.prototype.m = function …`: the member's class, recorded INSIDE the function so the record attributes to it const TSNode rhs = fieldChild( n, NodeField::Right ); if( !ts_node_is_null( obj ) && kindIs( ts_node_type( obj ), "member_expression" ) && !ts_node_is_null( rhs ) && ( kindIs( ts_node_type( rhs ), "function_expression" ) || kindIs( ts_node_type( rhs ), "function" ) || kindIs( ts_node_type( rhs ), "arrow_function" ) ) && nodeTextOf( fieldChild( obj, NodeField::Property ), src ) == "prototype" ) { const TSNode ctor = fieldChild( obj, NodeField::Object ); if( !ts_node_is_null( ctor ) && kindIs( ts_node_type( ctor ), "identifier" ) ) { pushEvidenceBind( cx, LocalBindKind::RecvType, {}, nodeTextOf( ctor, src ), {}, ts_node_start_byte( rhs ), true ); } } } } // FE-B: one node's receiver evidence: Python, JS/TS and Go, and (review B3) Java, C#, Kotlin and Swift declarations. // The per-language receiver-evidence capture (a hook per language family; nullptr = none). Python is handled inline above. using ReceiverEvidenceHook = void ( * )( BindCtx&, TSNode, const char*, std::uint32_t ); inline ReceiverEvidenceHook receiverEvidenceHookOf( Lang l ) noexcept { switch( l ) { case Lang::JavaScript: case Lang::TypeScript: return &captureJsEvidence; case Lang::Java: return &captureJavaEvidence; case Lang::CSharp: return &captureCSharpEvidence; case Lang::Kotlin: return &captureKotlinEvidence; case Lang::Swift: return &captureSwiftEvidence; case Lang::Go: return &captureGoEvidence; case Lang::Rust: return &captureRustEvidence; default: return nullptr; } } // PY-CTOR-ANNOT: the class a `self.f = v` / `self.f: A = v` field holds. A CONSTRUCTOR value (`X( … )`, `mod.X( … )`, X capitalized // as Python classes are: `make()` / `helpers.build()` name no class) beats the annotation, as it does for a local — `self.s: Strategy // = FastStrategy()` holds a FastStrategy, whatever the stub above it says; ReceiverEvidence::build then types the field only when // that name is an in-tree class (an outside or unresolved one leaves it untyped: a hedge, never the annotation). Otherwise the // annotation (`T`, `T | None`, `Optional[ T ]`; `= None` leaves it typed), else the value as read. `whole`: a qualified spelling's // whole text, for build()'s written-qualifier check. inline std::string_view pySelfFieldType( const BindCtx& cx, TSNode ann, TSNode rhs, std::uint32_t at, std::string_view& whole ) { whole = {}; std::string_view value = constructedBy( rhs, cx.lang, cx.src ); if( value.empty() ) { value = pyQualifiedConstructed( rhs, cx.src, whole ); } const std::string_view valueWhole = whole; if( ts_node_is_null( ann ) || ( !value.empty() && value.front() >= 'A' && value.front() <= 'Z' ) ) { return value; } whole = {}; const std::string_view written = writtenClass( cx, pyOptionalInner( ann, cx.src ), at, &whole ); if( !written.empty() ) { return written; } whole = valueWhole; return value; } inline void captureReceiverEvidence( BindCtx& cx, TSNode n, const char* t ) { const Lang lang = cx.lang; const std::string_view src = cx.src; const std::uint32_t at = ts_node_start_byte( n ); if( lang == Lang::Python ) { if( kindIs( t, "typed_parameter" ) || kindIs( t, "typed_default_parameter" ) ) { TSNode name = fieldChild( n, NodeField::Name ); if( ts_node_is_null( name ) && ts_node_named_child_count( n ) > 0 ) { name = ts_node_named_child( n, 0 ); // typed_parameter spells its identifier without a field } if( !ts_node_is_null( name ) && kindIs( ts_node_type( name ), "identifier" ) ) { std::string_view whole; // `s: mod.T`: the qualifier ReceiverEvidence::build checks (h1-annot) const std::string_view cls = writtenClass( cx, pyOptionalInner( fieldChild( n, NodeField::Type ), src ), at, &whole ); // `s: T | None` pushLocalEvidence( cx, nodeTextOf( name, src ), cls, BindSite{ at, 0u, 0u }, false, whole ); } } else if( kindIs( t, "type_parameter" ) ) { noteTypeParameters( cx, n ); // `def f[T]( … )` / `class C[T]:` } else if( kindIs( t, "import_from_statement" ) ) { ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { if( kindIs( ts_node_type( c ), "aliased_import" ) ) { const TSNode name = fieldChild( c, NodeField::Name ); const TSNode alias = fieldChild( c, NodeField::Alias ); if( !ts_node_is_null( name ) && !ts_node_is_null( alias ) ) { pushEvidenceBind( cx, LocalBindKind::NameAlias, nodeTextOf( alias, src ), finalSegment( nodeTextOf( name, src ) ), {}, ts_node_start_byte( c ), false ); } } return true; } ); } else if( kindIs( t, "assignment" ) ) { const TSNode lhs = fieldChild( n, NodeField::Left ); const TSNode rhs = fieldChild( n, NodeField::Right ); if( ts_node_is_null( lhs ) ) { return; } const char* lt = ts_node_type( lhs ); if( kindIs( lt, "identifier" ) && !ts_node_is_null( fieldChild( n, NodeField::Type ) ) ) { std::string_view whole; // `x: mod.T = …`: as a typed parameter's const std::string_view cls = writtenClass( cx, pyOptionalInner( fieldChild( n, NodeField::Type ), src ), at, &whole ); pushLocalEvidence( cx, nodeTextOf( lhs, src ), cls, BindSite{ at, 0u, 0u }, false, whole ); } else if( std::string_view whole; kindIs( lt, "identifier" ) && !pyQualifiedConstructed( rhs, src, whole ).empty() ) { // PY-CTOR-ANNOT: `x = mod.X( … )` — X when the qualifier is an in-tree module (build()'s written-qualifier check), // else the tombstone it replaces (bindCapture's `x = f()` record steps aside for this shape: pyAssignedType) pushLocalEvidence( cx, nodeTextOf( lhs, src ), pyQualifiedConstructed( rhs, src, whole ), BindSite{ at, 0u, 0u }, false, whole ); } else if( kindIs( lt, "identifier" ) && !ts_node_is_null( rhs ) && kindIs( ts_node_type( rhs ), "attribute" ) ) { const TSNode obj = fieldChild( rhs, NodeField::Object ); const TSNode attr = fieldChild( rhs, NodeField::Attribute ); if( !ts_node_is_null( obj ) && !ts_node_is_null( attr ) && kindIs( ts_node_type( obj ), "identifier" ) ) { pushEvidenceBind( cx, LocalBindKind::MethodAlias, nodeTextOf( lhs, src ), nodeTextOf( obj, src ), nodeTextOf( attr, src ), at, false ); } } else if( kindIs( lt, "attribute" ) ) { const TSNode obj = fieldChild( lhs, NodeField::Object ); const TSNode attr = fieldChild( lhs, NodeField::Attribute ); if( !ts_node_is_null( obj ) && !ts_node_is_null( attr ) && kindIs( ts_node_type( obj ), "identifier" ) && nodeTextOf( obj, src ) == "self" ) { std::string_view whole; const std::string_view type = pySelfFieldType( cx, fieldChild( n, NodeField::Type ), rhs, at, whole ); pushEvidenceBind( cx, LocalBindKind::MemberType, nodeTextOf( attr, src ), type, type.empty() ? std::string_view{} : whole, at, false ); } } } capturePythonInPlaceFacts( cx, n, t, at ); capturePythonBindingTombstones( cx, n, t, at ); return; } if( const ReceiverEvidenceHook hook = receiverEvidenceHookOf( lang ); hook != nullptr ) { hook( cx, n, t, at ); } } // lane L6 fix round 1 (reports/dispatch-classes.md §(a) 4 and 5): the blocks that implement a type, as ImplBlock records over the // node (model.h LocalBindKind::ImplBlock). The implemented name is the written type's final segment, generic arguments dropped. // An implementor the index keeps no class for records no implementor name; graph.h then opens the type's implementors list. // A trait written through a path (`impl other_crate::Renderer for X`) keeps the whole path, generics dropped, in importedName: the // reader asks whether that path leaves the tree (ifacedispatch.h openTraitAnswer). inline void pushImplBlock( BindCtx& cx, TSNode n, std::string_view written, std::string_view implementor ) { std::string_view name = written.substr( 0, std::min( written.find( '<' ), written.size() ) ); while( !name.empty() && ( name.back() == ' ' || name.back() == '\n' || name.back() == '\t' || name.back() == '\r' || name.back() == '?' ) ) { name.remove_suffix( 1 ); } const std::string_view path = name; const std::size_t cut = name.find_last_of( ".:" ); name = name.substr( cut == std::string_view::npos ? 0 : cut + 1 ); pushTypedBind( cx.fileId, cx.lang, name, DeclType{ std::string( implementor ), name.size() < path.size() ? std::string( path ) : std::string{} }, BindSite{ ts_node_start_byte( n ), ts_node_start_byte( n ), ts_node_end_byte( n ) }, LocalBindKind::ImplBlock, *cx.binds ); } // Rust (langfacts.h traitImplBlocks): `impl T for X { … }` — its methods are X's methods for T, never inherent ones; the names are // the ones ingest_relations.h rustImplVisitNode gives the inherit reference. A trait under a mock attribute: the mock it writes. inline void rustImplBlockOf( BindCtx& cx, TSNode n, const char* t ) { if( kindIs( t, "trait_item" ) ) { const TSNode name = fieldChild( n, NodeField::Name ); if( !ts_node_is_null( name ) && rustItemCarriesMockAttr( n, cx.src ) ) { pushImplBlock( cx, n, nodeTextOf( name, cx.src ), {} ); } return; } const TSNode trait = kindIs( t, "impl_item" ) ? fieldChild( n, NodeField::Trait ) : TSNode{}; const TSNode type = ts_node_is_null( trait ) ? TSNode{} : fieldChild( n, NodeField::Type ); if( !ts_node_is_null( type ) ) { pushImplBlock( cx, n, nodeTextOf( trait, cx.src ), finalSegment( nodeTextOf( type, cx.src ) ) ); } } // Java / Kotlin (langfacts.h anonymousImplementors): an implementor with no class — a Java anonymous class `new T( … ) { … }`, a // Kotlin object expression `object : A, B( x ) { … }` (each delegation specifier's user type), a Kotlin `fun interface T` (a lambda // implements it). inline void anonymousImplementorOf( BindCtx& cx, TSNode n, const char* t ) { const bool javaAnon = kindIs( t, "object_creation_expression" ); const bool ktObject = kindIs( t, "object_literal" ); const bool ktFunType = kindIs( t, "class_declaration" ); if( !javaAnon && !ktObject && !ktFunType ) { return; } bool hit = false; // Java: a class body; Kotlin `fun interface`: the `fun` keyword TSNode named{}; // Kotlin `fun interface`: its name ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* ct = ts_node_type( c ); hit = hit || ( javaAnon && kindIs( ct, "class_body" ) ) || ( ktFunType && !ts_node_is_named( c ) && kindIs( ct, "fun" ) ); named = ktFunType && ts_node_is_null( named ) && kindIs( ct, "type_identifier" ) ? c : named; if( ktObject && kindIs( ct, "delegation_specifier" ) && ts_node_named_child_count( c ) > 0 ) { const TSNode spec = ts_node_named_child( c, 0 ); const TSNode user = kindIs( ts_node_type( spec ), "constructor_invocation" ) && ts_node_named_child_count( spec ) > 0 ? ts_node_named_child( spec, 0 ) : spec; if( kindIs( ts_node_type( user ), "user_type" ) ) { pushImplBlock( cx, n, nodeTextOf( user, cx.src ), {} ); } } return !kindIs( ct, "class_body" ) || javaAnon; } ); const TSNode type = javaAnon ? fieldChild( n, NodeField::Type ) : named; if( hit && !ts_node_is_null( type ) ) { pushImplBlock( cx, n, nodeTextOf( type, cx.src ), {} ); } } void bindsVisitNode( BindCtx& cx, TSNode n, const char* t ) { // The body below is the pass's own node step, unchanged; these aliases keep it reading against the // same names it always had rather than sprinkling `cx.` through 150 lines of grammar branches. FUSEPROBE_BUMP( kBinds ); const std::uint32_t fileId = cx.fileId; const Lang lang = cx.lang; const std::string_view src = cx.src; std::vector& binds = *cx.binds; std::vector& fnPos = cx.fnPos; std::vector& fnUnk = cx.fnUnk; FnBindGateState& fnGate = cx.fnGate; const bool cFamilyFn = cx.cFamilyFn; if( langFacts( lang ).anonymousImplementors ) { anonymousImplementorOf( cx, n, t ); // lane L6 fix round 1: Java / Kotlin implementors with no class } else if( langFacts( lang ).traitImplBlocks ) { rustImplBlockOf( cx, n, t ); // lane L6 fix round 1: Rust trait impl blocks and mocked traits } // C++/ObjC: `Foo x;` · `Foo* x;` · `Foo x = Foo();` · `auto x = Foo();` if( ( lang == Lang::Cpp || lang == Lang::ObjC ) && kindIs( t, "declaration" ) ) { const DeclType written = declTypeOfName( fieldChild( n, NodeField::Type ), src ); // A5 fix round: the declared names shadow within their enclosing block (or, for a control-statement // header declaration, that whole statement) — one parent walk per declaration node, shared by every // declarator child below; each declarator then contributes its own declaration POINT as the span's // start (shadowSpanStart). const ShadowScope scope = enclosingShadowScope( n ); // a `declaration` can declare several variables (`Foo a, b;`) → one binding per declarator child. // O(children), not O(children³): this loop used to ask for `ts_node_child( n, i )` AND // `ts_node_field_name_for_child( n, i )` twice, and all three restart tree-sitter's child iterator at // the first child (src/infra/tschildren.h). A declaration's width is input-set — every comment between // the type and the declarator is a direct child — and 16 000 of them measured 77x the identical flood // beside the declaration (test/childwalkscalecheck.sh, arm B7, which also records why the cursor's // O(1) `ts_tree_cursor_current_field_name` is the same answer: node.c:689 vs tree_cursor.c:657). ChildCursor cursor( n ); forEachChild( n, cursor.cur, [ & ]( TSNode c ) { const char* field = ts_tree_cursor_current_field_name( &cursor.cur ); if( field == nullptr || !kindIs( field, "declarator" ) ) { return true; } const char* ct = ts_node_type( c ); // `init_declarator`: name lives in its `declarator`, the RHS in its `value` (for auto inference). // emitDeclBinds also records the r9 VarDecl shadow fact for the declared NAME regardless of type // resolvability (`int run = 0;` binds no type — writtenTypeOf refuses primitives — yet the local // exists and shadows). if( kindIs( ct, "init_declarator" ) ) { const TSNode declarator = fieldChild( c, NodeField::Declarator ); const BindSite declSite{ ts_node_start_byte( n ), shadowSpanStart( scope, declarator ), scope.end }; emitDeclBinds( fileId, lang, declarator, src, declaredTypeOf( written, fieldChild( c, NodeField::Value ), src ), declSite, binds ); // (L) `auto x = f()` / `const auto& x = f()`: the callee's declared result types x (graph.h's resolve loop) const TSNode type = fieldChild( n, NodeField::Type ); const TSNode value = fieldChild( c, NodeField::Value ); if( !ts_node_is_null( type ) && kindIs( ts_node_type( type ), "placeholder_type_specifier" ) && !ts_node_is_null( value ) && kindIs( ts_node_type( value ), "call_expression" ) ) { pushCallResult( cx, cppDeclaredName( declarator ), value, 0, 0, declSite ); } } else // plain declarator (identifier / pointer_declarator / reference_declarator), no initializer { emitDeclBinds( fileId, lang, c, src, written, BindSite{ ts_node_start_byte( n ), shadowSpanStart( scope, c ), scope.end }, binds ); } return true; } ); } // (L) fix round 2: a namespace body, a C++ using-directive / using-declaration — the scope a bare result type resolves in // (fix round 3) an alias, (fix round 4) a declaration the index keeps no class for, or a region it cannot read if( const ScopeFactNode* row = scopeFactRow( lang, t ) ) { switch( row->role ) { case ScopeFactRole::Scope: captureScopeFact( cx, n, t ); break; case ScopeFactRole::Alias: captureTypeAlias( cx, n, t ); break; case ScopeFactRole::Shadow: captureShadowDecl( cx, n, t ); break; case ScopeFactRole::Unparsed: captureCppUnparsed( cx, n, t ); break; case ScopeFactRole::UnparsedBlocks: captureUnparsedBlocks( cx, n ); break; } } // (L) a C++ function's declared result (a definition, or a declaration such as a header's prototype) if( callResultFamilyOf( lang ) == CallResultFamily::Cpp && ( kindIs( t, "function_definition" ) || kindIs( t, "declaration" ) || kindIs( t, "field_declaration" ) ) ) { captureCppReturnType( cx, n ); } // C++ `x = Foo();` (re-assignment to a constructor) — assignment_expression inside an expression_statement. The // record carries the constructor's qualified text like a declaration's does (`x = std::map()`, kParserVer 98), // and is marked an assignment's: its callee may be a function (`x = makeFoo()`), kParserVer 104. else if( ( lang == Lang::Cpp || lang == Lang::ObjC ) && kindIs( t, "assignment_expression" ) ) { const TSNode lhs = fieldChild( n, NodeField::Left ); const TSNode rhs = fieldChild( n, NodeField::Right ); if( !ts_node_is_null( lhs ) && kindIs( ts_node_type( lhs ), "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( lhs ), b = ts_node_end_byte( lhs ); if( a <= b && b <= src.size() ) { pushTypedBind( fileId, lang, src.substr( a, b - a ), assignedTypeOf( rhs, src ), BindSite{ ts_node_start_byte( n ), 0u, 0u }, LocalBindKind::Type, binds ); } } } // Python `x = Foo()` — assignment with a bare-identifier LHS and a constructor-call RHS. else if( lang == Lang::Python && kindIs( t, "assignment" ) ) { const TSNode lhs = fieldChild( n, NodeField::Left ); const TSNode rhs = fieldChild( n, NodeField::Right ); if( !ts_node_is_null( lhs ) && kindIs( ts_node_type( lhs ), "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( lhs ), b = ts_node_end_byte( lhs ); if( a <= b && b <= src.size() ) { // Python RHS constructor is a `call` node (not `call_expression`); reuse finalSegment on its callee. // PY-CTOR-ANNOT: `x = mod.X( … )` records no Type here — captureReceiverEvidence records it with its qualifier std::string_view whole; const bool qualifiedCtor = !pyQualifiedConstructed( rhs, src, whole ).empty(); std::string type; if( !ts_node_is_null( rhs ) && kindIs( ts_node_type( rhs ), "call" ) ) { const TSNode fn = fieldChild( rhs, NodeField::Function ); if( !ts_node_is_null( fn ) && kindIs( ts_node_type( fn ), "identifier" ) ) { const std::uint32_t fa = ts_node_start_byte( fn ), fb = ts_node_end_byte( fn ); if( fa <= fb && fb <= src.size() ) { type = finalSegment( src.substr( fa, fb - fa ) ); } } } if( !qualifiedCtor ) { emitBind( fileId, lang, src.substr( a, b - a ), std::move( type ), ts_node_start_byte( n ), binds ); } } } } // Java declarations are resolver veto evidence for `Identifier::method`. Tree-sitter cannot // distinguish a type receiver from a value receiver, so a parameter/local/field with the same // spelling must prevent the class-name proof — but only where that binding is in scope. else if( lang == Lang::Java ) { captureJavaShadowDecls( n, t, fileId, lang, src, binds ); } // TypeScript `const x = new Foo();` · `let y: Bar = ...;` — variable_declarator. else if( lang == Lang::TypeScript && kindIs( t, "variable_declarator" ) ) { const TSNode nameNode = fieldChild( n, NodeField::Name ); if( !ts_node_is_null( nameNode ) && kindIs( ts_node_type( nameNode ), "identifier" ) ) { const std::uint32_t a = ts_node_start_byte( nameNode ), b = ts_node_end_byte( nameNode ); if( a <= b && b <= src.size() ) { // prefer the `: Type` annotation; else infer from a `new Foo()` / `Foo()` initializer. std::string type; const TSNode ann = fieldChild( n, NodeField::Type ); // type_annotation if( !ts_node_is_null( ann ) ) { // O(children): the scan stops at the first type_identifier, but nothing bounds how many // comments precede one — `let x: /* … */ Foo` splices each into the annotation's child list. ChildCursor annCursor( ann ); forEachChild( ann, annCursor.cur, [ & ]( TSNode c ) { if( !kindIs( ts_node_type( c ), "type_identifier" ) ) { return true; } const std::uint32_t ta = ts_node_start_byte( c ), tb = ts_node_end_byte( c ); if( ta <= tb && tb <= src.size() ) { type = finalSegment( src.substr( ta, tb - ta ) ); } return false; } ); // the indexed loop's `break`: the FIRST type_identifier wins } if( type.empty() ) { type = ctorTypeOf( fieldChild( n, NodeField::Value ), src ); } emitBind( fileId, lang, src.substr( a, b - a ), std::move( type ), ts_node_start_byte( n ), binds ); } } } // r9 shadow suppression: the local-declaring shapes OUTSIDE `declaration` nodes — a function // DEFINITION's named parameters and a range-for's loop variable. Unconditional (the helper gates // language and node type itself); disjoint from every branch of the Rule-2 chain above. captureShadowScopeDecls( n, t, fileId, lang, src, binds ); captureReceiverEvidence( cx, n, t ); // FE-B: typed parameters/locals, field types, method aliases (Python, JS/TS, Go, Java, C#, Kotlin, Swift) // ── L3 fn-pointer/callback capture (C/C++/ObjC) — a SEPARATE if (not part of the Rule-2 chain above): // the same `declaration` node can carry BOTH a Rule-2 var→type fact and a var→function fact // (`H fnPtr = beta;` emits fnPtr:H for receiver narrowing AND fnPtr→beta for call resolution). ── if( cFamilyFn && kindIs( t, "declaration" ) ) { captureFnBindDecl( n, fileId, lang, src, fnPos, fnUnk, fnGate.pendingDecl ); } else if( cFamilyFn && kindIs( t, "assignment_expression" ) ) { captureFnBindAssign( n, fileId, lang, src, fnPos, fnUnk, fnGate.pending ); } else if( cFamilyFn && kindIs( t, "pointer_expression" ) ) { captureFnBindEscape( n, src, fnUnk ); // A5: `&fn` anywhere clobbers the variable (escape guard) } collectFnBindGateEvidence( n, t, src, cFamilyFn, fnGate ); // never an `else if` — see the helper's note } // End-of-file step for the bindings pass: the two noise gates and the L3 clobber sweep. Split out of the // walk (it was the tail of captureBindings) so the shared stream can run it once the last node is visited. void bindsFinalize( BindCtx& cx ) { const std::uint32_t fileId = cx.fileId; const Lang lang = cx.lang; std::vector& binds = *cx.binds; std::vector& fnPos = cx.fnPos; std::vector& fnUnk = cx.fnUnk; FnBindGateState& fnGate = cx.fnGate; resolvePendingFnBindDecls ( fileId, lang, fnGate, fnPos ); // both noise gates — BEFORE the sweep, which needs resolvePendingFnBindAssigns( fileId, lang, fnGate, fnPos ); // fnPos final (its var scan is a membership test, // so the deferral cannot change a clobber verdict) // ── L3 clobber sweep + merge. A clobbering assignment forces the tombstone (kFnBindClobberTarget) so a // stale earlier binding can never win (`void (*fn)() = α fn = getHandler(); fn();` → NO edge) — // but only for a var that HAS a recognizable fn binding somewhere in this file, an over-approximation // of "same scope" that errs toward the tombstone, never toward a resolve. posCount is captured BEFORE // the emits below so the sweep scans only the walk's own positives. if( !fnPos.empty() ) { const std::size_t posCount = fnPos.size(); for( const FnBindClobber& u : fnUnk ) { bool hasPos = false; for( std::size_t p = 0; p < posCount; ++p ) { if( fnPos[p].var == u.var ) { hasPos = true; break; } } if( hasPos ) { emitFnBind( fileId, lang, u.var, std::string( kFnBindClobberTarget ), u.startByte, LocalBindKind::FnAssign, binds ); } } for( RawBind& p : fnPos ) { binds.push_back( p ); } } } } // namespace — ingest_binds.h section of ingest.cpp } // namespace rw