Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cg-core/src/brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const UPSTREAM_CONFIG_FILE: &str = "codegraph.json";
pub const CONFIG_FILE: &str = "cgr.json";
/// Version of the resolution rules: changing resolver behaviour bumps this, which
/// marks older indexes as "built by an older engine" (design doc 5.8).
pub const ENGINE_VERSION: u32 = 4;
pub const ENGINE_VERSION: u32 = 5;
/// Version of the on-disk segment format; a mismatch forces a full rebuild.
pub const FORMAT_VERSION: u32 = 2;
/// The upstream commit whose kernel is vendored (see crates/cg-kernel/UPSTREAM_COMMIT).
Expand Down
6 changes: 6 additions & 0 deletions crates/cg-core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ pub fn truncate16(s: &str, max_units: usize) -> &str {
pub const fn pack_pos(line: u32, col16: u32) -> u64 {
((line as u64) << 32) | col16 as u64
}

/// Inverse of [`pack_pos`]: (line, utf16 column).
#[inline]
pub const fn unpack_pos(pos: u64) -> (u32, u32) {
((pos >> 32) as u32, pos as u32)
}
298 changes: 298 additions & 0 deletions crates/cg-extract/src/facts/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
//! Argument shapes of call sites, for overload selection (upstream `javaOverloadsFitting`):
//! a call's arguments decide which of several same-named methods it reaches. Each argument
//! is read from syntax alone and stored as one short string:
//!
//! * `T:<type>` - a literal, `new T(..)` or a cast: the static type as written
//! * `V:<name>` - a local, parameter or field, typed at resolution from its binding
//! * `F:<name>` - `this.<name>`: a field of the enclosing type
//! * `#this` - `this`
//! * `N` - `null` / `undefined`
//! * `L<k>` - a lambda / function literal taking `k` parameters; `L` alone: a method
//! reference, whose parameter count the text does not say
//! * `?` - anything else (a call, an operator, ...): fits every parameter
//!
//! A spread argument (`...xs`) or a named one (Kotlin `f(n = 1)`) makes the positional
//! reading unreliable, and such a call records nothing.

use super::util::{kids, Cx};
use super::CallArgsFact;
use cg_core::text::unpack_pos;
use tree_sitter::Node;

/// First item of a reference's candidate list that carries argument shapes.
pub const ARGS_MARK: &str = "#args";

/// More arguments than this are not worth a list (and no overload set differs that late).
const MAX_ARGS: usize = 24;

fn push(cx: &mut Cx, at: Node, tail: &str, args: Vec<String>) {
if tail.is_empty() || args.len() > MAX_ARGS {
return;
}
let (line, col) = unpack_pos(cx.start(at));
cx.facts.call_args.push(CallArgsFact { line, col, tail: tail.to_owned(), args });
}

fn lambda(params: usize) -> String {
format!("L{params}")
}

// ------------------------------------------------------------------- Java

/// `foo(a, "x")`, `obj.foo(1)`, `this.foo()`, `super.foo()`: the kernel's reference sits at
/// the start of the `method_invocation`.
pub fn java_call(cx: &mut Cx, call: Node) {
let Some(name) = call.child_by_field_name("name") else { return };
let Some(list) = call.child_by_field_name("arguments") else { return };
let tail = cx.text(name).to_owned();
let mut args = Vec::new();
for a in kids(list) {
if !a.is_named() || a.kind().ends_with("comment") {
continue;
}
args.push(java_shape(cx, a));
}
push(cx, call, &tail, args);
}

fn java_type(cx: &Cx, t: Node) -> String {
match t.kind() {
"generic_type" => t.named_child(0).map(|c| java_type(cx, c)).unwrap_or_default(),
"annotated_type" => kids(t).find(|c| !matches!(c.kind(), "annotation" | "marker_annotation")).map(|c| java_type(cx, c)).unwrap_or_default(),
"array_type" => {
let el = t.child_by_field_name("element").map(|c| java_type(cx, c)).unwrap_or_default();
if el.is_empty() {
String::new()
} else {
format!("{el}[]")
}
}
"type_identifier" if cx.text(t) == "var" => String::new(),
"type_identifier" | "scoped_type_identifier" | "integral_type" | "floating_point_type" | "boolean_type" => cx.text(t).to_owned(),
_ => String::new(),
}
}

fn java_shape(cx: &Cx, a: Node) -> String {
let text = cx.text(a);
let typed = |t: &str| format!("T:{t}");
match a.kind() {
"string_literal" | "text_block" => typed("String"),
"character_literal" => typed("char"),
"decimal_integer_literal" | "hex_integer_literal" | "octal_integer_literal" | "binary_integer_literal" => {
typed(if text.ends_with(['l', 'L']) { "long" } else { "int" })
}
"decimal_floating_point_literal" | "hex_floating_point_literal" => typed(if text.ends_with(['f', 'F']) { "float" } else { "double" }),
"true" | "false" => typed("boolean"),
"null_literal" => "N".into(),
"this" => "#this".into(),
"identifier" => format!("V:{text}"),
"object_creation_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) {
Some(t) if !t.is_empty() => typed(&t),
_ => "?".into(),
},
"array_creation_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) {
Some(t) if !t.is_empty() => typed(&format!("{t}[]")),
_ => "?".into(),
},
"cast_expression" => match a.child_by_field_name("type").map(|t| java_type(cx, t)) {
Some(t) if !t.is_empty() => typed(&t),
_ => "?".into(),
},
"field_access" => match (a.child_by_field_name("object"), a.child_by_field_name("field")) {
(Some(o), Some(f)) if o.kind() == "this" => format!("F:{}", cx.text(f)),
_ => "?".into(),
},
"lambda_expression" => {
let n = match a.child_by_field_name("parameters") {
Some(p) if p.kind() == "identifier" => 1,
Some(p) => kids(p).filter(|c| c.is_named() && !c.kind().ends_with("comment")).count(),
None => 0,
};
lambda(n)
}
"method_reference" => "L".into(),
"parenthesized_expression" => a.named_child(0).map(|c| java_shape(cx, c)).unwrap_or_else(|| "?".into()),
"unary_expression" => match a.child_by_field_name("operand") {
// `-1`, `-2.5`: the operand's numeric type
Some(o) if text.starts_with(['-', '+']) && o.kind().contains("literal") => java_shape(cx, o),
_ => "?".into(),
},
_ => "?".into(),
}
}

