MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / go_local_bindings

Function go_local_bindings

codegraph-kernel/src/go.rs:1549–1610  ·  view source on GitHub ↗

The names among `names` that a function binds itself, as (unit id, name): its func literals' parameters, `:=`, `var` and `const`, range and type-switch variables, and a select case's received value. A name the function's own receiver, parameters or results bind is left out: those cover its whole body. Mirrors goLocalBindings.

(units: &[Node<'t>], names: &HashSet<&str>, src: &str)

Source from the content-addressed store, hash-verified

1547/// function's own receiver, parameters or results bind is left out: those
1548/// cover its whole body. Mirrors goLocalBindings.
1549fn go_local_bindings<'t>(units: &[Node<'t>], names: &HashSet<&str>, src: &str) -> HashSet<(usize, String)> {
1550 let mut out: HashSet<(usize, String)> = HashSet::new();
1551 for unit in units {
1552 let mut bind = |id: Option<Node<'t>>| {
1553 if let Some(id) = id {
1554 if id.kind() == "identifier" {
1555 let name = &src[id.start_byte()..id.end_byte()];
1556 if names.contains(name) {
1557 out.insert((unit.id(), name.to_string()));
1558 }
1559 }
1560 }
1561 };
1562 let declares = |n: Node<'t>| {
1563 (0..n.child_count()).any(|i| n.child(i).map_or(false, |c| !c.is_named() && c.kind() == ":="))
1564 };
1565 let mut stack: Vec<Node<'t>> = vec![*unit];
1566 while let Some(n) = stack.pop() {
1567 let list = match n.kind() {
1568 "short_var_declaration" => n.child_by_field_name("left"),
1569 "range_clause" | "receive_statement" if declares(n) => n.child_by_field_name("left"),
1570 "type_switch_statement" => n.child_by_field_name("alias"),
1571 _ => None,
1572 };
1573 if let Some(list) = list {
1574 for i in 0..list.named_child_count() {
1575 bind(list.named_child(i));
1576 }
1577 }
1578 if matches!(n.kind(), "var_spec" | "const_spec" | "parameter_declaration" | "variadic_parameter_declaration") {
1579 let mut cursor = n.walk();
1580 for c in n.children_by_field_name("name", &mut cursor) {
1581 bind(Some(c));
1582 }
1583 }
1584 for i in (0..n.named_child_count()).rev() {
1585 if let Some(c) = n.named_child(i) {
1586 stack.push(c);
1587 }
1588 }
1589 }
1590 for field in ["receiver", "parameters", "result"] {
1591 let Some(list) = unit.child_by_field_name(field) else { continue };
1592 if list.kind() != "parameter_list" {
1593 continue;
1594 }
1595 for i in 0..list.named_child_count() {
1596 let Some(p) = list.named_child(i) else { continue };
1597 if !matches!(p.kind(), "parameter_declaration" | "variadic_parameter_declaration") {
1598 continue;
1599 }
1600 let mut cursor = p.walk();
1601 for c in p.children_by_field_name("name", &mut cursor) {
1602 if c.kind() == "identifier" {
1603 out.remove(&(unit.id(), src[c.start_byte()..c.end_byte()].to_string()));
1604 }
1605 }
1606 }

Callers 1

go_scoped_fn_ref_scopesFunction · 0.85

Calls 5

idMethod · 0.80
walkMethod · 0.80
pushMethod · 0.80
declaresFunction · 0.50
bindFunction · 0.50

Tested by

no test coverage detected