Skip to content
Merged
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
146 changes: 122 additions & 24 deletions codegenome-identity/src/index/resolver_multi.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::graph::edge::{Edge, Relation};
use crate::graph::node::{Provenance, Source, Timestamp};
use crate::graph::overlay::Overlay;
use crate::identity::{address_of, UorAddress};
use crate::index::parser::ParsedFile;
use crate::index::resolver::ResolvedEdges;
use crate::lang::LanguageSupport;
use crate::overlay::syntax::SyntaxOverlay;

type SymbolTable = HashMap<String, UorAddress>;
type SpanIndex = HashMap<(u32, u32), UorAddress>;
type ScopedSymbolTable = HashMap<(PathBuf, String), UorAddress>;
type GlobalSymbolIndex = HashMap<String, Vec<UorAddress>>;
type SpanIndex = HashMap<(PathBuf, u32, u32), UorAddress>;

struct SymbolTable {
scoped: ScopedSymbolTable,
by_name: GlobalSymbolIndex,
}

impl SymbolTable {
fn resolve(&self, path: &Path, name: &str) -> Option<UorAddress> {
if let Some(address) = self.scoped.get(&(path.to_path_buf(), name.to_string())) {
return Some(*address);
}

let candidates = self.by_name.get(name)?;
match candidates.as_slice() {
[only] => Some(*only),
_ => None,
}
}
}

