use std::cell::RefCell;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use cpp_parser::CppSyntaxNode;
use crate::index::project::{self, MemberList, ProjectIndex};
use crate::sema::types::Type;
use crate::{Known, ScopeTree};
pub struct SemanticModel<'a> {
index: &'a ProjectIndex,
root: &'a CppSyntaxNode,
scopes: &'a ScopeTree,
source: &'a str,
path: &'a Path,
resolver: RefCell<Box<dyn FnMut(&Path) -> Option<crate::FileView> + 'a>>,
types: crate::index::project::TypeMemo,
members: RefCell<BTreeMap<String, Known<MemberList>>>,
shapes: crate::sema::declarations::ShapeTable,
}
impl<'a> SemanticModel<'a> {
pub fn new(
index: &'a ProjectIndex,
root: &'a CppSyntaxNode,
scopes: &'a ScopeTree,
source: &'a str,
path: &'a Path,
resolver: Box<dyn FnMut(&Path) -> Option<crate::FileView> + 'a>,
) -> SemanticModel<'a> {
SemanticModel {
index,
root,
scopes,
source,
path,
resolver: RefCell::new(resolver),
types: crate::index::project::TypeMemo::default(),
members: RefCell::new(BTreeMap::new()),
shapes: crate::sema::declarations::ShapeTable::new(),
}
}
pub fn type_of(&self, expression: &CppSyntaxNode) -> Known<(Type, PathBuf)> {
if crate::probes::no_inference() {
return Known::Unknown(crate::UnknownReason::UnknownType(Box::from(
expression.text().to_string().trim(),
)));
}
let mut resolver = self.resolver.borrow_mut();
project::type_of_expression_memoed(
&self.types,
&self.shapes,
self.index,
&mut **resolver,
self.scopes,
self.root,
self.path,
expression,
0,
)
}
pub fn members_of(&self, class: &str) -> Known<MemberList> {
if let Some(answer) = self.members.borrow().get(class) {
return answer.clone();
}
let answer = self.members_of_uncached(class);
self.members
.borrow_mut()
.insert(class.to_string(), answer.clone());
answer
}
fn members_of_uncached(&self, class: &str) -> Known<MemberList> {
let listed = project::members_of_with(
&self.shapes,
self.index,
self.scopes,
self.root,
self.path,
class,
);
project::substituted_members(self.index, self.path, class, listed)
}
pub fn the_class_after_an_arrow(
&self,
written: &crate::sema::types::Type,
) -> Option<(String, bool)> {
crate::completion::the_class_after_the_operator(
self.index,
self.scopes,
self.root,
self.path,
written,
true,
)
}
pub fn own_members_of(&self, class: &str) -> Known<Vec<(std::path::PathBuf, crate::DeclFact)>> {
project::own_members_of_with(
&self.shapes,
self.index,
self.scopes,
self.root,
self.path,
class,
)
}
pub fn names_a_template_parameter(&self, name: &str, at: usize) -> bool {
if name.is_empty() {
return false;
}
let Some(innermost) = self.scopes.scope_at(at) else {
return false;
};
self.scopes.scope_chain(innermost).iter().any(|id| {
self.scopes.scope(*id).is_some_and(|scope| {
scope.kind == crate::sema::symbol::ScopeKind::TemplateParameters
&& scope.bindings.iter().any(|binding| binding.name.is(name))
})
})
}
pub fn path(&self) -> &Path {
self.path
}
pub fn source(&self) -> &str {
self.source
}
pub fn tree(&self) -> &CppSyntaxNode {
self.root
}
pub fn index(&self) -> &ProjectIndex {
self.index
}
pub fn scopes(&self) -> &ScopeTree {
self.scopes
}
}