Generalize Owned nodes

This commit is contained in:
Aleksey Kladov 2018-11-06 21:52:00 +03:00
parent 21797bf0ef
commit a5301e94d5
6 changed files with 1249 additions and 1818 deletions

View file

@ -11,11 +11,11 @@ use crate::descriptors::{
/// TODO: this should return something more type-safe then `SyntaxNode`
pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
let syntax = db.resolve_syntax_ptr(fn_id.0);
FnDef::cast(syntax.borrowed()).unwrap().into()
FnDef::cast(syntax.borrowed()).unwrap().owned()
}
pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc<FnScopes> {
let syntax = db.fn_syntax(fn_id);
let res = FnScopes::new(syntax.ast());
let res = FnScopes::new(syntax.borrowed());
Arc::new(res)
}

View file

@ -41,9 +41,9 @@ pub(crate) fn submodules(
db::check_canceled(db)?;
let file_id = source.file_id();
let submodules = match source.resolve(db) {
ModuleSourceNode::Root(it) => collect_submodules(file_id, it.ast()),
ModuleSourceNode::Root(it) => collect_submodules(file_id, it.borrowed()),
ModuleSourceNode::Inline(it) => it
.ast()
.borrowed()
.item_list()
.map(|it| collect_submodules(file_id, it))
.unwrap_or_else(Vec::new),
@ -89,8 +89,8 @@ pub(crate) fn module_scope(
let tree = db.module_tree(source_root_id)?;
let source = module_id.source(&tree).resolve(db);
let res = match source {
ModuleSourceNode::Root(root) => ModuleScope::new(root.ast().items()),
ModuleSourceNode::Inline(inline) => match inline.ast().item_list() {
ModuleSourceNode::Root(root) => ModuleScope::new(root.borrowed().items()),
ModuleSourceNode::Inline(inline) => match inline.borrowed().item_list() {
Some(items) => ModuleScope::new(items.items()),
None => ModuleScope::new(std::iter::empty()),
},

View file

@ -117,7 +117,7 @@ impl ModuleId {
.filter_map(|&it| {
let p = tree.link(it).problem.clone()?;
let s = it.bind_source(tree, db);
let s = s.ast().name().unwrap().syntax().owned();
let s = s.borrowed().name().unwrap().syntax().owned();
Some((s, p))
})
.collect()
@ -136,11 +136,11 @@ impl LinkId {
let owner = self.owner(tree);
match owner.source(tree).resolve(db) {
ModuleSourceNode::Root(root) => {
let ast = imp::modules(root.ast())
let ast = imp::modules(root.borrowed())
.find(|(name, _)| name == &tree.link(self).name)
.unwrap()
.1;
ast.into()
ast.owned()
}
ModuleSourceNode::Inline(it) => it,
}
@ -179,13 +179,13 @@ impl ModuleSource {
match self {
ModuleSource::File(file_id) => {
let syntax = db.file_syntax(file_id);
ModuleSourceNode::Root(syntax.ast().into())
ModuleSourceNode::Root(syntax.ast().owned())
}
ModuleSource::Inline(ptr) => {
let syntax = db.resolve_syntax_ptr(ptr);
let syntax = syntax.borrowed();
let module = ast::Module::cast(syntax).unwrap();
ModuleSourceNode::Inline(module.into())
ModuleSourceNode::Inline(module.owned())
}
}
}

View file

@ -236,7 +236,7 @@ impl AnalysisImpl {
let link = module_id.parent_link(&module_tree)?;
let file_id = link.owner(&module_tree).source(&module_tree).file_id();
let decl = link.bind_source(&module_tree, &*self.db);
let decl = decl.ast();
let decl = decl.borrowed();
let decl_name = decl.name().unwrap();

File diff suppressed because it is too large Load diff

View file

@ -8,27 +8,12 @@ the below applies to the result of this template
use crate::{
ast,
SyntaxNode, SyntaxNodeRef, AstNode,
yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot},
SyntaxKind::*,
};
{% for node, methods in ast %}
// {{ node }}
#[derive(Debug, Clone)]
pub struct {{ node }}Node(SyntaxNode);
impl {{ node }}Node {
pub fn ast(&self) -> {{ node }} {
{{ node }}::cast(self.0.borrowed()).unwrap()
}
}
impl<'a> From<{{ node }}<'a>> for {{ node }}Node {
fn from(ast: {{ node}}<'a>) -> {{ node }}Node {
let syntax = ast.syntax().owned();
{{ node }}Node(syntax)
}
}
{%- if methods.enum %}
#[derive(Debug, Clone, Copy)]
pub enum {{ node }}<'a> {
@ -56,9 +41,10 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
}
{% else %}
#[derive(Debug, Clone, Copy)]
pub struct {{ node }}<'a> {
syntax: SyntaxNodeRef<'a>,
pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> {
syntax: SyntaxNode<R>,
}
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>;
impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
@ -69,6 +55,16 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<R: TreeRoot<RaTypes>> {{ node }}Node<R> {
pub fn borrowed(&self) -> {{ node }} {
{{ node }}Node { syntax: self.syntax.borrowed() }
}
pub fn owned(&self) -> {{ node }}Node {
{{ node }}Node { syntax: self.syntax.owned() }
}
}
{% endif %}
{% if methods.traits -%}
{%- for t in methods.traits -%}