use super::*;
use smol_str::SmolStr;
pub(crate) fn direct_member(
shapes: &crate::sema::declarations::ShapeTable,
index: &ProjectIndex,
scopes: &crate::ScopeTree,
root: &cpp_parser::CppSyntaxNode,
path: &Path,
class: &str,
member: &str,
arguments: Option<&[String]>,
) -> Option<(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 Some(binding) = scope_data
.bindings
.iter()
.find(|binding| binding.name.identifier_text() == Some(member))
{
return Some((
fact_from_binding_with(
crate::sema::declarations::shapes_from(shapes, root),
root,
class,
binding,
),
path.to_path_buf(),
));
}
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 (class, arguments) = match (class.split_once('<'), from_the_spelling.as_ref()) {
(Some((head, _)), Some(written)) => (head.trim(), Some(written.as_slice())),
_ => (class.as_str(), arguments),
};
if let Some(found) = index
.declarations_in(class, path)
.into_iter()
.find(|declaration| {
declaration.fact.name == member
&& a_pattern_matches(
declaration.fact.pattern(),
declaration.fact.parameters(),
arguments,
)
})
{
return Some((found.fact.clone(), found.file.clone()));
}
match index.definition(&format!("{class}::{member}"), path) {
Known::Yes(found) => Some((found.fact, found.file)),
Known::Unknown(_) | Known::No => None,
}
}
pub(crate) fn a_pattern_matches(
pattern: Option<&str>,
parameters: &[String],
written: Option<&[String]>,
) -> bool {
let (Some(pattern), Some(written)) = (pattern, written) else {
return true;
};
let Some(slots) = arguments_in_a_spelling(pattern) else {
return true;
};
if slots.len() != written.len() {
return true;
}
slots.iter().zip(written).all(|(slot, argument)| {
a_slot_matches(slot, argument, parameters)
})
}
pub(crate) fn a_slot_matches(slot: &str, argument: &str, parameters: &[String]) -> bool {
let slot = slot.trim();
if parameters.iter().any(|name| name == slot) {
return true;
}
let argument = folded_for_matching(argument);
let argument = argument.trim();
if slot == argument {
return true;
}
let (Some((slot_head, slot_arguments)), Some((argument_head, argument_arguments))) = (
split_template_arguments(slot),
split_template_arguments(argument),
) else {
return false;
};
if slot_head.rsplit("::").next() != argument_head.rsplit("::").next() {
return false;
}
slot_arguments.len() == argument_arguments.len()
&& slot_arguments
.iter()
.zip(&argument_arguments)
.all(|(one, other)| a_slot_matches(one, other, parameters))
}
pub(crate) fn arguments_in_a_spelling(written: &str) -> Option<Vec<String>> {
let (_, after) = written.split_once('<')?;
let inside = after.trim_end().strip_suffix('>')?;
let mut out = Vec::new();
let mut depth = 0usize;
let mut at = 0usize;
for (index, character) in inside.char_indices() {
match character {
'<' | '(' | '[' => depth += 1,
'>' | ')' | ']' => depth = depth.saturating_sub(1),
',' if depth == 0 => {
out.push(inside[at..index].to_string());
at = index + 1;
}
_ => {}
}
}
out.push(inside[at..].to_string());
let out: Vec<String> = out
.into_iter()
.map(|argument| argument.trim().to_string())
.filter(|argument| !argument.is_empty())
.collect();
(!out.is_empty()).then_some(out)
}
pub(crate) fn fact_from_binding(
root: &cpp_parser::CppSyntaxNode,
class: &str,
binding: &crate::Binding,
) -> DeclFact {
fact_from_binding_with(
&crate::sema::declarations::DeclarationShapes::of(root),
root,
class,
binding,
)
}
pub(crate) fn fact_from_binding_with(
shapes: &crate::sema::declarations::DeclarationShapes,
root: &cpp_parser::CppSyntaxNode,
class: &str,
binding: &crate::Binding,
) -> DeclFact {
let _ = root;
DeclFact {
enumerator: binding.kind == crate::BindingKind::Enumerator,
split_by_a_directive: crate::sema::declarations::split_by_a_directive_with(shapes, binding),
name: binding.name.text().into(),
scope: (!class.is_empty()).then(|| SmolStr::from(class)),
local: false,
kind: crate::DeclKind::from_binding_kind(binding.kind),
modifiers: crate::sema::declarations::declared_modifiers_with(shapes, binding),
frees: crate::sema::declarations::freed_parameters_of(root, binding)
.min(u64::from(u16::MAX)) as u16,
type_of: crate::sema::declarations::declared_type_of_with(shapes, binding)
.or_else(|| {
crate::sema::declarations::an_unnamed_class_body_named(
Some(class),
&binding.name.text(),
shapes,
binding,
)
})
.map(SmolStr::from),
returns: crate::sema::declarations::declared_returns_of_in(shapes, root, binding)
.or_else(|| crate::sema::declarations::declared_returns_of_with(shapes, binding))
.map(SmolStr::from),
detail: crate::summary::DeclDetail::in_a_box(crate::summary::DeclDetail {
in_namespace: None,
returns_in_the_class: {
let returns =
crate::sema::declarations::declared_returns_of_in(shapes, root, binding)
.or_else(|| {
crate::sema::declarations::declared_returns_of_with(shapes, binding)
});
if returns.is_some() {
None
} else {
crate::sema::declarations::the_returns_the_class_has_to_resolve(
shapes, root, binding,
)
}
},
bases: crate::sema::declarations::declared_bases_of_with(shapes, binding),
reflection: None,
parameters: crate::sema::declarations::declared_template_parameters_with(
shapes, binding,
),
constraints: Vec::new(),
requires_clause: crate::sema::declarations::the_requires_clause_with(shapes, binding),
parameter_defaults: crate::sema::declarations::declared_parameter_defaults_with(
shapes, binding,
),
pattern: crate::sema::declarations::pattern_of(shapes, binding),
parameter_list: match binding.kind {
crate::BindingKind::Function
| crate::BindingKind::Constructor
| crate::BindingKind::Destructor
| crate::BindingKind::ConversionFunction
| crate::BindingKind::OperatorFunction
| crate::BindingKind::LiteralOperator => {
crate::sema::declarations::declared_parameters_with(shapes, binding)
}
_ => None,
},
}),
access: shapes.access_at(binding.name_range.start_offset),
exported: shapes.exported_at(binding.name_range.start_offset),
definition: true,
range: binding.range,
name_range: binding.name_range,
clean: true,
guard: FactGuard::Unconditional,
}
}
pub(crate) fn names_a_template_parameter(spelling: &str, parameters: &[String]) -> bool {
if parameters.is_empty() {
return false;
}
let head = without_arguments(spelling.trim());
let head = head.strip_prefix("::").unwrap_or(head);
parameters
.iter()
.any(|parameter| parameter == head || parameter == head.rsplit("::").next().unwrap_or(head))
}
pub(crate) fn bases_of(
index: &ProjectIndex,
scopes: &crate::ScopeTree,
root: &cpp_parser::CppSyntaxNode,
path: &Path,
class: &str,
) -> Known<Vec<String>> {
bases_of_with(
&crate::sema::declarations::ShapeTable::new(),
index,
scopes,
root,
path,
class,
)
}
pub(crate) fn bases_of_with(
shapes: &crate::sema::declarations::ShapeTable,
index: &ProjectIndex,
scopes: &crate::ScopeTree,
root: &cpp_parser::CppSyntaxNode,
path: &Path,
class: &str,
) -> Known<Vec<String>> {
bases_of_inner(shapes, index, scopes, root, path, class)
}
pub(crate) fn bases_of_inner(
shapes: &crate::sema::declarations::ShapeTable,
index: &ProjectIndex,
scopes: &crate::ScopeTree,
root: &cpp_parser::CppSyntaxNode,
path: &Path,
class: &str,
) -> Known<Vec<String>> {
if let Some(scope) = scopes.scope_with_qualified_name(class)
&& let Some(data) = scopes.scope(scope)
&& let Some(parent) = data.parent
&& let Some(name) = class.rsplit("::").next()
&& let Some(binding) = scopes.scope(parent).and_then(|scope| {
scope
.bindings
.iter()
.find(|binding| binding.name.identifier_text() == Some(name))
})
{
return Known::Yes(lookup_names(
&crate::sema::declarations::declared_bases_of_with(
crate::sema::declarations::shapes_from(shapes, root),
binding,
),
));
}
match index.definitions(class, path) {
Known::Yes(found) => {
let mut bases: Vec<String> = Vec::new();
for declaration in &found.found {
for base in lookup_names(declaration.fact.bases()) {
if !bases.contains(&base) {
bases.push(base);
}
}
}
if bases.is_empty()
&& let Some(bare) = class.rsplit("::").next()
{
for (_, fact) in index.every_fact_named(bare) {
if fact.kind != crate::DeclKind::Type {
continue;
}
for base in lookup_names(fact.bases()) {
if !bases.contains(&base) {
bases.push(base);
}
}
}
}
Known::Yes(bases)
}
Known::Unknown(reason) => Known::Unknown(reason),
Known::No => Known::No,
}
}
pub(crate) fn lookup_names(bases: &[String]) -> Vec<String> {
let mut names: Vec<String> = Vec::new();
for base in bases {
let parsed = crate::sema::types::parse_type_spelling(base);
collect_class_names(&parsed, &mut names);
}
names
}
pub(crate) fn collect_class_names(ty: &crate::sema::types::Type, into: &mut Vec<String>) {
if let Some(name) = ty.class_name()
&& !name.is_empty()
&& !into.iter().any(|kept| kept == name)
{
into.push(name.to_string());
}
for argument in ty.arguments() {
collect_class_names(argument, into);
}
}
pub(crate) fn resolved_in_the_enclosing_scopes(
index: &ProjectIndex,
scopes: &crate::ScopeTree,
path: &Path,
owner: &str,
base: &str,
) -> String {
if base.contains("::") {
return base.to_string();
}
let mut enclosing = owner;
while let Some((outer, _)) = enclosing.rsplit_once("::") {
let candidate = format!("{outer}::{base}");
if is_declared(index, scopes, path, &candidate) {
return candidate;
}
enclosing = outer;
}
base.to_string()
}
pub(crate) fn enclosing_class(scopes: &crate::ScopeTree, offset: usize) -> Option<String> {
let innermost = scopes.scope_at(offset)?;
scopes.scope_chain(innermost).into_iter().find_map(|scope| {
let data = scopes.scope(scope)?;
(data.kind == crate::ScopeKind::Class)
.then(|| scopes.qualified_name_of(scope))
.flatten()
})
}
pub(crate) fn class_of_the_enclosing_definition(
scopes: &crate::ScopeTree,
offset: usize,
) -> Option<String> {
if let Some(class) = enclosing_class(scopes, offset) {
return Some(class);
}
let innermost = scopes.scope_at(offset)?;
let qualified = scopes.qualified_name_of(innermost)?;
let (class, _member) = qualified.rsplit_once("::")?;
(!class.is_empty()).then(|| class.to_string())
}
pub(crate) fn the_trailing_argument_list(written: &str) -> Option<usize> {
if !written.ends_with('>') {
return None;
}
let mut depth = 0usize;
for (at, character) in written.char_indices().rev() {
match character {
'>' => depth += 1,
'<' => {
if depth == 0 {
return None;
}
depth -= 1;
if depth == 0 {
return Some(at);
}
}
_ => {}
}
}
None
}
pub(crate) fn base_type_name(written: &str) -> &str {
let mut name = written.trim();
loop {
let before = name;
name = name.trim_end_matches(['*', '&']).trim();
name = name.strip_prefix("::").unwrap_or(name).trim();
if let Some(opening) = the_trailing_argument_list(name) {
name = name[..opening].trim();
}
if name == before {
break;
}
}
for keyword in ["struct ", "class ", "union ", "enum "] {
if let Some(rest) = name.strip_prefix(keyword) {
name = rest.trim();
break;
}
}
name
}
pub(crate) fn a_type_spelling_with_its_arguments(written: &str) -> &str {
let mut name = written.trim();
loop {
let before = name;
name = name.trim_end_matches(['*', '&']).trim();
name = name.strip_prefix("::").unwrap_or(name).trim();
for qualifier in ["const ", "volatile ", "const volatile ", "volatile const "] {
if let Some(rest) = name.strip_prefix(qualifier) {
name = rest.trim();
break;
}
}
if name == before {
break;
}
}
for keyword in ["struct ", "class ", "union ", "enum "] {
if let Some(rest) = name.strip_prefix(keyword) {
name = rest.trim();
break;
}
}
name
}