// ------------------------------------------------------------------- Kotlin

/// `f(a, b)`, `obj.f(1)`, `f(1) { .. }`: the kernel's reference sits at the start of the
/// `call_expression` (the inner one, for a trailing lambda).
pub fn kotlin_call(cx: &mut Cx, call: Node) {
let mut c = call.walk();
let parts: Vec<Node> = call.named_children(&mut c).collect();
let (Some(callee), Some(suffix)) = (parts.first().copied(), parts.get(1).copied()) else { return };
if suffix.kind() != "call_suffix" {
return;
}
let tail = match callee.kind() {
"simple_identifier" => cx.text(callee).to_owned(),
"navigation_expression" => match kids(callee).filter(|k| k.kind() == "navigation_suffix").last() {
Some(s) => kids(s).find(|k| k.kind() == "simple_identifier").map(|k| cx.text(k).to_owned()).unwrap_or_default(),
None => String::new(),
},
_ => String::new(),
};
let mut args = Vec::new();
for part in kids(suffix) {
match part.kind() {
"value_arguments" => {
for va in kids(part).filter(|k| k.kind() == "value_argument") {
let mut vc = va.walk();
let inner: Vec<Node> = va.named_children(&mut vc).collect();
// `name = value` and `*spread` are not positional
if inner.len() != 1 || kids(va).any(|k| !k.is_named() && matches!(cx.text(k), "*" | "=")) {
return;
}
args.push(kotlin_shape(cx, inner[0]));
}
}
"annotated_lambda" => args.push(kotlin_lambda(cx, part)),
_ => {}
}
}
// `f(1) { .. }` parses as a call of the call `f(1)` with the lambda: it is one more
// argument of the inner call.
if let Some(parent) = call.parent() {
if parent.kind() == "call_expression" && parent.named_child(0) == Some(call) {
if let Some(outer) = parent.named_child(1).filter(|s| s.kind() == "call_suffix") {
if kids(outer).all(|k| !k.is_named() || k.kind() == "annotated_lambda") {
for l in kids(outer).filter(|k| k.kind() == "annotated_lambda") {
args.push(kotlin_lambda(cx, l));
}
}
}
}
}
push(cx, call, &tail, args);
}

fn kotlin_lambda(cx: &Cx, n: Node) -> String {
let lit = if n.kind() == "lambda_literal" { Some(n) } else { kids(n).find(|k| k.kind() == "lambda_literal") };
let Some(lit) = lit else { return "?".into() };
let params = kids(lit).find(|k| k.kind() == "lambda_parameters").map_or(0, |p| kids(p).filter(|k| k.is_named()).count());
let _ = cx;
// `{ it }` takes one implicit parameter or none: the count is unknown
if params == 0 {
"L".into()
} else {
lambda(params)
}
}

