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

Function go_local_bindings

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

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

Callers 1

go_scoped_fn_ref_scopesFunction · 0.85

Calls 5

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

Tested by

no test coverage detected