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

Function go_local_bindings

codegraph-kernel/src/go.rs:1577–1638  ·  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

1575/// function's own receiver, parameters or results bind is left out: those
1576/// cover its whole body. Mirrors goLocalBindings.
1577fn go_local_bindings<'t>(units: &[Node<'t>], names: &HashSet<&str>, src: &str) -> HashSet<(usize, String)> {
1578 let mut out: HashSet<(usize, String)> = HashSet::new();
1579 for unit in units {
1580 let mut bind = |id: Option<Node<'t>>| {
1581 if let Some(id) = id {
1582 if id.kind() == "identifier" {
1583 let name = &src[id.start_byte()..id.end_byte()];
1584 if names.contains(name) {
1585 out.insert((unit.id(), name.to_string()));
1586 }
1587 }
1588 }
1589 };
1590 let declares = |n: Node<'t>| {
1591 (0..n.child_count()).any(|i| n.child(i).map_or(false, |c| !c.is_named() && c.kind() == ":="))
1592 };
1593 let mut stack: Vec<Node<'t>> = vec![*unit];
1594 while let Some(n) = stack.pop() {
1595 let list = match n.kind() {
1596 "short_var_declaration" => n.child_by_field_name("left"),
1597 "range_clause" | "receive_statement" if declares(n) => n.child_by_field_name("left"),
1598 "type_switch_statement" => n.child_by_field_name("alias"),
1599 _ => None,
1600 };
1601 if let Some(list) = list {
1602 for i in 0..list.named_child_count() {
1603 bind(list.named_child(i));
1604 }
1605 }
1606 if matches!(n.kind(), "var_spec" | "const_spec" | "parameter_declaration" | "variadic_parameter_declaration") {
1607 let mut cursor = n.walk();
1608 for c in n.children_by_field_name("name", &mut cursor) {
1609 bind(Some(c));
1610 }
1611 }
1612 for i in (0..n.named_child_count()).rev() {
1613 if let Some(c) = n.named_child(i) {
1614 stack.push(c);
1615 }
1616 }
1617 }
1618 for field in ["receiver", "parameters", "result"] {
1619 let Some(list) = unit.child_by_field_name(field) else { continue };
1620 if list.kind() != "parameter_list" {
1621 continue;
1622 }
1623 for i in 0..list.named_child_count() {
1624 let Some(p) = list.named_child(i) else { continue };
1625 if !matches!(p.kind(), "parameter_declaration" | "variadic_parameter_declaration") {
1626 continue;
1627 }
1628 let mut cursor = p.walk();
1629 for c in p.children_by_field_name("name", &mut cursor) {
1630 if c.kind() == "identifier" {
1631 out.remove(&(unit.id(), src[c.start_byte()..c.end_byte()].to_string()));
1632 }
1633 }
1634 }

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