fn kotlin_shape(cx: &Cx, a: Node) -> String {
let text = cx.text(a);
let typed = |t: &str| format!("T:{t}");
match a.kind() {
"string_literal" | "line_string_literal" | "multi_line_string_literal" => typed("String"),
"character_literal" => typed("Char"),
"integer_literal" | "hex_literal" | "bin_literal" => typed("Int"),
"long_literal" => typed("Long"),
"real_literal" => typed(if text.ends_with(['f', 'F']) { "Float" } else { "Double" }),
"boolean_literal" => typed("Boolean"),
"null_literal" => "N".into(),
"this_expression" => "#this".into(),
"simple_identifier" if text == "null" => "N".into(),
"simple_identifier" => format!("V:{text}"),
"lambda_literal" | "annotated_lambda" => kotlin_lambda(cx, a),
"anonymous_function" => "L".into(),
"callable_reference" => "L".into(),
"parenthesized_expression" => a.named_child(0).map(|c| kotlin_shape(cx, c)).unwrap_or_else(|| "?".into()),
"as_expression" => match a.named_child(1) {
Some(t) => typed(cx.text(t).trim_end_matches('?')),
None => "?".into(),
},
"prefix_expression" if text.starts_with(['-', '+']) => match a.named_child(a.named_child_count().saturating_sub(1)) {
Some(o) if o.kind().ends_with("literal") => kotlin_shape(cx, o),
_ => "?".into(),
},
_ => "?".into(),
}
}

// ------------------------------------------------------------------- TypeScript / JavaScript

/// `f(a)`, `obj.f(1)`, `this.f()`: the kernel's reference sits at the call, or at its callee
/// when an `await` sits inside the callee; both positions are recorded.
pub fn js_call(cx: &mut Cx, call: Node) {
let Some(func) = call.child_by_field_name("function") else { return };
let Some(list) = call.child_by_field_name("arguments") else { return };
if list.kind() != "arguments" {
return; // a tagged template
}
let tail = match func.kind() {
"identifier" => cx.text(func).to_owned(),
"member_expression" => func.child_by_field_name("property").map(|p| cx.text(p).to_owned()).unwrap_or_default(),
_ => return,
};
let mut args = Vec::new();
for a in kids(list) {
if !a.is_named() || a.kind() == "comment" {
continue;
}
if a.kind() == "spread_element" {
return;
}
args.push(js_shape(cx, a));
}
if cx.start(func) != cx.start(call) {
push(cx, func, &tail, args.clone());
}
push(cx, call, &tail, args);
}

fn js_type(cx: &Cx, t: Node) -> String {
match t.kind() {
"type_annotation" | "parenthesized_type" => t.named_child(0).map(|c| js_type(cx, c)).unwrap_or_default(),
"predefined_type" | "type_identifier" | "nested_type_identifier" => cx.text(t).to_owned(),
"generic_type" => t.child_by_field_name("name").map(|c| cx.text(c).to_owned()).unwrap_or_default(),
_ => String::new(),
}
}

