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` /// TODO: this should return something more type-safe then `SyntaxNode`
pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode { pub(crate) fn fn_syntax(db: &impl DescriptorDatabase, fn_id: FnId) -> FnDefNode {
let syntax = db.resolve_syntax_ptr(fn_id.0); 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> { pub(crate) fn fn_scopes(db: &impl DescriptorDatabase, fn_id: FnId) -> Arc<FnScopes> {
let syntax = db.fn_syntax(fn_id); let syntax = db.fn_syntax(fn_id);
let res = FnScopes::new(syntax.ast()); let res = FnScopes::new(syntax.borrowed());
Arc::new(res) Arc::new(res)
} }

View file

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

View file

@ -117,7 +117,7 @@ impl ModuleId {
.filter_map(|&it| { .filter_map(|&it| {
let p = tree.link(it).problem.clone()?; let p = tree.link(it).problem.clone()?;
let s = it.bind_source(tree, db); 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)) Some((s, p))
}) })
.collect() .collect()
@ -136,11 +136,11 @@ impl LinkId {
let owner = self.owner(tree); let owner = self.owner(tree);
match owner.source(tree).resolve(db) { match owner.source(tree).resolve(db) {
ModuleSourceNode::Root(root) => { ModuleSourceNode::Root(root) => {
let ast = imp::modules(root.ast()) let ast = imp::modules(root.borrowed())
.find(|(name, _)| name == &tree.link(self).name) .find(|(name, _)| name == &tree.link(self).name)
.unwrap() .unwrap()
.1; .1;
ast.into() ast.owned()
} }
ModuleSourceNode::Inline(it) => it, ModuleSourceNode::Inline(it) => it,
} }
@ -179,13 +179,13 @@ impl ModuleSource {
match self { match self {
ModuleSource::File(file_id) => { ModuleSource::File(file_id) => {
let syntax = db.file_syntax(file_id); let syntax = db.file_syntax(file_id);
ModuleSourceNode::Root(syntax.ast().into()) ModuleSourceNode::Root(syntax.ast().owned())
} }
ModuleSource::Inline(ptr) => { ModuleSource::Inline(ptr) => {
let syntax = db.resolve_syntax_ptr(ptr); let syntax = db.resolve_syntax_ptr(ptr);
let syntax = syntax.borrowed(); let syntax = syntax.borrowed();
let module = ast::Module::cast(syntax).unwrap(); 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 link = module_id.parent_link(&module_tree)?;
let file_id = link.owner(&module_tree).source(&module_tree).file_id(); let file_id = link.owner(&module_tree).source(&module_tree).file_id();
let decl = link.bind_source(&module_tree, &*self.db); let decl = link.bind_source(&module_tree, &*self.db);
let decl = decl.ast(); let decl = decl.borrowed();
let decl_name = decl.name().unwrap(); 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::{ use crate::{
ast, ast,
SyntaxNode, SyntaxNodeRef, AstNode, SyntaxNode, SyntaxNodeRef, AstNode,
yellow::{TreeRoot, RaTypes, OwnedRoot, RefRoot},
SyntaxKind::*, SyntaxKind::*,
}; };
{% for node, methods in ast %} {% for node, methods in ast %}
// {{ node }} // {{ 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 %} {%- if methods.enum %}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum {{ node }}<'a> { pub enum {{ node }}<'a> {
@ -56,9 +41,10 @@ impl<'a> AstNode<'a> for {{ node }}<'a> {
} }
{% else %} {% else %}
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct {{ node }}<'a> { pub struct {{ node }}Node<R: TreeRoot<RaTypes> = OwnedRoot> {
syntax: SyntaxNodeRef<'a>, syntax: SyntaxNode<R>,
} }
pub type {{ node }}<'a> = {{ node }}Node<RefRoot<'a>>;
impl<'a> AstNode<'a> for {{ node }}<'a> { impl<'a> AstNode<'a> for {{ node }}<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> { 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 } 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 %} {% endif %}
{% if methods.traits -%} {% if methods.traits -%}
{%- for t in methods.traits -%} {%- for t in methods.traits -%}