Merge #209
209: Owned nodes r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
3928255b6f
@ -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)
|
||||
}
|
||||
|
@ -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()),
|
||||
},
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
@ -3,32 +3,23 @@ the below applies to the result of this template
|
||||
#}// This file is automatically generated based on the file `./generated.rs.tera` when `cargo gen-syntax` is run
|
||||
// Do not edit manually
|
||||
|
||||
//! This module contains auto-generated Rust AST. Like `SyntaxNode`s, AST nodes
|
||||
//! are generic over ownership: `X<'a>` things are `Copy` references, `XNode`
|
||||
//! are Arc-based. You can switch between the two variants using `.owned` and
|
||||
//! `.borrowed` functions. Most of the code works with borowed mode, and only
|
||||
//! this mode has all AST accessors.
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
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 +47,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 +61,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 -%}
|
||||
|
@ -12,6 +12,10 @@ use crate::{
|
||||
SyntaxNodeRef,
|
||||
};
|
||||
|
||||
/// The main trait to go from untyped `SyntaxNode` to a typed ast. The
|
||||
/// conversion itself has zero runtime cost: ast and syntax nodes have exactly
|
||||
/// the same representation: a pointer to the tree root and a pointer to the
|
||||
/// node itself.
|
||||
pub trait AstNode<'a>: Clone + Copy + 'a {
|
||||
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self>
|
||||
where
|
||||
|
Loading…
x
Reference in New Issue
Block a user