struct ResolveCtx<'a> {
symbols: &'a SymbolTable,
Expand All @@ -21,7 +40,7 @@ struct ResolveCtx<'a> {
/// Multi-language resolve: uses `LanguageSupport` per file group.
pub fn resolve_multi(
parsed: &[ParsedFile],
file_groups: &HashMap<&str, Vec<(std::path::PathBuf, Vec<u8>)>>,
file_groups: &HashMap<&str, Vec<(PathBuf, Vec<u8>)>>,
languages: &[Box<dyn LanguageSupport>],
) -> ResolvedEdges {
let lang_map: HashMap<&str, &dyn LanguageSupport> =
Expand All @@ -35,8 +54,7 @@ pub fn resolve_multi(
};

let symbols = build_symbol_table(file_groups, &lang_map);
let syntax = SyntaxOverlay::from_parsed(parsed);
let spans = build_span_index(&syntax);
let spans = build_span_index(parsed);
let ctx = ResolveCtx {
symbols: &symbols,
spans: &spans,
Expand All @@ -56,7 +74,15 @@ pub fn resolve_multi(
let Some(tree) = parser.parse(source.as_slice(), None) else {
continue;
};
resolve_file(backend, source, &tree, file_address(path), &ctx, &mut edges);
resolve_file(
backend,
path,
source,
&tree,
file_address(path),
&ctx,
&mut edges,
);
}
}

Expand All @@ -65,14 +91,15 @@ pub fn resolve_multi(

fn resolve_file(
backend: &dyn LanguageSupport,
path: &Path,
source: &[u8],
tree: &tree_sitter::Tree,
file_addr: UorAddress,
ctx: &ResolveCtx<'_>,
edges: &mut Vec<Edge>,
) {
for imp in backend.extract_imports(source, tree) {
if let Some(&addr) = ctx.symbols.get(&imp.imported_name) {
if let Some(addr) = ctx.symbols.resolve(path, &imp.imported_name) {
edges.push(Edge {
source: file_addr,
target: addr,
Expand All @@ -84,10 +111,10 @@ fn resolve_file(
}
}
for call in backend.extract_calls(source, tree) {
let Some(&callee) = ctx.symbols.get(&call.callee_name) else {
let Some(callee) = ctx.symbols.resolve(path, &call.callee_name) else {
continue;
};
let Some(caller_addr) = find_enclosing(&call.caller_span, ctx.spans) else {
let Some(caller_addr) = find_enclosing(path, &call.caller_span, ctx.spans) else {
continue;
};
edges.push(Edge {
Expand All @@ -103,10 +130,10 @@ fn resolve_file(
let Some(ref trait_name) = imp.trait_name else {
continue;
};
let Some(&type_addr) = ctx.symbols.get(&imp.type_name) else {
let Some(type_addr) = ctx.symbols.resolve(path, &imp.type_name) else {
continue;
};
let Some(&trait_addr) = ctx.symbols.get(trait_name) else {
let Some(trait_addr) = ctx.symbols.resolve(path, trait_name) else {
continue;
};
edges.push(Edge {
Expand All @@ -121,10 +148,12 @@ fn resolve_file(
}

fn build_symbol_table(
file_groups: &HashMap<&str, Vec<(std::path::PathBuf, Vec<u8>)>>,
file_groups: &HashMap<&str, Vec<(PathBuf, Vec<u8>)>>,
lang_map: &HashMap<&str, &dyn LanguageSupport>,
) -> SymbolTable {
let mut table = HashMap::new();
let mut scoped = HashMap::new();
let mut by_name: GlobalSymbolIndex = HashMap::new();

for (lang_name, files) in file_groups {
let Some(&backend) = lang_map.get(lang_name) else {
continue;
Expand All @@ -140,27 +169,96 @@ fn build_symbol_table(
for sym in backend.extract_symbols(source, &tree) {
let addr =
crate::lang::graph_builder::symbol_address(path, &sym.source_kind, &sym.name);
table.insert(sym.name, addr);
scoped.insert((path.clone(), sym.name.clone()), addr);
let candidates = by_name.entry(sym.name).or_default();
if !candidates.contains(&addr) {
candidates.push(addr);
}
}
}
}
table

SymbolTable { scoped, by_name }
}

fn build_span_index(syntax: &SyntaxOverlay) -> SpanIndex {
fn build_span_index(parsed: &[ParsedFile]) -> SpanIndex {
let mut index = HashMap::new();
for node in syntax.nodes() {
if let Some(span) = &node.span {
index.insert((span.start_line, span.end_line), node.address);
for file in parsed {
for node in &file.nodes {
if let Some(span) = &node.span {
index.insert(
(file.path.clone(), span.start_line, span.end_line),
node.address,
);
}
}
}
index
}

fn find_enclosing(fn_span: &crate::graph::node::Span, spans: &SpanIndex) -> Option<UorAddress> {
spans.get(&(fn_span.start_line, fn_span.end_line)).copied()
fn find_enclosing(
path: &Path,
fn_span: &crate::graph::node::Span,
spans: &SpanIndex,
) -> Option<UorAddress> {
spans
.get(&(path.to_path_buf(), fn_span.start_line, fn_span.end_line))
.copied()
}

fn file_address(path: &std::path::Path) -> UorAddress {
fn file_address(path: &Path) -> UorAddress {
address_of(format!("file:{}", path.display()).as_bytes())
}

#[cfg(test)]
mod tests {
use super::*;

fn edge_exists(edges: &[Edge], source: UorAddress, target: UorAddress) -> bool {
edges.iter().any(|edge| {
edge.source == source && edge.target == target && edge.relation == Relation::Calls
})
}

#[test]
fn duplicate_names_and_overlapping_spans_remain_file_scoped() {
let main_path = PathBuf::from("fixture/main.rs");
let decoy_path = PathBuf::from("fixture/decoy.rs");
let files = vec![
(
main_path.clone(),
b"fn leaf() {}\nfn middle() {\n leaf();\n}\nfn top() {\n middle();\n}\n"
.to_vec(),
),
(
decoy_path.clone(),
b"fn decoy_leaf() {}\nfn middle() {\n decoy_leaf();\n}\n".to_vec(),
),
];
let groups = crate::lang::detect::group_by_language(&files);
let languages = crate::lang::all_languages();
let parsed = crate::index::parser::parse_files_multi(&groups, &languages);
let lang_map: HashMap<&str, &dyn LanguageSupport> =
languages.iter().map(|l| (l.name(), l.as_ref())).collect();
let symbols = build_symbol_table(&groups, &lang_map);

let main_middle = symbols.resolve(&main_path, "middle").unwrap();
let main_leaf = symbols.resolve(&main_path, "leaf").unwrap();
let main_top = symbols.resolve(&main_path, "top").unwrap();
let decoy_middle = symbols.resolve(&decoy_path, "middle").unwrap();
let decoy_leaf = symbols.resolve(&decoy_path, "decoy_leaf").unwrap();

assert_ne!(main_middle, decoy_middle);
assert!(symbols
.resolve(Path::new("fixture/missing.rs"), "middle")
.is_none());

let resolved = resolve_multi(&parsed, &groups, &languages);

assert!(edge_exists(&resolved.edges, main_middle, main_leaf));
assert!(edge_exists(&resolved.edges, main_top, main_middle));
assert!(edge_exists(&resolved.edges, decoy_middle, decoy_leaf));
assert!(!edge_exists(&resolved.edges, main_middle, decoy_leaf));
assert!(!edge_exists(&resolved.edges, decoy_middle, main_leaf));
}
}
Loading