fn js_shape(cx: &Cx, a: Node) -> String {
let typed = |t: &str| format!("T:{t}");
match a.kind() {
"string" | "template_string" => typed("string"),
"number" => typed("number"),
"true" | "false" => typed("boolean"),
"null" | "undefined" => "N".into(),
"this" => "#this".into(),
"identifier" if cx.text(a) == "undefined" => "N".into(),
"identifier" => format!("V:{}", cx.text(a)),
"member_expression" => match (a.child_by_field_name("object"), a.child_by_field_name("property")) {
(Some(o), Some(p)) if o.kind() == "this" && p.kind() == "property_identifier" => format!("F:{}", cx.text(p)),
_ => "?".into(),
},
"new_expression" => match a.child_by_field_name("constructor") {
Some(c) if matches!(c.kind(), "identifier" | "member_expression") => typed(cx.text(c)),
_ => "?".into(),
},
"arrow_function" | "function_expression" | "function" => {
let n = if a.child_by_field_name("parameter").is_some() {
1
} else {
a.child_by_field_name("parameters").map_or(0, |p| kids(p).filter(|c| c.is_named() && c.kind() != "comment").count())
};
lambda(n)
}
"parenthesized_expression" | "non_null_expression" => a.named_child(0).map(|c| js_shape(cx, c)).unwrap_or_else(|| "?".into()),
"as_expression" | "satisfies_expression" => match a.named_child(1).map(|t| js_type(cx, t)) {
Some(t) if !t.is_empty() => typed(&t),
_ => "?".into(),
},
"unary_expression" => match a.child_by_field_name("argument") {
Some(o) if o.kind() == "number" && matches!(cx.text(a).as_bytes().first(), Some(b'-' | b'+')) => typed("number"),
_ => "?".into(),
},
_ => "?".into(),
}
}
15 changes: 15 additions & 0 deletions crates/cg-extract/src/facts/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) {
"function_declaration" | "method_declaration" => function(cx, n, scope),
"func_literal" => function(cx, n, scope),
"type_spec" => type_spec(cx, n, scope),
"type_alias" => {
// `type A = B` names another type; a defined `type A B` is a type of its own
if let Some(name) = n.child_by_field_name("name") {
let name = cx.text(name).to_owned();
cx.facts.go_types.push(GoTypeFact { name, alias: true, type_params: Vec::new() });
}
}
"short_var_declaration" => {
let (l, r) = (n.child_by_field_name("left"), n.child_by_field_name("right"));
if let (Some(l), Some(r)) = (l, r) {
Expand Down Expand Up @@ -186,6 +193,14 @@ fn function(cx: &mut Cx, n: Node, scope: u32) {
}

fn type_spec(cx: &mut Cx, n: Node, scope: u32) {
if let (Some(name), Some(tp)) = (n.child_by_field_name("name"), n.child_by_field_name("type_parameters")) {
let params: Vec<String> = kids(tp)
.filter(|d| d.kind() == "type_parameter_declaration")
.flat_map(|d| kids(d).filter(|c| c.kind() == "identifier").map(|c| cx.text(c).to_owned()).collect::<Vec<_>>())
.collect();
let name = cx.text(name).to_owned();
cx.facts.go_types.push(GoTypeFact { name, alias: false, type_params: params });
}
let Some(ty) = n.child_by_field_name("type") else { return };
if ty.kind() != "struct_type" {
return;
Expand Down
8 changes: 7 additions & 1 deletion crates/cg-extract/src/facts/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) {
"class_declaration" | "interface_declaration" | "enum_declaration" | "record_declaration" | "annotation_type_declaration" => class(cx, n, scope),
"method_declaration" | "constructor_declaration" | "compact_constructor_declaration" => function(cx, n, scope),
"lambda_expression" => lambda(cx, n, scope),
"method_invocation" => {
super::args::java_call(cx, n);
walk_children(cx, n, scope);
}
"local_variable_declaration" => {
declare(cx, n, scope, BindKind::Local);
walk_children(cx, n, scope);
Expand Down Expand Up @@ -69,9 +73,11 @@ fn java_type_name(cx: &Cx, n: Node) -> String {
match n.kind() {
"type_identifier" if cx.text(n) == "var" => String::new(),
"type_identifier" | "scoped_type_identifier" => cx.text(n).to_owned(),
// primitives type their arguments for overload selection (`args`); they name no member
"integral_type" | "floating_point_type" | "boolean_type" => cx.text(n).to_owned(),
"generic_type" => n.named_child(0).map(|c| java_type_name(cx, c)).unwrap_or_default(),
"annotated_type" => kids(n).find(|c| !matches!(c.kind(), "annotation" | "marker_annotation")).map(|c| java_type_name(cx, c)).unwrap_or_default(),
_ => String::new(), // primitives, arrays, `var`, void
_ => String::new(), // arrays, `var`, void
}
}

Expand Down
8 changes: 7 additions & 1 deletion crates/cg-extract/src/facts/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) {
"function_expression" | "function" | "generator_function" | "arrow_function" | "method_definition" => function(cx, n, scope),
"class_declaration" | "abstract_class_declaration" | "class" => class(cx, n, scope),
"lexical_declaration" | "variable_declaration" => var_decl(cx, n, scope),
"call_expression" => {
super::args::js_call(cx, n);
walk_children(cx, n, scope);
}
"for_in_statement" => {
if let Some(l) = n.child_by_field_name("left") {
bind_pattern(cx, l, scope, BindKind::Local, "", "");
Expand Down Expand Up @@ -99,6 +103,8 @@ fn ts_type_name(cx: &Cx, n: Node) -> String {
match n.kind() {
"type_annotation" => n.named_child(0).map(|c| ts_type_name(cx, c)).unwrap_or_default(),
"type_identifier" | "identifier" => cx.text(n).to_owned(),
// primitives type arguments for overload selection (`args`); `any` / `unknown` say nothing
"predefined_type" if matches!(cx.text(n), "string" | "number" | "boolean" | "bigint" | "symbol") => cx.text(n).to_owned(),
"nested_type_identifier" | "member_expression" => cx.text(n).to_owned(),
"generic_type" => n.child_by_field_name("name").map(|c| ts_type_name(cx, c)).unwrap_or_default(),
"parenthesized_type" | "readonly_type" | "required_parameter" => n.named_child(0).map(|c| ts_type_name(cx, c)).unwrap_or_default(),
Expand All @@ -108,7 +114,7 @@ fn ts_type_name(cx: &Cx, n: Node) -> String {
continue;
}
let t = ts_type_name(cx, ch);
if !t.is_empty() && t != "null" && t != "undefined" {
if !t.is_empty() && !matches!(t.as_str(), "null" | "undefined" | "string" | "number" | "boolean" | "bigint" | "symbol") {
return t;
}
}
Expand Down
Loading
Loading