rustlang.rs 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. //! Rust-language extraction — a faithful port of `TreeSitterExtractor`'s rust
  2. //! paths (src/extraction/tree-sitter.ts) plus languages/rust.ts. ("rustlang"
  3. //! because `rust` alone collides with the kernel's own implementation
  4. //! language.) Survey artifact: docs/design/rust-lang-kernel-port-checklist.md.
  5. //!
  6. //! Rust's shape quirks, mirrored exactly (bug-for-bug, all verified against
  7. //! the TS reference):
  8. //! - `isAsync` is dead code upstream: it scans DIRECT children for an `async`
  9. //! token, but the grammar nests it inside `function_modifiers` — every rust
  10. //! fn/method carries isAsync **false** (present-false, never absent).
  11. //! - impl blocks push NO scope: members re-dispatch at file scope, so an impl
  12. //! associated `const` becomes a FILE-level `variable`, and the method↔owner
  13. //! `contains` edge is a source-order name scan (an impl ABOVE its struct
  14. //! gets no edge). The receiver (method QN prefix, `contains` owner,
  15. //! `implements` source) is the impl_item's `type` field via
  16. //! impl_type_name — both sides moved to the grammar's trait:/type: fields
  17. //! together in #1588 (the earlier positional scan qualified every
  18. //! parameterized impl's methods by the TRAIT).
  19. //! - `const_item`/`static_item` ride the generic extractVariable fallback:
  20. //! kind is always `variable`, no signature, and EVERY direct `identifier`
  21. //! child mints a node (`const MAX: u32 = OTHER;` → two nodes, `MAX` + the
  22. //! phantom `OTHER`). Top-level initializer values are never body-walked.
  23. //! - Unit structs (`struct Unit;`, no body field) mint NO node; `mod_item`
  24. //! mints no module node and adds no QN prefix.
  25. //! - Chained-call re-encode is scoped_identifier-gated (`Foo::new().bar()` →
  26. //! `Foo::new().bar`); a call through a field of the enclosing type keeps
  27. //! the owner-field shape (`self.inner.run()` → `self.inner.run`, #1585);
  28. //! instance chains, parens, `.await`, deeper/non-self field chains, and
  29. //! bare `self` receivers all collapse to the bare method name (`self` is
  30. //! node kind `self`, not `identifier`, so it dodges SKIP_RECEIVERS by
  31. //! falling through). Turbofish callees keep the raw `helper::<T>` text.
  32. //! - `use` emits an import node named by the ROOT module (`crate`/`self`/…),
  33. //! one root `imports` ref, then one FULL-path `imports` ref per binding;
  34. //! `use x::*` (use_wildcard) emits nothing at all.
  35. //! - Trait supertraits come only from `trait_bounds`; a scoped supertrait
  36. //! (`fmt::Debug`) matches no case and is silently dropped.
  37. //! - Rocket `routes!`/`catchers!` are extracted ONLY inside function bodies,
  38. //! and only when the macro name is a bare identifier.
  39. //! - A rust type alias emits NO ref to its aliased type (the shared code
  40. //! reads a `value` field; rust's field is `type`).
  41. //! - An `attribute_item` between a doc comment and its item breaks the
  42. //! docstring sibling chain (`#[derive(..)]` kills the docstring).
  43. //! Files with parse errors defer to wasm.
  44. use crate::buffers::{
  45. build_meta, edge_kind_index, node_kind_index, Arena, BoolFlags, EdgeRow, EmitOut, NodeRow,
  46. RefRow, StrRef, Tables, FLAG_IS_ASYNC, FLAG_IS_EXPORTED, FUNCTION_REF_CODE, NONE, NONE_STR,
  47. };
  48. use crate::docstring::preceding_docstring;
  49. use crate::ids;
  50. use crate::textutil as util;
  51. use regex::Regex;
  52. use std::collections::{HashMap, HashSet};
  53. use std::sync::OnceLock;
  54. use tree_sitter::{Node, Parser};
  55. const MAX_VALUE_REF_NODES: usize = 20_000;
  56. /// JS `/<[^>]*>/g` — the non-nested generic strip (breaks on nested generics
  57. /// by design: `Result<Vec<Foo>, E>` → `Result, E>` → returnType undefined).
  58. fn generic_angle_re() -> &'static Regex {
  59. static RE: OnceLock<Regex> = OnceLock::new();
  60. RE.get_or_init(|| Regex::new(r"<[^>]*>").unwrap())
  61. }
  62. /// JS `/^[A-Za-z_]\w*$/` (ASCII \w — the regex crate's \w is Unicode).
  63. fn simple_ident_re() -> &'static Regex {
  64. static RE: OnceLock<Regex> = OnceLock::new();
  65. RE.get_or_init(|| Regex::new(r"^[A-Za-z_][0-9A-Za-z_]*$").unwrap())
  66. }
  67. struct Scope {
  68. row: u32,
  69. kind: &'static str,
  70. name: String,
  71. }
  72. #[derive(Default)]
  73. struct Extra {
  74. docstring: Option<String>,
  75. signature: Option<String>,
  76. return_type: Option<String>,
  77. qualified_name: Option<String>,
  78. visibility: Option<u8>,
  79. is_exported: Option<bool>,
  80. is_async: Option<bool>,
  81. }
  82. struct ValueScope<'t> {
  83. row: u32,
  84. node: Node<'t>,
  85. name: String,
  86. }
  87. struct Cand {
  88. from: u32,
  89. name: String,
  90. line: u32,
  91. column_byte: usize,
  92. row: usize,
  93. }
  94. /// Per-node metadata for the receiver-method owner lookup and
  95. /// findNodeByName (mirrors the TS scans over `this.nodes` — FIRST match
  96. /// wins, earlier-in-file only).
  97. struct NodeMeta {
  98. kind: &'static str,
  99. name: String,
  100. }
  101. pub struct Walker<'t> {
  102. src: &'t str,
  103. file_path: &'t str,
  104. line_starts: Vec<usize>,
  105. arena: Arena,
  106. tables: Tables,
  107. stack: Vec<Scope>,
  108. nodes_meta: Vec<NodeMeta>,
  109. node_ids: Vec<String>,
  110. defined_fn_names: HashSet<String>,
  111. imported_names: HashSet<String>,
  112. fn_ref_cands: Vec<Cand>,
  113. fs_values: HashMap<String, u32>,
  114. fs_value_counts: HashMap<String, u32>,
  115. value_scopes: Vec<ValueScope<'t>>,
  116. }
  117. pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
  118. let grammar = crate::langs::grammar_for("rust").ok_or("no rust grammar")?;
  119. let t0 = std::time::Instant::now();
  120. let mut parser = Parser::new();
  121. parser
  122. .set_language(&grammar)
  123. .map_err(|e| format!("set_language(rust) failed: {e}"))?;
  124. let tree = parser
  125. .parse(source, None)
  126. .ok_or_else(|| "parser returned null tree".to_string())?;
  127. if tree.root_node().has_error() {
  128. return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
  129. }
  130. let mut w = Walker {
  131. src: source,
  132. file_path,
  133. line_starts: util::line_starts(source),
  134. arena: Arena::default(),
  135. tables: Tables::default(),
  136. stack: Vec::new(),
  137. nodes_meta: Vec::new(),
  138. node_ids: Vec::new(),
  139. defined_fn_names: HashSet::new(),
  140. imported_names: HashSet::new(),
  141. fn_ref_cands: Vec::new(),
  142. fs_values: HashMap::new(),
  143. fs_value_counts: HashMap::new(),
  144. value_scopes: Vec::new(),
  145. };
  146. let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
  147. let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
  148. let mut flags = BoolFlags::default();
  149. flags.set(FLAG_IS_EXPORTED, false);
  150. let file_id = w.arena.put(&ids::file_node_id(file_path));
  151. let name_ref = w.arena.put(base_name);
  152. let qn_ref = w.arena.put(file_path);
  153. w.tables.push_node(&NodeRow {
  154. kind: node_kind_index("file").unwrap(),
  155. visibility: 0,
  156. flags,
  157. start_line: 1,
  158. end_line: line_count,
  159. start_column: 0,
  160. end_column: 0,
  161. name: name_ref,
  162. qualified_name: qn_ref,
  163. id: file_id,
  164. docstring: NONE_STR,
  165. signature: NONE_STR,
  166. decorators: NONE_STR,
  167. type_parameters: NONE_STR,
  168. return_type: NONE_STR,
  169. extra_json: NONE_STR,
  170. });
  171. w.nodes_meta.push(NodeMeta { kind: "file", name: base_name.to_string() });
  172. w.node_ids.push(ids::file_node_id(file_path));
  173. w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
  174. w.visit_node(tree.root_node());
  175. w.flush_fn_ref_candidates();
  176. w.flush_value_refs(tree.root_node());
  177. w.stack.pop();
  178. let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
  179. let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);
  180. Ok(EmitOut {
  181. meta,
  182. nodes: w.tables.nodes,
  183. edges: w.tables.edges,
  184. refs: w.tables.refs,
  185. arena: w.arena.into_vec(),
  186. })
  187. }
  188. impl<'t> Walker<'t> {
  189. fn text(&self, node: Node) -> &'t str {
  190. &self.src[node.byte_range()]
  191. }
  192. fn line_of(&self, node: Node) -> u32 {
  193. node.start_position().row as u32 + 1
  194. }
  195. fn col_of(&self, node: Node) -> u32 {
  196. util::col16(self.src, &self.line_starts, node.start_position().row, node.start_byte())
  197. }
  198. fn end_col_of(&self, node: Node) -> u32 {
  199. util::col16(self.src, &self.line_starts, node.end_position().row, node.end_byte())
  200. }
  201. fn top_row(&self) -> u32 {
  202. self.stack.last().map(|s| s.row).unwrap_or(0)
  203. }
  204. /// isInsideClassLikeNode — stack TOP only, file doesn't count.
  205. fn inside_class_like(&self) -> bool {
  206. self.stack
  207. .last()
  208. .map(|s| matches!(s.kind, "class" | "struct" | "union" | "interface" | "trait" | "enum" | "module"))
  209. .unwrap_or(false)
  210. }
  211. fn push_ref_at(&mut self, from_row: u32, name: &str, kind_code: u8, node: Node) {
  212. let name_ref = self.arena.put(name);
  213. self.tables.push_ref(&RefRow {
  214. from_idx: from_row,
  215. kind: kind_code,
  216. line: self.line_of(node),
  217. column: self.col_of(node),
  218. reference_name: name_ref,
  219. candidates: NONE_STR,
  220. from_id_str: NONE_STR,
  221. });
  222. if kind_code == edge_kind_index("imports").unwrap() {
  223. if util::simple_name().is_match(name) {
  224. self.imported_names.insert(name.to_string());
  225. } else if let Some(c) = util::qualified_import().captures(name) {
  226. // `::`-separated rust paths match NEITHER regex (separators are
  227. // `.`/`\`), so multi-segment use-imports contribute nothing to
  228. // the fn-ref gate — the rust gate is effectively same-file-only.
  229. self.imported_names.insert(c[1].to_string());
  230. }
  231. }
  232. }
  233. fn create_node(&mut self, kind: &'static str, name: &str, node: Node<'t>, extra: Extra) -> Option<u32> {
  234. if name.is_empty() {
  235. return None;
  236. }
  237. let start_line = self.line_of(node);
  238. let id = ids::node_id(self.file_path, kind, name, start_line);
  239. let end_line = node.end_position().row as u32 + 1;
  240. let qualified = extra.qualified_name.unwrap_or_else(|| {
  241. let mut parts: Vec<&str> = Vec::new();
  242. for s in &self.stack {
  243. if s.kind != "file" {
  244. parts.push(&s.name);
  245. }
  246. }
  247. let mut qn = parts.join("::");
  248. if !qn.is_empty() {
  249. qn.push_str("::");
  250. }
  251. qn.push_str(name);
  252. qn
  253. });
  254. let mut flags = BoolFlags::default();
  255. if let Some(v) = extra.is_exported {
  256. flags.set(FLAG_IS_EXPORTED, v);
  257. }
  258. if let Some(v) = extra.is_async {
  259. flags.set(FLAG_IS_ASYNC, v);
  260. }
  261. let name_ref = self.arena.put(name);
  262. let qn_ref = self.arena.put(&qualified);
  263. let id_ref = self.arena.put(&id);
  264. let doc_ref = opt_str(&mut self.arena, extra.docstring.as_deref());
  265. let sig_ref = opt_str(&mut self.arena, extra.signature.as_deref());
  266. let ret_ref = opt_str(&mut self.arena, extra.return_type.as_deref());
  267. let row = self.tables.push_node(&NodeRow {
  268. kind: node_kind_index(kind).unwrap(),
  269. visibility: extra.visibility.unwrap_or(0),
  270. flags,
  271. start_line,
  272. end_line,
  273. start_column: self.col_of(node),
  274. end_column: self.end_col_of(node),
  275. name: name_ref,
  276. qualified_name: qn_ref,
  277. id: id_ref,
  278. docstring: doc_ref,
  279. signature: sig_ref,
  280. decorators: NONE_STR,
  281. type_parameters: NONE_STR,
  282. return_type: ret_ref,
  283. extra_json: NONE_STR,
  284. });
  285. self.nodes_meta.push(NodeMeta { kind, name: name.to_string() });
  286. self.node_ids.push(id);
  287. let parent_row = self.top_row();
  288. self.tables.push_edge(&EdgeRow {
  289. source_idx: parent_row,
  290. target_idx: row,
  291. kind: edge_kind_index("contains").unwrap(),
  292. provenance: 0,
  293. line: NONE,
  294. column: NONE,
  295. metadata_json: NONE_STR,
  296. source_id_str: NONE_STR,
  297. target_id_str: NONE_STR,
  298. });
  299. if kind == "function" || kind == "method" {
  300. self.defined_fn_names.insert(name.to_string());
  301. }
  302. // captureValueRefScope: rust consts are kind `variable` — still targets.
  303. let target_kind_ok = kind == "constant" || kind == "variable";
  304. if target_kind_ok
  305. && util::utf16_len(name) >= 3
  306. && util::has_upper_or_underscore().is_match(name)
  307. {
  308. let parent_ok = self
  309. .stack
  310. .last()
  311. .map(|s| matches!(s.kind, "file" | "class" | "module" | "struct" | "union" | "enum"))
  312. .unwrap_or(false);
  313. if parent_ok {
  314. self.fs_values.insert(name.to_string(), row);
  315. *self.fs_value_counts.entry(name.to_string()).or_insert(0) += 1;
  316. }
  317. }
  318. if matches!(kind, "function" | "method" | "constant" | "variable") {
  319. self.value_scopes.push(ValueScope { row, node, name: name.to_string() });
  320. }
  321. Some(row)
  322. }
  323. /// extractName — nameField `name`, else the identifier-like child scan.
  324. fn extract_name(&self, node: Node) -> String {
  325. if let Some(name_node) = node.child_by_field_name("name") {
  326. return self.text(name_node).to_string();
  327. }
  328. for i in 0..node.named_child_count() {
  329. if let Some(c) = node.named_child(i) {
  330. if matches!(c.kind(), "identifier" | "type_identifier" | "simple_identifier" | "constant") {
  331. return self.text(c).to_string();
  332. }
  333. }
  334. }
  335. "<anonymous>".to_string()
  336. }
  337. /// rustExtractor.getSignature: raw params text + ` -> ` + raw return type.
  338. fn signature_of(&self, node: Node) -> Option<String> {
  339. let params = node.child_by_field_name("parameters")?;
  340. let mut sig = self.text(params).to_string();
  341. if let Some(rt) = node.child_by_field_name("return_type") {
  342. sig.push_str(" -> ");
  343. sig.push_str(self.text(rt));
  344. }
  345. Some(sig)
  346. }
  347. /// rustExtractor.getVisibility: direct `visibility_modifier` child whose
  348. /// text contains `pub` → public, else private; none → private.
  349. fn visibility_of(&self, node: Node) -> u8 {
  350. for i in 0..node.child_count() {
  351. if let Some(c) = node.child(i) {
  352. if c.kind() == "visibility_modifier" {
  353. return if self.text(c).contains("pub") { 1 } else { 2 };
  354. }
  355. }
  356. }
  357. 2 // private — Rust defaults to private
  358. }
  359. /// extractRustReturnType (languages/rust.ts:14).
  360. fn return_type_of(&self, node: Node) -> Option<String> {
  361. let mut rt = node.child_by_field_name("return_type")?;
  362. if rt.kind() == "reference_type" {
  363. rt = (0..rt.named_child_count())
  364. .filter_map(|i| rt.named_child(i))
  365. .find(|c| matches!(c.kind(), "type_identifier" | "scoped_type_identifier" | "generic_type"))
  366. .unwrap_or(rt);
  367. }
  368. if matches!(rt.kind(), "primitive_type" | "unit_type" | "tuple_type") {
  369. return None;
  370. }
  371. let text = self.text(rt).trim();
  372. let stripped = generic_angle_re().replace_all(text, "");
  373. let last = stripped.rsplit("::").next().unwrap_or("").trim();
  374. if last.is_empty() || !simple_ident_re().is_match(last) {
  375. return None;
  376. }
  377. Some(if last == "Self" { "self".to_string() } else { last.to_string() })
  378. }
  379. /// rustImplTypeName (languages/rust.ts) — the implementing type's simple
  380. /// name for an impl block, from the grammar's `type` field (#1588):
  381. /// `impl<T> Tr for G<T>` / `impl<'a> Iterator for Parents<'a>` /
  382. /// `impl Tr for &Foo` / `impl Tr for m::Foo` → `G` / `Parents` / `Foo` /
  383. /// `Foo`. Shapes naming no single type (tuple, `dyn Tr`, pointer,
  384. /// primitive, fn type…) → None. Mirrored byte-for-byte — change both.
  385. fn impl_type_name(&self, ty: Option<Node>) -> Option<String> {
  386. let ty = ty?;
  387. match ty.kind() {
  388. "type_identifier" | "identifier" => Some(self.text(ty).to_string()),
  389. "generic_type" => self.impl_type_name(ty.child_by_field_name("type")),
  390. "scoped_type_identifier" | "scoped_identifier" => {
  391. self.impl_type_name(ty.child_by_field_name("name"))
  392. }
  393. "reference_type" => self.impl_type_name(ty.child_by_field_name("type")),
  394. _ => None,
  395. }
  396. }
  397. /// rustExtractor.getReceiverType: parent-walk to the nearest impl_item and
  398. /// read its `type` field (impl_type_name). The pre-#1588 rule took the
  399. /// LAST direct type_identifier child, which for `impl Trait for Generic<T>`
  400. /// was the TRAIT — so every parameterized impl's methods were qualified by
  401. /// the trait.
  402. fn receiver_type_of(&self, node: Node) -> Option<String> {
  403. let mut parent = node.parent();
  404. while let Some(p) = parent {
  405. if p.kind() == "impl_item" {
  406. return self.impl_type_name(p.child_by_field_name("type"));
  407. }
  408. parent = p.parent();
  409. }
  410. None
  411. }
  412. // --- visitNode ------------------------------------------------------------
  413. fn visit_node(&mut self, node: Node<'t>) {
  414. stack_guard!();
  415. let kind = node.kind();
  416. let mut skip_children = false;
  417. self.maybe_capture_fn_refs(node);
  418. if matches!(kind, "function_item" | "function_signature_item") {
  419. self.extract_fn_or_method(node);
  420. skip_children = true;
  421. } else if kind == "trait_item" {
  422. self.extract_interface(node);
  423. skip_children = true;
  424. } else if kind == "struct_item" {
  425. self.extract_aggregate(node, "struct");
  426. skip_children = true;
  427. } else if kind == "union_item" {
  428. self.extract_aggregate(node, "union");
  429. skip_children = true;
  430. } else if kind == "enum_item" {
  431. self.extract_enum(node);
  432. skip_children = true;
  433. } else if kind == "type_item" {
  434. self.extract_type_alias(node);
  435. // extractTypeAlias returns false for rust (plain alias) — children
  436. // are visited (nothing in them has a branch).
  437. } else if matches!(kind, "let_declaration" | "const_item" | "static_item")
  438. && !self.inside_class_like()
  439. {
  440. // Inside a class-like scope the gate fails and the else-ladder
  441. // falls through with children VISITED — a trait const's value
  442. // expression emits calls refs from the trait node.
  443. self.extract_variable(node);
  444. self.scan_fn_ref_subtree(node, 0);
  445. skip_children = true;
  446. } else if kind == "use_declaration" {
  447. self.extract_import(node);
  448. // importTypes branch never sets skipChildren.
  449. } else if kind == "call_expression" {
  450. self.extract_call(node);
  451. } else if kind == "struct_expression" {
  452. self.extract_instantiation(node);
  453. } else if kind == "impl_item" {
  454. // Emits the implements back-reference; skipChildren stays false so
  455. // the declaration_list is visited at FILE scope (impl pushes
  456. // nothing on the stack).
  457. self.extract_rust_impl_item(node);
  458. }
  459. if !skip_children {
  460. for i in 0..node.named_child_count() {
  461. if let Some(c) = node.named_child(i) {
  462. self.visit_node(c);
  463. }
  464. }
  465. }
  466. }
  467. // --- extractors --------------------------------------------------------------
  468. /// extractFunction/extractMethod, decision resolved once: method iff a
  469. /// receiver is found (fn inside an impl — including a NESTED fn inside an
  470. /// impl method's body, whose parent walk passes through the outer fn) or
  471. /// the stack top is class-like (trait members).
  472. fn extract_fn_or_method(&mut self, node: Node<'t>) {
  473. stack_guard!();
  474. let receiver = self.receiver_type_of(node);
  475. let as_method = receiver.is_some() || self.inside_class_like();
  476. let name = self.extract_name(node);
  477. if name == "<anonymous>" {
  478. if let Some(body) = node.child_by_field_name("body") {
  479. self.visit_function_body(body);
  480. }
  481. return;
  482. }
  483. let extra = Extra {
  484. docstring: preceding_docstring(node, self.src),
  485. signature: self.signature_of(node),
  486. visibility: Some(self.visibility_of(node)),
  487. // isAsync hook exists but never finds a direct `async` child (it
  488. // nests in function_modifiers) — present-false on every node.
  489. is_async: Some(false),
  490. return_type: self.return_type_of(node),
  491. qualified_name: receiver.as_ref().map(|r| format!("{r}::{name}")),
  492. ..Extra::default() // isExported hook absent → flag not set
  493. };
  494. let kind: &'static str = if as_method { "method" } else { "function" };
  495. let Some(row) = self.create_node(kind, &name, node, extra) else { return };
  496. // Contains edge from the owner: receiver present AND not class-like —
  497. // FIRST earlier-in-file struct/class/enum/trait of the receiver's name.
  498. if as_method && !self.inside_class_like() {
  499. if let Some(receiver) = &receiver {
  500. let owner_row = self
  501. .nodes_meta
  502. .iter()
  503. .position(|m| {
  504. m.name == *receiver
  505. && matches!(m.kind, "struct" | "union" | "class" | "enum" | "trait")
  506. })
  507. .map(|i| i as u32);
  508. if let Some(owner_row) = owner_row {
  509. self.tables.push_edge(&EdgeRow {
  510. source_idx: owner_row,
  511. target_idx: row,
  512. kind: edge_kind_index("contains").unwrap(),
  513. provenance: 0,
  514. line: NONE,
  515. column: NONE,
  516. metadata_json: NONE_STR,
  517. source_id_str: NONE_STR,
  518. target_id_str: NONE_STR,
  519. });
  520. }
  521. }
  522. }
  523. self.extract_type_annotations(node, row);
  524. // extractDecoratorsFor: rust attribute_items are siblings, not
  525. // decorator/annotation/attribute node types — complete no-op.
  526. self.stack.push(Scope { row, kind, name });
  527. if let Some(body) = node.child_by_field_name("body") {
  528. self.visit_function_body(body);
  529. }
  530. self.stack.pop();
  531. }
  532. /// extractInterface — kind `trait` (interfaceKind), inheritance from
  533. /// trait_bounds, body children visited with the trait pushed.
  534. fn extract_interface(&mut self, node: Node<'t>) {
  535. stack_guard!();
  536. let name = self.extract_name(node);
  537. let extra = Extra {
  538. docstring: preceding_docstring(node, self.src),
  539. ..Extra::default() // no visibility/isExported on the interface path
  540. };
  541. let Some(row) = self.create_node("trait", &name, node, extra) else { return };
  542. self.extract_inheritance(node, row);
  543. self.stack.push(Scope { row, kind: "trait", name });
  544. let body = node.child_by_field_name("body").unwrap_or(node);
  545. for i in 0..body.named_child_count() {
  546. if let Some(c) = body.named_child(i) {
  547. self.visit_node(c);
  548. }
  549. }
  550. self.stack.pop();
  551. }
  552. /// Extract a Rust struct or union with a body; unit structs remain skipped.
  553. fn extract_aggregate(&mut self, node: Node<'t>, kind: &'static str) {
  554. stack_guard!();
  555. let Some(body) = node.child_by_field_name("body") else { return };
  556. let name = self.extract_name(node);
  557. let extra = Extra {
  558. docstring: preceding_docstring(node, self.src),
  559. visibility: Some(self.visibility_of(node)),
  560. ..Extra::default()
  561. };
  562. let Some(row) = self.create_node(kind, &name, node, extra) else { return };
  563. self.extract_inheritance(node, row);
  564. self.stack.push(Scope { row, kind, name });
  565. for i in 0..body.named_child_count() {
  566. if let Some(c) = body.named_child(i) {
  567. self.visit_node(c);
  568. }
  569. }
  570. self.stack.pop();
  571. }
  572. /// extractEnum — body required; enum_variant children → enum_member nodes
  573. /// (name field only, payloads never walked); other children re-dispatched.
  574. fn extract_enum(&mut self, node: Node<'t>) {
  575. stack_guard!();
  576. let Some(body) = node.child_by_field_name("body") else { return };
  577. let name = self.extract_name(node);
  578. let extra = Extra {
  579. docstring: preceding_docstring(node, self.src),
  580. visibility: Some(self.visibility_of(node)),
  581. ..Extra::default()
  582. };
  583. let Some(row) = self.create_node("enum", &name, node, extra) else { return };
  584. self.extract_inheritance(node, row);
  585. self.stack.push(Scope { row, kind: "enum", name });
  586. for i in 0..body.named_child_count() {
  587. let Some(c) = body.named_child(i) else { continue };
  588. if c.kind() == "enum_variant" {
  589. if let Some(name_node) = c.child_by_field_name("name") {
  590. let vname = self.text(name_node).to_string();
  591. self.create_node("enum_member", &vname, c, Extra::default());
  592. }
  593. } else {
  594. self.visit_node(c);
  595. }
  596. }
  597. self.stack.pop();
  598. }
  599. /// extractTypeAlias — plain `type_alias` node. QUIRK: the alias-value ref
  600. /// walk reads a `value` field; rust type_item's field is `type` → no ref
  601. /// to the aliased type. Returns children-visited (false) like the TS.
  602. fn extract_type_alias(&mut self, node: Node<'t>) {
  603. let name = self.extract_name(node);
  604. if name == "<anonymous>" {
  605. return;
  606. }
  607. let extra = Extra {
  608. docstring: preceding_docstring(node, self.src),
  609. ..Extra::default()
  610. };
  611. self.create_node("type_alias", &name, node, extra);
  612. }
  613. /// extractVariable's generic fallback: kind is ALWAYS `variable` (no
  614. /// isConst hook), every direct `identifier` child mints a node positioned
  615. /// at the CHILD, docstring shared, isExported present-false, no signature,
  616. /// and the initializer value is never body-walked.
  617. fn extract_variable(&mut self, node: Node<'t>) {
  618. let docstring = preceding_docstring(node, self.src);
  619. for i in 0..node.named_child_count() {
  620. let Some(child) = node.named_child(i) else { continue };
  621. if child.kind() != "identifier" {
  622. continue;
  623. }
  624. let name = self.text(child).to_string();
  625. if !name.is_empty() {
  626. self.create_node(
  627. "variable",
  628. &name,
  629. child,
  630. Extra {
  631. docstring: docstring.clone(),
  632. is_exported: Some(false),
  633. ..Extra::default()
  634. },
  635. );
  636. }
  637. }
  638. }
  639. /// extractImport via the rust hook: import node named by the ROOT module +
  640. /// one generic root `imports` ref + per-binding FULL-path refs.
  641. /// `use x::*;` (use_wildcard) → hook returns null → nothing at all.
  642. fn extract_import(&mut self, node: Node<'t>) {
  643. let use_arg = (0..node.named_child_count())
  644. .filter_map(|i| node.named_child(i))
  645. .find(|c| matches!(c.kind(), "scoped_use_list" | "scoped_identifier" | "use_list" | "identifier"));
  646. let Some(use_arg) = use_arg else { return };
  647. let module_name = self.root_module(use_arg);
  648. let signature = self.text(node).trim().to_string();
  649. self.create_node(
  650. "import",
  651. &module_name.clone(),
  652. node,
  653. Extra { signature: Some(signature), ..Extra::default() },
  654. );
  655. let parent = self.top_row();
  656. let imports_kind = edge_kind_index("imports").unwrap();
  657. if !module_name.is_empty() {
  658. self.push_ref_at(parent, &module_name, imports_kind, node);
  659. }
  660. self.emit_use_binding_refs(node, parent);
  661. }
  662. /// getRootModule (languages/rust.ts:124).
  663. fn root_module(&self, n: Node) -> String {
  664. stack_guard!();
  665. let Some(first) = n.named_child(0) else {
  666. return self.text(n).to_string();
  667. };
  668. match first.kind() {
  669. "identifier" | "crate" | "super" | "self" => self.text(first).to_string(),
  670. "scoped_identifier" => self.root_module(first),
  671. _ => self.text(first).to_string(),
  672. }
  673. }
  674. /// emitRustUseBindingRefs (tree-sitter.ts:3451) — one FULL-path `imports`
  675. /// ref per binding; `Path as Alias` links the source path; leaves that are
  676. /// only `self`/`super`/`crate`/`*` are skipped.
  677. fn emit_use_binding_refs(&mut self, node: Node<'t>, from_row: u32) {
  678. let mut paths: Vec<(String, Node)> = Vec::new();
  679. fn join(prefix: &str, seg: &str) -> String {
  680. if prefix.is_empty() { seg.to_string() } else { format!("{prefix}::{seg}") }
  681. }
  682. fn collect<'t>(w: &Walker<'t>, n: Node<'t>, prefix: &str, paths: &mut Vec<(String, Node<'t>)>) {
  683. stack_guard!();
  684. match n.kind() {
  685. "identifier" => paths.push((join(prefix, w.text(n)), n)),
  686. "scoped_identifier" => {
  687. let full = w.text(n).trim();
  688. paths.push((
  689. if prefix.is_empty() { full.to_string() } else { format!("{prefix}::{full}") },
  690. n,
  691. ));
  692. }
  693. "scoped_use_list" => {
  694. let seg = n
  695. .child_by_field_name("path")
  696. .map(|p| w.text(p).trim().to_string())
  697. .unwrap_or_default();
  698. let new_prefix = if seg.is_empty() { prefix.to_string() } else { join(prefix, &seg) };
  699. let list = n.child_by_field_name("list").or_else(|| {
  700. (0..n.named_child_count())
  701. .filter_map(|i| n.named_child(i))
  702. .find(|c| c.kind() == "use_list")
  703. });
  704. if let Some(list) = list {
  705. collect(w, list, &new_prefix, paths);
  706. }
  707. }
  708. "use_list" => {
  709. for i in 0..n.named_child_count() {
  710. if let Some(c) = n.named_child(i) {
  711. collect(w, c, prefix, paths);
  712. }
  713. }
  714. }
  715. "use_as_clause" => {
  716. let p = n.child_by_field_name("path").or_else(|| n.named_child(0));
  717. if let Some(p) = p {
  718. collect(w, p, prefix, paths);
  719. }
  720. }
  721. _ => {} // visibility_modifier, use_wildcard, bare crate/self/super
  722. }
  723. }
  724. for i in 0..node.named_child_count() {
  725. if let Some(c) = node.named_child(i) {
  726. collect(self, c, "", &mut paths);
  727. }
  728. }
  729. let imports_kind = edge_kind_index("imports").unwrap();
  730. for (text, n) in paths {
  731. let leaf = text.rsplit("::").next().unwrap_or("");
  732. if leaf.is_empty() || matches!(leaf, "self" | "super" | "crate" | "*") {
  733. continue;
  734. }
  735. self.push_ref_at(from_row, &text, imports_kind, n);
  736. }
  737. }
  738. /// extractCall — the rust paths of the generic else-branch (4312+).
  739. fn extract_call(&mut self, node: Node<'t>) {
  740. if self.stack.is_empty() {
  741. return;
  742. }
  743. let func = node
  744. .child_by_field_name("function")
  745. .or_else(|| node.named_child(0));
  746. let mut callee_name = String::new();
  747. if let Some(func) = func {
  748. if func.kind() == "field_expression" {
  749. let property = func
  750. .child_by_field_name("property")
  751. .or_else(|| func.child_by_field_name("field"))
  752. .or_else(|| func.named_child(1));
  753. if let Some(property) = property {
  754. let method_name = self.text(property);
  755. let receiver = func
  756. .child_by_field_name("object")
  757. .or_else(|| func.child_by_field_name("operand"))
  758. .or_else(|| func.child_by_field_name("argument"))
  759. .or_else(|| func.named_child(0));
  760. if let Some(r) = receiver {
  761. if is_literal_receiver(r.kind()) {
  762. return; // emit NOTHING (#1230)
  763. }
  764. }
  765. if let Some(r) = receiver {
  766. match r.kind() {
  767. // rust `self` is node kind `self`, NOT `identifier` —
  768. // it dodges this branch and falls to the bare-name
  769. // fallthrough (same net effect as SKIP_RECEIVERS).
  770. "identifier" | "simple_identifier" | "field_identifier" => {
  771. let receiver_name = self.text(r);
  772. if !matches!(receiver_name, "self" | "this" | "cls" | "super") {
  773. callee_name = format!("{receiver_name}.{method_name}");
  774. } else {
  775. callee_name = method_name.to_string();
  776. }
  777. }
  778. "call_expression" => {
  779. // Chained-call re-encode: ONLY an associated-
  780. // function chain (`Foo::new().bar()`, inner
  781. // callee a scoped_identifier). Instance chains
  782. // keep the bare method name.
  783. let inner_fn = r.child_by_field_name("function");
  784. let reencode =
  785. inner_fn.map(|f| f.kind() == "scoped_identifier").unwrap_or(false);
  786. if reencode {
  787. let inner: String = self
  788. .text(inner_fn.unwrap())
  789. .replace("->", ".")
  790. .chars()
  791. .filter(|c| !c.is_whitespace())
  792. .collect();
  793. callee_name = format!("{inner}().{method_name}");
  794. } else {
  795. callee_name = method_name.to_string();
  796. }
  797. }
  798. "field_expression" => {
  799. // `self.<field>.<method>()` — a call through a
  800. // field of the enclosing type (#1585): keep the
  801. // `self.` prefix so the resolver can type the
  802. // field from the owner struct's declaration
  803. // (or leave it unresolved). Any other
  804. // field_expression receiver — a deeper chain,
  805. // a non-self base — keeps the bare name.
  806. let base = r.child_by_field_name("value");
  807. let field = r.child_by_field_name("field");
  808. match (base, field) {
  809. (Some(b), Some(f))
  810. if b.kind() == "self" && f.kind() == "field_identifier" =>
  811. {
  812. let field_name = self.text(f);
  813. callee_name = format!("self.{field_name}.{method_name}");
  814. }
  815. _ => callee_name = method_name.to_string(),
  816. }
  817. }
  818. _ => {
  819. // parenthesized, await_expression, `self` —
  820. // bare method name.
  821. callee_name = method_name.to_string();
  822. }
  823. }
  824. } else {
  825. callee_name = method_name.to_string();
  826. }
  827. }
  828. } else if matches!(func.kind(), "scoped_identifier" | "scoped_call_expression") {
  829. callee_name = self.text(func).to_string();
  830. } else {
  831. // identifier; generic_function keeps the raw turbofish text
  832. // (`helper::<T>` — unresolvable downstream, preserved).
  833. callee_name = self.text(func).to_string();
  834. }
  835. }
  836. if !callee_name.is_empty() {
  837. // Parenthesized-callee normalization — `(f)(x)` → `f`.
  838. if let Some(c) = util::paren_conversion().captures(&callee_name) {
  839. callee_name = c[1].to_string();
  840. }
  841. let from = self.top_row();
  842. self.push_ref_at(from, &callee_name.clone(), edge_kind_index("calls").unwrap(), node);
  843. }
  844. }
  845. /// extractInstantiation — struct_expression via the GENERIC path: strip
  846. /// from the first `<`, keep the trailing `::`/`.` segment (JS slice
  847. /// semantics: slice(lastDot+1) after a `::` leaves one `:`, then ONE
  848. /// leading `[:.]` is stripped).
  849. fn extract_instantiation(&mut self, node: Node<'t>) {
  850. if self.stack.is_empty() {
  851. return;
  852. }
  853. let ctor = node
  854. .child_by_field_name("constructor")
  855. .or_else(|| node.child_by_field_name("type"))
  856. .or_else(|| node.child_by_field_name("name"))
  857. .or_else(|| node.named_child(0));
  858. let Some(ctor) = ctor else { return };
  859. let mut class_name = self.text(ctor).to_string();
  860. if let Some(lt) = class_name.find('<') {
  861. if lt > 0 {
  862. class_name.truncate(lt);
  863. }
  864. }
  865. let last_dot = class_name.rfind('.').map(|i| i as i64).unwrap_or(-1);
  866. let last_colon = class_name.rfind("::").map(|i| i as i64).unwrap_or(-1);
  867. let last = last_dot.max(last_colon);
  868. if last >= 0 {
  869. class_name = class_name[(last + 1) as usize..].to_string();
  870. if let Some(rest) = class_name.strip_prefix(&[':', '.'][..]) {
  871. class_name = rest.to_string();
  872. }
  873. }
  874. let class_name = class_name.trim().to_string();
  875. if !class_name.is_empty() {
  876. let from = self.top_row();
  877. self.push_ref_at(from, &class_name, edge_kind_index("instantiates").unwrap(), node);
  878. }
  879. }
  880. /// extractRustRouteMacro — body-walker-only; bare `routes`/`catchers`
  881. /// identifiers only (`rocket::routes![…]` is skipped); identifier runs in
  882. /// the token tree join with `::`, flushed on `,` and at end.
  883. fn extract_rust_route_macro(&mut self, node: Node<'t>) {
  884. let Some(macro_name) = node.named_child(0) else { return };
  885. let name = self.text(macro_name);
  886. if name != "routes" && name != "catchers" {
  887. return;
  888. }
  889. let token_tree = (0..node.named_child_count())
  890. .filter_map(|i| node.named_child(i))
  891. .find(|c| c.kind() == "token_tree");
  892. let Some(token_tree) = token_tree else { return };
  893. if self.stack.is_empty() {
  894. return;
  895. }
  896. let from = self.top_row();
  897. let refs_kind = edge_kind_index("references").unwrap();
  898. let mut parts: Vec<&str> = Vec::new();
  899. let mut line = 0u32;
  900. let mut column_byte = 0usize;
  901. let mut row = 0usize;
  902. macro_rules! flush {
  903. () => {
  904. if !parts.is_empty() {
  905. let joined = parts.join("::");
  906. let column = util::col16(self.src, &self.line_starts, row, column_byte);
  907. let name_ref = self.arena.put(&joined);
  908. self.tables.push_ref(&RefRow {
  909. from_idx: from,
  910. kind: refs_kind,
  911. line,
  912. column,
  913. reference_name: name_ref,
  914. candidates: NONE_STR,
  915. from_id_str: NONE_STR,
  916. });
  917. parts.clear();
  918. }
  919. };
  920. }
  921. for i in 0..token_tree.child_count() {
  922. let Some(t) = token_tree.child(i) else { continue };
  923. if t.kind() == "identifier" {
  924. if parts.is_empty() {
  925. line = t.start_position().row as u32 + 1;
  926. column_byte = t.start_byte();
  927. row = t.start_position().row;
  928. }
  929. parts.push(self.text(t));
  930. } else if t.kind() == "," {
  931. flush!();
  932. }
  933. }
  934. flush!();
  935. }
  936. /// extractInheritance — the rust-reachable cases: trait_bounds
  937. /// (supertraits; a scoped `fmt::Debug` bound matches NO case and is
  938. /// dropped), the Go embedding check on field_declaration (inert in rust —
  939. /// every field has a field_identifier), and the field_declaration_list
  940. /// recursion that reaches it.
  941. fn extract_inheritance(&mut self, node: Node<'t>, class_row: u32) {
  942. stack_guard!();
  943. let extends_kind = edge_kind_index("extends").unwrap();
  944. for i in 0..node.named_child_count() {
  945. let Some(child) = node.named_child(i) else { continue };
  946. match child.kind() {
  947. "trait_bounds" => {
  948. for j in 0..child.named_child_count() {
  949. let Some(bound) = child.named_child(j) else { continue };
  950. let type_node: Option<Node> = match bound.kind() {
  951. "type_identifier" => Some(bound),
  952. "generic_type" => (0..bound.named_child_count())
  953. .filter_map(|k| bound.named_child(k))
  954. .find(|c| c.kind() == "type_identifier"),
  955. "higher_ranked_trait_bound" => {
  956. let generic = (0..bound.named_child_count())
  957. .filter_map(|k| bound.named_child(k))
  958. .find(|c| c.kind() == "generic_type");
  959. generic
  960. .and_then(|g| {
  961. (0..g.named_child_count())
  962. .filter_map(|k| g.named_child(k))
  963. .find(|c| c.kind() == "type_identifier")
  964. })
  965. .or_else(|| {
  966. (0..bound.named_child_count())
  967. .filter_map(|k| bound.named_child(k))
  968. .find(|c| c.kind() == "type_identifier")
  969. })
  970. }
  971. _ => None, // scoped_type_identifier: dropped (quirk)
  972. };
  973. if let Some(tn) = type_node {
  974. let name = self.text(tn).to_string();
  975. self.push_ref_at(class_row, &name, extends_kind, tn);
  976. }
  977. }
  978. }
  979. "field_declaration" => {
  980. let has_field_identifier = (0..child.named_child_count())
  981. .filter_map(|j| child.named_child(j))
  982. .any(|c| c.kind() == "field_identifier");
  983. if !has_field_identifier {
  984. let type_id = (0..child.named_child_count())
  985. .filter_map(|j| child.named_child(j))
  986. .find(|c| c.kind() == "type_identifier");
  987. if let Some(type_id) = type_id {
  988. let name = self.text(type_id).to_string();
  989. self.push_ref_at(class_row, &name, extends_kind, type_id);
  990. }
  991. }
  992. }
  993. "field_declaration_list" | "class_heritage" => {
  994. self.extract_inheritance(child, class_row);
  995. }
  996. _ => {}
  997. }
  998. }
  999. }
  1000. /// extractRustImplItem — `impl Trait for Type` back-reference from the
  1001. /// grammar's `trait` / `type` fields (#1588; an inherent impl has no
  1002. /// `trait` field and emits nothing). Target = FIRST earlier node of kind
  1003. /// struct/union/enum/class (never trait) named by impl_type_name; ref FROM
  1004. /// the type's node, named by the trait's full text (scoped path / generic
  1005. /// args kept), at the trait node's position.
  1006. fn extract_rust_impl_item(&mut self, node: Node<'t>) {
  1007. let Some(trait_node) = node.child_by_field_name("trait") else {
  1008. return;
  1009. };
  1010. let trait_name = self.text(trait_node).to_string();
  1011. let Some(type_name) = self.impl_type_name(node.child_by_field_name("type")) else {
  1012. return;
  1013. };
  1014. let target_row = self
  1015. .nodes_meta
  1016. .iter()
  1017. .position(|m| m.name == type_name && matches!(m.kind, "struct" | "union" | "enum" | "class"))
  1018. .map(|i| i as u32);
  1019. if let Some(target_row) = target_row {
  1020. self.push_ref_at(target_row, &trait_name, edge_kind_index("implements").unwrap(), trait_node);
  1021. }
  1022. }
  1023. /// extractTypeAnnotations — parameters + return_type subtrees, one
  1024. /// `references` ref per type_identifier leaf not in BUILTIN_TYPES. The
  1025. /// trailing `type_annotation` child lookup is included for fidelity (the
  1026. /// rust grammar has no such node — always a no-op).
  1027. fn extract_type_annotations(&mut self, node: Node<'t>, from_row: u32) {
  1028. if let Some(params) = node.child_by_field_name("parameters") {
  1029. self.extract_type_refs_from_subtree(params, from_row);
  1030. }
  1031. if let Some(ret) = node.child_by_field_name("return_type") {
  1032. self.extract_type_refs_from_subtree(ret, from_row);
  1033. }
  1034. let type_annotation = (0..node.named_child_count())
  1035. .filter_map(|i| node.named_child(i))
  1036. .find(|c| c.kind() == "type_annotation");
  1037. if let Some(ta) = type_annotation {
  1038. self.extract_type_refs_from_subtree(ta, from_row);
  1039. }
  1040. }
  1041. fn extract_type_refs_from_subtree(&mut self, node: Node<'t>, from_row: u32) {
  1042. stack_guard!();
  1043. if node.kind() == "type_identifier" {
  1044. let type_name = self.text(node).to_string();
  1045. if !type_name.is_empty() && !is_builtin_type(&type_name) {
  1046. self.push_ref_at(from_row, &type_name, edge_kind_index("references").unwrap(), node);
  1047. }
  1048. return;
  1049. }
  1050. for i in 0..node.named_child_count() {
  1051. if let Some(c) = node.named_child(i) {
  1052. self.extract_type_refs_from_subtree(c, from_row);
  1053. }
  1054. }
  1055. }
  1056. // --- visitFunctionBody -----------------------------------------------------
  1057. fn visit_function_body(&mut self, body: Node<'t>) {
  1058. stack_guard!();
  1059. self.visit_for_calls_and_structure(body);
  1060. }
  1061. fn visit_for_calls_and_structure(&mut self, node: Node<'t>) {
  1062. stack_guard!();
  1063. let kind = node.kind();
  1064. self.maybe_capture_fn_refs(node);
  1065. // Rocket route macros: handler paths live in a raw token tree.
  1066. if kind == "macro_invocation" {
  1067. self.extract_rust_route_macro(node);
  1068. }
  1069. if kind == "call_expression" {
  1070. self.extract_call(node);
  1071. } else if kind == "struct_expression" {
  1072. self.extract_instantiation(node);
  1073. }
  1074. // Nested NAMED fns become their own nodes (a nested fn inside an impl
  1075. // method walks up to the impl and indexes as a METHOD).
  1076. if matches!(kind, "function_item" | "function_signature_item") {
  1077. let name = self.extract_name(node);
  1078. if name != "<anonymous>" {
  1079. self.extract_fn_or_method(node);
  1080. return;
  1081. }
  1082. }
  1083. // Structural nodes inside bodies.
  1084. if kind == "struct_item" {
  1085. self.extract_aggregate(node, "struct");
  1086. return;
  1087. }
  1088. if kind == "union_item" {
  1089. self.extract_aggregate(node, "union");
  1090. return;
  1091. }
  1092. if kind == "enum_item" {
  1093. self.extract_enum(node);
  1094. return;
  1095. }
  1096. if kind == "trait_item" {
  1097. self.extract_interface(node);
  1098. return;
  1099. }
  1100. for i in 0..node.named_child_count() {
  1101. if let Some(c) = node.named_child(i) {
  1102. self.visit_for_calls_and_structure(c);
  1103. }
  1104. }
  1105. }
  1106. // --- fn refs (RUST_SPEC) ----------------------------------------------------
  1107. /// maybeCaptureFnRefs with RUST_SPEC's dispatch: arguments→args,
  1108. /// assignment_expression→rhs(right), field_initializer→value(value),
  1109. /// array_expression→list, static_item/let_declaration→varinit(value).
  1110. /// No layers/unwrap/special — only bare identifiers qualify (`&handler`
  1111. /// captures nothing). QUIRK: const_item is NOT in the dispatch.
  1112. fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
  1113. enum Mode {
  1114. Args,
  1115. Rhs,
  1116. Value,
  1117. List,
  1118. Varinit,
  1119. }
  1120. let mode = match node.kind() {
  1121. "arguments" => Mode::Args,
  1122. "assignment_expression" => Mode::Rhs,
  1123. "field_initializer" => Mode::Value,
  1124. "array_expression" => Mode::List,
  1125. "static_item" | "let_declaration" => Mode::Varinit,
  1126. _ => return,
  1127. };
  1128. if self.stack.is_empty() {
  1129. return;
  1130. }
  1131. let from = self.top_row();
  1132. let mut values: Vec<Node> = Vec::new();
  1133. match mode {
  1134. Mode::Args | Mode::List => {
  1135. for i in 0..node.named_child_count() {
  1136. if let Some(c) = node.named_child(i) {
  1137. values.push(c);
  1138. }
  1139. }
  1140. }
  1141. Mode::Rhs => {
  1142. if let Some(rhs) = node.child_by_field_name("right") {
  1143. // Param-storage skip: `o.cb = cb`.
  1144. let lhs_text = node
  1145. .child_by_field_name("left")
  1146. .map(|l| self.text(l))
  1147. .unwrap_or("");
  1148. let lhs_last = util::lhs_last_name()
  1149. .captures(lhs_text)
  1150. .and_then(|c| c.get(1))
  1151. .map(|m| m.as_str());
  1152. if !(lhs_last.is_some() && lhs_last == Some(self.text(rhs).trim())) {
  1153. values.push(rhs);
  1154. }
  1155. }
  1156. }
  1157. Mode::Value => {
  1158. let v = node.child_by_field_name("value").or_else(|| {
  1159. if node.named_child_count() > 0 {
  1160. node.named_child(node.named_child_count() - 1)
  1161. } else {
  1162. None
  1163. }
  1164. });
  1165. if let Some(v) = v {
  1166. values.push(v);
  1167. }
  1168. }
  1169. Mode::Varinit => {
  1170. // Destructuring skip: a tuple/struct pattern LHS extracts data,
  1171. // never a function alias (static_item's name is an identifier,
  1172. // let_declaration's `pattern` field can be a pattern).
  1173. let name_node = node
  1174. .child_by_field_name("name")
  1175. .or_else(|| node.child_by_field_name("pattern"));
  1176. if let Some(nn) = name_node {
  1177. if matches!(
  1178. nn.kind(),
  1179. "object_pattern" | "array_pattern" | "tuple_pattern" | "struct_pattern"
  1180. ) {
  1181. return;
  1182. }
  1183. }
  1184. if let Some(v) = node.child_by_field_name("value") {
  1185. values.push(v);
  1186. }
  1187. }
  1188. }
  1189. for v in values {
  1190. // normalizeValue: idTypes = {identifier} only, no layers/unwrap.
  1191. if v.kind() == "identifier" {
  1192. let name = self.text(v).to_string();
  1193. if name.is_empty() || is_stoplisted(&name) {
  1194. continue;
  1195. }
  1196. let p = v.start_position();
  1197. self.fn_ref_cands.push(Cand {
  1198. from,
  1199. name,
  1200. line: p.row as u32 + 1,
  1201. column_byte: v.start_byte(),
  1202. row: p.row,
  1203. });
  1204. }
  1205. }
  1206. }
  1207. fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
  1208. stack_guard!();
  1209. if depth > 12 {
  1210. return;
  1211. }
  1212. if depth > 0
  1213. && matches!(
  1214. node.kind(),
  1215. "function_item" | "function_signature_item" | "arrow_function"
  1216. | "function_expression" | "lambda_literal" | "lambda_expression"
  1217. )
  1218. {
  1219. return;
  1220. }
  1221. self.maybe_capture_fn_refs(node);
  1222. for i in 0..node.named_child_count() {
  1223. if let Some(c) = node.named_child(i) {
  1224. self.scan_fn_ref_subtree(c, depth + 1);
  1225. }
  1226. }
  1227. }
  1228. fn flush_fn_ref_candidates(&mut self) {
  1229. let cands = std::mem::take(&mut self.fn_ref_cands);
  1230. if cands.is_empty() || util::is_generated_file(self.file_path) {
  1231. return;
  1232. }
  1233. let mut seen: HashSet<(String, String)> = HashSet::new();
  1234. for c in cands {
  1235. if !c.name.starts_with("this.")
  1236. && !c.name.contains("::")
  1237. && !self.defined_fn_names.contains(&c.name)
  1238. && !self.imported_names.contains(&c.name)
  1239. {
  1240. continue;
  1241. }
  1242. if !seen.insert((self.node_ids[c.from as usize].clone(), c.name.clone())) {
  1243. continue;
  1244. }
  1245. let column = util::col16(self.src, &self.line_starts, c.row, c.column_byte);
  1246. let name_ref = self.arena.put(&c.name);
  1247. self.tables.push_ref(&RefRow {
  1248. from_idx: c.from,
  1249. kind: FUNCTION_REF_CODE,
  1250. line: c.line,
  1251. column,
  1252. reference_name: name_ref,
  1253. candidates: NONE_STR,
  1254. from_id_str: NONE_STR,
  1255. });
  1256. }
  1257. }
  1258. // --- value refs -------------------------------------------------------------
  1259. fn flush_value_refs(&mut self, root: Node<'t>) {
  1260. let scopes = std::mem::take(&mut self.value_scopes);
  1261. let mut targets = std::mem::take(&mut self.fs_values);
  1262. let counts = std::mem::take(&mut self.fs_value_counts);
  1263. if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
  1264. return;
  1265. }
  1266. if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
  1267. return;
  1268. }
  1269. // Shadow prune — rust declarator shapes: const_item/static_item (name
  1270. // field) and let_declaration (the shadow source: `pattern` field; a
  1271. // tuple pattern bumps every named child).
  1272. let mut decl_counts: HashMap<&str, u32> = HashMap::new();
  1273. let mut bump = |decl_counts: &mut HashMap<&'t str, u32>, name_node: Option<Node<'t>>, src: &'t str, targets: &HashMap<String, u32>| {
  1274. if let Some(n) = name_node {
  1275. if matches!(n.kind(), "identifier" | "simple_identifier") {
  1276. let nm = &src[n.byte_range()];
  1277. if targets.contains_key(nm) {
  1278. *decl_counts.entry(nm).or_insert(0) += 1;
  1279. }
  1280. }
  1281. }
  1282. };
  1283. let mut dstack: Vec<Node> = vec![root];
  1284. let mut dvisited = 0usize;
  1285. while let Some(n) = dstack.pop() {
  1286. if dvisited >= MAX_VALUE_REF_NODES {
  1287. break;
  1288. }
  1289. dvisited += 1;
  1290. match n.kind() {
  1291. "const_item" | "static_item" => {
  1292. bump(&mut decl_counts, n.child_by_field_name("name"), self.src, &targets)
  1293. }
  1294. "let_declaration" => {
  1295. let left = n
  1296. .child_by_field_name("left")
  1297. .or_else(|| n.child_by_field_name("pattern"))
  1298. .or_else(|| n.named_child(0));
  1299. if let Some(left) = left {
  1300. if left.kind() == "identifier" {
  1301. bump(&mut decl_counts, Some(left), self.src, &targets);
  1302. } else {
  1303. for i in 0..left.named_child_count() {
  1304. bump(&mut decl_counts, left.named_child(i), self.src, &targets);
  1305. }
  1306. }
  1307. }
  1308. }
  1309. _ => {}
  1310. }
  1311. for i in 0..n.named_child_count() {
  1312. if let Some(c) = n.named_child(i) {
  1313. dstack.push(c);
  1314. }
  1315. }
  1316. }
  1317. let shadowed: Vec<String> = decl_counts
  1318. .iter()
  1319. .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
  1320. .map(|(nm, _)| nm.to_string())
  1321. .collect();
  1322. for nm in shadowed {
  1323. targets.remove(&nm);
  1324. }
  1325. if targets.is_empty() {
  1326. return;
  1327. }
  1328. let refs_kind = edge_kind_index("references").unwrap();
  1329. for scope in &scopes {
  1330. let mut seen: HashSet<&str> = HashSet::new();
  1331. let mut stack: Vec<Node> = vec![scope.node];
  1332. let mut visited = 0usize;
  1333. while let Some(n) = stack.pop() {
  1334. if visited >= MAX_VALUE_REF_NODES {
  1335. break;
  1336. }
  1337. visited += 1;
  1338. if matches!(n.kind(), "identifier" | "constant" | "name" | "simple_identifier") {
  1339. let ref_name = self.text(n);
  1340. if let Some(&target_row) = targets.get(ref_name) {
  1341. let target_id = self.node_ids[target_row as usize].as_str();
  1342. if target_id != self.node_ids[scope.row as usize]
  1343. && ref_name != scope.name
  1344. && !seen.contains(&target_id)
  1345. {
  1346. seen.insert(target_id);
  1347. let meta = self.arena.put(r#"{"valueRef":true}"#);
  1348. self.tables.push_edge(&EdgeRow {
  1349. source_idx: scope.row,
  1350. target_idx: target_row,
  1351. kind: refs_kind,
  1352. provenance: 0,
  1353. line: NONE,
  1354. column: NONE,
  1355. metadata_json: meta,
  1356. source_id_str: NONE_STR,
  1357. target_id_str: NONE_STR,
  1358. });
  1359. }
  1360. }
  1361. }
  1362. for i in 0..n.named_child_count() {
  1363. if let Some(c) = n.named_child(i) {
  1364. stack.push(c);
  1365. }
  1366. }
  1367. }
  1368. }
  1369. }
  1370. }
  1371. fn is_stoplisted(name: &str) -> bool {
  1372. matches!(
  1373. name,
  1374. "this" | "self" | "super" | "null" | "nil" | "true" | "false" | "undefined" | "new"
  1375. | "NULL" | "nullptr" | "None"
  1376. )
  1377. }
  1378. /// LITERAL_RECEIVER_TYPES (shared table).
  1379. fn is_literal_receiver(kind: &str) -> bool {
  1380. matches!(
  1381. kind,
  1382. "string" | "string_literal" | "interpreted_string_literal" | "raw_string_literal"
  1383. | "template_string" | "concatenated_string" | "formatted_string" | "f_string"
  1384. | "line_string_literal" | "string_content" | "heredoc_body"
  1385. | "number" | "number_literal" | "integer" | "integer_literal" | "float"
  1386. | "float_literal" | "int_literal" | "decimal_integer_literal" | "real_literal"
  1387. | "char_literal" | "character_literal" | "rune_literal" | "regex" | "regex_literal"
  1388. | "true" | "false" | "boolean_literal" | "bool_literal" | "none" | "null" | "nil"
  1389. | "null_literal" | "undefined"
  1390. | "list" | "list_literal" | "array" | "array_literal" | "array_creation_expression"
  1391. | "dictionary" | "dict_literal" | "object" | "tuple" | "set"
  1392. )
  1393. }
  1394. /// BUILTIN_TYPES (shared table — port the WHOLE set: a rust `String`
  1395. /// type_identifier IS suppressed via the Scala row).
  1396. fn is_builtin_type(name: &str) -> bool {
  1397. matches!(
  1398. name,
  1399. "string" | "number" | "boolean" | "void" | "null" | "undefined" | "never" | "any"
  1400. | "unknown" | "object" | "symbol" | "bigint" | "true" | "false"
  1401. | "str" | "bool" | "i8" | "i16" | "i32" | "i64" | "i128" | "isize"
  1402. | "u8" | "u16" | "u32" | "u64" | "u128" | "usize" | "f32" | "f64" | "char"
  1403. | "int" | "long" | "short" | "byte" | "float" | "double"
  1404. | "int8" | "int16" | "int32" | "int64" | "uint8" | "uint16" | "uint32" | "uint64"
  1405. | "float32" | "float64" | "complex64" | "complex128" | "rune" | "error"
  1406. | "Int" | "Long" | "Short" | "Byte" | "Float" | "Double" | "Boolean" | "Char"
  1407. | "Unit" | "String" | "Any" | "AnyRef" | "AnyVal" | "Nothing" | "Null"
  1408. )
  1409. }
  1410. fn opt_str(arena: &mut Arena, s: Option<&str>) -> StrRef {
  1411. match s {
  1412. Some(s) => arena.put(s),
  1413. None => NONE_STR,
  1414. }
  1415. }