kotlin.rs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. //! Kotlin extraction — a faithful Rust port of `TreeSitterExtractor`'s Kotlin
  2. //! paths (src/extraction/tree-sitter.ts) plus languages/kotlin.ts.
  3. //!
  4. //! Same porting contract as the other walkers: behavior parity, bug-for-bug.
  5. //! The authoritative quirk list is docs/design/kotlin-kernel-port-checklist.md.
  6. //! Two surfaces are FIRSTS for the kernel: extension-function receivers
  7. //! (getReceiverType → `Type::method` qualified-name OVERRIDE with no package
  8. //! prefix + the owner-contains fallback that excludes `interface` kinds and
  9. //! is source-order dependent) and extractModifiers (expect/actual platform
  10. //! modifiers → the node DECORATORS wire field, on every created node — the
  11. //! KMP synthesizer's input). Preserved on purpose: the FIELD_COUNT-0 dead
  12. //! cluster (no signatures, ZERO type-annotation refs), the bodiless-class
  13. //! header re-walk asymmetry, enum-entry bodies being invisible, KDoc
  14. //! (`multiline_comment`) never being a docstring AND chain-breaking,
  15. //! comment-gluing into import/package extents,
  16. //! `@Anno(args)` emitting nothing while `@Anno` emits decorates, zero
  17. //! instantiates refs (constructors are capitalized `calls`), the qualified-
  18. //! receiver `com::qext` bug, the paren-then-lambda `trailing()` garbage
  19. //! callee, and the packaged-file value-ref target drop (namespace parents are
  20. //! not accepted). The fun-interface misparse-recovery hook branches are
  21. //! DEFER-SHIELDED (every such file has_error → wasm) and are not ported.
  22. //! Positions in UTF-16 code units. Expected deferral 4.7–8.5% (both-arm,
  23. //! grammar-inherent — incl. phantom errors: trust the has_error FLAG).
  24. use crate::buffers::{
  25. build_meta, edge_kind_index, node_kind_index, Arena, BoolFlags, EdgeRow, EmitOut, NodeRow,
  26. RefRow, StrRef, Tables, FLAG_IS_ASYNC, FLAG_IS_EXPORTED, FLAG_IS_STATIC, FUNCTION_REF_CODE,
  27. NONE, NONE_STR,
  28. };
  29. use crate::docstring::preceding_docstring;
  30. use crate::ids;
  31. use crate::textutil as util;
  32. use regex::Regex;
  33. use std::collections::{HashMap, HashSet};
  34. use std::sync::OnceLock;
  35. use tree_sitter::{Node, Parser};
  36. const MAX_VALUE_REF_NODES: usize = 20_000;
  37. /// NAME_STOPLIST (function-ref.ts).
  38. fn is_stoplisted(name: &str) -> bool {
  39. matches!(
  40. name,
  41. "this" | "self" | "super" | "null" | "nil" | "true" | "false" | "undefined" | "new"
  42. | "NULL" | "nullptr" | "None"
  43. )
  44. }
  45. /// LITERAL_RECEIVER_TYPES (tree-sitter.ts:373) — full shared set.
  46. fn is_literal_receiver(kind: &str) -> bool {
  47. matches!(
  48. kind,
  49. "string" | "string_literal" | "interpreted_string_literal" | "raw_string_literal"
  50. | "template_string" | "concatenated_string" | "formatted_string" | "f_string"
  51. | "line_string_literal" | "string_content" | "heredoc_body"
  52. | "number" | "number_literal" | "integer" | "integer_literal" | "float"
  53. | "float_literal" | "int_literal" | "decimal_integer_literal" | "real_literal"
  54. | "char_literal" | "character_literal" | "rune_literal" | "regex" | "regex_literal"
  55. | "true" | "false" | "boolean_literal" | "bool_literal" | "none" | "null" | "nil"
  56. | "null_literal" | "undefined"
  57. | "list" | "list_literal" | "array" | "array_literal" | "array_creation_expression"
  58. | "dictionary" | "dict_literal" | "object" | "tuple" | "set"
  59. )
  60. }
  61. /// `/^[A-Za-z_]\w*$/` with JS's ASCII `\w` (getReturnType's ident test).
  62. fn ascii_ident_re() -> &'static Regex {
  63. static RE: OnceLock<Regex> = OnceLock::new();
  64. RE.get_or_init(|| Regex::new(r"^[A-Za-z_][0-9A-Za-z_]*$").unwrap())
  65. }
  66. /// extractStaticMemberRef's capitalized-receiver test.
  67. fn capitalized_re() -> &'static Regex {
  68. static RE: OnceLock<Regex> = OnceLock::new();
  69. RE.get_or_init(|| Regex::new(r"^[A-Z][A-Za-z0-9_]*$").unwrap())
  70. }
  71. /// JS `\s` for the #750 inner-callee strip.
  72. fn is_js_space(c: char) -> bool {
  73. matches!(
  74. c,
  75. '\t' | '\n' | '\x0B' | '\x0C' | '\r' | ' ' | '\u{00A0}' | '\u{1680}'
  76. | '\u{2000}'..='\u{200A}' | '\u{2028}' | '\u{2029}' | '\u{202F}' | '\u{205F}'
  77. | '\u{3000}' | '\u{FEFF}'
  78. )
  79. }
  80. fn strip_js_ws(s: &str) -> String {
  81. s.chars().filter(|c| !is_js_space(*c)).collect()
  82. }
  83. /// A property's CODE children: the named child right after the `=` token, a
  84. /// `property_delegate` (`by lazy { … }`), and an accessor the grammar nested
  85. /// under the declaration (`val x: Int get() = compute()` — written on ONE line;
  86. /// an accessor on its own line parses as a SIBLING of the property and is not
  87. /// reachable from here). What stays unwalked is the declaration itself —
  88. /// modifiers, the `val`/`var` keyword, the name+type, and an extension
  89. /// receiver's type and type parameters. (Go's #693 fix walks the `value` field
  90. /// for the same reason; this grammar exposes no fields at all, hence the `=`
  91. /// anchor.)
  92. fn property_initializers<'t>(node: Node<'t>) -> Vec<Node<'t>> {
  93. let mut out: Vec<Node<'t>> = Vec::new();
  94. let mut after_eq = false;
  95. for i in 0..node.child_count() {
  96. let Some(c) = node.child(i) else { continue };
  97. if !c.is_named() {
  98. if c.kind() == "=" {
  99. after_eq = true;
  100. }
  101. continue;
  102. }
  103. if after_eq {
  104. out.push(c);
  105. after_eq = false;
  106. } else if matches!(c.kind(), "property_delegate" | "getter" | "setter") {
  107. out.push(c);
  108. }
  109. }
  110. out
  111. }
  112. /// Accessors written on their OWN line parse as SIBLINGS of the property, not
  113. /// as children of it (same-line ones nest — see property_initializers). Walking
  114. /// back over any accessors between us and the declaration finds the property an
  115. /// accessor belongs to; None when this accessor stands alone.
  116. fn accessor_owner<'t>(node: Node<'t>) -> Option<Node<'t>> {
  117. let mut p = node.prev_named_sibling();
  118. while let Some(n) = p {
  119. if matches!(n.kind(), "getter" | "setter") {
  120. p = n.prev_named_sibling();
  121. continue;
  122. }
  123. return if n.kind() == "property_declaration" { Some(n) } else { None };
  124. }
  125. None
  126. }
  127. /// The sibling accessors that follow a property declaration, in source order.
  128. fn following_accessors<'t>(node: Node<'t>) -> Vec<Node<'t>> {
  129. let mut out = Vec::new();
  130. let mut n = node.next_named_sibling();
  131. while let Some(c) = n {
  132. if !matches!(c.kind(), "getter" | "setter") {
  133. break;
  134. }
  135. out.push(c);
  136. n = c.next_named_sibling();
  137. }
  138. out
  139. }
  140. struct Scope {
  141. row: u32,
  142. kind: &'static str,
  143. name: String,
  144. }
  145. /// Per-node metadata for the extension-fn owner-contains lookup.
  146. struct NodeMeta {
  147. kind: &'static str,
  148. name: String,
  149. }
  150. #[derive(Default)]
  151. struct Extra {
  152. docstring: Option<String>,
  153. signature: Option<String>,
  154. visibility: Option<u8>,
  155. is_static: Option<bool>,
  156. is_async: Option<bool>,
  157. return_type: Option<String>,
  158. /// composeReceiverQualifiedName override (extension methods) — the id
  159. /// still hashes the bare NAME; only the qualifiedName column changes.
  160. qualified_override: Option<String>,
  161. }
  162. struct ValueScope<'t> {
  163. row: u32,
  164. node: Node<'t>,
  165. name: String,
  166. }
  167. struct Cand {
  168. from: u32,
  169. name: String,
  170. line: u32,
  171. column_byte: usize,
  172. row: usize,
  173. }
  174. pub struct Walker<'t> {
  175. src: &'t str,
  176. file_path: &'t str,
  177. line_starts: Vec<usize>,
  178. arena: Arena,
  179. tables: Tables,
  180. stack: Vec<Scope>,
  181. node_ids: Vec<String>,
  182. nodes_meta: Vec<NodeMeta>,
  183. defined_fn_names: HashSet<String>,
  184. imported_names: HashSet<String>,
  185. fn_ref_cands: Vec<Cand>,
  186. fs_values: HashMap<String, u32>,
  187. fs_value_counts: HashMap<String, u32>,
  188. value_scopes: Vec<ValueScope<'t>>,
  189. }
  190. pub fn extract(file_path: &str, source: &str) -> Result<EmitOut, String> {
  191. let grammar = crate::langs::grammar_for("kotlin").ok_or("no kotlin grammar")?;
  192. let t0 = std::time::Instant::now();
  193. let mut parser = Parser::new();
  194. parser
  195. .set_language(&grammar)
  196. .map_err(|e| format!("set_language(kotlin) failed: {e}"))?;
  197. let tree = parser
  198. .parse(source, None)
  199. .ok_or_else(|| "parser returned null tree".to_string())?;
  200. if tree.root_node().has_error() {
  201. // Includes the PHANTOM errors (complete CSTs with hasError set) and
  202. // every fun-interface misparse — trust the flag, wasm is canonical.
  203. return Err("defer: parse tree contains errors — wasm recovery is canonical".to_string());
  204. }
  205. let mut w = Walker {
  206. src: source,
  207. file_path,
  208. line_starts: util::line_starts(source),
  209. arena: Arena::default(),
  210. tables: Tables::default(),
  211. stack: Vec::new(),
  212. node_ids: Vec::new(),
  213. nodes_meta: Vec::new(),
  214. defined_fn_names: HashSet::new(),
  215. imported_names: HashSet::new(),
  216. fn_ref_cands: Vec::new(),
  217. fs_values: HashMap::new(),
  218. fs_value_counts: HashMap::new(),
  219. value_scopes: Vec::new(),
  220. };
  221. let line_count = source.bytes().filter(|b| *b == b'\n').count() as u32 + 1;
  222. let base_name = file_path.rsplit(['/', '\\']).next().unwrap_or(file_path);
  223. let mut flags = BoolFlags::default();
  224. flags.set(FLAG_IS_EXPORTED, false);
  225. let file_id = w.arena.put(&ids::file_node_id(file_path));
  226. let name_ref = w.arena.put(base_name);
  227. let qn_ref = w.arena.put(file_path);
  228. w.tables.push_node(&NodeRow {
  229. kind: node_kind_index("file").unwrap(),
  230. visibility: 0,
  231. flags,
  232. start_line: 1,
  233. end_line: line_count,
  234. start_column: 0,
  235. end_column: 0,
  236. name: name_ref,
  237. qualified_name: qn_ref,
  238. id: file_id,
  239. docstring: NONE_STR,
  240. signature: NONE_STR,
  241. decorators: NONE_STR,
  242. type_parameters: NONE_STR,
  243. return_type: NONE_STR,
  244. extra_json: NONE_STR,
  245. });
  246. w.node_ids.push(ids::file_node_id(file_path));
  247. w.nodes_meta.push(NodeMeta { kind: "file", name: base_name.to_string() });
  248. w.stack.push(Scope { row: 0, kind: "file", name: base_name.to_string() });
  249. // extractFilePackage: the FIRST package_header among root's direct named
  250. // children → namespace node (comment-glued extents included), pushed for
  251. // the whole walk.
  252. let root = tree.root_node();
  253. let mut pkg_pushed = false;
  254. for i in 0..root.named_child_count() {
  255. let Some(child) = root.named_child(i) else { continue };
  256. if child.kind() != "package_header" {
  257. continue;
  258. }
  259. let id_node = (0..child.named_child_count())
  260. .filter_map(|j| child.named_child(j))
  261. .find(|c| c.kind() == "identifier");
  262. if let Some(id_node) = id_node {
  263. let pkg = w.text(id_node).trim().to_string();
  264. if !pkg.is_empty() {
  265. if let Some(row) = w.create_node("namespace", &pkg, child, Extra::default()) {
  266. w.stack.push(Scope { row, kind: "namespace", name: pkg });
  267. pkg_pushed = true;
  268. }
  269. }
  270. }
  271. break;
  272. }
  273. w.visit_node(root);
  274. w.flush_fn_ref_candidates();
  275. w.flush_value_refs(root);
  276. if pkg_pushed {
  277. w.stack.pop();
  278. }
  279. w.stack.pop();
  280. let duration_ms = t0.elapsed().as_secs_f64() * 1000.0;
  281. let meta = build_meta(&w.tables, w.arena.len(), NONE_STR, duration_ms);
  282. Ok(EmitOut {
  283. meta,
  284. nodes: w.tables.nodes,
  285. edges: w.tables.edges,
  286. refs: w.tables.refs,
  287. arena: w.arena.into_vec(),
  288. })
  289. }
  290. impl<'t> Walker<'t> {
  291. fn text(&self, node: Node) -> &'t str {
  292. &self.src[node.byte_range()]
  293. }
  294. fn line_of(&self, node: Node) -> u32 {
  295. node.start_position().row as u32 + 1
  296. }
  297. fn col_of(&self, node: Node) -> u32 {
  298. util::col16(self.src, &self.line_starts, node.start_position().row, node.start_byte())
  299. }
  300. fn end_col_of(&self, node: Node) -> u32 {
  301. util::col16(self.src, &self.line_starts, node.end_position().row, node.end_byte())
  302. }
  303. fn top_row(&self) -> u32 {
  304. self.stack.last().map(|s| s.row).unwrap_or(0)
  305. }
  306. fn inside_class_like(&self) -> bool {
  307. self.stack
  308. .last()
  309. .map(|s| matches!(s.kind, "class" | "struct" | "interface" | "trait" | "enum" | "module"))
  310. .unwrap_or(false)
  311. }
  312. fn push_ref(&mut self, from_row: u32, name: &str, kind_code: u8, line: u32, column: u32) {
  313. let name_ref = self.arena.put(name);
  314. self.tables.push_ref(&RefRow {
  315. from_idx: from_row,
  316. kind: kind_code,
  317. line,
  318. column,
  319. reference_name: name_ref,
  320. candidates: NONE_STR,
  321. from_id_str: NONE_STR,
  322. });
  323. if kind_code == edge_kind_index("imports").unwrap() {
  324. if util::simple_name().is_match(name) {
  325. self.imported_names.insert(name.to_string());
  326. } else if let Some(c) = util::qualified_import().captures(name) {
  327. self.imported_names.insert(c[1].to_string());
  328. }
  329. }
  330. }
  331. fn push_ref_at(&mut self, from_row: u32, name: &str, kind_code: u8, node: Node) {
  332. self.push_ref(from_row, name, kind_code, self.line_of(node), self.col_of(node));
  333. }
  334. /// resolveBody (kotlin.ts:219): first ERROR child whose child(0) is `{`
  335. /// (fun-interface parent body — unreachable post-defer, kept for
  336. /// contract), else first function_body | class_body | enum_class_body.
  337. fn resolve_body(&self, node: Node<'t>) -> Option<Node<'t>> {
  338. for i in 0..node.named_child_count() {
  339. let Some(child) = node.named_child(i) else { continue };
  340. if child.kind() == "ERROR" {
  341. if let Some(first) = child.child(0) {
  342. if first.kind() == "{" {
  343. return Some(child);
  344. }
  345. }
  346. }
  347. if matches!(child.kind(), "function_body" | "class_body" | "enum_class_body") {
  348. return Some(child);
  349. }
  350. }
  351. None
  352. }
  353. // --- createNode ------------------------------------------------------------
  354. fn create_node(&mut self, kind: &'static str, name: &str, node: Node<'t>, extra: Extra) -> Option<u32> {
  355. if name.is_empty() {
  356. return None;
  357. }
  358. let start_line = self.line_of(node);
  359. let id = ids::node_id(self.file_path, kind, name, start_line);
  360. // endLine extension via resolveBody — LIVE for kotlin function/method
  361. // kinds (in-range for this grammar, so practically a no-op — but the
  362. // hook is part of the contract).
  363. let mut end_line = node.end_position().row as u32 + 1;
  364. if kind == "function" || kind == "method" {
  365. if let Some(body) = self.resolve_body(node) {
  366. let be = body.end_position().row as u32 + 1;
  367. if be > end_line {
  368. end_line = be;
  369. }
  370. }
  371. }
  372. let qualified = match &extra.qualified_override {
  373. Some(qn) => qn.clone(),
  374. None => {
  375. let mut parts: Vec<&str> = Vec::new();
  376. for s in &self.stack {
  377. if s.kind != "file" {
  378. parts.push(&s.name);
  379. }
  380. }
  381. let mut qn = parts.join("::");
  382. if !qn.is_empty() {
  383. qn.push_str("::");
  384. }
  385. qn.push_str(name);
  386. qn
  387. }
  388. };
  389. let mut flags = BoolFlags::default();
  390. if let Some(v) = extra.is_async {
  391. flags.set(FLAG_IS_ASYNC, v);
  392. }
  393. if let Some(v) = extra.is_static {
  394. flags.set(FLAG_IS_STATIC, v);
  395. }
  396. // extractModifiers merge (tree-sitter.ts:1355) — runs for EVERY
  397. // created node: expect/actual platform modifiers → decorators.
  398. let mods = self.extract_modifiers(node);
  399. let dec_ref: StrRef = match &mods {
  400. Some(list) if !list.is_empty() => self.arena.put_list(list),
  401. _ => NONE_STR,
  402. };
  403. let name_ref = self.arena.put(name);
  404. let qn_ref = self.arena.put(&qualified);
  405. let id_ref = self.arena.put(&id);
  406. let doc_ref = opt_str(&mut self.arena, extra.docstring.as_deref());
  407. let sig_ref = opt_str(&mut self.arena, extra.signature.as_deref());
  408. let ret_ref = opt_str(&mut self.arena, extra.return_type.as_deref());
  409. let row = self.tables.push_node(&NodeRow {
  410. kind: node_kind_index(kind).unwrap(),
  411. visibility: extra.visibility.unwrap_or(0),
  412. flags,
  413. start_line,
  414. end_line,
  415. start_column: self.col_of(node),
  416. end_column: self.end_col_of(node),
  417. name: name_ref,
  418. qualified_name: qn_ref,
  419. id: id_ref,
  420. docstring: doc_ref,
  421. signature: sig_ref,
  422. decorators: dec_ref,
  423. type_parameters: NONE_STR,
  424. return_type: ret_ref,
  425. extra_json: NONE_STR,
  426. });
  427. self.node_ids.push(id);
  428. self.nodes_meta.push(NodeMeta { kind, name: name.to_string() });
  429. let parent_row = self.top_row();
  430. self.tables.push_edge(&EdgeRow {
  431. source_idx: parent_row,
  432. target_idx: row,
  433. kind: edge_kind_index("contains").unwrap(),
  434. provenance: 0,
  435. line: NONE,
  436. column: NONE,
  437. metadata_json: NONE_STR,
  438. source_id_str: NONE_STR,
  439. target_id_str: NONE_STR,
  440. });
  441. if kind == "function" || kind == "method" {
  442. self.defined_fn_names.insert(name.to_string());
  443. }
  444. // captureValueRefScope — namespace parents are NOT accepted, so
  445. // packaged files' top-level constants are never targets (quirk).
  446. let target_kind_ok = kind == "constant" || kind == "variable";
  447. if target_kind_ok
  448. && util::utf16_len(name) >= 3
  449. && util::has_upper_or_underscore().is_match(name)
  450. {
  451. let parent_ok = self
  452. .stack
  453. .last()
  454. .map(|s| matches!(s.kind, "file" | "class" | "module" | "struct" | "enum"))
  455. .unwrap_or(false);
  456. if parent_ok {
  457. self.fs_values.insert(name.to_string(), row);
  458. *self.fs_value_counts.entry(name.to_string()).or_insert(0) += 1;
  459. }
  460. }
  461. if matches!(kind, "function" | "method" | "constant" | "variable") {
  462. self.value_scopes.push(ValueScope { row, node, name: name.to_string() });
  463. }
  464. Some(row)
  465. }
  466. // --- hooks (languages/kotlin.ts) ----------------------------------------------
  467. /// extractName — the zero-field grammar means the nameField lookup always
  468. /// misses; names come from the shared fallback scan (first direct
  469. /// identifier-family child; backtick names keep their backticks).
  470. fn extract_name(&self, node: Node) -> String {
  471. if let Some(name_node) = node.child_by_field_name("simple_identifier") {
  472. // nameField is a TYPE name used as a FIELD name — never resolves
  473. // (mirrored for shape; the grammar has zero fields).
  474. return self.text(name_node).to_string();
  475. }
  476. for i in 0..node.named_child_count() {
  477. if let Some(c) = node.named_child(i) {
  478. if matches!(c.kind(), "identifier" | "type_identifier" | "simple_identifier" | "constant") {
  479. return self.text(c).to_string();
  480. }
  481. }
  482. }
  483. "<anonymous>".to_string()
  484. }
  485. /// getVisibility: modifiers text includes public/private/protected/
  486. /// internal in that order; default PUBLIC. Text-includes semantics —
  487. /// annotation text inside modifiers can flip it (bug-for-bug).
  488. fn visibility_of(&self, node: Node) -> u8 {
  489. for i in 0..node.child_count() {
  490. let Some(child) = node.child(i) else { continue };
  491. if child.kind() == "modifiers" {
  492. let text = self.text(child);
  493. if text.contains("public") {
  494. return 1;
  495. }
  496. if text.contains("private") {
  497. return 2;
  498. }
  499. if text.contains("protected") {
  500. return 3;
  501. }
  502. if text.contains("internal") {
  503. return 4;
  504. }
  505. }
  506. }
  507. 1 // Kotlin defaults to public
  508. }
  509. /// isAsync: modifiers text includes 'suspend' (text-includes false
  510. /// positive on `@suspendMarker` annotations — preserve).
  511. fn is_async(&self, node: Node) -> bool {
  512. (0..node.child_count())
  513. .filter_map(|i| node.child(i))
  514. .any(|c| c.kind() == "modifiers" && self.text(c).contains("suspend"))
  515. }
  516. /// extractKotlinReturnType — positional: the first user_type/nullable_type
  517. /// AFTER function_value_parameters; function_body/type_constraints first →
  518. /// None; Unit/Nothing → None; `: T` generic params leak (preserve).
  519. fn return_type_of(&self, node: Node) -> Option<String> {
  520. let mut seen_params = false;
  521. for i in 0..node.named_child_count() {
  522. let Some(child) = node.named_child(i) else { continue };
  523. if child.kind() == "function_value_parameters" {
  524. seen_params = true;
  525. continue;
  526. }
  527. if !seen_params {
  528. continue;
  529. }
  530. if matches!(child.kind(), "function_body" | "type_constraints") {
  531. return None;
  532. }
  533. if matches!(child.kind(), "user_type" | "nullable_type") {
  534. let ut = if child.kind() == "nullable_type" {
  535. (0..child.named_child_count())
  536. .filter_map(|j| child.named_child(j))
  537. .find(|c| c.kind() == "user_type")
  538. .unwrap_or(child)
  539. } else {
  540. child
  541. };
  542. let type_id = (0..ut.named_child_count())
  543. .filter_map(|j| ut.named_child(j))
  544. .find(|c| c.kind() == "type_identifier");
  545. let name = self.text(type_id.unwrap_or(ut)).trim();
  546. if name.is_empty() || !ascii_ident_re().is_match(name) {
  547. return None;
  548. }
  549. if matches!(name, "Unit" | "Nothing") {
  550. return None;
  551. }
  552. return Some(name.to_string());
  553. }
  554. }
  555. None
  556. }
  557. /// getReceiverType — extension functions: the last user_type BEFORE a `.`
  558. /// child; its FIRST type_identifier's text (qualified receivers take the
  559. /// FIRST segment — the `com::qext` bug, preserve).
  560. fn receiver_type_of(&self, node: Node<'t>) -> Option<String> {
  561. let mut found_user_type: Option<Node> = None;
  562. for i in 0..node.child_count() {
  563. let Some(child) = node.child(i) else { continue };
  564. match child.kind() {
  565. "user_type" => found_user_type = Some(child),
  566. "." => {
  567. if let Some(ut) = found_user_type {
  568. let type_id = (0..ut.named_child_count())
  569. .filter_map(|j| ut.named_child(j))
  570. .find(|c| c.kind() == "type_identifier");
  571. return Some(self.text(type_id.unwrap_or(ut)).to_string());
  572. }
  573. }
  574. "simple_identifier" | "function_value_parameters" => break,
  575. _ => {}
  576. }
  577. }
  578. None
  579. }
  580. /// extractModifiers — expect/actual platform modifiers, matched by NODE
  581. /// TYPE (never text), in order. Runs inside create_node for every node.
  582. fn extract_modifiers(&self, node: Node) -> Option<Vec<String>> {
  583. let mut mods: Vec<String> = Vec::new();
  584. for i in 0..node.child_count() {
  585. let Some(child) = node.child(i) else { continue };
  586. if child.kind() != "modifiers" {
  587. continue;
  588. }
  589. for j in 0..child.child_count() {
  590. let Some(pm) = child.child(j) else { continue };
  591. if pm.kind() != "platform_modifier" {
  592. continue;
  593. }
  594. for k in 0..pm.child_count() {
  595. let Some(kw) = pm.child(k) else { continue };
  596. if matches!(kw.kind(), "expect" | "actual") {
  597. mods.push(kw.kind().to_string());
  598. }
  599. }
  600. }
  601. }
  602. if mods.is_empty() { None } else { Some(mods) }
  603. }
  604. // --- the visitNode hook (property branch ONLY — fun-interface recovery is
  605. // defer-shielded and not ported) ------------------------------------------------
  606. /// A property's node kind, or None when the declaration mints no node at
  607. /// all: destructuring, an unreadable name, or a local (inside a function
  608. /// body / `init` block / lambda / accessor). Kind by enclosing scope — a
  609. /// singleton `object` / `companion object` (and a top-level property) holds
  610. /// SHARED values (`val`→constant, `var`→variable, the Scala-object rule; a
  611. /// `const val` is just a val); a class/interface/enum instance `val`/`var`
  612. /// is per-instance state → `field`.
  613. fn property_kind(&self, node: Node<'t>) -> Option<&'static str> {
  614. let var_decl = (0..node.named_child_count())
  615. .filter_map(|i| node.named_child(i))
  616. .find(|c| c.kind() == "variable_declaration")?;
  617. let name_node = (0..var_decl.named_child_count())
  618. .filter_map(|i| var_decl.named_child(i))
  619. .find(|c| c.kind() == "simple_identifier")?;
  620. if self.text(name_node).is_empty() {
  621. return None;
  622. }
  623. let mut scope: &str = "const";
  624. let mut p = node.parent();
  625. while let Some(pn) = p {
  626. match pn.kind() {
  627. "function_body" | "function_declaration" | "lambda_literal"
  628. | "anonymous_initializer" | "control_structure_body" | "getter" | "setter" => {
  629. scope = "local";
  630. break;
  631. }
  632. "companion_object" | "object_declaration" => {
  633. scope = "const";
  634. break;
  635. }
  636. "class_declaration" => {
  637. scope = "instance";
  638. break;
  639. }
  640. _ => {}
  641. }
  642. p = pn.parent();
  643. }
  644. if scope == "local" {
  645. return None;
  646. }
  647. let binding = (0..node.named_child_count())
  648. .filter_map(|i| node.named_child(i))
  649. .find(|c| c.kind() == "binding_pattern_kind");
  650. let is_val = binding.map(|b| self.text(b) == "val").unwrap_or(false);
  651. Some(if scope == "instance" {
  652. "field"
  653. } else if is_val {
  654. "constant"
  655. } else {
  656. "variable"
  657. })
  658. }
  659. fn try_visit_hook(&mut self, node: Node<'t>) -> bool {
  660. // An own-line accessor already walked by its owning property below. The
  661. // ownership test re-derives the property's kind rather than remembering
  662. // it: a destructured or local declaration mints no node, so its
  663. // accessors were NOT consumed and must keep falling through.
  664. if matches!(node.kind(), "getter" | "setter") {
  665. return accessor_owner(node)
  666. .and_then(|owner| self.property_kind(owner))
  667. .is_some();
  668. }
  669. if node.kind() != "property_declaration" {
  670. return false;
  671. }
  672. let var_decl = (0..node.named_child_count())
  673. .filter_map(|i| node.named_child(i))
  674. .find(|c| c.kind() == "variable_declaration");
  675. let name_node = var_decl.and_then(|vd| {
  676. (0..vd.named_child_count())
  677. .filter_map(|i| vd.named_child(i))
  678. .find(|c| c.kind() == "simple_identifier")
  679. });
  680. // Destructuring (`val (a, b) = makePair()`): NEITHER arm mints a symbol
  681. // for the destructured names — declining just routes the node to
  682. // extractField/extractVariable, which both find nothing for kotlin and
  683. // end in the same fn-ref scan. But the RHS is CODE, and it was vanishing
  684. // whole. Consume the node here and walk it at the ENCLOSING scope (no
  685. // symbol of its own to attribute to).
  686. let Some(name_node) = name_node else {
  687. for init in property_initializers(node) {
  688. self.visit_function_body(init);
  689. }
  690. return true;
  691. };
  692. let name = self.text(name_node).to_string();
  693. if name.is_empty() {
  694. return false;
  695. }
  696. let Some(kind) = self.property_kind(node) else {
  697. // A local — no node is minted, but the initializer is still code.
  698. // Walk it at the ENCLOSING scope: an `init { }` block's
  699. // `val q = load()` is the CLASS calling load, and it used to
  700. // disappear entirely (only the block's bare statements survived).
  701. for init in property_initializers(node) {
  702. self.visit_function_body(init);
  703. }
  704. return true;
  705. };
  706. // The `type`-field signature read is dead (zero fields) → signature
  707. // undefined; NO docstring/visibility/isStatic — the modifiers merge in
  708. // create_node still decorates expect/actual properties.
  709. let row = self.create_node(kind, &name, node, Extra::default());
  710. // Walk the initializer ATTRIBUTED to the declared symbol (#693, the Go
  711. // fix, ported): without this the subtree is only fn-ref-scanned, so a
  712. // lambda / SAM / object initializer (`val cb = Runnable { target() }` —
  713. // the idiomatic Android callback field) contributed NO call edge at all.
  714. // The property also OWNS any accessor written on its own line, which the
  715. // grammar makes a following SIBLING rather than a child; those bodies
  716. // used to attribute to the enclosing class.
  717. if let Some(row) = row {
  718. self.stack.push(Scope { row, kind, name: name.clone() });
  719. for init in property_initializers(node) {
  720. self.visit_function_body(init);
  721. }
  722. for acc in following_accessors(node) {
  723. self.visit_function_body(acc);
  724. }
  725. self.stack.pop();
  726. }
  727. true
  728. }
  729. // --- the dispatcher (visitNode, Kotlin-relevant branches) -----------------------
  730. fn visit_node(&mut self, node: Node<'t>) {
  731. stack_guard!();
  732. if self.try_visit_hook(node) {
  733. self.scan_fn_ref_subtree(node, 0);
  734. return;
  735. }
  736. let kind = node.kind();
  737. let mut skip_children = false;
  738. self.maybe_capture_fn_refs(node);
  739. if kind == "function_declaration" {
  740. if self.inside_class_like() {
  741. self.extract_method(node);
  742. } else {
  743. self.extract_function(node);
  744. }
  745. skip_children = true;
  746. } else if kind == "class_declaration" {
  747. // classifyClassNode: `interface`/`enum` keyword children.
  748. let mut classified = "class";
  749. for i in 0..node.child_count() {
  750. if let Some(c) = node.child(i) {
  751. if c.kind() == "interface" {
  752. classified = "interface";
  753. break;
  754. }
  755. if c.kind() == "enum" {
  756. classified = "enum";
  757. break;
  758. }
  759. }
  760. }
  761. match classified {
  762. "interface" => self.extract_interface(node),
  763. "enum" => self.extract_enum(node),
  764. _ => self.extract_class(node),
  765. }
  766. skip_children = true;
  767. } else if kind == "object_declaration" {
  768. // extraClassNodeTypes → extractClass → kind `class`.
  769. self.extract_class(node);
  770. skip_children = true;
  771. } else if kind == "type_alias" {
  772. skip_children = self.extract_type_alias(node);
  773. } else if kind == "property_declaration" {
  774. // Hook-declined destructuring: extractField/extractVariable both
  775. // find no matching children for kotlin — NOTHING minted, RHS
  776. // invisible; candidates-only scan.
  777. self.scan_fn_ref_subtree(node, 0);
  778. skip_children = true;
  779. } else if kind == "import_header" {
  780. self.extract_import(node);
  781. } else if kind == "call_expression" {
  782. self.extract_call(node);
  783. }
  784. // companion_object, anonymous_initializer, secondary_constructor,
  785. // getter/setter siblings, file_annotation, object_literal, if/when at
  786. // top level: no branch — recursed (calls attribute to the stack top).
  787. if !skip_children {
  788. for i in 0..node.named_child_count() {
  789. if let Some(c) = node.named_child(i) {
  790. self.visit_node(c);
  791. }
  792. }
  793. }
  794. }
  795. // --- visitFunctionBody ----------------------------------------------------------
  796. fn visit_function_body(&mut self, body: Node<'t>) {
  797. stack_guard!();
  798. self.visit_for_calls_and_structure(body);
  799. }
  800. fn visit_for_calls_and_structure(&mut self, node: Node<'t>) {
  801. stack_guard!();
  802. let kind = node.kind();
  803. self.maybe_capture_fn_refs(node);
  804. if kind == "call_expression" {
  805. self.extract_call(node);
  806. }
  807. // (INSTANTIATION_KINDS has no kotlin members; extractBareCall absent.)
  808. self.extract_static_member_ref(node);
  809. if kind == "function_declaration" {
  810. let name = self.extract_name(node);
  811. if name != "<anonymous>" {
  812. // extractFunction diverts receiver-bearing nested fns to
  813. // extractMethod itself.
  814. self.extract_function(node);
  815. return;
  816. }
  817. }
  818. if kind == "class_declaration" {
  819. let mut classified = "class";
  820. for i in 0..node.child_count() {
  821. if let Some(c) = node.child(i) {
  822. if c.kind() == "interface" {
  823. classified = "interface";
  824. break;
  825. }
  826. if c.kind() == "enum" {
  827. classified = "enum";
  828. break;
  829. }
  830. }
  831. }
  832. match classified {
  833. "interface" => self.extract_interface(node),
  834. "enum" => self.extract_enum(node),
  835. _ => self.extract_class(node),
  836. }
  837. return;
  838. }
  839. // object_declaration is NOT dispatched here — a body-local object's
  840. // `fun`s hit the function branch above and leak out as FUNCTIONS
  841. // under the enclosing fn; its properties mint nothing (quirk).
  842. for i in 0..node.named_child_count() {
  843. if let Some(c) = node.named_child(i) {
  844. self.visit_for_calls_and_structure(c);
  845. }
  846. }
  847. }
  848. // --- extractors ------------------------------------------------------------------
  849. fn extract_function(&mut self, node: Node<'t>) {
  850. stack_guard!();
  851. // getReceiverType short-circuit (1522) — extension fns at any scope.
  852. if self.receiver_type_of(node).is_some() {
  853. self.extract_method(node);
  854. return;
  855. }
  856. let name = self.extract_name(node);
  857. if name == "<anonymous>" {
  858. if let Some(body) = self.resolve_body(node) {
  859. self.visit_function_body(body);
  860. }
  861. return;
  862. }
  863. let extra = Extra {
  864. docstring: preceding_docstring(node, self.src),
  865. signature: None, // dead hook (zero fields)
  866. visibility: Some(self.visibility_of(node)),
  867. is_async: Some(self.is_async(node)),
  868. is_static: Some(false), // kotlin isStatic is always false
  869. return_type: self.return_type_of(node),
  870. ..Extra::default()
  871. };
  872. let Some(row) = self.create_node("function", &name, node, extra) else { return };
  873. // extractTypeAnnotations: the generic path's field lookups all miss
  874. // (zero fields) — kotlin emits ZERO type-annotation refs.
  875. self.extract_decorators_for(node, row);
  876. self.stack.push(Scope { row, kind: "function", name });
  877. if let Some(body) = self.resolve_body(node) {
  878. self.visit_function_body(body);
  879. }
  880. self.stack.pop();
  881. }
  882. fn extract_method(&mut self, node: Node<'t>) {
  883. stack_guard!();
  884. let receiver = self.receiver_type_of(node);
  885. let name = self.extract_name(node);
  886. let qualified_override = receiver.as_ref().map(|r| format!("{r}::{name}"));
  887. let extra = Extra {
  888. docstring: preceding_docstring(node, self.src),
  889. signature: None,
  890. visibility: Some(self.visibility_of(node)),
  891. is_async: Some(self.is_async(node)),
  892. is_static: Some(false),
  893. return_type: self.return_type_of(node),
  894. qualified_override,
  895. };
  896. let Some(row) = self.create_node("method", &name, node, extra) else { return };
  897. // Owner-contains fallback (1799): receiver present, not class-like →
  898. // the FIRST same-file node named like the receiver with kind ∈
  899. // {struct, class, enum, trait} (interface EXCLUDED; source-order
  900. // dependent — both quirks preserved). Additive to the normal edge.
  901. if let Some(recv) = &receiver {
  902. if !self.inside_class_like() {
  903. let owner = self
  904. .nodes_meta
  905. .iter()
  906. .position(|m| {
  907. m.name == *recv && matches!(m.kind, "struct" | "class" | "enum" | "trait")
  908. })
  909. .map(|i| i as u32);
  910. if let Some(owner_row) = owner {
  911. self.tables.push_edge(&EdgeRow {
  912. source_idx: owner_row,
  913. target_idx: row,
  914. kind: edge_kind_index("contains").unwrap(),
  915. provenance: 0,
  916. line: NONE,
  917. column: NONE,
  918. metadata_json: NONE_STR,
  919. source_id_str: NONE_STR,
  920. target_id_str: NONE_STR,
  921. });
  922. }
  923. }
  924. }
  925. // Type annotations: dead. Decorators: live.
  926. self.extract_decorators_for(node, row);
  927. self.stack.push(Scope { row, kind: "method", name });
  928. if let Some(body) = self.resolve_body(node) {
  929. self.visit_function_body(body);
  930. }
  931. self.stack.pop();
  932. }
  933. fn extract_class(&mut self, node: Node<'t>) {
  934. stack_guard!();
  935. let resolved_body = self.resolve_body(node);
  936. let name = self.extract_name(node);
  937. let extra = Extra {
  938. docstring: preceding_docstring(node, self.src),
  939. visibility: Some(self.visibility_of(node)),
  940. ..Extra::default()
  941. };
  942. let Some(row) = self.create_node("class", &name, node, extra) else { return };
  943. self.extract_inheritance(node, row);
  944. // primaryCtor refs: csharp-gated no-op.
  945. self.extract_decorators_for(node, row);
  946. self.stack.push(Scope { row, kind: "class", name });
  947. // Bodied: ONLY class_body children (primary-ctor properties/defaults
  948. // invisible). Bodiless: the class node itself → header children
  949. // visited → ctor default-value + super-arg calls attribute to the
  950. // CLASS (the asymmetry, pinned).
  951. let body = resolved_body.unwrap_or(node);
  952. for i in 0..body.named_child_count() {
  953. if let Some(c) = body.named_child(i) {
  954. self.visit_node(c);
  955. }
  956. }
  957. self.stack.pop();
  958. }
  959. fn extract_interface(&mut self, node: Node<'t>) {
  960. stack_guard!();
  961. let name = self.extract_name(node);
  962. let extra = Extra {
  963. docstring: preceding_docstring(node, self.src),
  964. ..Extra::default() // NO visibility
  965. };
  966. let Some(row) = self.create_node("interface", &name, node, extra) else { return };
  967. self.extract_inheritance(node, row);
  968. self.stack.push(Scope { row, kind: "interface", name });
  969. let body = self.resolve_body(node).unwrap_or(node);
  970. for i in 0..body.named_child_count() {
  971. if let Some(c) = body.named_child(i) {
  972. self.visit_node(c);
  973. }
  974. }
  975. self.stack.pop();
  976. }
  977. fn extract_enum(&mut self, node: Node<'t>) {
  978. stack_guard!();
  979. let Some(body) = self.resolve_body(node) else { return };
  980. let name = self.extract_name(node);
  981. let extra = Extra {
  982. docstring: preceding_docstring(node, self.src),
  983. visibility: Some(self.visibility_of(node)),
  984. ..Extra::default()
  985. };
  986. let Some(row) = self.create_node("enum", &name, node, extra) else { return };
  987. self.extract_inheritance(node, row);
  988. self.stack.push(Scope { row, kind: "enum", name });
  989. for i in 0..body.named_child_count() {
  990. let Some(child) = body.named_child(i) else { continue };
  991. if child.kind() == "enum_entry" {
  992. self.extract_enum_members(child);
  993. } else {
  994. self.visit_node(child);
  995. }
  996. }
  997. self.stack.pop();
  998. }
  999. fn extract_enum_members(&mut self, node: Node<'t>) {
  1000. // name field → null (zero fields) → the identifier-children scan: one
  1001. // enum_member per direct simple_identifier, positioned AT the
  1002. // identifier. Entry value_arguments and entry class_bodies (override
  1003. // methods!) are never visited — invisible (quirk).
  1004. for i in 0..node.named_child_count() {
  1005. let Some(child) = node.named_child(i) else { continue };
  1006. if matches!(child.kind(), "simple_identifier" | "identifier" | "property_identifier") {
  1007. let name = self.text(child).to_string();
  1008. self.create_node("enum_member", &name, child, Extra::default());
  1009. }
  1010. }
  1011. }
  1012. /// extractTypeAlias — plain node; the alias-value ref walk reads the
  1013. /// `value` FIELD → null (zero fields) → NO refs. Returns false →
  1014. /// children re-visited (harmless).
  1015. fn extract_type_alias(&mut self, node: Node<'t>) -> bool {
  1016. let name = self.extract_name(node);
  1017. if name == "<anonymous>" {
  1018. return false;
  1019. }
  1020. let extra = Extra {
  1021. docstring: preceding_docstring(node, self.src),
  1022. ..Extra::default()
  1023. };
  1024. self.create_node("type_alias", &name, node, extra);
  1025. false
  1026. }
  1027. fn extract_import(&mut self, node: Node<'t>) {
  1028. // Comment-gluing: the header's extent (and thus the signature) can
  1029. // include trailing comment lines — the trimmed FULL text is the
  1030. // signature; the ref stays at the header start.
  1031. let import_text = self.text(node).trim().to_string();
  1032. let identifier = (0..node.named_child_count())
  1033. .filter_map(|i| node.named_child(i))
  1034. .find(|c| c.kind() == "identifier");
  1035. let Some(identifier) = identifier else { return };
  1036. let module_name = self.text(identifier).to_string();
  1037. if module_name.is_empty() {
  1038. return;
  1039. }
  1040. self.create_node(
  1041. "import",
  1042. &module_name,
  1043. node,
  1044. Extra { signature: Some(import_text), ..Extra::default() },
  1045. );
  1046. let parent = self.top_row();
  1047. self.push_ref_at(parent, &module_name.clone(), edge_kind_index("imports").unwrap(), node);
  1048. }
  1049. /// extractCall — the kotlin paths: navigation member branch (+ the #750
  1050. /// re-encode) and the raw-text else (paren-then-lambda / glued-invoke
  1051. /// garbage preserved).
  1052. fn extract_call(&mut self, node: Node<'t>) {
  1053. if self.stack.is_empty() {
  1054. return;
  1055. }
  1056. let caller = self.top_row();
  1057. let func = node
  1058. .child_by_field_name("function")
  1059. .or_else(|| node.named_child(0));
  1060. let Some(func) = func else { return };
  1061. let mut callee_name = String::new();
  1062. if func.kind() == "navigation_expression" {
  1063. let property = func
  1064. .child_by_field_name("property")
  1065. .or_else(|| func.child_by_field_name("field"))
  1066. .or_else(|| {
  1067. let c1 = func.named_child(1);
  1068. match c1 {
  1069. Some(c) if c.kind() == "navigation_suffix" => (0..c.named_child_count())
  1070. .filter_map(|i| c.named_child(i))
  1071. .find(|g| g.kind() == "simple_identifier")
  1072. .or(Some(c)),
  1073. other => other,
  1074. }
  1075. });
  1076. if let Some(property) = property {
  1077. let method_name = self.text(property);
  1078. let receiver = func
  1079. .child_by_field_name("object")
  1080. .or_else(|| func.child_by_field_name("operand"))
  1081. .or_else(|| func.child_by_field_name("argument"))
  1082. .or_else(|| func.named_child(0));
  1083. if let Some(r) = receiver {
  1084. if is_literal_receiver(r.kind()) {
  1085. return; // `"literal".uppercase()` / `5.toString()`
  1086. }
  1087. }
  1088. let recv_ident = receiver.filter(|r| {
  1089. matches!(r.kind(), "identifier" | "simple_identifier" | "field_identifier")
  1090. });
  1091. if let Some(r) = recv_ident {
  1092. let receiver_name = self.text(r);
  1093. if matches!(receiver_name, "self" | "this" | "cls" | "super") {
  1094. callee_name = method_name.to_string();
  1095. } else {
  1096. callee_name = format!("{receiver_name}.{method_name}");
  1097. }
  1098. } else if receiver.map(|r| r.kind() == "call_expression").unwrap_or(false) {
  1099. // #750 kotlin re-encode: innerNav = receiver.namedChild(0)
  1100. // (NOT a function field), ws-stripped, /^[A-Z]/ gate.
  1101. let inner = receiver.unwrap().named_child(0);
  1102. let inner_callee =
  1103. inner.map(|n| strip_js_ws(self.text(n))).unwrap_or_default();
  1104. let reencode = inner_callee
  1105. .as_bytes()
  1106. .first()
  1107. .map(|b| b.is_ascii_uppercase())
  1108. .unwrap_or(false);
  1109. callee_name = if reencode {
  1110. format!("{inner_callee}().{method_name}")
  1111. } else {
  1112. method_name.to_string()
  1113. };
  1114. } else {
  1115. // this_expression / super_expression / 2-hop nav /
  1116. // postfix `!!` / parenthesized → bare method name.
  1117. callee_name = method_name.to_string();
  1118. }
  1119. }
  1120. } else {
  1121. // Raw func text: bare `helper`, constructor `WidgetK` (NO
  1122. // instantiates ever), backticked names verbatim, the
  1123. // paren-then-lambda `trailing()` and glued-invoke chains
  1124. // byte-for-byte.
  1125. callee_name = self.text(func).to_string();
  1126. }
  1127. if !callee_name.is_empty() {
  1128. if let Some(c) = util::paren_conversion().captures(&callee_name) {
  1129. callee_name = c[1].to_string();
  1130. }
  1131. self.push_ref_at(caller, &callee_name.clone(), edge_kind_index("calls").unwrap(), node);
  1132. }
  1133. }
  1134. /// extractStaticMemberRef — navigation_expression value reads, body
  1135. /// walker only (assignment WRITES parse as directly_assignable_expression
  1136. /// — not a member-access kind — and emit nothing).
  1137. fn extract_static_member_ref(&mut self, node: Node<'t>) {
  1138. if node.kind() != "navigation_expression" {
  1139. return;
  1140. }
  1141. if self.stack.is_empty() {
  1142. return;
  1143. }
  1144. let owner = self.top_row();
  1145. if let Some(parent) = node.parent() {
  1146. if parent.kind() == "call_expression" {
  1147. let callee = parent
  1148. .child_by_field_name("function")
  1149. .or_else(|| parent.child_by_field_name("method"))
  1150. .or_else(|| parent.named_child(0));
  1151. if let Some(callee) = callee {
  1152. if callee.start_byte() == node.start_byte() {
  1153. return;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. let recv = node
  1159. .child_by_field_name("object")
  1160. .or_else(|| node.child_by_field_name("expression"))
  1161. .or_else(|| node.child_by_field_name("scope"))
  1162. .or_else(|| node.named_child(0));
  1163. let Some(recv) = recv else { return };
  1164. if matches!(
  1165. recv.kind(),
  1166. "identifier" | "type_identifier" | "simple_identifier" | "name" | "scoped_type_identifier"
  1167. ) {
  1168. let text = self.text(recv);
  1169. if capitalized_re().is_match(text) {
  1170. self.push_ref_at(owner, &text.to_string(), edge_kind_index("references").unwrap(), recv);
  1171. }
  1172. }
  1173. }
  1174. /// extractInheritance — delegation_specifier: user_type ?? its
  1175. /// constructor_invocation's user_type → FIRST type_identifier → ONE
  1176. /// `extends` ref at the typeId (interfaces ride extends too; qualified
  1177. /// supertypes take the FIRST segment — `com`; `by`-delegation emits
  1178. /// NOTHING).
  1179. fn extract_inheritance(&mut self, node: Node<'t>, class_row: u32) {
  1180. let extends_kind = edge_kind_index("extends").unwrap();
  1181. for i in 0..node.named_child_count() {
  1182. let Some(child) = node.named_child(i) else { continue };
  1183. if child.kind() != "delegation_specifier" {
  1184. continue;
  1185. }
  1186. let user_type = (0..child.named_child_count())
  1187. .filter_map(|j| child.named_child(j))
  1188. .find(|c| c.kind() == "user_type");
  1189. let ctor_inv = (0..child.named_child_count())
  1190. .filter_map(|j| child.named_child(j))
  1191. .find(|c| c.kind() == "constructor_invocation");
  1192. let target = user_type.or(ctor_inv);
  1193. let Some(target) = target else { continue };
  1194. let type_id: Node = if target.kind() == "user_type" {
  1195. (0..target.named_child_count())
  1196. .filter_map(|j| target.named_child(j))
  1197. .find(|c| c.kind() == "type_identifier")
  1198. .unwrap_or(target)
  1199. } else {
  1200. // constructor_invocation → its user_type → first type_identifier
  1201. let ut = (0..target.named_child_count())
  1202. .filter_map(|j| target.named_child(j))
  1203. .find(|c| c.kind() == "user_type");
  1204. match ut {
  1205. Some(ut) => (0..ut.named_child_count())
  1206. .filter_map(|j| ut.named_child(j))
  1207. .find(|c| c.kind() == "type_identifier")
  1208. .unwrap_or(ut),
  1209. None => target,
  1210. }
  1211. };
  1212. let name = self.text(type_id).to_string();
  1213. self.push_ref_at(class_row, &name, extends_kind, type_id);
  1214. }
  1215. }
  1216. /// extractDecoratorsFor — kotlin annotations inside `modifiers`:
  1217. /// `@Marker` (user_type child) → decorates ref; `@Anno(args)`
  1218. /// (constructor_invocation) → NOTHING. Runs for functions/methods/classes
  1219. /// only (hook properties never call it).
  1220. fn extract_decorators_for(&mut self, decl: Node<'t>, decorated_row: u32) {
  1221. for i in 0..decl.named_child_count() {
  1222. let Some(child) = decl.named_child(i) else { continue };
  1223. self.consider_decorator(child, decorated_row);
  1224. if child.kind() == "modifiers" {
  1225. for j in 0..child.named_child_count() {
  1226. if let Some(m) = child.named_child(j) {
  1227. self.consider_decorator(m, decorated_row);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. let Some(parent) = decl.parent() else { return };
  1233. let decl_start = decl.start_byte();
  1234. let mut decl_idx: isize = -1;
  1235. for i in 0..parent.named_child_count() {
  1236. if let Some(sib) = parent.named_child(i) {
  1237. if sib.start_byte() == decl_start {
  1238. decl_idx = i as isize;
  1239. break;
  1240. }
  1241. }
  1242. }
  1243. if decl_idx > 0 {
  1244. let mut j = decl_idx - 1;
  1245. while j >= 0 {
  1246. let Some(sib) = parent.named_child(j as usize) else {
  1247. j -= 1;
  1248. continue;
  1249. };
  1250. if !matches!(sib.kind(), "decorator" | "annotation" | "marker_annotation") {
  1251. break;
  1252. }
  1253. self.consider_decorator(sib, decorated_row);
  1254. j -= 1;
  1255. }
  1256. }
  1257. }
  1258. fn consider_decorator(&mut self, n: Node<'t>, decorated_row: u32) {
  1259. if !matches!(n.kind(), "decorator" | "annotation" | "marker_annotation" | "attribute") {
  1260. return;
  1261. }
  1262. let mut target: Option<Node> = None;
  1263. for i in 0..n.named_child_count() {
  1264. let Some(child) = n.named_child(i) else { continue };
  1265. if child.kind() == "call_expression" {
  1266. target = child.child_by_field_name("function").or_else(|| child.named_child(0));
  1267. if target.is_some() {
  1268. break;
  1269. }
  1270. }
  1271. if matches!(
  1272. child.kind(),
  1273. "identifier" | "member_expression" | "scoped_identifier" | "navigation_expression"
  1274. | "user_type" | "type_identifier"
  1275. ) {
  1276. target = Some(child);
  1277. break;
  1278. }
  1279. }
  1280. let Some(target) = target else { return };
  1281. let name = strip_generic_and_qualifier(self.text(target));
  1282. if name.is_empty() {
  1283. return;
  1284. }
  1285. self.push_ref_at(decorated_row, &name, edge_kind_index("decorates").unwrap(), n);
  1286. }
  1287. // --- function-as-value refs (KOTLIN_SPEC, function-ref.ts:240) ------------------
  1288. fn maybe_capture_fn_refs(&mut self, node: Node<'t>) {
  1289. enum Mode {
  1290. Args,
  1291. Rhs,
  1292. }
  1293. let mode = match node.kind() {
  1294. "value_arguments" => Mode::Args,
  1295. "assignment" => Mode::Rhs, // NO field — RHS = LAST named child
  1296. _ => return,
  1297. };
  1298. if self.stack.is_empty() {
  1299. return;
  1300. }
  1301. let from = self.top_row();
  1302. let mut values: Vec<Node> = Vec::new();
  1303. match mode {
  1304. Mode::Args => {
  1305. for i in 0..node.named_child_count() {
  1306. if let Some(c) = node.named_child(i) {
  1307. values.push(c);
  1308. }
  1309. }
  1310. }
  1311. Mode::Rhs => {
  1312. let rhs = if node.named_child_count() > 0 {
  1313. node.named_child(node.named_child_count() - 1)
  1314. } else {
  1315. None
  1316. };
  1317. if let Some(rhs) = rhs {
  1318. let lhs = node
  1319. .child_by_field_name("left")
  1320. .or_else(|| node.child_by_field_name("lhs"))
  1321. .or_else(|| node.child_by_field_name("target"))
  1322. .or_else(|| {
  1323. if node.named_child_count() >= 2 { node.named_child(0) } else { None }
  1324. });
  1325. let lhs_text = lhs.map(|l| self.text(l)).unwrap_or("");
  1326. let lhs_last = util::lhs_last_name()
  1327. .captures(lhs_text)
  1328. .and_then(|c| c.get(1))
  1329. .map(|m| m.as_str());
  1330. let rhs_text = self.text(rhs).trim();
  1331. if !(lhs_last.is_some() && lhs_last == Some(rhs_text)) {
  1332. values.push(rhs);
  1333. }
  1334. }
  1335. }
  1336. }
  1337. for v in values {
  1338. self.normalize_fn_ref_value(v, from, 0);
  1339. }
  1340. }
  1341. fn normalize_fn_ref_value(&mut self, v: Node<'t>, from: u32, depth: u32) {
  1342. stack_guard!();
  1343. if depth > 4 {
  1344. return;
  1345. }
  1346. match v.kind() {
  1347. // value_argument layer with NO field resolution (zero fields) —
  1348. // the label-forward skip is DEAD for kotlin; fan out namedChildren.
  1349. "value_argument" => {
  1350. for i in 0..v.named_child_count() {
  1351. if let Some(c) = v.named_child(i) {
  1352. self.normalize_fn_ref_value(c, from, depth + 1);
  1353. }
  1354. }
  1355. }
  1356. // `::topLevel` / `OtherClass::handle` — receiver = LAST
  1357. // type_identifier child, member = LAST simple_identifier child;
  1358. // `String::class` has no member (anon keyword) → nothing;
  1359. // lowercase receivers dropped by the CASE regex, not node type.
  1360. "callable_reference" => {
  1361. let mut receiver: Option<Node> = None;
  1362. let mut member: Option<Node> = None;
  1363. for i in 0..v.named_child_count() {
  1364. let Some(child) = v.named_child(i) else { continue };
  1365. if child.kind() == "type_identifier" {
  1366. receiver = Some(child);
  1367. }
  1368. if child.kind() == "simple_identifier" {
  1369. member = Some(child);
  1370. }
  1371. }
  1372. let Some(member) = member else { return };
  1373. let m = self.text(member);
  1374. match receiver {
  1375. None => self.push_fn_ref_cand(from, m, member),
  1376. Some(recv) => {
  1377. let recv_text = self.text(recv);
  1378. if recv_text.as_bytes().first().map(|b| b.is_ascii_uppercase()).unwrap_or(false) {
  1379. let name = format!("{recv_text}::{m}");
  1380. self.push_fn_ref_cand(from, &name, member);
  1381. }
  1382. }
  1383. }
  1384. }
  1385. // `this::caller` → this.<member> (class-scoped, always flushes).
  1386. "navigation_expression" => {
  1387. if !self.text(v).starts_with("this::") {
  1388. return;
  1389. }
  1390. for i in 0..v.named_child_count() {
  1391. let Some(child) = v.named_child(i) else { continue };
  1392. if child.kind() == "navigation_suffix" && self.text(child).starts_with("::") {
  1393. if child.named_child_count() > 0 {
  1394. if let Some(id) = child.named_child(child.named_child_count() - 1) {
  1395. let name = format!("this.{}", self.text(id));
  1396. self.push_fn_ref_cand(from, &name, id);
  1397. }
  1398. }
  1399. return;
  1400. }
  1401. }
  1402. }
  1403. _ => {}
  1404. }
  1405. }
  1406. fn push_fn_ref_cand(&mut self, from: u32, name: &str, node: Node) {
  1407. if name.is_empty() || is_stoplisted(name) {
  1408. return;
  1409. }
  1410. let p = node.start_position();
  1411. self.fn_ref_cands.push(Cand {
  1412. from,
  1413. name: name.to_string(),
  1414. line: p.row as u32 + 1,
  1415. column_byte: node.start_byte(),
  1416. row: p.row,
  1417. });
  1418. }
  1419. fn scan_fn_ref_subtree(&mut self, node: Node<'t>, depth: u32) {
  1420. stack_guard!();
  1421. if depth > 12 {
  1422. return;
  1423. }
  1424. // Halts at functionTypes (function_declaration) + the fixed list —
  1425. // lambda_literal halts (no captures inside `by lazy { }` under a
  1426. // hook-consumed property).
  1427. if depth > 0
  1428. && matches!(
  1429. node.kind(),
  1430. "function_declaration" | "arrow_function" | "function_expression" | "lambda_literal"
  1431. | "lambda_expression"
  1432. )
  1433. {
  1434. return;
  1435. }
  1436. self.maybe_capture_fn_refs(node);
  1437. for i in 0..node.named_child_count() {
  1438. if let Some(c) = node.named_child(i) {
  1439. self.scan_fn_ref_subtree(c, depth + 1);
  1440. }
  1441. }
  1442. }
  1443. fn flush_fn_ref_candidates(&mut self) {
  1444. let cands = std::mem::take(&mut self.fn_ref_cands);
  1445. if cands.is_empty() || util::is_generated_file(self.file_path) {
  1446. return;
  1447. }
  1448. let mut seen: HashSet<(String, String)> = HashSet::new();
  1449. for c in cands {
  1450. if !c.name.starts_with("this.")
  1451. && !c.name.contains("::")
  1452. && !self.defined_fn_names.contains(&c.name)
  1453. && !self.imported_names.contains(&c.name)
  1454. {
  1455. continue;
  1456. }
  1457. if !seen.insert((self.node_ids[c.from as usize].clone(), c.name.clone())) {
  1458. continue;
  1459. }
  1460. let column = util::col16(self.src, &self.line_starts, c.row, c.column_byte);
  1461. let name_ref = self.arena.put(&c.name);
  1462. self.tables.push_ref(&RefRow {
  1463. from_idx: c.from,
  1464. kind: FUNCTION_REF_CODE,
  1465. line: c.line,
  1466. column,
  1467. reference_name: name_ref,
  1468. candidates: NONE_STR,
  1469. from_id_str: NONE_STR,
  1470. });
  1471. }
  1472. }
  1473. // --- value references --------------------------------------------------------------
  1474. fn flush_value_refs(&mut self, root: Node<'t>) {
  1475. let scopes = std::mem::take(&mut self.value_scopes);
  1476. let mut targets = std::mem::take(&mut self.fs_values);
  1477. let counts = std::mem::take(&mut self.fs_value_counts);
  1478. if std::env::var("CODEGRAPH_VALUE_REFS").as_deref() == Ok("0") {
  1479. return;
  1480. }
  1481. if targets.is_empty() || scopes.is_empty() || util::is_generated_file(self.file_path) {
  1482. return;
  1483. }
  1484. // Shadow prune — kotlin cases: property_declaration (its
  1485. // variable_declaration's first simple_identifier; destructuring bumps
  1486. // nothing) AND the shared `assignment` case (the swift-sweep lesson —
  1487. // directly_assignable_expression children bump).
  1488. let mut decl_counts: HashMap<&str, u32> = HashMap::new();
  1489. let mut dstack: Vec<Node> = vec![root];
  1490. let mut dvisited = 0usize;
  1491. while let Some(n) = dstack.pop() {
  1492. if dvisited >= MAX_VALUE_REF_NODES {
  1493. break;
  1494. }
  1495. dvisited += 1;
  1496. if n.kind() == "assignment" {
  1497. let left = n
  1498. .child_by_field_name("left")
  1499. .or_else(|| n.child_by_field_name("pattern"))
  1500. .or_else(|| n.named_child(0));
  1501. if let Some(left) = left {
  1502. if left.kind() == "identifier" {
  1503. let nm = self.text(left);
  1504. if targets.contains_key(nm) {
  1505. *decl_counts.entry(nm).or_insert(0) += 1;
  1506. }
  1507. } else {
  1508. for i in 0..left.named_child_count() {
  1509. let Some(c) = left.named_child(i) else { continue };
  1510. if matches!(c.kind(), "identifier" | "simple_identifier") {
  1511. let nm = self.text(c);
  1512. if targets.contains_key(nm) {
  1513. *decl_counts.entry(nm).or_insert(0) += 1;
  1514. }
  1515. }
  1516. }
  1517. }
  1518. }
  1519. }
  1520. if n.kind() == "property_declaration" {
  1521. let vd = (0..n.named_child_count())
  1522. .filter_map(|i| n.named_child(i))
  1523. .find(|c| c.kind() == "variable_declaration");
  1524. if let Some(vd) = vd {
  1525. let id = (0..vd.named_child_count())
  1526. .filter_map(|i| vd.named_child(i))
  1527. .find(|c| c.kind() == "simple_identifier");
  1528. if let Some(id) = id {
  1529. let nm = self.text(id);
  1530. if targets.contains_key(nm) {
  1531. *decl_counts.entry(nm).or_insert(0) += 1;
  1532. }
  1533. }
  1534. }
  1535. // (the Swift name-field half of the shared case is a null
  1536. // path for kotlin — variable_declaration always present)
  1537. }
  1538. for i in 0..n.named_child_count() {
  1539. if let Some(c) = n.named_child(i) {
  1540. dstack.push(c);
  1541. }
  1542. }
  1543. }
  1544. let shadowed: Vec<String> = decl_counts
  1545. .iter()
  1546. .filter(|(nm, c)| **c > counts.get(**nm).copied().unwrap_or(1))
  1547. .map(|(nm, _)| nm.to_string())
  1548. .collect();
  1549. for nm in shadowed {
  1550. targets.remove(&nm);
  1551. }
  1552. if targets.is_empty() {
  1553. return;
  1554. }
  1555. let refs_kind = edge_kind_index("references").unwrap();
  1556. for scope in &scopes {
  1557. let mut seen: HashSet<&str> = HashSet::new();
  1558. let mut stack: Vec<Node> = vec![scope.node];
  1559. let mut visited = 0usize;
  1560. while let Some(n) = stack.pop() {
  1561. if visited >= MAX_VALUE_REF_NODES {
  1562. break;
  1563. }
  1564. visited += 1;
  1565. // simple_identifier is kotlin's live reader kind —
  1566. // `${TARGET}` interpolations read, `$TARGET`
  1567. // (interpolated_identifier) doesn't.
  1568. if matches!(n.kind(), "identifier" | "constant" | "name" | "simple_identifier") {
  1569. let ref_name = self.text(n);
  1570. if let Some(&target_row) = targets.get(ref_name) {
  1571. let target_id = self.node_ids[target_row as usize].as_str();
  1572. if target_id != self.node_ids[scope.row as usize]
  1573. && ref_name != scope.name
  1574. && !seen.contains(&target_id)
  1575. {
  1576. seen.insert(target_id);
  1577. let meta = self.arena.put(r#"{"valueRef":true}"#);
  1578. self.tables.push_edge(&EdgeRow {
  1579. source_idx: scope.row,
  1580. target_idx: target_row,
  1581. kind: refs_kind,
  1582. provenance: 0,
  1583. line: NONE,
  1584. column: NONE,
  1585. metadata_json: meta,
  1586. source_id_str: NONE_STR,
  1587. target_id_str: NONE_STR,
  1588. });
  1589. }
  1590. }
  1591. }
  1592. for i in 0..n.named_child_count() {
  1593. if let Some(c) = n.named_child(i) {
  1594. stack.push(c);
  1595. }
  1596. }
  1597. }
  1598. }
  1599. }
  1600. }
  1601. /// The shared decorator-name normalization.
  1602. fn strip_generic_and_qualifier(raw: &str) -> String {
  1603. let mut name = raw.to_string();
  1604. if let Some(lt) = name.find('<') {
  1605. if lt > 0 {
  1606. name.truncate(lt);
  1607. }
  1608. }
  1609. let last_dot = name
  1610. .rfind('.')
  1611. .map(|i| i as isize)
  1612. .unwrap_or(-1)
  1613. .max(name.rfind("::").map(|i| i as isize).unwrap_or(-1));
  1614. if last_dot >= 0 {
  1615. name = name[(last_dot as usize + 1)..].to_string();
  1616. if name.starts_with(':') || name.starts_with('.') {
  1617. name.remove(0);
  1618. }
  1619. }
  1620. name.trim().to_string()
  1621. }
  1622. fn opt_str(arena: &mut Arena, s: Option<&str>) -> StrRef {
  1623. match s {
  1624. Some(s) => arena.put(s),
  1625. None => NONE_STR,
  1626. }
  1627. }