use super::*;
pub(crate) fn member_candidates(
shapes: &crate::sema::declarations::ShapeTable,
index: &ProjectIndex,
scopes: &crate::ScopeTree,
root: &cpp_parser::CppSyntaxNode,
path: &Path,
class: &str,
member: &str,
arguments: Option<&[String]>,
) -> Vec<(DeclFact, PathBuf)> {
let class = &resolve_aliases(shapes, index, scopes, root, path, class);
if let Some(scope) = scopes.scope_with_qualified_name(class)
&& let Some(scope_data) = scopes.scope(scope)
{
let declared: Vec<(DeclFact, PathBuf)> = scope_data
.bindings
.iter()
.filter(|binding| binding.name.identifier_text() == Some(member))
.map(|binding| {
(
fact_from_binding_with(
crate::sema::declarations::shapes_from(shapes, root),
root,
class,
binding,
),
path.to_path_buf(),
)
})
.collect();
if !declared.is_empty() {
return declared;
}
}
let from_the_spelling: Option<Vec<String>> = class.split_once('<').map(|(_, rest)| {
crate::sema::builtins::split_arguments(rest.trim_end().strip_suffix('>').unwrap_or(rest))
});
let (bare, written) = match (class.split_once('<'), from_the_spelling.as_ref()) {
(Some((head, _)), Some(written)) => (head.trim(), Some(written.as_slice())),
_ => (class.as_str(), arguments),
};
let all = index.declarations_in(bare, path);
let named_of = |declarations: &[crate::VisibleDeclaration<'_>]| -> usize {
declarations
.iter()
.filter(|declaration| declaration.fact.name == member)
.count()
};
if crate::probes::inference() {
eprintln!(
"[candidates] {bare}::{member}: {} declaration(s) in scope, {} named",
all.len(),
named_of(&all)
);
}
let from_the_index: Vec<(DeclFact, PathBuf)> = all
.into_iter()
.filter(|declaration| {
declaration.fact.name == member
&& a_pattern_matches(
declaration.fact.pattern(),
declaration.fact.parameters(),
written,
)
})
.map(|declaration| (declaration.fact.clone(), declaration.file.clone()))
.collect();
if !from_the_index.is_empty() {
return from_the_index;
}
match index.definition(&format!("{class}::{member}"), path) {
Known::Yes(found) => vec![(found.fact, found.file)],
Known::Unknown(_) | Known::No => Vec::new(),
}
}